util_test.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include "util.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. #include <android-base/file.h>
  21. #include <android-base/stringprintf.h>
  22. #include <gtest/gtest.h>
  23. using namespace std::literals::string_literals;
  24. namespace android {
  25. namespace init {
  26. TEST(util, ReadFile_ENOENT) {
  27. errno = 0;
  28. auto file_contents = ReadFile("/proc/does-not-exist");
  29. EXPECT_EQ(ENOENT, errno);
  30. ASSERT_FALSE(file_contents);
  31. EXPECT_EQ("open() failed: No such file or directory", file_contents.error_string());
  32. }
  33. TEST(util, ReadFileGroupWriteable) {
  34. std::string s("hello");
  35. TemporaryFile tf;
  36. ASSERT_TRUE(tf.fd != -1);
  37. EXPECT_TRUE(WriteFile(tf.path, s)) << strerror(errno);
  38. EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0620, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
  39. auto file_contents = ReadFile(tf.path);
  40. ASSERT_FALSE(file_contents) << strerror(errno);
  41. EXPECT_EQ("Skipping insecure file", file_contents.error_string());
  42. }
  43. TEST(util, ReadFileWorldWiteable) {
  44. std::string s("hello");
  45. TemporaryFile tf;
  46. ASSERT_TRUE(tf.fd != -1);
  47. EXPECT_TRUE(WriteFile(tf.path, s)) << strerror(errno);
  48. EXPECT_NE(-1, fchmodat(AT_FDCWD, tf.path, 0602, AT_SYMLINK_NOFOLLOW)) << strerror(errno);
  49. auto file_contents = ReadFile(tf.path);
  50. ASSERT_FALSE(file_contents) << strerror(errno);
  51. EXPECT_EQ("Skipping insecure file", file_contents.error_string());
  52. }
  53. TEST(util, ReadFileSymbolicLink) {
  54. errno = 0;
  55. // lrwxrwxrwx 1 root root 13 1970-01-01 00:00 charger -> /sbin/healthd
  56. auto file_contents = ReadFile("/charger");
  57. EXPECT_EQ(ELOOP, errno);
  58. ASSERT_FALSE(file_contents);
  59. EXPECT_EQ("open() failed: Too many symbolic links encountered", file_contents.error_string());
  60. }
  61. TEST(util, ReadFileSuccess) {
  62. auto file_contents = ReadFile("/proc/version");
  63. ASSERT_TRUE(file_contents);
  64. EXPECT_GT(file_contents->length(), 6U);
  65. EXPECT_EQ('\n', file_contents->at(file_contents->length() - 1));
  66. (*file_contents)[5] = 0;
  67. EXPECT_STREQ("Linux", file_contents->c_str());
  68. }
  69. TEST(util, WriteFileBinary) {
  70. std::string contents("abcd");
  71. contents.push_back('\0');
  72. contents.push_back('\0');
  73. contents.append("dcba");
  74. ASSERT_EQ(10u, contents.size());
  75. TemporaryFile tf;
  76. ASSERT_TRUE(tf.fd != -1);
  77. EXPECT_TRUE(WriteFile(tf.path, contents)) << strerror(errno);
  78. auto read_back_contents = ReadFile(tf.path);
  79. ASSERT_TRUE(read_back_contents) << strerror(errno);
  80. EXPECT_EQ(contents, *read_back_contents);
  81. EXPECT_EQ(10u, read_back_contents->size());
  82. }
  83. TEST(util, WriteFileNotExist) {
  84. std::string s("hello");
  85. TemporaryDir test_dir;
  86. std::string path = android::base::StringPrintf("%s/does-not-exist", test_dir.path);
  87. EXPECT_TRUE(WriteFile(path, s));
  88. auto file_contents = ReadFile(path);
  89. ASSERT_TRUE(file_contents);
  90. EXPECT_EQ(s, *file_contents);
  91. struct stat sb;
  92. int fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
  93. EXPECT_NE(-1, fd);
  94. EXPECT_EQ(0, fstat(fd, &sb));
  95. EXPECT_EQ((const unsigned int)(S_IRUSR | S_IWUSR), sb.st_mode & 0777);
  96. EXPECT_EQ(0, unlink(path.c_str()));
  97. }
  98. TEST(util, WriteFileExist) {
  99. TemporaryFile tf;
  100. ASSERT_TRUE(tf.fd != -1);
  101. EXPECT_TRUE(WriteFile(tf.path, "1hello1")) << strerror(errno);
  102. auto file_contents = ReadFile(tf.path);
  103. ASSERT_TRUE(file_contents);
  104. EXPECT_EQ("1hello1", *file_contents);
  105. EXPECT_TRUE(WriteFile(tf.path, "2ll2"));
  106. file_contents = ReadFile(tf.path);
  107. ASSERT_TRUE(file_contents);
  108. EXPECT_EQ("2ll2", *file_contents);
  109. }
  110. TEST(util, DecodeUid) {
  111. auto decoded_uid = DecodeUid("root");
  112. EXPECT_TRUE(decoded_uid);
  113. EXPECT_EQ(0U, *decoded_uid);
  114. decoded_uid = DecodeUid("toot");
  115. EXPECT_FALSE(decoded_uid);
  116. EXPECT_EQ("getpwnam failed: No such file or directory", decoded_uid.error_string());
  117. decoded_uid = DecodeUid("123");
  118. EXPECT_TRUE(decoded_uid);
  119. EXPECT_EQ(123U, *decoded_uid);
  120. }
  121. TEST(util, is_dir) {
  122. TemporaryDir test_dir;
  123. EXPECT_TRUE(is_dir(test_dir.path));
  124. TemporaryFile tf;
  125. EXPECT_FALSE(is_dir(tf.path));
  126. }
  127. TEST(util, mkdir_recursive) {
  128. TemporaryDir test_dir;
  129. std::string path = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
  130. EXPECT_TRUE(mkdir_recursive(path, 0755));
  131. std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
  132. EXPECT_TRUE(is_dir(path1.c_str()));
  133. std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
  134. EXPECT_TRUE(is_dir(path1.c_str()));
  135. std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
  136. EXPECT_TRUE(is_dir(path1.c_str()));
  137. }
  138. TEST(util, mkdir_recursive_extra_slashes) {
  139. TemporaryDir test_dir;
  140. std::string path = android::base::StringPrintf("%s/three////directories/deep//", test_dir.path);
  141. EXPECT_TRUE(mkdir_recursive(path, 0755));
  142. std::string path1 = android::base::StringPrintf("%s/three", test_dir.path);
  143. EXPECT_TRUE(is_dir(path1.c_str()));
  144. std::string path2 = android::base::StringPrintf("%s/three/directories", test_dir.path);
  145. EXPECT_TRUE(is_dir(path1.c_str()));
  146. std::string path3 = android::base::StringPrintf("%s/three/directories/deep", test_dir.path);
  147. EXPECT_TRUE(is_dir(path1.c_str()));
  148. }
  149. } // namespace init
  150. } // namespace android