blob: a24185b00f83e47d20a749d2461c4ab9d22115de [file] [log] [blame]
Elliott Hughesf682b472015-02-06 12:19:48 -08001/*
2 * Copyright (C) 2015 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
17#include "util.h"
18
19#include <errno.h>
Yongqin Liudbe88e72016-12-28 16:06:19 +080020#include <fcntl.h>
21#include <sys/stat.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070022
Yongqin Liudbe88e72016-12-28 16:06:19 +080023#include <android-base/stringprintf.h>
Tom Cherry53089aa2017-03-31 15:47:33 -070024#include <android-base/test_utils.h>
Elliott Hughesf682b472015-02-06 12:19:48 -080025#include <gtest/gtest.h>
26
27TEST(util, read_file_ENOENT) {
28 std::string s("hello");
29 errno = 0;
30 EXPECT_FALSE(read_file("/proc/does-not-exist", &s));
31 EXPECT_EQ(ENOENT, errno);
32 EXPECT_EQ("", s); // s was cleared.
33}
34
Yongqin Liudbe88e72016-12-28 16:06:19 +080035TEST(util, read_file_group_writeable) {
36 std::string s("hello");
37 TemporaryFile tf;
38 ASSERT_TRUE(tf.fd != -1);
39 EXPECT_TRUE(write_file(tf.path, s)) << strerror(errno);
40 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
41 EXPECT_FALSE(read_file(tf.path, &s)) << strerror(errno);
42 EXPECT_EQ("", s); // s was cleared.
43}
44
45TEST(util, read_file_world_writeable) {
46 std::string s("hello");
47 TemporaryFile tf;
48 ASSERT_TRUE(tf.fd != -1);
49 EXPECT_TRUE(write_file(tf.path, s.c_str())) << strerror(errno);
50 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
51 EXPECT_FALSE(read_file(tf.path, &s)) << strerror(errno);
52 EXPECT_EQ("", s); // s was cleared.
53}
54
55TEST(util, read_file_symbolic_link) {
56 std::string s("hello");
57 errno = 0;
58 // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd
59 EXPECT_FALSE(read_file("/charger", &s));
60 EXPECT_EQ(ELOOP, errno);
61 EXPECT_EQ("", s); // s was cleared.
62}
63
Elliott Hughesf682b472015-02-06 12:19:48 -080064TEST(util, read_file_success) {
65 std::string s("hello");
66 EXPECT_TRUE(read_file("/proc/version", &s));
67 EXPECT_GT(s.length(), 6U);
68 EXPECT_EQ('\n', s[s.length() - 1]);
69 s[5] = 0;
70 EXPECT_STREQ("Linux", s.c_str());
71}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080072
Tom Cherry53089aa2017-03-31 15:47:33 -070073TEST(util, write_file_binary) {
74 std::string contents("abcd");
75 contents.push_back('\0');
76 contents.push_back('\0');
77 contents.append("dcba");
78 ASSERT_EQ(10u, contents.size());
79
80 TemporaryFile tf;
81 ASSERT_TRUE(tf.fd != -1);
82 EXPECT_TRUE(write_file(tf.path, contents)) << strerror(errno);
83
84 std::string read_back_contents;
85 EXPECT_TRUE(read_file(tf.path, &read_back_contents)) << strerror(errno);
86 EXPECT_EQ(contents, read_back_contents);
87 EXPECT_EQ(10u, read_back_contents.size());
88}
89
Yongqin Liudbe88e72016-12-28 16:06:19 +080090TEST(util, write_file_not_exist) {
91 std::string s("hello");
92 std::string s2("hello");
93 TemporaryDir test_dir;
94 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
95 EXPECT_TRUE(write_file(path, s));
96 EXPECT_TRUE(read_file(path, &s2));
97 EXPECT_EQ(s, s2);
98 struct stat sb;
99 int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
100 EXPECT_NE(-1, fd);
101 EXPECT_EQ(0, fstat(fd, &sb));
102 EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
103 EXPECT_EQ(0, unlink(path.c_str()));
104}
105
106TEST(util, write_file_exist) {
107 std::string s2("");
108 TemporaryFile tf;
109 ASSERT_TRUE(tf.fd != -1);
110 EXPECT_TRUE(write_file(tf.path, "1hello1")) << strerror(errno);
111 EXPECT_TRUE(read_file(tf.path, &s2));
112 EXPECT_STREQ("1hello1", s2.c_str());
113 EXPECT_TRUE(write_file(tf.path, "2ll2"));
114 EXPECT_TRUE(read_file(tf.path, &s2));
115 EXPECT_STREQ("2ll2", s2.c_str());
116}
117
Tom Cherry517e1f12017-05-04 17:40:33 -0700118TEST(util, DecodeUid) {
119 uid_t decoded_uid;
120 std::string err;
121
122 EXPECT_TRUE(DecodeUid("root", &decoded_uid, &err));
123 EXPECT_EQ("", err);
124 EXPECT_EQ(0U, decoded_uid);
125
126 EXPECT_FALSE(DecodeUid("toot", &decoded_uid, &err));
127 EXPECT_EQ("getpwnam failed: No such file or directory", err);
128 EXPECT_EQ(UINT_MAX, decoded_uid);
129
130 EXPECT_TRUE(DecodeUid("123", &decoded_uid, &err));
131 EXPECT_EQ("", err);
132 EXPECT_EQ(123U, decoded_uid);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800133}
Tom Cherry060b74b2017-04-12 14:27:51 -0700134
135TEST(util, is_dir) {
136 TemporaryDir test_dir;
137 EXPECT_TRUE(is_dir(test_dir.path));
138 TemporaryFile tf;
139 EXPECT_FALSE(is_dir(tf.path));
140}
141
Tom Cherry060b74b2017-04-12 14:27:51 -0700142TEST(util, mkdir_recursive) {
143 TemporaryDir test_dir;
144 std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
Tom Cherrye7656b72017-05-01 17:10:09 -0700145 EXPECT_EQ(0, mkdir_recursive(path, 0755, nullptr));
Tom Cherry060b74b2017-04-12 14:27:51 -0700146 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
147 EXPECT_TRUE(is_dir(path1.c_str()));
148 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
149 EXPECT_TRUE(is_dir(path1.c_str()));
150 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
151 EXPECT_TRUE(is_dir(path1.c_str()));
152}
153
154TEST(util, mkdir_recursive_extra_slashes) {
155 TemporaryDir test_dir;
156 std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
Tom Cherrye7656b72017-05-01 17:10:09 -0700157 EXPECT_EQ(0, mkdir_recursive(path, 0755, nullptr));
Tom Cherry060b74b2017-04-12 14:27:51 -0700158 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
159 EXPECT_TRUE(is_dir(path1.c_str()));
160 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
161 EXPECT_TRUE(is_dir(path1.c_str()));
162 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
163 EXPECT_TRUE(is_dir(path1.c_str()));
164}