mirror of
https://github.com/systemd/systemd.git
synced 2025-01-21 22:04:01 +03:00
test-sd-hwdb: add a simple test for export sd-hwdb functions
This commit is contained in:
parent
3f608087bd
commit
ca7a9afc7e
@ -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);
|
||||
|
@ -661,6 +661,10 @@ tests += [
|
||||
[['src/test/test-bus-util.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-sd-hwdb.c'],
|
||||
[],
|
||||
[]],
|
||||
]
|
||||
|
||||
############################################################
|
||||
|
63
src/test/test-sd-hwdb.c
Normal file
63
src/test/test-sd-hwdb.c
Normal file
@ -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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user