[Spice-devel] [PATCH spice-server 11/16] test-sasl: Add tests for different mechanism names

Frediano Ziglio fziglio at redhat.com
Thu Dec 14 10:07:43 UTC 2017


Try different connections with different tricky names.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/tests/test-sasl.c | 60 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 5 deletions(-)

diff --git a/server/tests/test-sasl.c b/server/tests/test-sasl.c
index 500164157..f6e1bd7f0 100644
--- a/server/tests/test-sasl.c
+++ b/server/tests/test-sasl.c
@@ -23,6 +23,7 @@
 #if HAVE_SASL
 #include <unistd.h>
 #include <errno.h>
+#include <string.h>
 #include <stdbool.h>
 #include <spice.h>
 #include <sasl/sasl.h>
@@ -339,9 +340,40 @@ idle_add(GSourceFunc func, void *arg)
     g_source_unref(source);
 }
 
+typedef struct {
+    const char *mechname;
+    int mechlen;
+    bool success;
+} TestData;
+
+static char long_mechname[128];
+static TestData tests_data[] = {
+    // these should just succeed
+#define TEST_SUCCESS(mech) \
+    { mech, -1, true },
+    TEST_SUCCESS("ONE")
+    TEST_SUCCESS("TWO")
+    TEST_SUCCESS("THREE")
+
+    // these test bad mech names
+#define TEST_BAD_NAME(mech, len) \
+    { mech, len, false },
+    TEST_BAD_NAME("ON", -1)
+    TEST_BAD_NAME("NE", -1)
+    TEST_BAD_NAME("THRE", -1)
+    TEST_BAD_NAME("HREE", -1)
+    TEST_BAD_NAME("ON\x00", 3)
+    TEST_BAD_NAME("O\x00\x00", 3)
+    TEST_BAD_NAME("", -1)
+    TEST_BAD_NAME(long_mechname, 101)
+    TEST_BAD_NAME("ONE,TWO", -1)
+};
+
 static void *
 client_emulator(void *arg)
 {
+    const TestData *data = &tests_data[test_num];
+
     int sock = GPOINTER_TO_INT(arg);
 
     // send initial message
@@ -374,8 +406,8 @@ client_emulator(void *arg)
     read_all(sock, buf, mechlen);
 
     // mech name
-    write_u32(sock, 3);
-    write_all(sock, "ONE", 3);
+    write_u32(sock, data->mechlen);
+    write_all(sock, data->mechname, data->mechlen);
 
     // first challenge
     write_u32(sock, 5);
@@ -395,16 +427,32 @@ idle_next_test(void *arg)
 {
     // end previous test
     if (test_num >= 0) {
-        g_assert(encode_called);
+        const TestData *data = &tests_data[test_num];
+        if (data->success) {
+            g_assert(encode_called);
+        } else {
+            g_assert(mechlist_called);
+            g_assert(!encode_called);
+        }
         reset_test();
-        basic_event_loop_quit();
-        return FALSE;
     }
 
     // start next test
     ++test_num;
     alarm(4);
 
+    if (test_num >= G_N_ELEMENTS(tests_data)) {
+        basic_event_loop_quit();
+        return FALSE;
+    }
+
+    TestData *data = &tests_data[test_num];
+    if (data->mechlen < 0) {
+        data->mechlen = strlen(data->mechname);
+    }
+    int len = data->mechlen;
+    printf("\nRunning test %d ('%*.*s' %d)\n", test_num, len, len, data->mechname, len);
+
     int sv[2];
     g_assert_cmpint(socketpair(AF_LOCAL, SOCK_STREAM, 0, sv), ==, 0);
 
@@ -422,6 +470,8 @@ sasl_mechs(void)
 {
     start_test();
 
+    memset(long_mechname, 'X', sizeof(long_mechname));
+
     idle_add(idle_next_test, NULL);
     alarm(4);
     basic_event_loop_mainloop();
-- 
2.14.3



More information about the Spice-devel mailing list