Merge pull request #27283 from mrc0mmand/assorted-test-tweaks

test: a bunch of assorted tweaks, Saturday edition
This commit is contained in:
Yu Watanabe
2023-04-16 19:39:58 +09:00
committed by GitHub
12 changed files with 245 additions and 4 deletions
+2
View File
@@ -1,6 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <stdbool.h>
#include "missing_securebits.h"
int secure_bits_to_string_alloc(int i, char **s);
+1
View File
@@ -141,6 +141,7 @@ simple_tests += files(
'test-rm-rf.c',
'test-sd-hwdb.c',
'test-sd-path.c',
'test-secure-bits.c',
'test-selinux.c',
'test-serialize.c',
'test-set.c',
+19 -1
View File
@@ -58,7 +58,6 @@
"d= \" \\n\\t\\$\\`\\\\\n" \
"\" \n"
TEST(load_env_file_1) {
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
assert_se(write_tmpfile(name, env_file_1) == 0);
@@ -129,6 +128,25 @@ TEST(load_env_file_6) {
assert_se(data[4] == NULL);
}
TEST(load_env_file_invalid_utf8) {
/* Test out a couple of assignments where the key/value has an invalid
* UTF-8 character ("noncharacter")
*
* See: https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Non-characters
*/
FOREACH_STRING(s,
"fo\ufffeo=bar",
"foo=b\uffffar",
"baz=hello world\ufffe") {
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX";
assert_se(write_tmpfile(name, s) == 0);
_cleanup_strv_free_ char **data = NULL;
assert_se(load_env_file(NULL, name, &data) == -EINVAL);
assert_se(!data);
}
}
TEST(write_and_load_env_file) {
/* Make sure that our writer, parser and the shell agree on what our env var files mean */
+35
View File
@@ -484,4 +484,39 @@ TEST(strv_env_name_is_valid) {
assert_se(!strv_env_name_is_valid(STRV_MAKE("HOME", "USER", "SHELL", "USER")));
}
TEST(getenv_path_list) {
_cleanup_strv_free_ char **path_list = NULL;
/* Empty paths */
FOREACH_STRING(s, "", ":", ":::::", " : ::: :: :") {
assert_se(setenv("TEST_GETENV_PATH_LIST", s, 1) >= 0);
assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) == -EINVAL);
assert_se(!path_list);
}
/* Invalid paths */
FOREACH_STRING(s, ".", "..", "/../", "/", "/foo/bar/baz/../foo", "foo/bar/baz") {
assert_se(setenv("TEST_GETENV_PATH_LIST", s, 1) >= 0);
assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) == -EINVAL);
assert_se(!path_list);
}
/* Valid paths mixed with invalid ones */
assert_se(setenv("TEST_GETENV_PATH_LIST", "/foo:/bar/baz:/../:/hello", 1) >= 0);
assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) == -EINVAL);
assert_se(!path_list);
/* Finally some valid paths */
assert_se(setenv("TEST_GETENV_PATH_LIST", "/foo:/bar/baz:/hello/world:/path with spaces:/final", 1) >= 0);
assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) >= 0);
assert_se(streq(path_list[0], "/foo"));
assert_se(streq(path_list[1], "/bar/baz"));
assert_se(streq(path_list[2], "/hello/world"));
assert_se(streq(path_list[3], "/path with spaces"));
assert_se(streq(path_list[4], "/final"));
assert_se(path_list[5] == NULL);
assert_se(unsetenv("TEST_GETENV_PATH_LIST") >= 0);
}
DEFINE_TEST_MAIN(LOG_DEBUG);
+43
View File
@@ -583,6 +583,49 @@ TEST(path_basename) {
assert_se(streq(formatted, "bar\nbar\nbaz\n"));
}
TEST(dup_cell) {
_cleanup_(table_unrefp) Table *t = NULL;
_cleanup_free_ char *formatted = NULL;
assert_se(t = table_new("foo", "bar", "x", "baz", ".", "%", "!", "~", "+"));
table_set_width(t, 75);
assert_se(table_add_many(t,
TABLE_STRING, "hello",
TABLE_UINT8, UINT8_C(42),
TABLE_UINT16, UINT16_C(666),
TABLE_UINT32, UINT32_C(253),
TABLE_PERCENT, 0,
TABLE_PATH_BASENAME, "/foo/bar",
TABLE_STRING, "aaa",
TABLE_STRING, "bbb",
TABLE_STRING, "ccc") >= 0);
/* Add the second row by duping cells */
for (size_t i = 0; i < table_get_columns(t); i++)
assert_se(table_dup_cell(t, table_get_cell(t, 1, i)) >= 0);
/* Another row, but dupe the last three strings from the same cell */
assert_se(table_add_many(t,
TABLE_STRING, "aaa",
TABLE_UINT8, UINT8_C(0),
TABLE_UINT16, UINT16_C(65535),
TABLE_UINT32, UINT32_C(4294967295),
TABLE_PERCENT, 100,
TABLE_PATH_BASENAME, "../") >= 0);
for (size_t i = 6; i < table_get_columns(t); i++)
assert_se(table_dup_cell(t, table_get_cell(t, 2, 0)) >= 0);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"FOO BAR X BAZ . % ! ~ +\n"
"hello 42 666 253 0% bar aaa bbb ccc\n"
"hello 42 666 253 0% bar aaa bbb ccc\n"
"aaa 0 65535 4294967295 100% ../ hello hello hello\n"));
}
static int intro(void) {
assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
assert_se(setenv("COLUMNS", "40", 1) >= 0);
+84
View File
@@ -0,0 +1,84 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include "securebits-util.h"
#include "strv.h"
#include "tests.h"
#include "unit-file.h"
static const char * const string_bits[] = {
"keep-caps",
"keep-caps-locked",
"no-setuid-fixup",
"no-setuid-fixup-locked",
"noroot",
"noroot-locked",
NULL
};
TEST(secure_bits_basic) {
_cleanup_free_ char *joined = NULL, *str = NULL;
int r;
/* Check if converting each bit from string and back to string yields
* the same value */
STRV_FOREACH(bit, string_bits) {
_cleanup_free_ char *s = NULL;
r = secure_bits_from_string(*bit);
assert_se(r > 0);
assert_se(secure_bits_to_string_alloc(r, &s) >= 0);
printf("%s = 0x%x = %s\n", *bit, (unsigned)r, s);
assert_se(streq(*bit, s));
}
/* Ditto, but with all bits at once */
joined = strv_join((char**)string_bits, " ");
assert_se(joined);
r = secure_bits_from_string(joined);
assert_se(r > 0);
assert_se(secure_bits_to_string_alloc(r, &str) >= 0);
printf("%s = 0x%x = %s\n", joined, (unsigned)r, str);
assert_se(streq(joined, str));
str = mfree(str);
/* Empty string */
assert_se(secure_bits_from_string("") == 0);
assert_se(secure_bits_from_string(" ") == 0);
/* Only invalid entries */
assert_se(secure_bits_from_string("foo bar baz") == 0);
/* Empty secure bits */
assert_se(secure_bits_to_string_alloc(0, &str) >= 0);
assert_se(isempty(str));
}
TEST(secure_bits_mix) {
static struct sbit_table {
const char *input;
const char *expected;
} sbit_table[] = {
{ "keep-caps keep-caps keep-caps", "keep-caps" },
{ "keep-caps noroot keep-caps", "keep-caps noroot" },
{ "noroot foo bar baz noroot", "noroot" },
{ "noroot \"foo\" \"bar keep-caps", "noroot" },
{ "\"noroot foo\" bar keep-caps", "keep-caps" },
{}
};
for (const struct sbit_table *s = sbit_table; s->input; s++) {
_cleanup_free_ char *str = NULL;
int r;
r = secure_bits_from_string(s->input);
assert_se(r > 0);
assert_se(secure_bits_to_string_alloc(r, &str) >= 0);
printf("%s = 0x%x = %s\n", s->input, (unsigned)r, str);
assert_se(streq(s->expected, str));
}
}
DEFINE_TEST_MAIN(LOG_DEBUG);
+27
View File
@@ -1,5 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "sd-id128.h"
#include "alloc-util.h"
#include "log.h"
#include "specifier.h"
@@ -144,6 +146,31 @@ TEST(specifiers) {
}
}
/* Bunch of specifiers that are not part of the common lists */
TEST(specifiers_assorted) {
const sd_id128_t id = SD_ID128_ALLF;
const uint64_t llu = UINT64_MAX;
const Specifier table[] = {
/* Used in src/partition/repart.c */
{ 'a', specifier_uuid, &id },
{ 'b', specifier_uint64, &llu },
{}
};
for (const Specifier *s = table; s->specifier; s++) {
char spec[3];
_cleanup_free_ char *resolved = NULL;
int r;
xsprintf(spec, "%%%c", s->specifier);
r = specifier_printf(spec, SIZE_MAX, table, NULL, NULL, &resolved);
assert_se(r >= 0);
log_info("%%%c → %s", s->specifier, resolved);
}
}
TEST(specifiers_missing_data_ok) {
_cleanup_free_ char *resolved = NULL;