Systemd/src/test/test-sd-hwdb.c
Topi Miettinen 3c14dc61f7 tests: various small fixes for strict systems
Don't assume that 4MB can be allocated from stack since there could be smaller
DefaultLimitSTACK= in force, so let's use malloc(). NUL terminate the huge
strings by hand, also ensure termination in test_lz4_decompress_partial() and
optimize the memset() for the string.

Some items in /proc and /etc may not be accessible to poor unprivileged users
due to e.g. SELinux, BOFH or both, so check for EACCES and EPERM.

/var/tmp may be a symlink to /tmp and then path_compare() will always fail, so
let's stick to /tmp like elsewhere.

/tmp may be mounted with noexec option and then trying to execute scripts from
there would fail.

Detect and warn if seccomp is already in use, which could make seccomp test
fail if the syscalls are already blocked.

Unset $TMPDIR so it will not break specifier tests where %T is assumed to be
/tmp and %V /var/tmp.
2020-04-26 20:18:48 +02:00

76 lines
2 KiB
C

#include "sd-hwdb.h"
#include "alloc-util.h"
#include "errno-util.h"
#include "errno.h"
#include "tests.h"
static int test_failed_enumerate(void) {
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
const char *key, *value;
int r;
log_info("/* %s */", __func__);
r = sd_hwdb_new(&hwdb);
if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
return r;
assert_se(r == 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);
return 0;
}
#define DELL_MODALIAS \
"evdev:atkbd:dmi:bvnXXX:bvrYYY:bdZZZ:svnDellXXX:pnYYY"
static void test_basic_enumerate(void) {
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
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[]) {
int r;
test_setup_logging(LOG_DEBUG);
r = test_failed_enumerate();
if (r < 0)
return log_tests_skipped_errno(r, "cannot open hwdb");
test_basic_enumerate();
return 0;
}