blob: 3ae53a481fc74704299132c8a3a48cb3a395dcc6 [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
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070027using namespace std::literals::string_literals;
28
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070032TEST(util, ReadFile_ENOENT) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070033 errno = 0;
Tom Cherry11a3aee2017-08-03 12:54:07 -070034 auto file_contents = ReadFile("/proc/does-not-exist");
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070035 EXPECT_EQ(ENOENT, errno);
Tom Cherry11a3aee2017-08-03 12:54:07 -070036 ASSERT_FALSE(file_contents);
Tom Cherry130e3d72017-08-22 16:07:15 -070037 EXPECT_EQ("open() failed: No such file or directory", file_contents.error_string());
Elliott Hughesf682b472015-02-06 12:19:48 -080038}
39
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070040TEST(util, ReadFileGroupWriteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080041 std::string s("hello");
42 TemporaryFile tf;
43 ASSERT_TRUE(tf.fd != -1);
Tom Cherry11a3aee2017-08-03 12:54:07 -070044 EXPECT_TRUE(WriteFile(tf.path, s)) << strerror(errno);
Yongqin Liudbe88e72016-12-28 16:06:19 +080045 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry11a3aee2017-08-03 12:54:07 -070046 auto file_contents = ReadFile(tf.path);
47 ASSERT_FALSE(file_contents) << strerror(errno);
Tom Cherry130e3d72017-08-22 16:07:15 -070048 EXPECT_EQ("Skipping insecure file", file_contents.error_string());
Yongqin Liudbe88e72016-12-28 16:06:19 +080049}
50
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070051TEST(util, ReadFileWorldWiteable) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080052 std::string s("hello");
53 TemporaryFile tf;
54 ASSERT_TRUE(tf.fd != -1);
Tom Cherry11a3aee2017-08-03 12:54:07 -070055 EXPECT_TRUE(WriteFile(tf.path, s)) << strerror(errno);
Yongqin Liudbe88e72016-12-28 16:06:19 +080056 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
Tom Cherry11a3aee2017-08-03 12:54:07 -070057 auto file_contents = ReadFile(tf.path);
58 ASSERT_FALSE(file_contents) << strerror(errno);
Tom Cherry130e3d72017-08-22 16:07:15 -070059 EXPECT_EQ("Skipping insecure file", file_contents.error_string());
Yongqin Liudbe88e72016-12-28 16:06:19 +080060}
61
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070062TEST(util, ReadFileSymbolicLink) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080063 errno = 0;
64 // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd
Tom Cherry11a3aee2017-08-03 12:54:07 -070065 auto file_contents = ReadFile("/charger");
Yongqin Liudbe88e72016-12-28 16:06:19 +080066 EXPECT_EQ(ELOOP, errno);
Tom Cherry11a3aee2017-08-03 12:54:07 -070067 ASSERT_FALSE(file_contents);
Tom Cherry130e3d72017-08-22 16:07:15 -070068 EXPECT_EQ("open() failed: Too many symbolic links encountered", file_contents.error_string());
Yongqin Liudbe88e72016-12-28 16:06:19 +080069}
70
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070071TEST(util, ReadFileSuccess) {
Tom Cherry11a3aee2017-08-03 12:54:07 -070072 auto file_contents = ReadFile("/proc/version");
73 ASSERT_TRUE(file_contents);
74 EXPECT_GT(file_contents->length(), 6U);
75 EXPECT_EQ('\n', file_contents->at(file_contents->length() - 1));
76 (*file_contents)[5] = 0;
77 EXPECT_STREQ("Linux", file_contents->c_str());
Elliott Hughesf682b472015-02-06 12:19:48 -080078}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080079
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070080TEST(util, WriteFileBinary) {
Tom Cherry53089aa2017-03-31 15:47:33 -070081 std::string contents("abcd");
82 contents.push_back('\0');
83 contents.push_back('\0');
84 contents.append("dcba");
85 ASSERT_EQ(10u, contents.size());
86
87 TemporaryFile tf;
88 ASSERT_TRUE(tf.fd != -1);
Tom Cherry11a3aee2017-08-03 12:54:07 -070089 EXPECT_TRUE(WriteFile(tf.path, contents)) << strerror(errno);
Tom Cherry53089aa2017-03-31 15:47:33 -070090
Tom Cherry11a3aee2017-08-03 12:54:07 -070091 auto read_back_contents = ReadFile(tf.path);
92 ASSERT_TRUE(read_back_contents) << strerror(errno);
93 EXPECT_EQ(contents, *read_back_contents);
94 EXPECT_EQ(10u, read_back_contents->size());
Tom Cherry53089aa2017-03-31 15:47:33 -070095}
96
Tom Cherry2cbbe9f2017-05-04 18:17:33 -070097TEST(util, WriteFileNotExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +080098 std::string s("hello");
Yongqin Liudbe88e72016-12-28 16:06:19 +080099 TemporaryDir test_dir;
100 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
Tom Cherry11a3aee2017-08-03 12:54:07 -0700101 EXPECT_TRUE(WriteFile(path, s));
102 auto file_contents = ReadFile(path);
103 ASSERT_TRUE(file_contents);
104 EXPECT_EQ(s, *file_contents);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800105 struct stat sb;
106 int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
107 EXPECT_NE(-1, fd);
108 EXPECT_EQ(0, fstat(fd, &sb));
109 EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
110 EXPECT_EQ(0, unlink(path.c_str()));
111}
112
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700113TEST(util, WriteFileExist) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800114 TemporaryFile tf;
115 ASSERT_TRUE(tf.fd != -1);
Tom Cherry11a3aee2017-08-03 12:54:07 -0700116 EXPECT_TRUE(WriteFile(tf.path, "1hello1")) << strerror(errno);
117 auto file_contents = ReadFile(tf.path);
118 ASSERT_TRUE(file_contents);
119 EXPECT_EQ("1hello1", *file_contents);
120 EXPECT_TRUE(WriteFile(tf.path, "2ll2"));
121 file_contents = ReadFile(tf.path);
122 ASSERT_TRUE(file_contents);
123 EXPECT_EQ("2ll2", *file_contents);
Yongqin Liudbe88e72016-12-28 16:06:19 +0800124}
125
Tom Cherry517e1f12017-05-04 17:40:33 -0700126TEST(util, DecodeUid) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700127 auto decoded_uid = DecodeUid("root");
128 EXPECT_TRUE(decoded_uid);
129 EXPECT_EQ(0U, *decoded_uid);
Tom Cherry517e1f12017-05-04 17:40:33 -0700130
Tom Cherry11a3aee2017-08-03 12:54:07 -0700131 decoded_uid = DecodeUid("toot");
132 EXPECT_FALSE(decoded_uid);
Tom Cherry130e3d72017-08-22 16:07:15 -0700133 EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error_string());
Tom Cherry517e1f12017-05-04 17:40:33 -0700134
Tom Cherry11a3aee2017-08-03 12:54:07 -0700135 decoded_uid = DecodeUid("123");
136 EXPECT_TRUE(decoded_uid);
137 EXPECT_EQ(123U, *decoded_uid);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800138}
Tom Cherry060b74b2017-04-12 14:27:51 -0700139
140TEST(util, is_dir) {
141 TemporaryDir test_dir;
142 EXPECT_TRUE(is_dir(test_dir.path));
143 TemporaryFile tf;
144 EXPECT_FALSE(is_dir(tf.path));
145}
146
Tom Cherry060b74b2017-04-12 14:27:51 -0700147TEST(util, mkdir_recursive) {
148 TemporaryDir test_dir;
149 std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700150 EXPECT_TRUE(mkdir_recursive(path, 0755));
Tom Cherry060b74b2017-04-12 14:27:51 -0700151 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
152 EXPECT_TRUE(is_dir(path1.c_str()));
153 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
154 EXPECT_TRUE(is_dir(path1.c_str()));
155 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
156 EXPECT_TRUE(is_dir(path1.c_str()));
157}
158
159TEST(util, mkdir_recursive_extra_slashes) {
160 TemporaryDir test_dir;
161 std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700162 EXPECT_TRUE(mkdir_recursive(path, 0755));
Tom Cherry060b74b2017-04-12 14:27:51 -0700163 std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
164 EXPECT_TRUE(is_dir(path1.c_str()));
165 std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
166 EXPECT_TRUE(is_dir(path1.c_str()));
167 std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
168 EXPECT_TRUE(is_dir(path1.c_str()));
169}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700170
171} // namespace init
172} // namespace android