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 <android-base/scopeguard.h>
20#include <android-base/test_utils.h>
21#include <gtest/gtest.h>
22
23#include "util.h"
24
25using namespace std::string_literals;
26
27namespace android {
28namespace init {
29
30class DeviceHandlerTester {
31  public:
32    void TestGetSymlinks(const std::string& platform_device, const Uevent& uevent,
33                         const std::vector<std::string> expected_links) {
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;
38        mkdir_recursive(platform_device_dir, 0777, nullptr);
39
40        std::string platform_bus = fake_sys_root.path + "/bus/platform"s;
41        mkdir_recursive(platform_bus, 0777, nullptr);
42        symlink(platform_bus.c_str(), (platform_device_dir + "/subsystem").c_str());
43
44        mkdir_recursive(android::base::Dirname(fake_sys_root.path + uevent.path), 0777, nullptr);
45
46        std::vector<std::string> result;
47        result = device_handler_.GetBlockDeviceSymlinks(uevent);
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
63TEST(device_handler, get_block_device_symlinks_success_platform) {
64    // These are actual paths from bullhead
65    const char* platform_device = "/devices/soc.0/f9824900.sdhci";
66    Uevent uevent = {
67        .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0",
68        .partition_name = "",
69        .partition_num = -1,
70    };
71    std::vector<std::string> expected_result{"/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0"};
72
73    DeviceHandlerTester device_handler_tester_;
74    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
75}
76
77TEST(device_handler, get_block_device_symlinks_success_platform_with_partition) {
78    // These are actual paths from bullhead
79    const char* platform_device = "/devices/soc.0/f9824900.sdhci";
80    Uevent uevent = {
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",
87        "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
88        "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
89    };
90
91    DeviceHandlerTester device_handler_tester_;
92    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
93}
94
95TEST(device_handler, get_block_device_symlinks_success_platform_with_partition_only_num) {
96    const char* platform_device = "/devices/soc.0/f9824900.sdhci";
97    Uevent uevent = {
98        .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
99        .partition_name = "",
100        .partition_num = 1,
101    };
102    std::vector<std::string> expected_result{
103        "/dev/block/platform/soc.0/f9824900.sdhci/by-num/p1",
104        "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
105    };
106
107    DeviceHandlerTester device_handler_tester_;
108    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
109}
110
111TEST(device_handler, get_block_device_symlinks_success_platform_with_partition_only_name) {
112    const char* platform_device = "/devices/soc.0/f9824900.sdhci";
113    Uevent uevent = {
114        .path = "/devices/soc.0/f9824900.sdhci/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
115        .partition_name = "modem",
116        .partition_num = -1,
117    };
118    std::vector<std::string> expected_result{
119        "/dev/block/platform/soc.0/f9824900.sdhci/by-name/modem",
120        "/dev/block/platform/soc.0/f9824900.sdhci/mmcblk0p1",
121    };
122
123    DeviceHandlerTester device_handler_tester_;
124    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
125}
126
127TEST(device_handler, get_block_device_symlinks_success_pci) {
128    const char* platform_device = "/devices/do/not/match";
129    Uevent uevent = {
130        .path = "/devices/pci0000:00/0000:00:1f.2/mmcblk0", .partition_name = "", .partition_num = -1,
131    };
132    std::vector<std::string> expected_result{"/dev/block/pci/pci0000:00/0000:00:1f.2/mmcblk0"};
133
134    DeviceHandlerTester device_handler_tester_;
135    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
136}
137
138TEST(device_handler, get_block_device_symlinks_pci_bad_format) {
139    const char* platform_device = "/devices/do/not/match";
140    Uevent uevent = {
141        .path = "/devices/pci//mmcblk0", .partition_name = "", .partition_num = -1,
142    };
143    std::vector<std::string> expected_result{};
144
145    DeviceHandlerTester device_handler_tester_;
146    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
147}
148
149TEST(device_handler, get_block_device_symlinks_success_vbd) {
150    const char* platform_device = "/devices/do/not/match";
151    Uevent uevent = {
152        .path = "/devices/vbd-1234/mmcblk0", .partition_name = "", .partition_num = -1,
153    };
154    std::vector<std::string> expected_result{"/dev/block/vbd/1234/mmcblk0"};
155
156    DeviceHandlerTester device_handler_tester_;
157    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
158}
159
160TEST(device_handler, get_block_device_symlinks_vbd_bad_format) {
161    const char* platform_device = "/devices/do/not/match";
162    Uevent uevent = {
163        .path = "/devices/vbd-/mmcblk0", .partition_name = "", .partition_num = -1,
164    };
165    std::vector<std::string> expected_result{};
166
167    DeviceHandlerTester device_handler_tester_;
168    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
169}
170
171TEST(device_handler, get_block_device_symlinks_no_matches) {
172    const char* platform_device = "/devices/soc.0/f9824900.sdhci";
173    Uevent uevent = {
174        .path = "/devices/soc.0/not_the_device/mmc_host/mmc0/mmc0:0001/block/mmcblk0p1",
175        .partition_name = "",
176        .partition_num = -1,
177    };
178    std::vector<std::string> expected_result;
179
180    DeviceHandlerTester device_handler_tester_;
181    device_handler_tester_.TestGetSymlinks(platform_device, uevent, expected_result);
182}
183
184TEST(device_handler, sanitize_null) {
185    SanitizePartitionName(nullptr);
186}
187
188TEST(device_handler, sanitize_empty) {
189    std::string empty;
190    SanitizePartitionName(&empty);
191    EXPECT_EQ(0u, empty.size());
192}
193
194TEST(device_handler, sanitize_allgood) {
195    std::string good =
196        "abcdefghijklmnopqrstuvwxyz"
197        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
198        "0123456789"
199        "_-.";
200    std::string good_copy = good;
201    SanitizePartitionName(&good);
202    EXPECT_EQ(good_copy, good);
203}
204
205TEST(device_handler, sanitize_somebad) {
206    std::string string = "abc!@#$%^&*()";
207    SanitizePartitionName(&string);
208    EXPECT_EQ("abc__________", string);
209}
210
211TEST(device_handler, sanitize_allbad) {
212    std::string string = "!@#$%^&*()";
213    SanitizePartitionName(&string);
214    EXPECT_EQ("__________", string);
215}
216
217TEST(device_handler, sanitize_onebad) {
218    std::string string = ")";
219    SanitizePartitionName(&string);
220    EXPECT_EQ("_", string);
221}
222
223TEST(device_handler, DevPermissionsMatchNormal) {
224    // Basic from ueventd.rc
225    // /dev/null                 0666   root       root
226    Permissions permissions("/dev/null", 0666, 0, 0);
227    EXPECT_TRUE(permissions.Match("/dev/null"));
228    EXPECT_FALSE(permissions.Match("/dev/nullsuffix"));
229    EXPECT_FALSE(permissions.Match("/dev/nul"));
230    EXPECT_EQ(0666U, permissions.perm());
231    EXPECT_EQ(0U, permissions.uid());
232    EXPECT_EQ(0U, permissions.gid());
233}
234
235TEST(device_handler, DevPermissionsMatchPrefix) {
236    // Prefix from ueventd.rc
237    // /dev/dri/*                0666   root       graphics
238    Permissions permissions("/dev/dri/*", 0666, 0, 1000);
239    EXPECT_TRUE(permissions.Match("/dev/dri/some_dri_device"));
240    EXPECT_TRUE(permissions.Match("/dev/dri/some_other_dri_device"));
241    EXPECT_TRUE(permissions.Match("/dev/dri/"));
242    EXPECT_FALSE(permissions.Match("/dev/dr/non_match"));
243    EXPECT_EQ(0666U, permissions.perm());
244    EXPECT_EQ(0U, permissions.uid());
245    EXPECT_EQ(1000U, permissions.gid());
246}
247
248TEST(device_handler, DevPermissionsMatchWildcard) {
249    // Wildcard example
250    // /dev/device*name                0666   root       graphics
251    Permissions permissions("/dev/device*name", 0666, 0, 1000);
252    EXPECT_TRUE(permissions.Match("/dev/devicename"));
253    EXPECT_TRUE(permissions.Match("/dev/device123name"));
254    EXPECT_TRUE(permissions.Match("/dev/deviceabcname"));
255    EXPECT_FALSE(permissions.Match("/dev/device123name/subdevice"));
256    EXPECT_FALSE(permissions.Match("/dev/deviceame"));
257    EXPECT_EQ(0666U, permissions.perm());
258    EXPECT_EQ(0U, permissions.uid());
259    EXPECT_EQ(1000U, permissions.gid());
260}
261
262TEST(device_handler, DevPermissionsMatchWildcardPrefix) {
263    // Wildcard+Prefix example
264    // /dev/device*name*                0666   root       graphics
265    Permissions permissions("/dev/device*name*", 0666, 0, 1000);
266    EXPECT_TRUE(permissions.Match("/dev/devicename"));
267    EXPECT_TRUE(permissions.Match("/dev/device123name"));
268    EXPECT_TRUE(permissions.Match("/dev/deviceabcname"));
269    EXPECT_TRUE(permissions.Match("/dev/device123namesomething"));
270    // FNM_PATHNAME doesn't match '/' with *
271    EXPECT_FALSE(permissions.Match("/dev/device123name/something"));
272    EXPECT_FALSE(permissions.Match("/dev/deviceame"));
273    EXPECT_EQ(0666U, permissions.perm());
274    EXPECT_EQ(0U, permissions.uid());
275    EXPECT_EQ(1000U, permissions.gid());
276}
277
278TEST(device_handler, SysfsPermissionsMatchWithSubsystemNormal) {
279    // /sys/devices/virtual/input/input*   enable      0660  root   input
280    SysfsPermissions permissions("/sys/devices/virtual/input/input*", "enable", 0660, 0, 1001);
281    EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/input0", "input"));
282    EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/virtual/input/not_input0", "input"));
283    EXPECT_EQ(0660U, permissions.perm());
284    EXPECT_EQ(0U, permissions.uid());
285    EXPECT_EQ(1001U, permissions.gid());
286}
287
288TEST(device_handler, SysfsPermissionsMatchWithSubsystemClass) {
289    // /sys/class/input/event*   enable      0660  root   input
290    SysfsPermissions permissions("/sys/class/input/event*", "enable", 0660, 0, 1001);
291    EXPECT_TRUE(permissions.MatchWithSubsystem(
292        "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "input"));
293    EXPECT_FALSE(permissions.MatchWithSubsystem(
294        "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/not_event0", "input"));
295    EXPECT_FALSE(permissions.MatchWithSubsystem(
296        "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/input/input0/event0", "not_input"));
297    EXPECT_EQ(0660U, permissions.perm());
298    EXPECT_EQ(0U, permissions.uid());
299    EXPECT_EQ(1001U, permissions.gid());
300}
301
302TEST(device_handler, SysfsPermissionsMatchWithSubsystemBus) {
303    // /sys/bus/i2c/devices/i2c-*   enable      0660  root   input
304    SysfsPermissions permissions("/sys/bus/i2c/devices/i2c-*", "enable", 0660, 0, 1001);
305    EXPECT_TRUE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "i2c"));
306    EXPECT_FALSE(permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/not-i2c", "i2c"));
307    EXPECT_FALSE(
308        permissions.MatchWithSubsystem("/sys/devices/soc.0/f9967000.i2c/i2c-5", "not_i2c"));
309    EXPECT_EQ(0660U, permissions.perm());
310    EXPECT_EQ(0U, permissions.uid());
311    EXPECT_EQ(1001U, permissions.gid());
312}
313
314}  // namespace init
315}  // namespace android
316