test: add tests for device_new_from_nulstr()

This commit is contained in:
Yu Watanabe 2020-12-06 21:12:17 +09:00
parent 31063db0b4
commit e6f8828715
1 changed files with 45 additions and 0 deletions

View File

@ -1,9 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "device-enumerator-private.h"
#include "device-internal.h"
#include "device-private.h"
#include "device-util.h"
#include "hashmap.h"
#include "nulstr-util.h"
#include "string-util.h"
#include "tests.h"
#include "time-util.h"
@ -161,6 +163,47 @@ static void test_sd_device_enumerator_filter_subsystem(void) {
assert_se(n_new_dev <= 10);
}
static void test_sd_device_new_from_nulstr(void) {
const char *devlinks =
"/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
"/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
"/dev/disk/by-label/Arch\\x20Linux\0"
"/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
"/dev/disk/by-partlabel/Arch\\x20Linux\0"
"\0";
_cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL;
_cleanup_free_ uint8_t *nulstr_copy = NULL;
const char *devlink;
const uint8_t *nulstr;
size_t len;
log_info("/* %s */", __func__);
assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0);
/* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
* test for generating and parsing nulstr. For issue #17772. */
NULSTR_FOREACH(devlink, devlinks) {
log_device_info(device, "setting devlink: %s", devlink);
assert_se(device_add_devlink(device, devlink) >= 0);
assert_se(set_contains(device->devlinks, devlink));
}
/* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
assert_se(device_add_property_internal(device, "SEQNUM", "1") >= 0);
assert_se(device_add_property_internal(device, "ACTION", "change") >= 0);
assert_se(device_get_properties_nulstr(device, &nulstr, &len) >= 0);
assert_se(nulstr_copy = newdup(uint8_t, nulstr, len));
assert_se(device_new_from_nulstr(&from_nulstr, nulstr_copy, len) >= 0);
NULSTR_FOREACH(devlink, devlinks) {
log_device_info(from_nulstr, "checking devlink: %s", devlink);
assert_se(set_contains(from_nulstr->devlinks, devlink));
}
}
int main(int argc, char **argv) {
test_setup_logging(LOG_INFO);
@ -168,5 +211,7 @@ int main(int argc, char **argv) {
test_sd_device_enumerator_subsystems();
test_sd_device_enumerator_filter_subsystem();
test_sd_device_new_from_nulstr();
return 0;
}