Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 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> |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame^] | 20 | #include <fcntl.h> |
| 21 | #include <sys/stat.h> |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 22 | |
Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame^] | 23 | #include <android-base/stringprintf.h> |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame] | 24 | #include <android-base/test_utils.h> |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | TEST(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 Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame^] | 35 | TEST(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 | |
| 45 | TEST(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 | |
| 55 | TEST(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 Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 64 | TEST(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 Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 72 | |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame] | 73 | TEST(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 Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame^] | 90 | TEST(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 | |
| 106 | TEST(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 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 118 | TEST(util, decode_uid) { |
| 119 | EXPECT_EQ(0U, decode_uid("root")); |
Nick Kralevich | d2104df | 2015-06-18 17:46:54 -0700 | [diff] [blame] | 120 | EXPECT_EQ(UINT_MAX, decode_uid("toot")); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 121 | EXPECT_EQ(123U, decode_uid("123")); |
| 122 | } |