1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "tests.h"
4 #include "udev-node.h"
5 
test_udev_node_escape_path_one(const char * path,const char * expected)6 static void test_udev_node_escape_path_one(const char *path, const char *expected) {
7         char buf[NAME_MAX+1];
8         size_t r;
9 
10         r = udev_node_escape_path(path, buf, sizeof buf);
11         log_debug("udev_node_escape_path(%s) -> %s (expected: %s)", path, buf, expected);
12         assert_se(r == strlen(expected));
13         assert_se(streq(buf, expected));
14 }
15 
TEST(udev_node_escape_path)16 TEST(udev_node_escape_path) {
17         char a[NAME_MAX+1], b[NAME_MAX+1];
18 
19         test_udev_node_escape_path_one("/disk/by-id/nvme-eui.1922908022470001001b448b44ccb9d6", "\\x2fdisk\\x2fby-id\\x2fnvme-eui.1922908022470001001b448b44ccb9d6");
20         test_udev_node_escape_path_one("/disk/by-id/nvme-eui.1922908022470001001b448b44ccb9d6-part1", "\\x2fdisk\\x2fby-id\\x2fnvme-eui.1922908022470001001b448b44ccb9d6-part1");
21         test_udev_node_escape_path_one("/disk/by-id/nvme-eui.1922908022470001001b448b44ccb9d6-part2", "\\x2fdisk\\x2fby-id\\x2fnvme-eui.1922908022470001001b448b44ccb9d6-part2");
22         test_udev_node_escape_path_one("/disk/by-id/nvme-WDC_PC_SN720_SDAQNTW-512G-1001_192290802247", "\\x2fdisk\\x2fby-id\\x2fnvme-WDC_PC_SN720_SDAQNTW-512G-1001_192290802247");
23         test_udev_node_escape_path_one("/disk/by-id/nvme-WDC_PC_SN720_SDAQNTW-512G-1001_192290802247-part1", "\\x2fdisk\\x2fby-id\\x2fnvme-WDC_PC_SN720_SDAQNTW-512G-1001_192290802247-part1");
24         test_udev_node_escape_path_one("/disk/by-id/nvme-WDC_PC_SN720_SDAQNTW-512G-1001_192290802247-part2", "\\x2fdisk\\x2fby-id\\x2fnvme-WDC_PC_SN720_SDAQNTW-512G-1001_192290802247-part2");
25         test_udev_node_escape_path_one("/disk/by-id/usb-Generic-_SD_MMC_20120501030900000-0:0", "\\x2fdisk\\x2fby-id\\x2fusb-Generic-_SD_MMC_20120501030900000-0:0");
26 
27         memset(a, 'a', sizeof(a) - 1);
28         memcpy(a, "/disk/by-id/", strlen("/disk/by-id/"));
29         char_array_0(a);
30 
31         memset(b, 'a', sizeof(b) - 1);
32         memcpy(b, "\\x2fdisk\\x2fby-id\\x2f", strlen("\\x2fdisk\\x2fby-id\\x2f"));
33         strcpy(b + sizeof(b) - 12, "N3YhcCqFeID");
34 
35         test_udev_node_escape_path_one(a, b);
36 
37         strcpy(a + sizeof(a) - 12 - 9, "N3YhcCqFeID");
38         strcpy(b + sizeof(b) - 12, "L1oK9iKWdmi");
39         test_udev_node_escape_path_one(a, b);
40 
41         strcpy(a + sizeof(a) - 12 - 9, "a");
42         strcpy(b + sizeof(b) - 12, "A7oaHBRuuZq");
43         test_udev_node_escape_path_one(a, b);
44 
45         a[sizeof(a) - 12 - 9] = '\0';
46         b[sizeof(a) - 12] = '\0';
47         test_udev_node_escape_path_one(a, b);
48 }
49 
50 DEFINE_TEST_MAIN(LOG_INFO);
51