]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sd-hwdb.c
f18634cc34e32fc2251f2f2cd7db7f4d406056ba
[thirdparty/systemd.git] / src / test / test-sd-hwdb.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-hwdb.h"
4
5 #include "alloc-util.h"
6 #include "errno-util.h"
7 #include "errno.h"
8 #include "hwdb-internal.h"
9 #include "nulstr-util.h"
10 #include "tests.h"
11
12 TEST(failed_enumerate) {
13 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
14 const char *key, *value;
15
16 assert_se(sd_hwdb_new(&hwdb) == 0);
17
18 assert_se(sd_hwdb_seek(hwdb, "no-such-modalias-should-exist") == 0);
19
20 assert_se(sd_hwdb_enumerate(hwdb, &key, &value) == 0);
21 assert_se(sd_hwdb_enumerate(hwdb, &key, NULL) == -EINVAL);
22 assert_se(sd_hwdb_enumerate(hwdb, NULL, &value) == -EINVAL);
23 }
24
25 #define DELL_MODALIAS \
26 "evdev:atkbd:dmi:bvnXXX:bvrYYY:bdZZZ:svnDellXXX:pnYYY:"
27
28 TEST(basic_enumerate) {
29 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
30 const char *key, *value;
31 size_t len1 = 0, len2 = 0;
32 int r;
33
34 assert_se(sd_hwdb_new(&hwdb) == 0);
35
36 assert_se(sd_hwdb_seek(hwdb, DELL_MODALIAS) == 0);
37
38 for (;;) {
39 r = sd_hwdb_enumerate(hwdb, &key, &value);
40 assert_se(IN_SET(r, 0, 1));
41 if (r == 0)
42 break;
43 assert_se(key);
44 assert_se(value);
45 log_debug("A: \"%s\" \"%s\"", key, value);
46 len1 += strlen(key) + strlen(value);
47 }
48
49 SD_HWDB_FOREACH_PROPERTY(hwdb, DELL_MODALIAS, key, value) {
50 log_debug("B: \"%s\" \"%s\"", key, value);
51 len2 += strlen(key) + strlen(value);
52 }
53
54 assert_se(len1 == len2);
55 }
56
57 TEST(sd_hwdb_new_from_path) {
58 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
59 const char *hwdb_bin_path = NULL;
60 int r;
61
62 assert_se(sd_hwdb_new_from_path(NULL, &hwdb) == -EINVAL);
63 assert_se(sd_hwdb_new_from_path("", &hwdb) == -EINVAL);
64 assert_se(sd_hwdb_new_from_path("/path/that/should/not/exist", &hwdb) < 0);
65
66 NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
67 r = sd_hwdb_new_from_path(hwdb_bin_path, &hwdb);
68 if (r >= 0)
69 break;
70 }
71
72 assert_se(r >= 0);
73 }
74
75 static int intro(void) {
76 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
77 int r;
78
79 r = sd_hwdb_new(&hwdb);
80 if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
81 return log_tests_skipped_errno(r, "cannot open hwdb");
82
83 return EXIT_SUCCESS;
84 }
85
86 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);