]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4368277c | 2 | |
ca7a9afc ZJS |
3 | #include "sd-hwdb.h" |
4 | ||
5 | #include "alloc-util.h" | |
3c14dc61 | 6 | #include "errno-util.h" |
ca7a9afc ZJS |
7 | #include "errno.h" |
8 | #include "tests.h" | |
9 | ||
4dd73b88 | 10 | static int test_failed_enumerate(void) { |
86f4edef | 11 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
ca7a9afc | 12 | const char *key, *value; |
4dd73b88 | 13 | int r; |
ca7a9afc ZJS |
14 | |
15 | log_info("/* %s */", __func__); | |
16 | ||
4dd73b88 | 17 | r = sd_hwdb_new(&hwdb); |
3c14dc61 | 18 | if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r)) |
4dd73b88 ZJS |
19 | return r; |
20 | assert_se(r == 0); | |
ca7a9afc ZJS |
21 | |
22 | assert_se(sd_hwdb_seek(hwdb, "no-such-modalias-should-exist") == 0); | |
23 | ||
24 | assert_se(sd_hwdb_enumerate(hwdb, &key, &value) == 0); | |
25 | assert_se(sd_hwdb_enumerate(hwdb, &key, NULL) == -EINVAL); | |
26 | assert_se(sd_hwdb_enumerate(hwdb, NULL, &value) == -EINVAL); | |
4dd73b88 ZJS |
27 | |
28 | return 0; | |
ca7a9afc ZJS |
29 | } |
30 | ||
31 | #define DELL_MODALIAS \ | |
32 | "evdev:atkbd:dmi:bvnXXX:bvrYYY:bdZZZ:svnDellXXX:pnYYY" | |
33 | ||
34 | static void test_basic_enumerate(void) { | |
86f4edef | 35 | _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
ca7a9afc ZJS |
36 | const char *key, *value; |
37 | size_t len1 = 0, len2 = 0; | |
38 | int r; | |
39 | ||
40 | log_info("/* %s */", __func__); | |
41 | ||
42 | assert_se(sd_hwdb_new(&hwdb) == 0); | |
43 | ||
44 | assert_se(sd_hwdb_seek(hwdb, DELL_MODALIAS) == 0); | |
45 | ||
46 | for (;;) { | |
47 | r = sd_hwdb_enumerate(hwdb, &key, &value); | |
48 | assert(IN_SET(r, 0, 1)); | |
49 | if (r == 0) | |
50 | break; | |
51 | assert(key); | |
52 | assert(value); | |
53 | log_debug("A: \"%s\" → \"%s\"", key, value); | |
54 | len1 += strlen(key) + strlen(value); | |
55 | } | |
56 | ||
57 | SD_HWDB_FOREACH_PROPERTY(hwdb, DELL_MODALIAS, key, value) { | |
58 | log_debug("B: \"%s\" → \"%s\"", key, value); | |
59 | len2 += strlen(key) + strlen(value); | |
60 | } | |
61 | ||
62 | assert_se(len1 == len2); | |
63 | } | |
64 | ||
65 | int main(int argc, char *argv[]) { | |
4dd73b88 ZJS |
66 | int r; |
67 | ||
ca7a9afc ZJS |
68 | test_setup_logging(LOG_DEBUG); |
69 | ||
4dd73b88 ZJS |
70 | r = test_failed_enumerate(); |
71 | if (r < 0) | |
72 | return log_tests_skipped_errno(r, "cannot open hwdb"); | |
73 | ||
ca7a9afc ZJS |
74 | test_basic_enumerate(); |
75 | ||
76 | return 0; | |
77 | } |