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 | |
Mark Salyzyn | 9f1cf25 | 2018-11-12 12:45:59 -0800 | [diff] [blame] | 19 | #include <android-base/file.h> |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 20 | #include <android-base/scopeguard.h> |
| 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 | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 33 | const std::vector<std::string>& expected_links) { |
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; |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 38 | mkdir_recursive(platform_device_dir, 0777); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 39 | |
| 40 | std::string platform_bus = fake_sys_root.path + "/bus/platform"s; |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 41 | mkdir_recursive(platform_bus, 0777); |
Sandeep Patil | cd2ba0d | 2017-06-21 12:46:41 -0700 | [diff] [blame] | 42 | symlink(platform_bus.c_str(), (platform_device_dir + "/subsystem").c_str()); |
| 43 | |
Tom Cherry | c3692b3 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 44 | mkdir_recursive(android::base::Dirname(fake_sys_root.path + uevent.path), 0777); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 45 | |
| 46 | std::vector<std::string> result; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 47 | result = device_handler_.GetBlockDeviceSymlinks(uevent); |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 48 | |
| 49 | auto expected_size = expected_links.size(); |
| 50 | ASSERT_EQ(expected_size, result.size()); |
| 51 | if (expected_size == 0) return; |
| 52 | |
| 53 | // Explicitly iterate so the results are visible if a failure occurs |
| 54 | for (unsigned int i = 0; i < expected_size; ++i) { |
| 55 | EXPECT_EQ(expected_links[i], result[i]); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | DeviceHandler device_handler_; |
| 61 | }; |
| 62 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 63 | TEST(device_handler, get_block_device_symlinks_success_platform) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 64 | // These are actual paths from bullhead |
| 65 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
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/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0", |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 68 | .partition_name = "", |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 69 | .partition_num = -1, |
| 70 | }; |
| 71 | std::vector<std::string> expected_result{"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0"}; |
| 72 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 73 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 74 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 77 | TEST(device_handler, get_block_device_symlinks_success_platform_with_partition) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 78 | // These are actual paths from bullhead |
| 79 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 80 | Uevent uevent = { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 81 | .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", |
| 82 | .partition_name = "modem", |
| 83 | .partition_num = 1, |
| 84 | }; |
| 85 | std::vector<std::string> expected_result{ |
| 86 | "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem", |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 87 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", |
| 88 | }; |
| 89 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 90 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 91 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 94 | 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] | 95 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 96 | Uevent uevent = { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 97 | .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] | 98 | .partition_name = "", |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 99 | .partition_num = 1, |
| 100 | }; |
| 101 | std::vector<std::string> expected_result{ |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 102 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", |
| 103 | }; |
| 104 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 105 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 106 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
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_block_device_symlinks_success_platform_with_partition_only_name) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 110 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
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/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1", |
| 113 | .partition_name = "modem", |
| 114 | .partition_num = -1, |
| 115 | }; |
| 116 | std::vector<std::string> expected_result{ |
| 117 | "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem", |
| 118 | "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1", |
| 119 | }; |
| 120 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 121 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 122 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 125 | TEST(device_handler, get_block_device_symlinks_success_pci) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 126 | const char* platform_device = "/devices/do/not/match"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 127 | Uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 128 | .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] | 129 | }; |
| 130 | std::vector<std::string> expected_result{"/dev/block/pci/pci0000:00/0000:00:1f.2/mmcblk0"}; |
| 131 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 132 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 133 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 136 | TEST(device_handler, get_block_device_symlinks_pci_bad_format) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 137 | const char* platform_device = "/devices/do/not/match"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 138 | Uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 139 | .path = "/devices/pci//mmcblk0", .partition_name = "", .partition_num = -1, |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 140 | }; |
| 141 | std::vector<std::string> expected_result{}; |
| 142 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 143 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 144 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 147 | TEST(device_handler, get_block_device_symlinks_success_vbd) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 148 | const char* platform_device = "/devices/do/not/match"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 149 | Uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 150 | .path = "/devices/vbd-1234/mmcblk0", .partition_name = "", .partition_num = -1, |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 151 | }; |
| 152 | std::vector<std::string> expected_result{"/dev/block/vbd/1234/mmcblk0"}; |
| 153 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 154 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 155 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 158 | TEST(device_handler, get_block_device_symlinks_vbd_bad_format) { |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 159 | const char* platform_device = "/devices/do/not/match"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 160 | Uevent uevent = { |
Tom Cherry | e3e4821 | 2017-04-11 13:53:37 -0700 | [diff] [blame] | 161 | .path = "/devices/vbd-/mmcblk0", .partition_name = "", .partition_num = -1, |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 162 | }; |
| 163 | std::vector<std::string> expected_result{}; |
| 164 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 165 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 166 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | 2e344f9 | 2017-04-04 17:53:45 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 169 | TEST(device_handler, get_block_device_symlinks_no_matches) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 170 | const char* platform_device = "/devices/soc.0/f9824900.sdhci"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 171 | Uevent uevent = { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 172 | .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] | 173 | .partition_name = "", |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 174 | .partition_num = -1, |
| 175 | }; |
| 176 | std::vector<std::string> expected_result; |
| 177 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 178 | DeviceHandlerTester device_handler_tester_; |
Tom Cherry | b4dd881 | 2017-06-23 12:43:48 -0700 | [diff] [blame] | 179 | device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 182 | TEST(device_handler, sanitize_null) { |
| 183 | SanitizePartitionName(nullptr); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 186 | TEST(device_handler, sanitize_empty) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 187 | std::string empty; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 188 | SanitizePartitionName(&empty); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 189 | EXPECT_EQ(0u, empty.size()); |
| 190 | } |
| 191 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 192 | TEST(device_handler, sanitize_allgood) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 193 | std::string good = |
| 194 | "abcdefghijklmnopqrstuvwxyz" |
| 195 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 196 | "0123456789" |
| 197 | "_-."; |
| 198 | std::string good_copy = good; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 199 | SanitizePartitionName(&good); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 200 | EXPECT_EQ(good_copy, good); |
| 201 | } |
| 202 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 203 | TEST(device_handler, sanitize_somebad) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 204 | std::string string = "abc!@#$%^&*()"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 205 | SanitizePartitionName(&string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 206 | EXPECT_EQ("abc__________", string); |
| 207 | } |
| 208 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 209 | TEST(device_handler, sanitize_allbad) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 210 | std::string string = "!@#$%^&*()"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 211 | SanitizePartitionName(&string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 212 | EXPECT_EQ("__________", string); |
| 213 | } |
| 214 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 215 | TEST(device_handler, sanitize_onebad) { |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 216 | std::string string = ")"; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 217 | SanitizePartitionName(&string); |
Tom Cherry | c44f6a4 | 2017-04-05 15:58:31 -0700 | [diff] [blame] | 218 | EXPECT_EQ("_", string); |
| 219 | } |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 220 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 221 | TEST(device_handler, DevPermissionsMatchNormal) { |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 222 | // Basic from ueventd.rc |
| 223 | // /dev/null 0666 root root |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 224 | Permissions permissions("/dev/null", 0666, 0, 0, false); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 225 | EXPECT_TRUE(permissions.Match("/dev/null")); |
| 226 | EXPECT_FALSE(permissions.Match("/dev/nullsuffix")); |
| 227 | EXPECT_FALSE(permissions.Match("/dev/nul")); |
| 228 | EXPECT_EQ(0666U, permissions.perm()); |
| 229 | EXPECT_EQ(0U, permissions.uid()); |
| 230 | EXPECT_EQ(0U, permissions.gid()); |
| 231 | } |
| 232 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 233 | TEST(device_handler, DevPermissionsMatchPrefix) { |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 234 | // Prefix from ueventd.rc |
| 235 | // /dev/dri/* 0666 root graphics |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 236 | Permissions permissions("/dev/dri/*", 0666, 0, 1000, false); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 237 | EXPECT_TRUE(permissions.Match("/dev/dri/some_dri_device")); |
| 238 | EXPECT_TRUE(permissions.Match("/dev/dri/some_other_dri_device")); |
| 239 | EXPECT_TRUE(permissions.Match("/dev/dri/")); |
| 240 | EXPECT_FALSE(permissions.Match("/dev/dr/non_match")); |
| 241 | EXPECT_EQ(0666U, permissions.perm()); |
| 242 | EXPECT_EQ(0U, permissions.uid()); |
| 243 | EXPECT_EQ(1000U, permissions.gid()); |
| 244 | } |
| 245 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 246 | TEST(device_handler, DevPermissionsMatchWildcard) { |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 247 | // Wildcard example |
| 248 | // /dev/device*name 0666 root graphics |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 249 | Permissions permissions("/dev/device*name", 0666, 0, 1000, false); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 250 | EXPECT_TRUE(permissions.Match("/dev/devicename")); |
| 251 | EXPECT_TRUE(permissions.Match("/dev/device123name")); |
| 252 | EXPECT_TRUE(permissions.Match("/dev/deviceabcname")); |
| 253 | EXPECT_FALSE(permissions.Match("/dev/device123name/subdevice")); |
| 254 | EXPECT_FALSE(permissions.Match("/dev/deviceame")); |
| 255 | EXPECT_EQ(0666U, permissions.perm()); |
| 256 | EXPECT_EQ(0U, permissions.uid()); |
| 257 | EXPECT_EQ(1000U, permissions.gid()); |
| 258 | } |
| 259 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 260 | TEST(device_handler, DevPermissionsMatchWildcardPrefix) { |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 261 | // Wildcard+Prefix example |
| 262 | // /dev/device*name* 0666 root graphics |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 263 | Permissions permissions("/dev/device*name*", 0666, 0, 1000, false); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 264 | EXPECT_TRUE(permissions.Match("/dev/devicename")); |
| 265 | EXPECT_TRUE(permissions.Match("/dev/device123name")); |
| 266 | EXPECT_TRUE(permissions.Match("/dev/deviceabcname")); |
| 267 | EXPECT_TRUE(permissions.Match("/dev/device123namesomething")); |
| 268 | // FNM_PATHNAME doesn't match '/' with * |
| 269 | EXPECT_FALSE(permissions.Match("/dev/device123name/something")); |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 270 | EXPECT_FALSE(permissions.Match("/dev/device/1/2/3name/something")); |
| 271 | EXPECT_FALSE(permissions.Match("/dev/deviceame")); |
| 272 | EXPECT_EQ(0666U, permissions.perm()); |
| 273 | EXPECT_EQ(0U, permissions.uid()); |
| 274 | EXPECT_EQ(1000U, permissions.gid()); |
| 275 | } |
| 276 | |
| 277 | TEST(device_handler, DevPermissionsMatchWildcardPrefix_NoFnmPathName) { |
| 278 | // Wildcard+Prefix example with no_fnm_pathname |
| 279 | // /dev/device*name* 0666 root graphics |
| 280 | Permissions permissions("/dev/device*name*", 0666, 0, 1000, true); |
| 281 | EXPECT_TRUE(permissions.Match("/dev/devicename")); |
| 282 | EXPECT_TRUE(permissions.Match("/dev/device123name")); |
| 283 | EXPECT_TRUE(permissions.Match("/dev/deviceabcname")); |
| 284 | EXPECT_TRUE(permissions.Match("/dev/device123namesomething")); |
| 285 | // With NoFnmPathName, the below matches, unlike DevPermissionsMatchWildcardPrefix. |
| 286 | EXPECT_TRUE(permissions.Match("/dev/device123name/something")); |
| 287 | EXPECT_TRUE(permissions.Match("/dev/device/1/2/3name/something")); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 288 | EXPECT_FALSE(permissions.Match("/dev/deviceame")); |
| 289 | EXPECT_EQ(0666U, permissions.perm()); |
| 290 | EXPECT_EQ(0U, permissions.uid()); |
| 291 | EXPECT_EQ(1000U, permissions.gid()); |
| 292 | } |
| 293 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 294 | TEST(device_handler, SysfsPermissionsMatchWithSubsystemNormal) { |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 295 | // /sys/devices/virtual/input/input* enable 0660 root input |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 296 | SysfsPermissions permissions("/sys/devices/virtual/input/input*", "enable", 0660, 0, 1001, |
| 297 | false); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 298 | EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/input0", "input")); |
| 299 | EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/not_input0", "input")); |
| 300 | EXPECT_EQ(0660U, permissions.perm()); |
| 301 | EXPECT_EQ(0U, permissions.uid()); |
| 302 | EXPECT_EQ(1001U, permissions.gid()); |
| 303 | } |
| 304 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 305 | TEST(device_handler, SysfsPermissionsMatchWithSubsystemClass) { |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 306 | // /sys/class/input/event* enable 0660 root input |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 307 | SysfsPermissions permissions("/sys/class/input/event*", "enable", 0660, 0, 1001, false); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 308 | EXPECT_TRUE(permissions.MatchWithSubsystem( |
| 309 | "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "input")); |
| 310 | EXPECT_FALSE(permissions.MatchWithSubsystem( |
| 311 | "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/not_event0", "input")); |
| 312 | EXPECT_FALSE(permissions.MatchWithSubsystem( |
| 313 | "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "not_input")); |
| 314 | EXPECT_EQ(0660U, permissions.perm()); |
| 315 | EXPECT_EQ(0U, permissions.uid()); |
| 316 | EXPECT_EQ(1001U, permissions.gid()); |
| 317 | } |
| 318 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 319 | TEST(device_handler, SysfsPermissionsMatchWithSubsystemBus) { |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 320 | // /sys/bus/i2c/devices/i2c-* enable 0660 root input |
Tom Cherry | 47031c8 | 2020-12-07 13:33:46 -0800 | [diff] [blame] | 321 | SysfsPermissions permissions("/sys/bus/i2c/devices/i2c-*", "enable", 0660, 0, 1001, false); |
Tom Cherry | cc054c9 | 2017-04-05 17:55:46 -0700 | [diff] [blame] | 322 | EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "i2c")); |
| 323 | EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/not-i2c", "i2c")); |
| 324 | EXPECT_FALSE( |
| 325 | permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "not_i2c")); |
| 326 | EXPECT_EQ(0660U, permissions.perm()); |
| 327 | EXPECT_EQ(0U, permissions.uid()); |
| 328 | EXPECT_EQ(1001U, permissions.gid()); |
| 329 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 330 | |
| 331 | } // namespace init |
| 332 | } // namespace android |