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