util_test.cpp revision dbe88e7953ed53961056c7f5531d91d229293462
1/*
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>
20#include <fcntl.h>
21#include <sys/stat.h>
22
23#include <android-base/stringprintf.h>
24#include <android-base/test_utils.h>
25#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
35TEST(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
64TEST(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}
72
73TEST(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
90TEST(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
118TEST(util, decode_uid) {
119  EXPECT_EQ(0U, decode_uid("root"));
120  EXPECT_EQ(UINT_MAX, decode_uid("toot"));
121  EXPECT_EQ(123U, decode_uid("123"));
122}
123