blob: 66521db6b0b021c4f69c47d1727016b3466edcbd [file] [log] [blame]
Tom Cherryc44f6a42017-04-05 15:58:31 -07001/*
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 Cherry1ab8f552017-04-06 14:41:30 -070025void 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
32void 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 Cherry2e344f92017-04-04 17:53:45 -070039template <std::vector<std::string> (*Function)(uevent*)>
Tom Cherryc44f6a42017-04-05 15:58:31 -070040void test_get_symlinks(const std::string& platform_device_name, uevent* uevent,
41 const std::vector<std::string> expected_links) {
Tom Cherry1ab8f552017-04-06 14:41:30 -070042 add_platform_device(platform_device_name);
Tom Cherryc44f6a42017-04-05 15:58:31 -070043 auto platform_device_remover = android::base::make_scope_guard(
Tom Cherry1ab8f552017-04-06 14:41:30 -070044 [&platform_device_name]() { remove_platform_device(platform_device_name); });
Tom Cherryc44f6a42017-04-05 15:58:31 -070045
Tom Cherry2e344f92017-04-04 17:53:45 -070046 std::vector<std::string> result = Function(uevent);
Tom Cherryc44f6a42017-04-05 15:58:31 -070047
48 auto expected_size = expected_links.size();
Tom Cherry2e344f92017-04-04 17:53:45 -070049 ASSERT_EQ(expected_size, result.size());
50 if (expected_size == 0) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -070051
Tom Cherry2e344f92017-04-04 17:53:45 -070052 // 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 Cherryc44f6a42017-04-05 15:58:31 -070055 }
56}
57
Tom Cherry1ab8f552017-04-06 14:41:30 -070058TEST(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
66TEST(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 Cherryc44f6a42017-04-05 15:58:31 -070088TEST(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
99TEST(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
109TEST(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
119TEST(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
129TEST(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
139TEST(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
149TEST(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
159TEST(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
169TEST(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 Cherrye3e48212017-04-11 13:53:37 -0700174 .partition_name = "",
Tom Cherryc44f6a42017-04-05 15:58:31 -0700175 .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
182TEST(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
199TEST(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 Cherrye3e48212017-04-11 13:53:37 -0700203 .partition_name = "",
Tom Cherryc44f6a42017-04-05 15:58:31 -0700204 .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
214TEST(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
229TEST(devices, get_block_device_symlinks_success_pci) {
230 const char* platform_device = "/devices/do/not/match";
231 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700232 .path = "/devices/pci0000:00/0000:00:1f.2/mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherryc44f6a42017-04-05 15:58:31 -0700233 };
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 Cherry2e344f92017-04-04 17:53:45 -0700239TEST(devices, get_block_device_symlinks_pci_bad_format) {
240 const char* platform_device = "/devices/do/not/match";
241 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700242 .path = "/devices/pci//mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherry2e344f92017-04-04 17:53:45 -0700243 };
244 std::vector<std::string> expected_result{};
245
246 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
247}
248
Tom Cherryc44f6a42017-04-05 15:58:31 -0700249TEST(devices, get_block_device_symlinks_success_vbd) {
250 const char* platform_device = "/devices/do/not/match";
251 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700252 .path = "/devices/vbd-1234/mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherryc44f6a42017-04-05 15:58:31 -0700253 };
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 Cherry2e344f92017-04-04 17:53:45 -0700259TEST(devices, get_block_device_symlinks_vbd_bad_format) {
260 const char* platform_device = "/devices/do/not/match";
261 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700262 .path = "/devices/vbd-/mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherry2e344f92017-04-04 17:53:45 -0700263 };
264 std::vector<std::string> expected_result{};
265
266 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
267}
268
Tom Cherryc44f6a42017-04-05 15:58:31 -0700269TEST(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 Cherrye3e48212017-04-11 13:53:37 -0700273 .partition_name = "",
Tom Cherryc44f6a42017-04-05 15:58:31 -0700274 .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
281TEST(devices, sanitize_null) {
282 sanitize_partition_name(nullptr);
283}
284
285TEST(devices, sanitize_empty) {
286 std::string empty;
Tom Cherry2e344f92017-04-04 17:53:45 -0700287 sanitize_partition_name(&empty);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700288 EXPECT_EQ(0u, empty.size());
289}
290
291TEST(devices, sanitize_allgood) {
292 std::string good =
293 "abcdefghijklmnopqrstuvwxyz"
294 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
295 "0123456789"
296 "_-.";
297 std::string good_copy = good;
Tom Cherry2e344f92017-04-04 17:53:45 -0700298 sanitize_partition_name(&good);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700299 EXPECT_EQ(good_copy, good);
300}
301
302TEST(devices, sanitize_somebad) {
303 std::string string = "abc!@#$%^&*()";
Tom Cherry2e344f92017-04-04 17:53:45 -0700304 sanitize_partition_name(&string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700305 EXPECT_EQ("abc__________", string);
306}
307
308TEST(devices, sanitize_allbad) {
309 std::string string = "!@#$%^&*()";
Tom Cherry2e344f92017-04-04 17:53:45 -0700310 sanitize_partition_name(&string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700311 EXPECT_EQ("__________", string);
312}
313
314TEST(devices, sanitize_onebad) {
315 std::string string = ")";
Tom Cherry2e344f92017-04-04 17:53:45 -0700316 sanitize_partition_name(&string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700317 EXPECT_EQ("_", string);
318}
Tom Cherrycc054c92017-04-05 17:55:46 -0700319
320TEST(devices, DevPermissionsMatchNormal) {
321 // Basic from ueventd.rc
322 // /dev/null 0666 root root
323 Permissions permissions("/dev/null", 0666, 0, 0);
324 EXPECT_TRUE(permissions.Match("/dev/null"));
325 EXPECT_FALSE(permissions.Match("/dev/nullsuffix"));
326 EXPECT_FALSE(permissions.Match("/dev/nul"));
327 EXPECT_EQ(0666U, permissions.perm());
328 EXPECT_EQ(0U, permissions.uid());
329 EXPECT_EQ(0U, permissions.gid());
330}
331
332TEST(devices, DevPermissionsMatchPrefix) {
333 // Prefix from ueventd.rc
334 // /dev/dri/* 0666 root graphics
335 Permissions permissions("/dev/dri/*", 0666, 0, 1000);
336 EXPECT_TRUE(permissions.Match("/dev/dri/some_dri_device"));
337 EXPECT_TRUE(permissions.Match("/dev/dri/some_other_dri_device"));
338 EXPECT_TRUE(permissions.Match("/dev/dri/"));
339 EXPECT_FALSE(permissions.Match("/dev/dr/non_match"));
340 EXPECT_EQ(0666U, permissions.perm());
341 EXPECT_EQ(0U, permissions.uid());
342 EXPECT_EQ(1000U, permissions.gid());
343}
344
345TEST(devices, DevPermissionsMatchWildcard) {
346 // Wildcard example
347 // /dev/device*name 0666 root graphics
348 Permissions permissions("/dev/device*name", 0666, 0, 1000);
349 EXPECT_TRUE(permissions.Match("/dev/devicename"));
350 EXPECT_TRUE(permissions.Match("/dev/device123name"));
351 EXPECT_TRUE(permissions.Match("/dev/deviceabcname"));
352 EXPECT_FALSE(permissions.Match("/dev/device123name/subdevice"));
353 EXPECT_FALSE(permissions.Match("/dev/deviceame"));
354 EXPECT_EQ(0666U, permissions.perm());
355 EXPECT_EQ(0U, permissions.uid());
356 EXPECT_EQ(1000U, permissions.gid());
357}
358
359TEST(devices, DevPermissionsMatchWildcardPrefix) {
360 // Wildcard+Prefix example
361 // /dev/device*name* 0666 root graphics
362 Permissions permissions("/dev/device*name*", 0666, 0, 1000);
363 EXPECT_TRUE(permissions.Match("/dev/devicename"));
364 EXPECT_TRUE(permissions.Match("/dev/device123name"));
365 EXPECT_TRUE(permissions.Match("/dev/deviceabcname"));
366 EXPECT_TRUE(permissions.Match("/dev/device123namesomething"));
367 // FNM_PATHNAME doesn't match '/' with *
368 EXPECT_FALSE(permissions.Match("/dev/device123name/something"));
369 EXPECT_FALSE(permissions.Match("/dev/deviceame"));
370 EXPECT_EQ(0666U, permissions.perm());
371 EXPECT_EQ(0U, permissions.uid());
372 EXPECT_EQ(1000U, permissions.gid());
373}
374
375TEST(devices, SysfsPermissionsMatchWithSubsystemNormal) {
376 // /sys/devices/virtual/input/input* enable 0660 root input
377 SysfsPermissions permissions("/sys/devices/virtual/input/input*", "enable", 0660, 0, 1001);
378 EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/input0", "input"));
379 EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/not_input0", "input"));
380 EXPECT_EQ(0660U, permissions.perm());
381 EXPECT_EQ(0U, permissions.uid());
382 EXPECT_EQ(1001U, permissions.gid());
383}
384
385TEST(devices, SysfsPermissionsMatchWithSubsystemClass) {
386 // /sys/class/input/event* enable 0660 root input
387 SysfsPermissions permissions("/sys/class/input/event*", "enable", 0660, 0, 1001);
388 EXPECT_TRUE(permissions.MatchWithSubsystem(
389 "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "input"));
390 EXPECT_FALSE(permissions.MatchWithSubsystem(
391 "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/not_event0", "input"));
392 EXPECT_FALSE(permissions.MatchWithSubsystem(
393 "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "not_input"));
394 EXPECT_EQ(0660U, permissions.perm());
395 EXPECT_EQ(0U, permissions.uid());
396 EXPECT_EQ(1001U, permissions.gid());
397}
398
399TEST(devices, SysfsPermissionsMatchWithSubsystemBus) {
400 // /sys/bus/i2c/devices/i2c-* enable 0660 root input
401 SysfsPermissions permissions("/sys/bus/i2c/devices/i2c-*", "enable", 0660, 0, 1001);
402 EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "i2c"));
403 EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/not-i2c", "i2c"));
404 EXPECT_FALSE(
405 permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "not_i2c"));
406 EXPECT_EQ(0660U, permissions.perm());
407 EXPECT_EQ(0U, permissions.uid());
408 EXPECT_EQ(1001U, permissions.gid());
409}