| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 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 "devices.h" | 
|  | 18 |  | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 19 | #include <android-base/scopeguard.h> | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 20 | #include <android-base/test_utils.h> | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> | 
|  | 22 |  | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 23 | #include "util.h" | 
|  | 24 |  | 
|  | 25 | using namespace std::string_literals; | 
|  | 26 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 27 | namespace android { | 
|  | 28 | namespace init { | 
|  | 29 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 30 | class DeviceHandlerTester { | 
|  | 31 | public: | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 32 | void TestGetSymlinks(const std::string& platform_device, const Uevent& uevent, | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 33 | const std::vector<std::string> expected_links, bool block) { | 
| Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 34 | TemporaryDir fake_sys_root; | 
|  | 35 | device_handler_.sysfs_mount_point_ = fake_sys_root.path; | 
|  | 36 |  | 
|  | 37 | std::string platform_device_dir = fake_sys_root.path + platform_device; | 
|  | 38 | mkdir_recursive(platform_device_dir, 0777, nullptr); | 
|  | 39 |  | 
|  | 40 | std::string platform_bus = fake_sys_root.path + "/bus/platform"s; | 
|  | 41 | mkdir_recursive(platform_bus, 0777, nullptr); | 
|  | 42 | symlink(platform_bus.c_str(), (platform_device_dir + "/subsystem").c_str()); | 
|  | 43 |  | 
|  | 44 | mkdir_recursive(android::base::Dirname(fake_sys_root.path + uevent.path), 0777, nullptr); | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | std::vector<std::string> result; | 
|  | 47 | if (block) { | 
|  | 48 | result = device_handler_.GetBlockDeviceSymlinks(uevent); | 
|  | 49 | } else { | 
|  | 50 | result = device_handler_.GetCharacterDeviceSymlinks(uevent); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | auto expected_size = expected_links.size(); | 
|  | 54 | ASSERT_EQ(expected_size, result.size()); | 
|  | 55 | if (expected_size == 0) return; | 
|  | 56 |  | 
|  | 57 | // Explicitly iterate so the results are visible if a failure occurs | 
|  | 58 | for (unsigned int i = 0; i < expected_size; ++i) { | 
|  | 59 | EXPECT_EQ(expected_links[i], result[i]); | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | private: | 
|  | 64 | DeviceHandler device_handler_; | 
|  | 65 | }; | 
|  | 66 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 67 | TEST(device_handler, get_character_device_symlinks_success) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 68 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 69 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 70 | .path = "/devices/platform/some_device_name/usb/usb_device/name/tty2-1:1.0", | 
|  | 71 | .subsystem = "tty", | 
|  | 72 | }; | 
|  | 73 | std::vector<std::string> expected_result{"/dev/usb/ttyname"}; | 
|  | 74 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 75 | DeviceHandlerTester device_handler_tester_; | 
|  | 76 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 79 | TEST(device_handler, get_character_device_symlinks_no_pdev_match) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 80 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 81 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 82 | .path = "/device/name/tty2-1:1.0", .subsystem = "tty", | 
|  | 83 | }; | 
|  | 84 | std::vector<std::string> expected_result; | 
|  | 85 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 86 | DeviceHandlerTester device_handler_tester_; | 
|  | 87 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 90 | TEST(device_handler, get_character_device_symlinks_nothing_after_platform_device) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 91 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 92 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 93 | .path = "/devices/platform/some_device_name", .subsystem = "tty", | 
|  | 94 | }; | 
|  | 95 | std::vector<std::string> expected_result; | 
|  | 96 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 97 | DeviceHandlerTester device_handler_tester_; | 
|  | 98 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 101 | TEST(device_handler, get_character_device_symlinks_no_usb_found) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 102 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 103 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 104 | .path = "/devices/platform/some_device_name/bad/bad/", .subsystem = "tty", | 
|  | 105 | }; | 
|  | 106 | std::vector<std::string> expected_result; | 
|  | 107 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 108 | DeviceHandlerTester device_handler_tester_; | 
|  | 109 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 112 | TEST(device_handler, get_character_device_symlinks_no_roothub) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 113 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 114 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 115 | .path = "/devices/platform/some_device_name/usb/", .subsystem = "tty", | 
|  | 116 | }; | 
|  | 117 | std::vector<std::string> expected_result; | 
|  | 118 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 119 | DeviceHandlerTester device_handler_tester_; | 
|  | 120 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 123 | TEST(device_handler, get_character_device_symlinks_no_usb_device) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 124 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 125 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 126 | .path = "/devices/platform/some_device_name/usb/usb_device/", .subsystem = "tty", | 
|  | 127 | }; | 
|  | 128 | std::vector<std::string> expected_result; | 
|  | 129 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 130 | DeviceHandlerTester device_handler_tester_; | 
|  | 131 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 134 | TEST(device_handler, get_character_device_symlinks_no_final_slash) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 135 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 136 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 137 | .path = "/devices/platform/some_device_name/usb/usb_device/name", .subsystem = "tty", | 
|  | 138 | }; | 
|  | 139 | std::vector<std::string> expected_result; | 
|  | 140 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 141 | DeviceHandlerTester device_handler_tester_; | 
|  | 142 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 145 | TEST(device_handler, get_character_device_symlinks_no_final_name) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 146 | const char* platform_device = "/devices/platform/some_device_name"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 147 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 148 | .path = "/devices/platform/some_device_name/usb/usb_device//", .subsystem = "tty", | 
|  | 149 | }; | 
|  | 150 | std::vector<std::string> expected_result; | 
|  | 151 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 152 | DeviceHandlerTester device_handler_tester_; | 
|  | 153 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, false); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 156 | TEST(device_handler, get_block_device_symlinks_success_platform) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 157 | // These are actual paths from bullhead | 
|  | 158 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 159 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 160 | .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0", | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 161 | .partition_name = "", | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 162 | .partition_num = -1, | 
|  | 163 | }; | 
|  | 164 | std::vector<std::string> expected_result{"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0"}; | 
|  | 165 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 166 | DeviceHandlerTester device_handler_tester_; | 
|  | 167 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 170 | TEST(device_handler, get_block_device_symlinks_success_platform_with_partition) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 171 | // These are actual paths from bullhead | 
|  | 172 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 173 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 174 | .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", | 
|  | 175 | .partition_name = "modem", | 
|  | 176 | .partition_num = 1, | 
|  | 177 | }; | 
|  | 178 | std::vector<std::string> expected_result{ | 
|  | 179 | "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem", | 
|  | 180 | "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1", | 
|  | 181 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", | 
|  | 182 | }; | 
|  | 183 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 184 | DeviceHandlerTester device_handler_tester_; | 
|  | 185 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 188 | TEST(device_handler, get_block_device_symlinks_success_platform_with_partition_only_num) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 189 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 190 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 191 | .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 192 | .partition_name = "", | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 193 | .partition_num = 1, | 
|  | 194 | }; | 
|  | 195 | std::vector<std::string> expected_result{ | 
|  | 196 | "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1", | 
|  | 197 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", | 
|  | 198 | }; | 
|  | 199 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 200 | DeviceHandlerTester device_handler_tester_; | 
|  | 201 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 204 | TEST(device_handler, get_block_device_symlinks_success_platform_with_partition_only_name) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 205 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 206 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 207 | .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", | 
|  | 208 | .partition_name = "modem", | 
|  | 209 | .partition_num = -1, | 
|  | 210 | }; | 
|  | 211 | std::vector<std::string> expected_result{ | 
|  | 212 | "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem", | 
|  | 213 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", | 
|  | 214 | }; | 
|  | 215 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 216 | DeviceHandlerTester device_handler_tester_; | 
|  | 217 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 220 | TEST(device_handler, get_block_device_symlinks_success_pci) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 221 | const char* platform_device = "/devices/do/not/match"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 222 | Uevent uevent = { | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 223 | .path = "/devices/pci0000:00/0000:00:1f.2/mmcblk0", .partition_name = "", .partition_num = -1, | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 224 | }; | 
|  | 225 | std::vector<std::string> expected_result{"/dev/block/pci/pci0000:00/0000:00:1f.2/mmcblk0"}; | 
|  | 226 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 227 | DeviceHandlerTester device_handler_tester_; | 
|  | 228 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 231 | TEST(device_handler, get_block_device_symlinks_pci_bad_format) { | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 232 | const char* platform_device = "/devices/do/not/match"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 233 | Uevent uevent = { | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 234 | .path = "/devices/pci//mmcblk0", .partition_name = "", .partition_num = -1, | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 235 | }; | 
|  | 236 | std::vector<std::string> expected_result{}; | 
|  | 237 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 238 | DeviceHandlerTester device_handler_tester_; | 
|  | 239 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 242 | TEST(device_handler, get_block_device_symlinks_success_vbd) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 243 | const char* platform_device = "/devices/do/not/match"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 244 | Uevent uevent = { | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 245 | .path = "/devices/vbd-1234/mmcblk0", .partition_name = "", .partition_num = -1, | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 246 | }; | 
|  | 247 | std::vector<std::string> expected_result{"/dev/block/vbd/1234/mmcblk0"}; | 
|  | 248 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 249 | DeviceHandlerTester device_handler_tester_; | 
|  | 250 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 253 | TEST(device_handler, get_block_device_symlinks_vbd_bad_format) { | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 254 | const char* platform_device = "/devices/do/not/match"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 255 | Uevent uevent = { | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 256 | .path = "/devices/vbd-/mmcblk0", .partition_name = "", .partition_num = -1, | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 257 | }; | 
|  | 258 | std::vector<std::string> expected_result{}; | 
|  | 259 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 260 | DeviceHandlerTester device_handler_tester_; | 
|  | 261 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 264 | TEST(device_handler, get_block_device_symlinks_no_matches) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 265 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 266 | Uevent uevent = { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 267 | .path = "/devices/soc.0/not_the_device/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", | 
| Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 268 | .partition_name = "", | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 269 | .partition_num = -1, | 
|  | 270 | }; | 
|  | 271 | std::vector<std::string> expected_result; | 
|  | 272 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 273 | DeviceHandlerTester device_handler_tester_; | 
|  | 274 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result, true); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 275 | } | 
|  | 276 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 277 | TEST(device_handler, sanitize_null) { | 
|  | 278 | SanitizePartitionName(nullptr); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 279 | } | 
|  | 280 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 281 | TEST(device_handler, sanitize_empty) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 282 | std::string empty; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 283 | SanitizePartitionName(&empty); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 284 | EXPECT_EQ(0u, empty.size()); | 
|  | 285 | } | 
|  | 286 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 287 | TEST(device_handler, sanitize_allgood) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 288 | std::string good = | 
|  | 289 | "abcdefghijklmnopqrstuvwxyz" | 
|  | 290 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 
|  | 291 | "0123456789" | 
|  | 292 | "_-."; | 
|  | 293 | std::string good_copy = good; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 294 | SanitizePartitionName(&good); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 295 | EXPECT_EQ(good_copy, good); | 
|  | 296 | } | 
|  | 297 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 298 | TEST(device_handler, sanitize_somebad) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 299 | std::string string = "abc!@#$%^&*()"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 300 | SanitizePartitionName(&string); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 301 | EXPECT_EQ("abc__________", string); | 
|  | 302 | } | 
|  | 303 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 304 | TEST(device_handler, sanitize_allbad) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 305 | std::string string = "!@#$%^&*()"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 306 | SanitizePartitionName(&string); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 307 | EXPECT_EQ("__________", string); | 
|  | 308 | } | 
|  | 309 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 310 | TEST(device_handler, sanitize_onebad) { | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 311 | std::string string = ")"; | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 312 | SanitizePartitionName(&string); | 
| Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 313 | EXPECT_EQ("_", string); | 
|  | 314 | } | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 315 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 316 | TEST(device_handler, DevPermissionsMatchNormal) { | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 317 | // Basic from ueventd.rc | 
|  | 318 | // /dev/null                 0666   root       root | 
|  | 319 | Permissions permissions("/dev/null", 0666, 0, 0); | 
|  | 320 | EXPECT_TRUE(permissions.Match("/dev/null")); | 
|  | 321 | EXPECT_FALSE(permissions.Match("/dev/nullsuffix")); | 
|  | 322 | EXPECT_FALSE(permissions.Match("/dev/nul")); | 
|  | 323 | EXPECT_EQ(0666U, permissions.perm()); | 
|  | 324 | EXPECT_EQ(0U, permissions.uid()); | 
|  | 325 | EXPECT_EQ(0U, permissions.gid()); | 
|  | 326 | } | 
|  | 327 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 328 | TEST(device_handler, DevPermissionsMatchPrefix) { | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 329 | // Prefix from ueventd.rc | 
|  | 330 | // /dev/dri/*                0666   root       graphics | 
|  | 331 | Permissions permissions("/dev/dri/*", 0666, 0, 1000); | 
|  | 332 | EXPECT_TRUE(permissions.Match("/dev/dri/some_dri_device")); | 
|  | 333 | EXPECT_TRUE(permissions.Match("/dev/dri/some_other_dri_device")); | 
|  | 334 | EXPECT_TRUE(permissions.Match("/dev/dri/")); | 
|  | 335 | EXPECT_FALSE(permissions.Match("/dev/dr/non_match")); | 
|  | 336 | EXPECT_EQ(0666U, permissions.perm()); | 
|  | 337 | EXPECT_EQ(0U, permissions.uid()); | 
|  | 338 | EXPECT_EQ(1000U, permissions.gid()); | 
|  | 339 | } | 
|  | 340 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 341 | TEST(device_handler, DevPermissionsMatchWildcard) { | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 342 | // Wildcard example | 
|  | 343 | // /dev/device*name                0666   root       graphics | 
|  | 344 | Permissions permissions("/dev/device*name", 0666, 0, 1000); | 
|  | 345 | EXPECT_TRUE(permissions.Match("/dev/devicename")); | 
|  | 346 | EXPECT_TRUE(permissions.Match("/dev/device123name")); | 
|  | 347 | EXPECT_TRUE(permissions.Match("/dev/deviceabcname")); | 
|  | 348 | EXPECT_FALSE(permissions.Match("/dev/device123name/subdevice")); | 
|  | 349 | EXPECT_FALSE(permissions.Match("/dev/deviceame")); | 
|  | 350 | EXPECT_EQ(0666U, permissions.perm()); | 
|  | 351 | EXPECT_EQ(0U, permissions.uid()); | 
|  | 352 | EXPECT_EQ(1000U, permissions.gid()); | 
|  | 353 | } | 
|  | 354 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 355 | TEST(device_handler, DevPermissionsMatchWildcardPrefix) { | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 356 | // Wildcard+Prefix example | 
|  | 357 | // /dev/device*name*                0666   root       graphics | 
|  | 358 | Permissions permissions("/dev/device*name*", 0666, 0, 1000); | 
|  | 359 | EXPECT_TRUE(permissions.Match("/dev/devicename")); | 
|  | 360 | EXPECT_TRUE(permissions.Match("/dev/device123name")); | 
|  | 361 | EXPECT_TRUE(permissions.Match("/dev/deviceabcname")); | 
|  | 362 | EXPECT_TRUE(permissions.Match("/dev/device123namesomething")); | 
|  | 363 | // FNM_PATHNAME doesn't match '/' with * | 
|  | 364 | EXPECT_FALSE(permissions.Match("/dev/device123name/something")); | 
|  | 365 | EXPECT_FALSE(permissions.Match("/dev/deviceame")); | 
|  | 366 | EXPECT_EQ(0666U, permissions.perm()); | 
|  | 367 | EXPECT_EQ(0U, permissions.uid()); | 
|  | 368 | EXPECT_EQ(1000U, permissions.gid()); | 
|  | 369 | } | 
|  | 370 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 371 | TEST(device_handler, SysfsPermissionsMatchWithSubsystemNormal) { | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 372 | // /sys/devices/virtual/input/input*   enable      0660  root   input | 
|  | 373 | SysfsPermissions permissions("/sys/devices/virtual/input/input*", "enable", 0660, 0, 1001); | 
|  | 374 | EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/input0", "input")); | 
|  | 375 | EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/not_input0", "input")); | 
|  | 376 | EXPECT_EQ(0660U, permissions.perm()); | 
|  | 377 | EXPECT_EQ(0U, permissions.uid()); | 
|  | 378 | EXPECT_EQ(1001U, permissions.gid()); | 
|  | 379 | } | 
|  | 380 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 381 | TEST(device_handler, SysfsPermissionsMatchWithSubsystemClass) { | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 382 | // /sys/class/input/event*   enable      0660  root   input | 
|  | 383 | SysfsPermissions permissions("/sys/class/input/event*", "enable", 0660, 0, 1001); | 
|  | 384 | EXPECT_TRUE(permissions.MatchWithSubsystem( | 
|  | 385 | "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "input")); | 
|  | 386 | EXPECT_FALSE(permissions.MatchWithSubsystem( | 
|  | 387 | "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/not_event0", "input")); | 
|  | 388 | EXPECT_FALSE(permissions.MatchWithSubsystem( | 
|  | 389 | "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "not_input")); | 
|  | 390 | EXPECT_EQ(0660U, permissions.perm()); | 
|  | 391 | EXPECT_EQ(0U, permissions.uid()); | 
|  | 392 | EXPECT_EQ(1001U, permissions.gid()); | 
|  | 393 | } | 
|  | 394 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 395 | TEST(device_handler, SysfsPermissionsMatchWithSubsystemBus) { | 
| Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 396 | // /sys/bus/i2c/devices/i2c-*   enable      0660  root   input | 
|  | 397 | SysfsPermissions permissions("/sys/bus/i2c/devices/i2c-*", "enable", 0660, 0, 1001); | 
|  | 398 | EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "i2c")); | 
|  | 399 | EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/not-i2c", "i2c")); | 
|  | 400 | EXPECT_FALSE( | 
|  | 401 | permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "not_i2c")); | 
|  | 402 | EXPECT_EQ(0660U, permissions.perm()); | 
|  | 403 | EXPECT_EQ(0U, permissions.uid()); | 
|  | 404 | EXPECT_EQ(1001U, permissions.gid()); | 
|  | 405 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 406 |  | 
|  | 407 | }  // namespace init | 
|  | 408 | }  // namespace android |