blob: 2158421e905ef191afe1e75258ada89a206d53a0 [file] [log] [blame]
Elliott Hughesdec12b22015-02-02 17:31:27 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Albertc007bc32015-03-16 10:08:46 -070017#include "base/file.h"
18
19#include <gtest/gtest.h>
Elliott Hughesdec12b22015-02-02 17:31:27 -080020
21#include <errno.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080022#include <fcntl.h>
23#include <unistd.h>
Dan Albertc007bc32015-03-16 10:08:46 -070024
25#include <string>
Elliott Hughesdec12b22015-02-02 17:31:27 -080026
Alex Vallée47d67c92015-05-06 16:26:00 -040027#include "base/test_utils.h"
Elliott Hughesf682b472015-02-06 12:19:48 -080028
Elliott Hughesdec12b22015-02-02 17:31:27 -080029TEST(file, ReadFileToString_ENOENT) {
30 std::string s("hello");
31 errno = 0;
Dan Albertc007bc32015-03-16 10:08:46 -070032 ASSERT_FALSE(android::base::ReadFileToString("/proc/does-not-exist", &s));
Elliott Hughesdec12b22015-02-02 17:31:27 -080033 EXPECT_EQ(ENOENT, errno);
Dan Albertc007bc32015-03-16 10:08:46 -070034 EXPECT_EQ("", s); // s was cleared.
Elliott Hughesdec12b22015-02-02 17:31:27 -080035}
36
Spencer Lowcf168a82015-05-24 15:36:28 -070037TEST(file, ReadFileToString_WriteStringToFile) {
Elliott Hughesf682b472015-02-06 12:19:48 -080038 TemporaryFile tf;
39 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040040 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path))
Dan Albert850188f2015-04-29 17:09:53 -070041 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080042 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040043 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070044 << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080045 EXPECT_EQ("abc", s);
46}
47
Dan Albert0c4b3a32015-04-29 11:32:23 -070048// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
49// sense on Windows.
50#if !defined(_WIN32)
Elliott Hughes9d1f5152015-02-17 10:16:04 -080051TEST(file, WriteStringToFile2) {
52 TemporaryFile tf;
53 ASSERT_TRUE(tf.fd != -1);
Alex Vallée47d67c92015-05-06 16:26:00 -040054 ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660,
Dan Albertc007bc32015-03-16 10:08:46 -070055 getuid(), getgid()))
Dan Albert850188f2015-04-29 17:09:53 -070056 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080057 struct stat sb;
Alex Vallée47d67c92015-05-06 16:26:00 -040058 ASSERT_EQ(0, stat(tf.path, &sb));
Colin Cross56b37342015-04-30 15:12:21 -070059 ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT));
Elliott Hughes9d1f5152015-02-17 10:16:04 -080060 ASSERT_EQ(getuid(), sb.st_uid);
61 ASSERT_EQ(getgid(), sb.st_gid);
62 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -040063 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -070064 << strerror(errno);
Elliott Hughes9d1f5152015-02-17 10:16:04 -080065 EXPECT_EQ("abc", s);
66}
Dan Albert0c4b3a32015-04-29 11:32:23 -070067#endif
Elliott Hughes9d1f5152015-02-17 10:16:04 -080068
Elliott Hughesf682b472015-02-06 12:19:48 -080069TEST(file, WriteStringToFd) {
70 TemporaryFile tf;
71 ASSERT_TRUE(tf.fd != -1);
Dan Albertc007bc32015-03-16 10:08:46 -070072 ASSERT_TRUE(android::base::WriteStringToFd("abc", tf.fd));
Elliott Hughesf682b472015-02-06 12:19:48 -080073
Dan Albert850188f2015-04-29 17:09:53 -070074 ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080075
76 std::string s;
Dan Albert850188f2015-04-29 17:09:53 -070077 ASSERT_TRUE(android::base::ReadFdToString(tf.fd, &s)) << strerror(errno);
Elliott Hughesf682b472015-02-06 12:19:48 -080078 EXPECT_EQ("abc", s);
79}
Elliott Hughes56085ed2015-04-24 21:57:16 -070080
81TEST(file, ReadFully) {
Spencer Lowcf168a82015-05-24 15:36:28 -070082#ifdef _WIN32
83 VersionFile ver;
84 ASSERT_NE(ver.filename, nullptr);
85 const char* filename = ver.filename;
86 // Note that ReadFully() does CR/LF translation, so we expect \n, not \r\n.
87 const char expected[] = "\nMicrosoft Windows";
88#else
89 const char* filename = "/proc/version";
90 const char expected[] = "Linux";
91#endif
92 int fd = open(filename, O_RDONLY);
Elliott Hughes56085ed2015-04-24 21:57:16 -070093 ASSERT_NE(-1, fd) << strerror(errno);
94
95 char buf[1024];
96 memset(buf, 0, sizeof(buf));
Spencer Lowcf168a82015-05-24 15:36:28 -070097 ASSERT_TRUE(android::base::ReadFully(fd, buf, sizeof(expected) - 1));
98 ASSERT_STREQ(expected, buf);
Elliott Hughes56085ed2015-04-24 21:57:16 -070099
100 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)) << strerror(errno);
101
102 ASSERT_FALSE(android::base::ReadFully(fd, buf, sizeof(buf)));
103
104 close(fd);
105}
106
107TEST(file, WriteFully) {
108 TemporaryFile tf;
109 ASSERT_TRUE(tf.fd != -1);
110 ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3));
111 std::string s;
Alex Vallée47d67c92015-05-06 16:26:00 -0400112 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s))
Dan Albert850188f2015-04-29 17:09:53 -0700113 << strerror(errno);
Elliott Hughes56085ed2015-04-24 21:57:16 -0700114 EXPECT_EQ("abc", s);
115}