1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-11 09:18:07 +03:00

test: add unit tests for the new iovec helpers

This commit is contained in:
Lennart Poettering 2024-01-05 11:34:34 +01:00
parent dddc12f9eb
commit 9d1c3e9413
3 changed files with 79 additions and 0 deletions

View File

@ -107,6 +107,7 @@ simple_tests += files(
'test-install-file.c',
'test-install-root.c',
'test-io-util.c',
'test-iovec-util.c',
'test-journal-importer.c',
'test-kbd-util.c',
'test-limits-util.c',

View File

@ -0,0 +1,57 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "iovec-util.h"
#include "tests.h"
TEST(iovec_memcmp) {
struct iovec iov1 = CONST_IOVEC_MAKE_STRING("abcdef"), iov2 = IOVEC_MAKE_STRING("bcdefg"), empty = {};
struct iovec iov1_truncated = iov1;
iov1_truncated.iov_len /= 2;
assert_se(iovec_memcmp(NULL, NULL) == 0);
assert_se(iovec_memcmp(&iov1, &iov1) == 0);
assert_se(iovec_memcmp(&iov2, &iov2) == 0);
assert_se(iovec_memcmp(&empty, &empty) == 0);
assert_se(iovec_memcmp(&iov1_truncated, &iov1_truncated) == 0);
assert_se(iovec_memcmp(&empty, NULL) == 0);
assert_se(iovec_memcmp(NULL, &empty) == 0);
assert_se(iovec_memcmp(&iov1, &iov2) < 0);
assert_se(iovec_memcmp(&iov2, &iov1) > 0);
assert_se(iovec_memcmp(&iov1, &empty) > 0);
assert_se(iovec_memcmp(&empty, &iov1) < 0);
assert_se(iovec_memcmp(&iov2, &empty) > 0);
assert_se(iovec_memcmp(&empty, &iov2) < 0);
assert_se(iovec_memcmp(&iov1_truncated, &empty) > 0);
assert_se(iovec_memcmp(&empty, &iov1_truncated) < 0);
assert_se(iovec_memcmp(&iov1, &iov1_truncated) > 0);
assert_se(iovec_memcmp(&iov1_truncated, &iov1) < 0);
assert_se(iovec_memcmp(&iov2, &iov1_truncated) > 0);
assert_se(iovec_memcmp(&iov1_truncated, &iov2) < 0);
_cleanup_(iovec_done) struct iovec copy = {};
assert_se(iovec_memdup(&iov1, &copy));
assert_se(iovec_memcmp(&iov1, &copy) == 0);
}
TEST(iovec_set_and_valid) {
struct iovec empty = {},
filled = CONST_IOVEC_MAKE_STRING("waldo"),
half = { .iov_base = (char*) "piff", .iov_len = 0 },
invalid = { .iov_base = NULL, .iov_len = 47 };
assert_se(!iovec_is_set(NULL));
assert_se(!iovec_is_set(&empty));
assert_se(iovec_is_set(&filled));
assert_se(!iovec_is_set(&half));
assert_se(!iovec_is_set(&invalid));
assert_se(iovec_is_valid(NULL));
assert_se(iovec_is_valid(&empty));
assert_se(iovec_is_valid(&filled));
assert_se(iovec_is_valid(&half));
assert_se(!iovec_is_valid(&invalid));
}
DEFINE_TEST_MAIN(LOG_INFO);

View File

@ -6,6 +6,7 @@
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "iovec-util.h"
#include "json-internal.h"
#include "json.h"
#include "math-util.h"
@ -918,4 +919,24 @@ TEST(json_sensitive) {
assert_se(!s);
}
TEST(json_iovec) {
struct iovec iov1 = CONST_IOVEC_MAKE_STRING("üxknürz"), iov2 = CONST_IOVEC_MAKE_STRING("wuffwuffmiau");
_cleanup_(json_variant_unrefp) JsonVariant *j = NULL;
assert_se(json_build(&j, JSON_BUILD_OBJECT(
JSON_BUILD_PAIR("nr1", JSON_BUILD_IOVEC_BASE64(&iov1)),
JSON_BUILD_PAIR("nr2", JSON_BUILD_IOVEC_HEX(&iov2)))) >= 0);
json_variant_dump(j, JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO, /* f= */ NULL, /* prefix= */ NULL);
_cleanup_(iovec_done) struct iovec a = {}, b = {};
assert_se(json_variant_unbase64_iovec(json_variant_by_key(j, "nr1"), &a) >= 0);
assert_se(json_variant_unhex_iovec(json_variant_by_key(j, "nr2"), &b) >= 0);
assert_se(iovec_memcmp(&iov1, &a) == 0);
assert_se(iovec_memcmp(&iov2, &b) == 0);
assert_se(iovec_memcmp(&iov2, &a) < 0);
assert_se(iovec_memcmp(&iov1, &b) > 0);
}
DEFINE_TEST_MAIN(LOG_DEBUG);