blob: 4e82e76f9f1652281a9f21f8df2a35bf1a6771e1 [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 Liu82bac0d2016-12-28 16:06:19 +080020#include <fcntl.h>
21
22#include <sys/stat.h>
Mark Salyzyn62767fe2016-10-27 07:45:34 -070023
Elliott Hughesf682b472015-02-06 12:19:48 -080024#include <gtest/gtest.h>
25
Yongqin Liu82bac0d2016-12-28 16:06:19 +080026#include <android-base/stringprintf.h>
27#include <android-base/test_utils.h>
28
Elliott Hughesf682b472015-02-06 12:19:48 -080029TEST(util, read_file_ENOENT) {
30 std::string s("hello");
31 errno = 0;
32 EXPECT_FALSE(read_file("/proc/does-not-exist", &s));
33 EXPECT_EQ(ENOENT, errno);
34 EXPECT_EQ("", s); // s was cleared.
35}
36
Yongqin Liu82bac0d2016-12-28 16:06:19 +080037TEST(util, read_file_group_writeable) {
38 std::string s("hello");
39 TemporaryFile tf;
40 ASSERT_TRUE(tf.fd != -1);
41 EXPECT_TRUE(write_file(tf.path, s.c_str())) << strerror(errno);
42 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
43 EXPECT_FALSE(read_file(tf.path, &s)) << strerror(errno);
44 EXPECT_EQ("", s); // s was cleared.
45}
46
47TEST(util, read_file_world_writeable) {
48 std::string s("hello");
49 TemporaryFile tf;
50 ASSERT_TRUE(tf.fd != -1);
51 EXPECT_TRUE(write_file(tf.path, s.c_str())) << strerror(errno);
52 EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
53 EXPECT_FALSE(read_file(tf.path, &s)) << strerror(errno);
54 EXPECT_EQ("", s); // s was cleared.
55}
56
57TEST(util, read_file_symbol_link) {
58 std::string s("hello");
59 errno = 0;
60 // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd
61 EXPECT_FALSE(read_file("/charger", &s));
62 EXPECT_EQ(ELOOP, errno);
63 EXPECT_EQ("", s); // s was cleared.
64}
65
Elliott Hughesf682b472015-02-06 12:19:48 -080066TEST(util, read_file_success) {
67 std::string s("hello");
68 EXPECT_TRUE(read_file("/proc/version", &s));
69 EXPECT_GT(s.length(), 6U);
70 EXPECT_EQ('\n', s[s.length() - 1]);
71 s[5] = 0;
72 EXPECT_STREQ("Linux", s.c_str());
73}
Elliott Hughes8d82ea02015-02-06 20:15:18 -080074
Yongqin Liu82bac0d2016-12-28 16:06:19 +080075TEST(util, write_file_not_exist) {
76 std::string s("hello");
77 std::string s2("hello");
78 TemporaryDir test_dir;
79 std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
80 EXPECT_TRUE(write_file(path.c_str(), s.c_str()));
81 EXPECT_TRUE(read_file(path.c_str(), &s2));
82 EXPECT_EQ(s, s2);
83 struct stat sb;
84 int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
85 EXPECT_NE(-1, fd);
86 EXPECT_EQ(0, fstat(fd, &sb));
87 EXPECT_NE(0u, sb.st_mode & S_IRUSR);
88 EXPECT_NE(0u, sb.st_mode & S_IWUSR);
89 EXPECT_EQ(0u, sb.st_mode & S_IXUSR);
90 EXPECT_EQ(0u, sb.st_mode & S_IRGRP);
91 EXPECT_EQ(0u, sb.st_mode & S_IWGRP);
92 EXPECT_EQ(0u, sb.st_mode & S_IXGRP);
93 EXPECT_EQ(0u, sb.st_mode & S_IROTH);
94 EXPECT_EQ(0u, sb.st_mode & S_IWOTH);
95 EXPECT_EQ(0u, sb.st_mode & S_IXOTH);
96 EXPECT_EQ(0, unlink(path.c_str()));
97}
98
99TEST(util, write_file_exist) {
100 std::string s2("");
101 TemporaryFile tf;
102 ASSERT_TRUE(tf.fd != -1);
103 EXPECT_TRUE(write_file(tf.path, "1hello1")) << strerror(errno);
104 EXPECT_TRUE(read_file(tf.path, &s2));
105 EXPECT_STREQ("1hello1", s2.c_str());
106 EXPECT_TRUE(write_file(tf.path, "2hello2"));
107 EXPECT_TRUE(read_file(tf.path, &s2));
108 EXPECT_STREQ("2hello2", s2.c_str());
109}
110
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800111TEST(util, decode_uid) {
112 EXPECT_EQ(0U, decode_uid("root"));
Nick Kralevichd2104df2015-06-18 17:46:54 -0700113 EXPECT_EQ(UINT_MAX, decode_uid("toot"));
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800114 EXPECT_EQ(123U, decode_uid("123"));
115}