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