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