blob: b6b4b820949de732cde3a757e981ffc7583ae698 [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 Cherry2e344f92017-04-04 17:53:45 -070025template <std::vector<std::string> (*Function)(uevent*)>
Tom Cherryc44f6a42017-04-05 15:58:31 -070026void 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 Cherry2e344f92017-04-04 17:53:45 -070032 std::vector<std::string> result = Function(uevent);
Tom Cherryc44f6a42017-04-05 15:58:31 -070033
34 auto expected_size = expected_links.size();
Tom Cherry2e344f92017-04-04 17:53:45 -070035 ASSERT_EQ(expected_size, result.size());
36 if (expected_size == 0) return;
Tom Cherryc44f6a42017-04-05 15:58:31 -070037
Tom Cherry2e344f92017-04-04 17:53:45 -070038 // 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 Cherryc44f6a42017-04-05 15:58:31 -070041 }
42}
43
44TEST(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
55TEST(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
65TEST(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
75TEST(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
85TEST(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
95TEST(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
105TEST(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
115TEST(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
125TEST(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",
Tom Cherrye3e48212017-04-11 13:53:37 -0700130 .partition_name = "",
Tom Cherryc44f6a42017-04-05 15:58:31 -0700131 .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
138TEST(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
155TEST(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",
Tom Cherrye3e48212017-04-11 13:53:37 -0700159 .partition_name = "",
Tom Cherryc44f6a42017-04-05 15:58:31 -0700160 .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
170TEST(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
185TEST(devices, get_block_device_symlinks_success_pci) {
186 const char* platform_device = "/devices/do/not/match";
187 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700188 .path = "/devices/pci0000:00/0000:00:1f.2/mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherryc44f6a42017-04-05 15:58:31 -0700189 };
190 std::vector<std::string> expected_result{"/dev/block/pci/pci0000:00/0000:00:1f.2/mmcblk0"};
191
192 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
193}
194
Tom Cherry2e344f92017-04-04 17:53:45 -0700195TEST(devices, get_block_device_symlinks_pci_bad_format) {
196 const char* platform_device = "/devices/do/not/match";
197 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700198 .path = "/devices/pci//mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherry2e344f92017-04-04 17:53:45 -0700199 };
200 std::vector<std::string> expected_result{};
201
202 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
203}
204
Tom Cherryc44f6a42017-04-05 15:58:31 -0700205TEST(devices, get_block_device_symlinks_success_vbd) {
206 const char* platform_device = "/devices/do/not/match";
207 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700208 .path = "/devices/vbd-1234/mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherryc44f6a42017-04-05 15:58:31 -0700209 };
210 std::vector<std::string> expected_result{"/dev/block/vbd/1234/mmcblk0"};
211
212 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
213}
214
Tom Cherry2e344f92017-04-04 17:53:45 -0700215TEST(devices, get_block_device_symlinks_vbd_bad_format) {
216 const char* platform_device = "/devices/do/not/match";
217 uevent uevent = {
Tom Cherrye3e48212017-04-11 13:53:37 -0700218 .path = "/devices/vbd-/mmcblk0", .partition_name = "", .partition_num = -1,
Tom Cherry2e344f92017-04-04 17:53:45 -0700219 };
220 std::vector<std::string> expected_result{};
221
222 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
223}
224
Tom Cherryc44f6a42017-04-05 15:58:31 -0700225TEST(devices, get_block_device_symlinks_no_matches) {
226 const char* platform_device = "/devices/soc.0/f9824900.sdhci";
227 uevent uevent = {
228 .path = "/devices/soc.0/not_the_device/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
Tom Cherrye3e48212017-04-11 13:53:37 -0700229 .partition_name = "",
Tom Cherryc44f6a42017-04-05 15:58:31 -0700230 .partition_num = -1,
231 };
232 std::vector<std::string> expected_result;
233
234 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
235}
236
237TEST(devices, sanitize_null) {
238 sanitize_partition_name(nullptr);
239}
240
241TEST(devices, sanitize_empty) {
242 std::string empty;
Tom Cherry2e344f92017-04-04 17:53:45 -0700243 sanitize_partition_name(&empty);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700244 EXPECT_EQ(0u, empty.size());
245}
246
247TEST(devices, sanitize_allgood) {
248 std::string good =
249 "abcdefghijklmnopqrstuvwxyz"
250 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
251 "0123456789"
252 "_-.";
253 std::string good_copy = good;
Tom Cherry2e344f92017-04-04 17:53:45 -0700254 sanitize_partition_name(&good);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700255 EXPECT_EQ(good_copy, good);
256}
257
258TEST(devices, sanitize_somebad) {
259 std::string string = "abc!@#$%^&*()";
Tom Cherry2e344f92017-04-04 17:53:45 -0700260 sanitize_partition_name(&string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700261 EXPECT_EQ("abc__________", string);
262}
263
264TEST(devices, sanitize_allbad) {
265 std::string string = "!@#$%^&*()";
Tom Cherry2e344f92017-04-04 17:53:45 -0700266 sanitize_partition_name(&string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700267 EXPECT_EQ("__________", string);
268}
269
270TEST(devices, sanitize_onebad) {
271 std::string string = ")";
Tom Cherry2e344f92017-04-04 17:53:45 -0700272 sanitize_partition_name(&string);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700273 EXPECT_EQ("_", string);
274}