[Spice-devel] [PATCH 2/2] Add a test for options

Frediano Ziglio fziglio at redhat.com
Fri Jul 26 14:28:36 UTC 2019


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 .gitignore               |  1 +
 configure.ac             |  2 +-
 src/tests/Makefile.am    |  5 +++
 src/tests/options_test.c | 93 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100644 src/tests/options_test.c

diff --git a/.gitignore b/.gitignore
index 74e2a87..aee6c93 100644
--- a/.gitignore
+++ b/.gitignore
@@ -24,6 +24,7 @@ src/tests/run
 *.log
 *.trs
 x11spice_test
+options_test
 
 *.gcno
 *.gcda
diff --git a/configure.ac b/configure.ac
index fa23483..1786416 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,5 @@
 AC_INIT([x11spice], 1.1)
-AM_INIT_AUTOMAKE
+AM_INIT_AUTOMAKE([subdir-objects])
 
 PKG_CHECK_MODULES(XCB, xcb)
 PKG_CHECK_MODULES(DAMAGE, xcb-damage)
diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am
index 38b735d..2b711bb 100644
--- a/src/tests/Makefile.am
+++ b/src/tests/Makefile.am
@@ -16,6 +16,11 @@ x11spice_test_SOURCES = \
     util.h \
     main.c
 
+TESTS += options_test
+options_test_CPPFLAGS = -I$(top_srcdir)/src
+options_test_LDADD = $(GLIB2_LIBS)
+options_test_SOURCES = options_test.c ../options.c
+
 noinst_PROGRAMS = $(TESTS)
 
 .PHONY: leakcheck.log callgrind.out.x
diff --git a/src/tests/options_test.c b/src/tests/options_test.c
new file mode 100644
index 0000000..2dc5b7f
--- /dev/null
+++ b/src/tests/options_test.c
@@ -0,0 +1,93 @@
+#undef NDEBUG
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+#include <glib.h>
+
+#include "options.h"
+
+static void test(const char *cmd_line, const char *expected)
+{
+    options_t options;
+    int argc = 0;
+    int rc;
+
+    char **argv = g_strsplit_set(cmd_line, " ", 0);
+    while (argv[argc]) {
+        ++argc;
+    }
+
+    options_init(&options);
+    rc = options_load(&options, argc, argv);
+    g_strfreev(argv);
+    if (rc != 0) {
+        fprintf(stderr, "Unable to load options, rc %d\n", rc);
+        options_free(&options);
+        exit(1);
+    }
+
+    // parse expected fields
+    for (;;) {
+        char name[102], value[102], buf[102];
+        int pos = -1;
+        // 2 next token are couple <NAME> <VALUE>
+        if (sscanf(expected, " %100s %100s %n", name, value, &pos) < 2)
+            break;
+        assert(pos > 0);
+
+#define VAL(fmt, fld) \
+        if (strcmp(name, #fld) == 0) \
+            snprintf(buf, sizeof(buf), fmt, options.fld); \
+        else
+
+        // extract expected value
+        VAL("%s", display)
+        VAL("%d", allow_control)
+        VAL("%s", listen)
+        VAL("%s", ssl.ca_cert_file)
+        {
+            fprintf(stderr, "name '%s' not recognized\n", name);
+            exit(1);
+        }
+
+        // compare expected and parsed
+        if (strcmp(buf, value) != 0) {
+            fprintf(stderr, "Unexpected results:\n\tcmd:'%s'\n\tfld:%s\n\tout:%s\n\texp:%s\n",
+                    cmd_line, name, buf, value);
+            exit(1);
+        }
+        expected += pos;
+    }
+    options_free(&options);
+}
+
+#define TEST(cmd, exp) test("PROGRAM " cmd, exp)
+
+int main(int argc, char **argv)
+{
+    FILE *f = fopen("test.conf", "w");
+
+    fprintf(f, "[spice]\ndisplay=config_display\nlisten=8765\nallow-control=true");
+    fclose(f);
+
+    // some test, input and output
+    test("PROGRAM", "display (null) allow_control 0 listen 5900 ssl.ca_cert_file (null)");
+    TEST("--hide", "display (null) allow_control 0 listen 5900 ssl.ca_cert_file (null)");
+    TEST("1234",
+         "display (null) allow_control 0 listen 1234");
+    TEST("--config=test.conf",
+         "display config_display allow_control 1 listen 8765");
+    TEST("--config=test.conf 123",
+         "display config_display allow_control 1 listen 123");
+    TEST("--no-allow-control --config=test.conf 123",
+         "display config_display allow_control 0 listen 123");
+    TEST("--display DISPLAY --config=test.conf 123",
+         "display DISPLAY allow_control 1 listen 123 ssl.ca_cert_file (null)");
+    TEST("--ssl=foo",
+         "display (null) allow_control 0 listen 5900 ssl.ca_cert_file foo");
+
+    unlink("test.conf");
+    return 0;
+}
-- 
2.20.1



More information about the Spice-devel mailing list