[Spice-devel] [PATCH spice-common v2] test: Add a test for subject_to_x509_name function

Frediano Ziglio fziglio at redhat.com
Tue Jan 8 12:47:05 UTC 2019


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 tests/Makefile.am       |  20 +++++-
 tests/meson.build       |   2 +-
 tests/test-ssl-verify.c | 134 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+), 2 deletions(-)
 create mode 100644 tests/test-ssl-verify.c

Changes since v1:
- do not change ssl_verify.c;
- add Meson support;
- always compile the test

diff --git a/tests/Makefile.am b/tests/Makefile.am
index f54e394..da65762 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,6 @@
 NULL =
 
-TESTS = test_logging test_marshallers
+TESTS = test_logging test_marshallers test_ssl_verify
 noinst_PROGRAMS = $(TESTS)
 
 test_logging_SOURCES = test-logging.c
@@ -103,4 +103,22 @@ EXTRA_DIST =				\
 	test-marshallers.proto		\
 	$(NULL)
 
+
+test_ssl_verify_SOURCES =			\
+	test-ssl-verify.c			\
+	$(NULL)
+test_ssl_verify_CFLAGS =		\
+	-I$(top_srcdir)			\
+	$(GLIB2_CFLAGS)			\
+	$(PROTOCOL_CFLAGS)		\
+	$(OPENSSL_CFLAGS)		\
+	$(NULL)
+test_ssl_verify_LDADD =						\
+	$(top_builddir)/common/libspice-common-client.la	\
+	$(top_builddir)/common/libspice-common.la		\
+	$(GLIB2_LIBS)						\
+	$(OPENSSL_LIBS)						\
+	$(NULL)
+
+
 -include $(top_srcdir)/git.mk
diff --git a/tests/meson.build b/tests/meson.build
index e53fd64..f1a9334 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,7 +1,7 @@
 #
 # Build tests
 #
-tests = ['test-logging', 'test-region']
+tests = ['test-logging', 'test-region', 'test-ssl-verify']
 tests_deps = [spice_common_dep]
 
 foreach t : tests
diff --git a/tests/test-ssl-verify.c b/tests/test-ssl-verify.c
new file mode 100644
index 0000000..1b18a3d
--- /dev/null
+++ b/tests/test-ssl-verify.c
@@ -0,0 +1,134 @@
+/*
+   Copyright (C) 2018 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#include "common/ssl_verify.c"
+
+static gchar **result_set = NULL;
+static gchar **next_result = NULL;
+static int result_len = 0;
+
+// set expected result for next test, these will be checked
+// results will be separate by ':' which is not a special character
+static void setup_results(const char *results)
+{
+    g_assert_null(result_set);
+    g_assert_null(next_result);
+    result_set = g_strsplit_set(results, ":", -1);
+    guint len = g_strv_length(result_set);
+    g_assert_true(len % 2 == 0);
+    next_result = result_set;
+    result_len = len / 2;
+}
+
+// cleanup results and prepare for next test
+static void tear_results(void)
+{
+    g_assert_nonnull(next_result);
+    g_assert_null(*next_result);
+    g_strfreev(result_set);
+    result_set = NULL;
+    result_len = 0;
+    next_result = NULL;
+}
+
+// get next expected value
+static const char *get_next_result(void)
+{
+    g_assert_nonnull(next_result);
+    g_assert_nonnull(*next_result);
+    return *next_result++;
+}
+
+// This override the OpenSSL function
+int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
+                               const unsigned char *bytes, int len, int loc,
+                               int set)
+{
+    g_assert_nonnull(name);
+    g_assert_nonnull(field);
+    g_assert_cmpint(type, ==, MBSTRING_UTF8);
+    g_assert_nonnull(bytes);
+    g_assert_cmpint(len, ==, -1);
+    g_assert_cmpint(loc, ==, -1);
+    g_assert_cmpint(set, ==, 0);
+    g_assert_cmpstr(field, ==, get_next_result());
+    g_assert_cmpstr((const char *)bytes, ==, get_next_result());
+    return 1;
+}
+
+typedef struct {
+    const char *input;
+    const char *output;
+    gboolean success;
+} TestGenericParams;
+
+static void test_generic(const void *arg)
+{
+    const TestGenericParams *params = arg;
+    X509_NAME *name;
+    int num_entries;
+
+    setup_results(params->output);
+    name = subject_to_x509_name(params->input, &num_entries);
+    if (params->success) {
+        g_assert_cmpint(num_entries, ==, result_len);
+        g_assert_nonnull(name);
+        X509_NAME_free(name);
+    } else {
+        g_assert_null(name);
+    }
+    tear_results();
+}
+
+int main(int argc, char *argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+#define TEST_SUCCESS(name, input, output) \
+    const TestGenericParams test_ ## name = { input, output, TRUE }; \
+    g_test_add_data_func("/ssl_verify/" #name, &test_ ## name, test_generic)
+#define TEST_ERROR(name, input, output) \
+    const TestGenericParams test_ ## name = { input, output, FALSE }; \
+    g_test_add_data_func("/ssl_verify/" #name, &test_ ## name, test_generic)
+
+    // normal
+    TEST_SUCCESS(easy1, "C=UK", "C:UK");
+    TEST_SUCCESS(easy2, "a=b,c=d", "a:b:c:d");
+
+    // check spaces before keys are ignored
+    TEST_SUCCESS(space1, "    C=UK", "C:UK");
+    TEST_SUCCESS(space2, "C=UK,    A=B", "C:UK:A:B");
+
+    // empty key
+    TEST_SUCCESS(empty1, "", "");
+    TEST_SUCCESS(empty2, "a=b,", "a:b");
+    TEST_SUCCESS(empty3, "   ", "");
+    TEST_SUCCESS(empty4, "a=b,  ", "a:b");
+
+    // empty value
+    TEST_ERROR(empty5, "a=", "");
+
+    // quoting
+    TEST_SUCCESS(quote1, "\\,=a", ",:a");
+    TEST_SUCCESS(quote2, "\\\\=a", "\\:a");
+    TEST_SUCCESS(quote3, "a=\\,b,c=d", "a:,b:c:d");
+    TEST_ERROR(quote4, ",", "");
+
+    TEST_ERROR(no_value1, "a", "");
+    TEST_ERROR(no_value2, "a,b=c", "");
+
+    return g_test_run();
+}
-- 
2.20.1



More information about the Spice-devel mailing list