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 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <android-base/scopeguard.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame^] | 25 | void add_platform_device(const std::string& path) { |
| 26 | uevent uevent = { |
| 27 | .action = "add", .subsystem = "platform", .path = path, |
| 28 | }; |
| 29 | handle_platform_device_event(&uevent); |
| 30 | } |
| 31 | |
| 32 | void remove_platform_device(const std::string& path) { |
| 33 | uevent uevent = { |
| 34 | .action = "remove", .subsystem = "platform", .path = path, |
| 35 | }; |
| 36 | handle_platform_device_event(&uevent); |
| 37 | } |
| 38 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 39 | template <std::vector<std::string> (*Function)(uevent*)> |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 40 | void test_get_symlinks(const std::string& platform_device_name, uevent* uevent, |
| 41 | const std::vector<std::string> expected_links) { |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame^] | 42 | add_platform_device(platform_device_name); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 43 | auto platform_device_remover = android::base::make_scope_guard( |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame^] | 44 | [&platform_device_name]() { remove_platform_device(platform_device_name); }); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 45 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 46 | std::vector<std::string> result = Function(uevent); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 47 | |
| 48 | auto expected_size = expected_links.size(); |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 49 | ASSERT_EQ(expected_size, result.size()); |
| 50 | if (expected_size == 0) return; |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 51 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 52 | // Explicitly iterate so the results are visible if a failure occurs |
| 53 | for (unsigned int i = 0; i < expected_size; ++i) { |
| 54 | EXPECT_EQ(expected_links[i], result[i]); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Tom Cherry | 1ab8f55 | 2017-04-06 14:41:30 -0700 | [diff] [blame^] | 58 | TEST(devices, handle_platform_device_event) { |
| 59 | platform_devices.clear(); |
| 60 | add_platform_device("/devices/platform/some_device_name"); |
| 61 | ASSERT_EQ(1U, platform_devices.size()); |
| 62 | remove_platform_device("/devices/platform/some_device_name"); |
| 63 | ASSERT_EQ(0U, platform_devices.size()); |
| 64 | } |
| 65 | |
| 66 | TEST(devices, find_platform_device) { |
| 67 | platform_devices.clear(); |
| 68 | add_platform_device("/devices/platform/some_device_name"); |
| 69 | add_platform_device("/devices/platform/some_device_name/longer"); |
| 70 | add_platform_device("/devices/platform/other_device_name"); |
| 71 | EXPECT_EQ(3U, platform_devices.size()); |
| 72 | |
| 73 | std::string out_path; |
| 74 | EXPECT_FALSE(find_platform_device("/devices/platform/not_found", &out_path)); |
| 75 | EXPECT_EQ("", out_path); |
| 76 | |
| 77 | EXPECT_FALSE( |
| 78 | find_platform_device("/devices/platform/some_device_name_with_same_prefix", &out_path)); |
| 79 | |
| 80 | EXPECT_TRUE( |
| 81 | find_platform_device("/devices/platform/some_device_name/longer/longer_child", &out_path)); |
| 82 | EXPECT_EQ("/devices/platform/some_device_name/longer", out_path); |
| 83 | |
| 84 | EXPECT_TRUE(find_platform_device("/devices/platform/some_device_name/other_child", &out_path)); |
| 85 | EXPECT_EQ("/devices/platform/some_device_name", out_path); |
| 86 | } |
| 87 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 88 | TEST(devices, get_character_device_symlinks_success) { |
| 89 | const char* platform_device = "/devices/platform/some_device_name"; |
| 90 | uevent uevent = { |
| 91 | .path = "/devices/platform/some_device_name/usb/usb_device/name/tty2-1:1.0", |
| 92 | .subsystem = "tty", |
| 93 | }; |
| 94 | std::vector<std::string> expected_result{"/dev/usb/ttyname"}; |
| 95 | |
| 96 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 97 | } |
| 98 | |
| 99 | TEST(devices, get_character_device_symlinks_no_pdev_match) { |
| 100 | const char* platform_device = "/devices/platform/some_device_name"; |
| 101 | uevent uevent = { |
| 102 | .path = "/device/name/tty2-1:1.0", .subsystem = "tty", |
| 103 | }; |
| 104 | std::vector<std::string> expected_result; |
| 105 | |
| 106 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 107 | } |
| 108 | |
| 109 | TEST(devices, get_character_device_symlinks_nothing_after_platform_device) { |
| 110 | const char* platform_device = "/devices/platform/some_device_name"; |
| 111 | uevent uevent = { |
| 112 | .path = "/devices/platform/some_device_name", .subsystem = "tty", |
| 113 | }; |
| 114 | std::vector<std::string> expected_result; |
| 115 | |
| 116 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 117 | } |
| 118 | |
| 119 | TEST(devices, get_character_device_symlinks_no_usb_found) { |
| 120 | const char* platform_device = "/devices/platform/some_device_name"; |
| 121 | uevent uevent = { |
| 122 | .path = "/devices/platform/some_device_name/bad/bad/", .subsystem = "tty", |
| 123 | }; |
| 124 | std::vector<std::string> expected_result; |
| 125 | |
| 126 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 127 | } |
| 128 | |
| 129 | TEST(devices, get_character_device_symlinks_no_roothub) { |
| 130 | const char* platform_device = "/devices/platform/some_device_name"; |
| 131 | uevent uevent = { |
| 132 | .path = "/devices/platform/some_device_name/usb/", .subsystem = "tty", |
| 133 | }; |
| 134 | std::vector<std::string> expected_result; |
| 135 | |
| 136 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 137 | } |
| 138 | |
| 139 | TEST(devices, get_character_device_symlinks_no_usb_device) { |
| 140 | const char* platform_device = "/devices/platform/some_device_name"; |
| 141 | uevent uevent = { |
| 142 | .path = "/devices/platform/some_device_name/usb/usb_device/", .subsystem = "tty", |
| 143 | }; |
| 144 | std::vector<std::string> expected_result; |
| 145 | |
| 146 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 147 | } |
| 148 | |
| 149 | TEST(devices, get_character_device_symlinks_no_final_slash) { |
| 150 | const char* platform_device = "/devices/platform/some_device_name"; |
| 151 | uevent uevent = { |
| 152 | .path = "/devices/platform/some_device_name/usb/usb_device/name", .subsystem = "tty", |
| 153 | }; |
| 154 | std::vector<std::string> expected_result; |
| 155 | |
| 156 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 157 | } |
| 158 | |
| 159 | TEST(devices, get_character_device_symlinks_no_final_name) { |
| 160 | const char* platform_device = "/devices/platform/some_device_name"; |
| 161 | uevent uevent = { |
| 162 | .path = "/devices/platform/some_device_name/usb/usb_device//", .subsystem = "tty", |
| 163 | }; |
| 164 | std::vector<std::string> expected_result; |
| 165 | |
| 166 | test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result); |
| 167 | } |
| 168 | |
| 169 | TEST(devices, get_block_device_symlinks_success_platform) { |
| 170 | // These are actual paths from bullhead |
| 171 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
| 172 | uevent uevent = { |
| 173 | .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] | 174 | .partition_name = "", |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 175 | .partition_num = -1, |
| 176 | }; |
| 177 | std::vector<std::string> expected_result{"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0"}; |
| 178 | |
| 179 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 180 | } |
| 181 | |
| 182 | TEST(devices, get_block_device_symlinks_success_platform_with_partition) { |
| 183 | // These are actual paths from bullhead |
| 184 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
| 185 | uevent uevent = { |
| 186 | .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", |
| 187 | .partition_name = "modem", |
| 188 | .partition_num = 1, |
| 189 | }; |
| 190 | std::vector<std::string> expected_result{ |
| 191 | "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem", |
| 192 | "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1", |
| 193 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", |
| 194 | }; |
| 195 | |
| 196 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 197 | } |
| 198 | |
| 199 | TEST(devices, get_block_device_symlinks_success_platform_with_partition_only_num) { |
| 200 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
| 201 | uevent uevent = { |
| 202 | .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] | 203 | .partition_name = "", |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 204 | .partition_num = 1, |
| 205 | }; |
| 206 | std::vector<std::string> expected_result{ |
| 207 | "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1", |
| 208 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", |
| 209 | }; |
| 210 | |
| 211 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 212 | } |
| 213 | |
| 214 | TEST(devices, get_block_device_symlinks_success_platform_with_partition_only_name) { |
| 215 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
| 216 | uevent uevent = { |
| 217 | .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", |
| 218 | .partition_name = "modem", |
| 219 | .partition_num = -1, |
| 220 | }; |
| 221 | std::vector<std::string> expected_result{ |
| 222 | "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem", |
| 223 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", |
| 224 | }; |
| 225 | |
| 226 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 227 | } |
| 228 | |
| 229 | TEST(devices, get_block_device_symlinks_success_pci) { |
| 230 | const char* platform_device = "/devices/do/not/match"; |
| 231 | uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 232 | .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] | 233 | }; |
| 234 | std::vector<std::string> expected_result{"/dev/block/pci/pci0000:00/0000:00:1f.2/mmcblk0"}; |
| 235 | |
| 236 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 237 | } |
| 238 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 239 | TEST(devices, get_block_device_symlinks_pci_bad_format) { |
| 240 | const char* platform_device = "/devices/do/not/match"; |
| 241 | uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 242 | .path = "/devices/pci//mmcblk0", .partition_name = "", .partition_num = -1, |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 243 | }; |
| 244 | std::vector<std::string> expected_result{}; |
| 245 | |
| 246 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 247 | } |
| 248 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 249 | TEST(devices, get_block_device_symlinks_success_vbd) { |
| 250 | const char* platform_device = "/devices/do/not/match"; |
| 251 | uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 252 | .path = "/devices/vbd-1234/mmcblk0", .partition_name = "", .partition_num = -1, |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 253 | }; |
| 254 | std::vector<std::string> expected_result{"/dev/block/vbd/1234/mmcblk0"}; |
| 255 | |
| 256 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 257 | } |
| 258 | |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 259 | TEST(devices, get_block_device_symlinks_vbd_bad_format) { |
| 260 | const char* platform_device = "/devices/do/not/match"; |
| 261 | uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 262 | .path = "/devices/vbd-/mmcblk0", .partition_name = "", .partition_num = -1, |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 263 | }; |
| 264 | std::vector<std::string> expected_result{}; |
| 265 | |
| 266 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 267 | } |
| 268 | |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 269 | TEST(devices, get_block_device_symlinks_no_matches) { |
| 270 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
| 271 | uevent uevent = { |
| 272 | .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] | 273 | .partition_name = "", |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 274 | .partition_num = -1, |
| 275 | }; |
| 276 | std::vector<std::string> expected_result; |
| 277 | |
| 278 | test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result); |
| 279 | } |
| 280 | |
| 281 | TEST(devices, sanitize_null) { |
| 282 | sanitize_partition_name(nullptr); |
| 283 | } |
| 284 | |
| 285 | TEST(devices, sanitize_empty) { |
| 286 | std::string empty; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 287 | sanitize_partition_name(&empty); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 288 | EXPECT_EQ(0u, empty.size()); |
| 289 | } |
| 290 | |
| 291 | TEST(devices, sanitize_allgood) { |
| 292 | std::string good = |
| 293 | "abcdefghijklmnopqrstuvwxyz" |
| 294 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 295 | "0123456789" |
| 296 | "_-."; |
| 297 | std::string good_copy = good; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 298 | sanitize_partition_name(&good); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 299 | EXPECT_EQ(good_copy, good); |
| 300 | } |
| 301 | |
| 302 | TEST(devices, sanitize_somebad) { |
| 303 | std::string string = "abc!@#$%^&*()"; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 304 | sanitize_partition_name(&string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 305 | EXPECT_EQ("abc__________", string); |
| 306 | } |
| 307 | |
| 308 | TEST(devices, sanitize_allbad) { |
| 309 | std::string string = "!@#$%^&*()"; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 310 | sanitize_partition_name(&string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 311 | EXPECT_EQ("__________", string); |
| 312 | } |
| 313 | |
| 314 | TEST(devices, sanitize_onebad) { |
| 315 | std::string string = ")"; |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 316 | sanitize_partition_name(&string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 317 | EXPECT_EQ("_", string); |
| 318 | } |