test: Use TEST macro in more cases

This converts to TEST macro in less trivial cases. This is mostly
due to having an intro or outro before/after the actual tests.

Some notable changes:
 - add a "test" to make sure the hashmap and ordered_hashmap tests
   from different compilation units are actually run in test-hashmap.c
 - make root arg a global var in test-install-root.c
 - slightly rework an EFI specific test in test-proc-cmdline.c
 - usage of saved_argv/saved_argc in test-process-util.c
 - splitting test-rlimit-util.c into several tests
 - moving the hwdb open check into intro in test-sd-hwdb.c
 - condense several "tests" into one in test-udev-util.c
This commit is contained in:
Jan Janssen
2021-11-25 15:03:10 +01:00
parent 4f7452a8eb
commit c462e63eea
13 changed files with 299 additions and 668 deletions
+26 -159
View File
@@ -5,6 +5,7 @@
#include "macro.h"
#include "string-util.h"
#include "tests.h"
#include "udev-util.h"
static void test_udev_rule_parse_value_one(const char *in, const char *expected_value, int expected_retval) {
@@ -12,6 +13,8 @@ static void test_udev_rule_parse_value_one(const char *in, const char *expected_
char *value = UINT_TO_PTR(0x12345678U);
char *endpos = UINT_TO_PTR(0x87654321U);
log_info("/* %s (%s, %s, %d) */", __func__, in, expected_value, expected_retval);
assert_se(str = strdup(in));
assert_se(udev_rule_parse_value(str, &value, &endpos) == expected_retval);
if (expected_retval < 0) {
@@ -24,161 +27,50 @@ static void test_udev_rule_parse_value_one(const char *in, const char *expected_
}
}
static void test_parse_value(void) {
TEST(udev_rule_parse_value) {
/* input: "valid operand"
* parsed: valid operand
* use the following command to help generate textual C strings:
* python3 -c 'import json; print(json.dumps(input()))' */
test_udev_rule_parse_value_one(
"\"valid operand\"",
"valid operand",
0
);
}
static void test_parse_value_with_backslashes(void) {
test_udev_rule_parse_value_one("\"valid operand\"", "valid operand", 0);
/* input: "va'l\'id\"op\"erand"
* parsed: va'l\'id"op"erand */
test_udev_rule_parse_value_one(
"\"va'l\\'id\\\"op\\\"erand\"",
"va'l\\'id\"op\"erand",
0
);
}
static void test_parse_value_no_quotes(void) {
test_udev_rule_parse_value_one(
"no quotes",
0,
-EINVAL
);
}
static void test_parse_value_noescape(void) {
test_udev_rule_parse_value_one(
"\"\\\\a\\b\\x\\y\"",
"\\\\a\\b\\x\\y",
0
);
}
static void test_parse_value_nul(void) {
test_udev_rule_parse_value_one(
"\"reject\0nul\"",
0,
-EINVAL
);
}
static void test_parse_value_escape_nothing(void) {
test_udev_rule_parse_value_one("\"va'l\\'id\\\"op\\\"erand\"", "va'l\\'id\"op\"erand", 0);
test_udev_rule_parse_value_one("no quotes", 0, -EINVAL);
test_udev_rule_parse_value_one("\"\\\\a\\b\\x\\y\"", "\\\\a\\b\\x\\y", 0);
test_udev_rule_parse_value_one("\"reject\0nul\"", 0, -EINVAL);
/* input: e"" */
test_udev_rule_parse_value_one(
"e\"\"",
"",
0
);
}
static void test_parse_value_escape_nothing2(void) {
test_udev_rule_parse_value_one("e\"\"", "", 0);
/* input: e"1234" */
test_udev_rule_parse_value_one(
"e\"1234\"",
"1234",
0
);
}
static void test_parse_value_escape_double_quote(void) {
test_udev_rule_parse_value_one("e\"1234\"", "1234", 0);
/* input: e"\"" */
test_udev_rule_parse_value_one(
"e\"\\\"\"",
"\"",
0
);
}
static void test_parse_value_escape_backslash(void) {
test_udev_rule_parse_value_one("e\"\\\"\"", "\"", 0);
/* input: e"\ */
test_udev_rule_parse_value_one(
"e\"\\",
0,
-EINVAL
);
test_udev_rule_parse_value_one("e\"\\", 0, -EINVAL);
/* input: e"\" */
test_udev_rule_parse_value_one(
"e\"\\\"",
0,
-EINVAL
);
test_udev_rule_parse_value_one("e\"\\\"", 0, -EINVAL);
/* input: e"\\" */
test_udev_rule_parse_value_one(
"e\"\\\\\"",
"\\",
0
);
test_udev_rule_parse_value_one("e\"\\\\\"", "\\", 0);
/* input: e"\\\" */
test_udev_rule_parse_value_one(
"e\"\\\\\\\"",
0,
-EINVAL
);
test_udev_rule_parse_value_one("e\"\\\\\\\"", 0, -EINVAL);
/* input: e"\\\"" */
test_udev_rule_parse_value_one(
"e\"\\\\\\\"\"",
"\\\"",
0
);
test_udev_rule_parse_value_one("e\"\\\\\\\"\"", "\\\"", 0);
/* input: e"\\\\" */
test_udev_rule_parse_value_one(
"e\"\\\\\\\\\"",
"\\\\",
0
);
}
static void test_parse_value_newline(void) {
test_udev_rule_parse_value_one("e\"\\\\\\\\\"", "\\\\", 0);
/* input: e"operand with newline\n" */
test_udev_rule_parse_value_one(
"e\"operand with newline\\n\"",
"operand with newline\n",
0
);
}
static void test_parse_value_escaped(void) {
test_udev_rule_parse_value_one("e\"operand with newline\\n\"", "operand with newline\n", 0);
/* input: e"single\rcharacter\t\aescape\bsequence" */
test_udev_rule_parse_value_one(
"e\"single\\rcharacter\\t\\aescape\\bsequence\"",
"single\rcharacter\t\aescape\bsequence",
0
);
}
static void test_parse_value_invalid_escape(void) {
"e\"single\\rcharacter\\t\\aescape\\bsequence\"", "single\rcharacter\t\aescape\bsequence", 0);
/* input: e"reject\invalid escape sequence" */
test_udev_rule_parse_value_one(
"e\"reject\\invalid escape sequence",
0,
-EINVAL
);
}
static void test_parse_value_invalid_termination(void) {
test_udev_rule_parse_value_one("e\"reject\\invalid escape sequence", 0, -EINVAL);
/* input: e"\ */
test_udev_rule_parse_value_one(
"e\"\\",
0,
-EINVAL
);
}
static void test_parse_value_unicode(void) {
test_udev_rule_parse_value_one("e\"\\", 0, -EINVAL);
/* input: "s\u1d1c\u1d04\u029c \u1d1c\u0274\u026a\u1d04\u1d0f\u1d05\u1d07 \U0001d568\U0001d560\U0001d568" */
test_udev_rule_parse_value_one(
"e\"s\\u1d1c\\u1d04\\u029c \\u1d1c\\u0274\\u026a\\u1d04\\u1d0f\\u1d05\\u1d07 \\U0001d568\\U0001d560\\U0001d568\"",
"s\xe1\xb4\x9c\xe1\xb4\x84\xca\x9c \xe1\xb4\x9c\xc9\xb4\xc9\xaa\xe1\xb4\x84\xe1\xb4\x8f\xe1\xb4\x85\xe1\xb4\x87 \xf0\x9d\x95\xa8\xf0\x9d\x95\xa0\xf0\x9d\x95\xa8",
0
);
0);
}
static void test_udev_replace_whitespace_one_len(const char *str, size_t len, const char *expected) {
@@ -196,9 +88,7 @@ static void test_udev_replace_whitespace_one(const char *str, const char *expect
test_udev_replace_whitespace_one_len(str, strlen(str), expected);
}
static void test_udev_replace_whitespace(void) {
log_info("/* %s */", __func__);
TEST(udev_replace_whitespace) {
test_udev_replace_whitespace_one("hogehoge", "hogehoge");
test_udev_replace_whitespace_one("hoge hoge", "hoge_hoge");
test_udev_replace_whitespace_one(" hoge hoge ", "hoge_hoge");
@@ -246,9 +136,7 @@ static void test_udev_resolve_subsys_kernel_one(const char *str, bool read_value
assert_se(streq(result, expected));
}
static void test_udev_resolve_subsys_kernel(void) {
log_info("/* %s */", __func__);
TEST(udev_resolve_subsys_kernel) {
test_udev_resolve_subsys_kernel_one("hoge", false, -EINVAL, NULL);
test_udev_resolve_subsys_kernel_one("[hoge", false, -EINVAL, NULL);
test_udev_resolve_subsys_kernel_one("[hoge/foo", false, -EINVAL, NULL);
@@ -267,25 +155,4 @@ static void test_udev_resolve_subsys_kernel(void) {
test_udev_resolve_subsys_kernel_one("[net/lo]/address", true, 0, "00:00:00:00:00:00");
}
int main(int argc, char **argv) {
test_parse_value();
test_parse_value_with_backslashes();
test_parse_value_no_quotes();
test_parse_value_nul();
test_parse_value_noescape();
test_parse_value_escape_nothing();
test_parse_value_escape_nothing2();
test_parse_value_escape_double_quote();
test_parse_value_escape_backslash();
test_parse_value_newline();
test_parse_value_escaped();
test_parse_value_invalid_escape();
test_parse_value_invalid_termination();
test_parse_value_unicode();
test_udev_replace_whitespace();
test_udev_resolve_subsys_kernel();
return EXIT_SUCCESS;
}
DEFINE_TEST_MAIN(LOG_INFO);