]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-sd-hwdb.c
nulstr-util: Declare NULSTR_FOREACH() iterator inline
[thirdparty/systemd.git] / src / test / test-sd-hwdb.c
CommitLineData
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 7#include "errno.h"
60f0ba75
NR
8#include "hwdb-internal.h"
9#include "nulstr-util.h"
ca7a9afc
ZJS
10#include "tests.h"
11
c462e63e 12TEST(failed_enumerate) {
86f4edef 13 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
ca7a9afc 14 const char *key, *value;
ca7a9afc 15
c462e63e 16 assert_se(sd_hwdb_new(&hwdb) == 0);
ca7a9afc
ZJS
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 \
6b0485c2 26 "evdev:atkbd:dmi:bvnXXX:bvrYYY:bdZZZ:svnDellXXX:pnYYY:"
ca7a9afc 27
c462e63e 28TEST(basic_enumerate) {
86f4edef 29 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
ca7a9afc
ZJS
30 const char *key, *value;
31 size_t len1 = 0, len2 = 0;
32 int r;
33
ca7a9afc
ZJS
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);
f21b863e 40 assert_se(IN_SET(r, 0, 1));
ca7a9afc
ZJS
41 if (r == 0)
42 break;
f21b863e
YW
43 assert_se(key);
44 assert_se(value);
ca7a9afc
ZJS
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
60f0ba75
NR
57TEST(sd_hwdb_new_from_path) {
58 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
60f0ba75
NR
59 int r;
60
61 assert_se(sd_hwdb_new_from_path(NULL, &hwdb) == -EINVAL);
62 assert_se(sd_hwdb_new_from_path("", &hwdb) == -EINVAL);
63 assert_se(sd_hwdb_new_from_path("/path/that/should/not/exist", &hwdb) < 0);
64
65 NULSTR_FOREACH(hwdb_bin_path, hwdb_bin_paths) {
66 r = sd_hwdb_new_from_path(hwdb_bin_path, &hwdb);
67 if (r >= 0)
68 break;
69 }
70
71 assert_se(r >= 0);
72}
73
99839c7e
LP
74static int intro(void) {
75 _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
76 int r;
77
78 r = sd_hwdb_new(&hwdb);
79 if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
80 return log_tests_skipped_errno(r, "cannot open hwdb");
81
82 return EXIT_SUCCESS;
83}
84
e85fdacc 85DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);