summaryrefslogtreecommitdiffstats
path: root/src/test/test-parse-argument.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-02-14 13:35:32 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-02-15 08:50:45 +0100
commit1751efdf6fd2d2f1b34014a1f19a4db3b5083d27 (patch)
tree1dbd7541e92c29ad18cb578d58622e214f2888c2 /src/test/test-parse-argument.c
parentMove and rename parse_json_argument() function (diff)
downloadsystemd-1751efdf6fd2d2f1b34014a1f19a4db3b5083d27.tar.xz
systemd-1751efdf6fd2d2f1b34014a1f19a4db3b5083d27.zip
test-parse-argument: add a test for the three parse_*_argument() functions
This mostly tests the return values and that the xsprintf buffers are big enough.
Diffstat (limited to 'src/test/test-parse-argument.c')
-rw-r--r--src/test/test-parse-argument.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/test/test-parse-argument.c b/src/test/test-parse-argument.c
new file mode 100644
index 0000000000..4081a9f25a
--- /dev/null
+++ b/src/test/test-parse-argument.c
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "parse-argument.h"
+#include "stdio-util.h"
+#include "tests.h"
+
+static void test_parse_json_argument(void) {
+ log_info("/* %s */", __func__);
+
+ JsonFormatFlags flags = JSON_FORMAT_PRETTY;
+
+ assert_se(parse_json_argument("help", &flags) == 0);
+ assert_se(flags == JSON_FORMAT_PRETTY);
+
+ assert_se(parse_json_argument("off", &flags) == 1);
+ assert_se(flags == JSON_FORMAT_OFF);
+}
+
+static void test_parse_path_argument(void) {
+ log_info("/* %s */", __func__);
+
+ _cleanup_free_ char *path = NULL;
+
+ assert_se(parse_path_argument("help", false, &path) == 0);
+ assert_se(streq(basename(path), "help"));
+
+ assert_se(parse_path_argument("/", false, &path) == 0);
+ assert_se(streq(path, "/"));
+
+ assert_se(parse_path_argument("/", true, &path) == 0);
+ assert_se(path == NULL);
+}
+
+static void test_parse_signal_argument(void) {
+ log_info("/* %s */", __func__);
+
+ int signal = -1;
+
+ assert_se(parse_signal_argument("help", &signal) == 0);
+ assert_se(signal == -1);
+
+ assert_se(parse_signal_argument("list", &signal) == 0);
+ assert_se(signal == -1);
+
+ assert_se(parse_signal_argument("SIGABRT", &signal) == 1);
+ assert_se(signal == SIGABRT);
+
+ assert_se(parse_signal_argument("ABRT", &signal) == 1);
+ assert_se(signal == SIGABRT);
+
+ char buf[DECIMAL_STR_MAX(int)];
+ xsprintf(buf, "%d", SIGABRT);
+ assert_se(parse_signal_argument(buf, &signal) == 1);
+ assert_se(signal == SIGABRT);
+}
+
+int main(int argc, char *argv[]) {
+ test_setup_logging(LOG_INFO);
+
+ test_parse_json_argument();
+ test_parse_path_argument();
+ test_parse_signal_argument();
+}