ueventd_parser_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (C) 2018 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 "ueventd_parser.h"
  17. #include <android-base/file.h>
  18. #include <gtest/gtest.h>
  19. #include <private/android_filesystem_config.h>
  20. namespace android {
  21. namespace init {
  22. void TestSubsystems(const Subsystem& expected, const Subsystem& test) {
  23. EXPECT_EQ(expected.name_, test.name_);
  24. EXPECT_EQ(expected.devname_source_, test.devname_source_) << expected.name_;
  25. EXPECT_EQ(expected.dir_name_, test.dir_name_) << expected.name_;
  26. }
  27. void TestPermissions(const Permissions& expected, const Permissions& test) {
  28. EXPECT_EQ(expected.name_, test.name_);
  29. EXPECT_EQ(expected.perm_, test.perm_) << expected.name_;
  30. EXPECT_EQ(expected.uid_, test.uid_) << expected.name_;
  31. EXPECT_EQ(expected.gid_, test.gid_) << expected.name_;
  32. EXPECT_EQ(expected.prefix_, test.prefix_) << expected.name_;
  33. EXPECT_EQ(expected.wildcard_, test.wildcard_) << expected.name_;
  34. }
  35. void TestSysfsPermissions(const SysfsPermissions& expected, const SysfsPermissions& test) {
  36. TestPermissions(expected, test);
  37. EXPECT_EQ(expected.attribute_, test.attribute_);
  38. }
  39. template <typename T, typename F>
  40. void TestVector(const T& expected, const T& test, F function) {
  41. ASSERT_EQ(expected.size(), test.size());
  42. auto expected_it = expected.begin();
  43. auto test_it = test.begin();
  44. for (; expected_it != expected.end(); ++expected_it, ++test_it) {
  45. function(*expected_it, *test_it);
  46. }
  47. }
  48. void TestUeventdFile(const std::string& content, const UeventdConfiguration& expected) {
  49. TemporaryFile tf;
  50. ASSERT_TRUE(tf.fd != -1);
  51. ASSERT_TRUE(android::base::WriteStringToFd(content, tf.fd));
  52. auto result = ParseConfig({tf.path});
  53. TestVector(expected.subsystems, result.subsystems, TestSubsystems);
  54. TestVector(expected.sysfs_permissions, result.sysfs_permissions, TestSysfsPermissions);
  55. TestVector(expected.dev_permissions, result.dev_permissions, TestPermissions);
  56. EXPECT_EQ(expected.firmware_directories, result.firmware_directories);
  57. }
  58. TEST(ueventd_parser, EmptyFile) {
  59. TestUeventdFile("", {});
  60. }
  61. TEST(ueventd_parser, Subsystems) {
  62. auto ueventd_file = R"(
  63. subsystem test_devname
  64. devname uevent_devname
  65. subsystem test_devpath_no_dirname
  66. devname uevent_devpath
  67. subsystem test_devname2
  68. devname uevent_devname
  69. subsystem test_devpath_dirname
  70. devname uevent_devpath
  71. dirname /dev/graphics
  72. )";
  73. auto subsystems = std::vector<Subsystem>{
  74. {"test_devname", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
  75. {"test_devpath_no_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev"},
  76. {"test_devname2", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
  77. {"test_devpath_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev/graphics"}};
  78. TestUeventdFile(ueventd_file, {subsystems, {}, {}, {}});
  79. }
  80. TEST(ueventd_parser, Permissions) {
  81. auto ueventd_file = R"(
  82. /dev/rtc0 0640 system system
  83. /dev/graphics/* 0660 root graphics
  84. /dev/*/test 0660 root system
  85. /sys/devices/platform/trusty.* trusty_version 0440 root log
  86. /sys/devices/virtual/input/input enable 0660 root input
  87. /sys/devices/virtual/*/input poll_delay 0660 root input
  88. )";
  89. auto permissions = std::vector<Permissions>{
  90. {"/dev/rtc0", 0640, AID_SYSTEM, AID_SYSTEM},
  91. {"/dev/graphics/*", 0660, AID_ROOT, AID_GRAPHICS},
  92. {"/dev/*/test", 0660, AID_ROOT, AID_SYSTEM},
  93. };
  94. auto sysfs_permissions = std::vector<SysfsPermissions>{
  95. {"/sys/devices/platform/trusty.*", "trusty_version", 0440, AID_ROOT, AID_LOG},
  96. {"/sys/devices/virtual/input/input", "enable", 0660, AID_ROOT, AID_INPUT},
  97. {"/sys/devices/virtual/*/input", "poll_delay", 0660, AID_ROOT, AID_INPUT},
  98. };
  99. TestUeventdFile(ueventd_file, {{}, sysfs_permissions, permissions, {}});
  100. }
  101. TEST(ueventd_parser, FirmwareDirectories) {
  102. auto ueventd_file = R"(
  103. firmware_directories /first/ /second /third
  104. firmware_directories /more
  105. )";
  106. auto firmware_directories = std::vector<std::string>{
  107. "/first/",
  108. "/second",
  109. "/third",
  110. "/more",
  111. };
  112. TestUeventdFile(ueventd_file, {{}, {}, {}, firmware_directories});
  113. }
  114. TEST(ueventd_parser, UeventSocketRcvbufSize) {
  115. auto ueventd_file = R"(
  116. uevent_socket_rcvbuf_size 8k
  117. uevent_socket_rcvbuf_size 8M
  118. )";
  119. TestUeventdFile(ueventd_file, {{}, {}, {}, {}, false, 8 * 1024 * 1024});
  120. }
  121. TEST(ueventd_parser, AllTogether) {
  122. auto ueventd_file = R"(
  123. /dev/rtc0 0640 system system
  124. firmware_directories /first/ /second /third
  125. /sys/devices/platform/trusty.* trusty_version 0440 root log
  126. subsystem test_devname
  127. devname uevent_devname
  128. /dev/graphics/* 0660 root graphics
  129. subsystem test_devpath_no_dirname
  130. devname uevent_devpath
  131. /sys/devices/virtual/input/input enable 0660 root input
  132. ## this is a comment
  133. subsystem test_devname2
  134. ## another comment
  135. devname uevent_devname
  136. subsystem test_devpath_dirname
  137. devname uevent_devpath
  138. dirname /dev/graphics
  139. /dev/*/test 0660 root system
  140. /sys/devices/virtual/*/input poll_delay 0660 root input
  141. firmware_directories /more
  142. uevent_socket_rcvbuf_size 6M
  143. #ending comment
  144. )";
  145. auto subsystems = std::vector<Subsystem>{
  146. {"test_devname", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
  147. {"test_devpath_no_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev"},
  148. {"test_devname2", Subsystem::DEVNAME_UEVENT_DEVNAME, "/dev"},
  149. {"test_devpath_dirname", Subsystem::DEVNAME_UEVENT_DEVPATH, "/dev/graphics"}};
  150. auto permissions = std::vector<Permissions>{
  151. {"/dev/rtc0", 0640, AID_SYSTEM, AID_SYSTEM},
  152. {"/dev/graphics/*", 0660, AID_ROOT, AID_GRAPHICS},
  153. {"/dev/*/test", 0660, AID_ROOT, AID_SYSTEM},
  154. };
  155. auto sysfs_permissions = std::vector<SysfsPermissions>{
  156. {"/sys/devices/platform/trusty.*", "trusty_version", 0440, AID_ROOT, AID_LOG},
  157. {"/sys/devices/virtual/input/input", "enable", 0660, AID_ROOT, AID_INPUT},
  158. {"/sys/devices/virtual/*/input", "poll_delay", 0660, AID_ROOT, AID_INPUT},
  159. };
  160. auto firmware_directories = std::vector<std::string>{
  161. "/first/",
  162. "/second",
  163. "/third",
  164. "/more",
  165. };
  166. size_t uevent_socket_rcvbuf_size = 6 * 1024 * 1024;
  167. TestUeventdFile(ueventd_file, {subsystems, sysfs_permissions, permissions, firmware_directories,
  168. false, uevent_socket_rcvbuf_size});
  169. }
  170. // All of these lines are ill-formed, so test that there is 0 output.
  171. TEST(ueventd_parser, ParseErrors) {
  172. auto ueventd_file = R"(
  173. /dev/rtc0 badmode baduidbad system
  174. /dev/rtc0 0640 baduidbad system
  175. /dev/rtc0 0640 system baduidbad
  176. firmware_directories #no directory listed
  177. /sys/devices/platform/trusty.* trusty_version badmode root log
  178. /sys/devices/platform/trusty.* trusty_version 0440 baduidbad log
  179. /sys/devices/platform/trusty.* trusty_version 0440 root baduidbad
  180. uevent_socket_rcvbuf_size blah
  181. subsystem #no name
  182. )";
  183. TestUeventdFile(ueventd_file, {});
  184. }
  185. } // namespace init
  186. } // namespace android