diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-09-30 20:34:11 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-09-30 20:39:02 +0200 |
commit | ca7a9afc7e399b179d27324cdec328499d319b5c (patch) | |
tree | d39914bd01b79f9a321e84b973e2d1dfb8bd5e56 /src | |
parent | libsystemd: export all functions in sd-device.h (diff) | |
download | systemd-ca7a9afc7e399b179d27324cdec328499d319b5c.tar.xz systemd-ca7a9afc7e399b179d27324cdec328499d319b5c.zip |
test-sd-hwdb: add a simple test for export sd-hwdb functions
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/tests.h | 2 | ||||
-rw-r--r-- | src/test/meson.build | 4 | ||||
-rw-r--r-- | src/test/test-sd-hwdb.c | 63 |
3 files changed, 69 insertions, 0 deletions
diff --git a/src/shared/tests.h b/src/shared/tests.h index 549959edf2..58a3e1c32d 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once +#include <stdbool.h> + char* setup_fake_runtime_dir(void); const char* get_testdata_dir(void); const char* get_catalog_dir(void); diff --git a/src/test/meson.build b/src/test/meson.build index 21a8c3b12b..63779374eb 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -661,6 +661,10 @@ tests += [ [['src/test/test-bus-util.c'], [], []], + + [['src/test/test-sd-hwdb.c'], + [], + []], ] ############################################################ diff --git a/src/test/test-sd-hwdb.c b/src/test/test-sd-hwdb.c new file mode 100644 index 0000000000..dc6191c808 --- /dev/null +++ b/src/test/test-sd-hwdb.c @@ -0,0 +1,63 @@ +#include "sd-hwdb.h" + +#include "alloc-util.h" +#include "errno.h" +#include "tests.h" + +static void test_failed_enumerate(void) { + _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb; + const char *key, *value; + + log_info("/* %s */", __func__); + + assert_se(sd_hwdb_new(&hwdb) == 0); + + assert_se(sd_hwdb_seek(hwdb, "no-such-modalias-should-exist") == 0); + + assert_se(sd_hwdb_enumerate(hwdb, &key, &value) == 0); + assert_se(sd_hwdb_enumerate(hwdb, &key, NULL) == -EINVAL); + assert_se(sd_hwdb_enumerate(hwdb, NULL, &value) == -EINVAL); +} + +#define DELL_MODALIAS \ + "evdev:atkbd:dmi:bvnXXX:bvrYYY:bdZZZ:svnDellXXX:pnYYY" + +static void test_basic_enumerate(void) { + _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb; + const char *key, *value; + size_t len1 = 0, len2 = 0; + int r; + + log_info("/* %s */", __func__); + + assert_se(sd_hwdb_new(&hwdb) == 0); + + assert_se(sd_hwdb_seek(hwdb, DELL_MODALIAS) == 0); + + for (;;) { + r = sd_hwdb_enumerate(hwdb, &key, &value); + assert(IN_SET(r, 0, 1)); + if (r == 0) + break; + assert(key); + assert(value); + log_debug("A: \"%s\" → \"%s\"", key, value); + len1 += strlen(key) + strlen(value); + } + + SD_HWDB_FOREACH_PROPERTY(hwdb, DELL_MODALIAS, key, value) { + log_debug("B: \"%s\" → \"%s\"", key, value); + len2 += strlen(key) + strlen(value); + } + + assert_se(len1 == len2); +} + +int main(int argc, char *argv[]) { + test_setup_logging(LOG_DEBUG); + + test_failed_enumerate(); + test_basic_enumerate(); + + return 0; +} |