[PATCH spice-server v5 04/10] test-sasl: Add tests for different mechanism names

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Try different connections with different tricky names.

Signed-off-by: Frediano Ziglio <fziglio@xxxxxxxxxx>
---
 server/tests/test-sasl.c | 77 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 68 insertions(+), 9 deletions(-)

diff --git a/server/tests/test-sasl.c b/server/tests/test-sasl.c
index 41bcf7e8..3818ba1c 100644
--- a/server/tests/test-sasl.c
+++ b/server/tests/test-sasl.c
@@ -22,6 +22,7 @@
 
 #include <unistd.h>
 #include <errno.h>
+#include <string.h>
 #include <stdbool.h>
 #include <spice.h>
 #include <sasl/sasl.h>
@@ -49,6 +50,8 @@ static bool encode_called;
 static SpiceCoreInterface *core;
 static SpiceServer *server;
 
+static unsigned int test_num;
+
 static gboolean idle_end_test(void *arg);
 
 static void
@@ -340,9 +343,41 @@ 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, 100)
+    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
@@ -376,8 +411,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);
@@ -394,6 +429,13 @@ client_emulator(void *arg)
 static pthread_t
 setup_thread(void)
 {
+    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);
 
@@ -413,18 +455,35 @@ idle_end_test(void *arg)
     return G_SOURCE_REMOVE;
 }
 
+static void
+check_test_results(void)
+{
+    const TestData *data = &tests_data[test_num];
+    if (data->success) {
+        g_assert(encode_called);
+        return;
+    }
+
+    g_assert(mechlist_called);
+    g_assert(!encode_called);
+}
+
 static void
 sasl_mechs(void)
 {
     start_test();
 
-    pthread_t thread = setup_thread();
-    alarm(4);
-    basic_event_loop_mainloop();
-    g_assert_cmpint(pthread_join(thread, NULL), ==, 0);
-    alarm(0);
-    g_assert(encode_called);
-    reset_test();
+    memset(long_mechname, 'X', sizeof(long_mechname));
+
+    for (test_num = 0; test_num < G_N_ELEMENTS(tests_data); test_num++) {
+        pthread_t thread = setup_thread();
+        alarm(4);
+        basic_event_loop_mainloop();
+        g_assert_cmpint(pthread_join(thread, NULL), ==, 0);
+        alarm(0);
+        check_test_results();
+        reset_test();
+    }
 
     end_tests();
 }
-- 
2.14.3

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]