blob: f79c96dba3446dd658b59cb584d29f6d72dded92 [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
25template <char** (*Function)(uevent*)>
26void 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
32 char** result = Function(uevent);
33 auto result_freer = android::base::make_scope_guard([result]() {
34 if (result) {
35 for (int i = 0; result[i]; i++) {
36 free(result[i]);
37 }
38 free(result);
39 }
40 });
41
42 auto expected_size = expected_links.size();
43 if (expected_size == 0) {
44 ASSERT_EQ(nullptr, result);
45 } else {
46 ASSERT_NE(nullptr, result);
47 // First assert size is equal, so we don't overrun expected_links
48 unsigned int size = 0;
49 while (result[size]) ++size;
50 ASSERT_EQ(expected_size, size);
51
52 for (unsigned int i = 0; i < size; ++i) {
53 EXPECT_EQ(expected_links[i], result[i]);
54 }
55 }
56}
57
58TEST(devices, get_character_device_symlinks_success) {
59 const char* platform_device = "/devices/platform/some_device_name";
60 uevent uevent = {
61 .path = "/devices/platform/some_device_name/usb/usb_device/name/tty2-1:1.0",
62 .subsystem = "tty",
63 };
64 std::vector<std::string> expected_result{"/dev/usb/ttyname"};
65
66 test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
67}
68
69TEST(devices, get_character_device_symlinks_no_pdev_match) {
70 const char* platform_device = "/devices/platform/some_device_name";
71 uevent uevent = {
72 .path = "/device/name/tty2-1:1.0", .subsystem = "tty",
73 };
74 std::vector<std::string> expected_result;
75
76 test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
77}
78
79TEST(devices, get_character_device_symlinks_nothing_after_platform_device) {
80 const char* platform_device = "/devices/platform/some_device_name";
81 uevent uevent = {
82 .path = "/devices/platform/some_device_name", .subsystem = "tty",
83 };
84 std::vector<std::string> expected_result;
85
86 test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
87}
88
89TEST(devices, get_character_device_symlinks_no_usb_found) {
90 const char* platform_device = "/devices/platform/some_device_name";
91 uevent uevent = {
92 .path = "/devices/platform/some_device_name/bad/bad/", .subsystem = "tty",
93 };
94 std::vector<std::string> expected_result;
95
96 test_get_symlinks<get_character_device_symlinks>(platform_device, &uevent, expected_result);
97}
98
99TEST(devices, get_character_device_symlinks_no_roothub) {
100 const char* platform_device = "/devices/platform/some_device_name";
101 uevent uevent = {
102 .path = "/devices/platform/some_device_name/usb/", .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_no_usb_device) {
110 const char* platform_device = "/devices/platform/some_device_name";
111 uevent uevent = {
112 .path = "/devices/platform/some_device_name/usb/usb_device/", .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_final_slash) {
120 const char* platform_device = "/devices/platform/some_device_name";
121 uevent uevent = {
122 .path = "/devices/platform/some_device_name/usb/usb_device/name", .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_final_name) {
130 const char* platform_device = "/devices/platform/some_device_name";
131 uevent uevent = {
132 .path = "/devices/platform/some_device_name/usb/usb_device//", .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_block_device_symlinks_success_platform) {
140 // These are actual paths from bullhead
141 const char* platform_device = "/devices/soc.0/f9824900.sdhci";
142 uevent uevent = {
143 .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0",
144 .partition_name = nullptr,
145 .partition_num = -1,
146 };
147 std::vector<std::string> expected_result{"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0"};
148
149 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
150}
151
152TEST(devices, get_block_device_symlinks_success_platform_with_partition) {
153 // These are actual paths from bullhead
154 const char* platform_device = "/devices/soc.0/f9824900.sdhci";
155 uevent uevent = {
156 .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
157 .partition_name = "modem",
158 .partition_num = 1,
159 };
160 std::vector<std::string> expected_result{
161 "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem",
162 "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
163 "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
164 };
165
166 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
167}
168
169TEST(devices, get_block_device_symlinks_success_platform_with_partition_only_num) {
170 const char* platform_device = "/devices/soc.0/f9824900.sdhci";
171 uevent uevent = {
172 .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
173 .partition_name = nullptr,
174 .partition_num = 1,
175 };
176 std::vector<std::string> expected_result{
177 "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
178 "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
179 };
180
181 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
182}
183
184TEST(devices, get_block_device_symlinks_success_platform_with_partition_only_name) {
185 const char* platform_device = "/devices/soc.0/f9824900.sdhci";
186 uevent uevent = {
187 .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
188 .partition_name = "modem",
189 .partition_num = -1,
190 };
191 std::vector<std::string> expected_result{
192 "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem",
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_pci) {
200 const char* platform_device = "/devices/do/not/match";
201 uevent uevent = {
202 .path = "/devices/pci0000:00/0000:00:1f.2/mmcblk0",
203 .partition_name = nullptr,
204 .partition_num = -1,
205 };
206 std::vector<std::string> expected_result{"/dev/block/pci/pci0000:00/0000:00:1f.2/mmcblk0"};
207
208 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
209}
210
211TEST(devices, get_block_device_symlinks_success_vbd) {
212 const char* platform_device = "/devices/do/not/match";
213 uevent uevent = {
214 .path = "/devices/vbd-1234/mmcblk0", .partition_name = nullptr, .partition_num = -1,
215 };
216 std::vector<std::string> expected_result{"/dev/block/vbd/1234/mmcblk0"};
217
218 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
219}
220
221TEST(devices, get_block_device_symlinks_no_matches) {
222 const char* platform_device = "/devices/soc.0/f9824900.sdhci";
223 uevent uevent = {
224 .path = "/devices/soc.0/not_the_device/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
225 .partition_name = nullptr,
226 .partition_num = -1,
227 };
228 std::vector<std::string> expected_result;
229
230 test_get_symlinks<get_block_device_symlinks>(platform_device, &uevent, expected_result);
231}
232
233TEST(devices, sanitize_null) {
234 sanitize_partition_name(nullptr);
235}
236
237TEST(devices, sanitize_empty) {
238 std::string empty;
239 sanitize_partition_name(&empty[0]);
240 EXPECT_EQ(0u, empty.size());
241}
242
243TEST(devices, sanitize_allgood) {
244 std::string good =
245 "abcdefghijklmnopqrstuvwxyz"
246 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
247 "0123456789"
248 "_-.";
249 std::string good_copy = good;
250 sanitize_partition_name(&good[0]);
251 EXPECT_EQ(good_copy, good);
252}
253
254TEST(devices, sanitize_somebad) {
255 std::string string = "abc!@#$%^&*()";
256 sanitize_partition_name(&string[0]);
257 EXPECT_EQ("abc__________", string);
258}
259
260TEST(devices, sanitize_allbad) {
261 std::string string = "!@#$%^&*()";
262 sanitize_partition_name(&string[0]);
263 EXPECT_EQ("__________", string);
264}
265
266TEST(devices, sanitize_onebad) {
267 std::string string = ")";
268 sanitize_partition_name(&string[0]);
269 EXPECT_EQ("_", string);
270}