[Spice-devel] [PATCH spice-server v2 09/13] test-sasl: Add tests for different failures and cases
Frediano Ziglio
fziglio at redhat.com
Wed Dec 20 09:35:06 UTC 2017
Use some flags to specify which behaviour to change and different test
cases to test them.
Some cases specify when client stop sending data at different steps of
the process.
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
server/tests/test-sasl.c | 198 ++++++++++++++++++++++++++++++++++++-----------
1 file changed, 154 insertions(+), 44 deletions(-)
diff --git a/server/tests/test-sasl.c b/server/tests/test-sasl.c
index df0561b3a..90f9dc2e6 100644
--- a/server/tests/test-sasl.c
+++ b/server/tests/test-sasl.c
@@ -42,11 +42,24 @@ typedef struct SPICE_ATTR_PACKED SpiceInitialMessage {
} SpiceInitialMessage;
#include <spice/end-packed.h>
+typedef enum {
+ FLAG_NONE = 0,
+ FLAG_START_OK = 1,
+ FLAG_LOW_SSF = 2,
+ FLAG_SERVER_NULL_START = 4,
+ FLAG_SERVER_NULL_STEP = 8,
+ FLAG_CLIENT_NULL_START = 16,
+ FLAG_CLIENT_NULL_STEP = 32,
+ FLAG_START_ERROR = 64,
+ FLAG_STEP_ERROR = 128,
+} TestFlags;
+
static char *mechlist;
static bool mechlist_called;
static bool start_called;
static bool step_called;
static bool encode_called;
+static unsigned test_flags;
static SpiceCoreInterface *core;
static SpiceServer *server;
@@ -61,6 +74,19 @@ check_sasl_conn(sasl_conn_t *conn)
g_assert_nonnull(conn);
}
+static void
+get_step_out(const char **serverout, unsigned *serveroutlen,
+ const char *normal_data, unsigned null_flag)
+{
+ if (!(test_flags & null_flag)) {
+ *serverout = normal_data;
+ *serveroutlen = strlen(normal_data);
+ } else {
+ *serverout = NULL;
+ *serveroutlen = 0;
+ }
+}
+
int
sasl_server_init(const sasl_callback_t *callbacks, const char *appname)
{
@@ -114,7 +140,8 @@ sasl_getprop(sasl_conn_t *conn, int propnum,
g_assert_nonnull(pvalue);
if (propnum == SASL_SSF) {
- static const int val = 64;
+ static int val;
+ val = (test_flags & FLAG_LOW_SSF) ? 44 : 64;
*pvalue = &val;
}
return SASL_OK;
@@ -195,9 +222,11 @@ sasl_server_start(sasl_conn_t *conn,
g_assert(!step_called);
start_called = true;
- *serverout = "foo";
- *serveroutlen = 3;
- return SASL_OK;
+ get_step_out(serverout, serveroutlen, "foo", FLAG_SERVER_NULL_START);
+ if (test_flags & FLAG_START_ERROR) {
+ return SASL_FAIL;
+ }
+ return (test_flags & FLAG_START_OK) ? SASL_OK : SASL_CONTINUE;
}
int
@@ -212,8 +241,10 @@ sasl_server_step(sasl_conn_t *conn,
g_assert(start_called);
step_called = true;
- *serverout = "foo";
- *serveroutlen = 3;
+ get_step_out(serverout, serveroutlen, "foo", FLAG_SERVER_NULL_STEP);
+ if (test_flags & FLAG_STEP_ERROR) {
+ return SASL_FAIL;
+ }
return SASL_OK;
}
@@ -245,6 +276,7 @@ reset_test(void)
start_called = false;
step_called = false;
encode_called = false;
+ test_flags = FLAG_NONE;
}
static void
@@ -340,24 +372,41 @@ idle_add(GSourceFunc func, void *arg)
g_source_unref(source);
}
+typedef enum {
+ STEP_NONE,
+ STEP_READ_MECHLIST_LEN,
+ STEP_READ_MECHLIST,
+ STEP_WRITE_MECHNAME_LEN,
+ STEP_WRITE_MECHNAME,
+ STEP_WRITE_START_LEN,
+ STEP_WRITE_START,
+ STEP_READ_START_LEN,
+ STEP_READ_START,
+ STEP_WRITE_STEP_LEN,
+ STEP_WRITE_STEP,
+} ClientEmulationSteps;
+
typedef struct {
const char *mechname;
int mechlen;
bool success;
+ ClientEmulationSteps last_step;
+ unsigned flags;
+ int line;
} TestData;
static char long_mechname[128];
static TestData tests_data[] = {
// these should just succeed
#define TEST_SUCCESS(mech) \
- { mech, -1, true },
+ { mech, -1, true, STEP_WRITE_STEP, FLAG_NONE, __LINE__ },
TEST_SUCCESS("ONE")
TEST_SUCCESS("TWO")
TEST_SUCCESS("THREE")
// these test bad mech names
#define TEST_BAD_NAME(mech, len) \
- { mech, len, false },
+ { mech, len, false, STEP_WRITE_STEP, FLAG_NONE, __LINE__ },
TEST_BAD_NAME("ON", -1)
TEST_BAD_NAME("NE", -1)
TEST_BAD_NAME("THRE", -1)
@@ -367,51 +416,111 @@ static TestData tests_data[] = {
TEST_BAD_NAME("", -1)
TEST_BAD_NAME(long_mechname, 101)
TEST_BAD_NAME("ONE,TWO", -1)
+
+ // stop before filling everything
+#define TEST_EARLY_STOP(step) \
+ { "ONE", -1, false, step, FLAG_NONE, __LINE__},
+ TEST_EARLY_STOP(STEP_READ_MECHLIST_LEN)
+ TEST_EARLY_STOP(STEP_READ_MECHLIST)
+ TEST_EARLY_STOP(STEP_WRITE_MECHNAME_LEN)
+ TEST_EARLY_STOP(STEP_WRITE_MECHNAME)
+ TEST_EARLY_STOP(STEP_WRITE_START_LEN)
+ TEST_EARLY_STOP(STEP_WRITE_START)
+ TEST_EARLY_STOP(STEP_WRITE_STEP_LEN)
+
+#define TEST_ANY(mech, mechlen, result, step, flags) \
+ { mech, mechlen, result, step, flags, __LINE__},
+ TEST_ANY("ONE", -1, false, STEP_WRITE_STEP, FLAG_LOW_SSF)
+ TEST_ANY("ONE", -1, false, STEP_WRITE_STEP, FLAG_START_OK|FLAG_LOW_SSF)
+ TEST_ANY("ONE", -1, true, STEP_WRITE_STEP, FLAG_START_OK)
+ TEST_ANY("ONE", -1, true, STEP_WRITE_STEP, FLAG_SERVER_NULL_START)
+ TEST_ANY("ONE", -1, true, STEP_WRITE_STEP, FLAG_SERVER_NULL_STEP)
+ TEST_ANY("ONE", -1, true, STEP_WRITE_STEP, FLAG_CLIENT_NULL_START)
+ TEST_ANY("ONE", -1, true, STEP_WRITE_STEP, FLAG_CLIENT_NULL_STEP)
+ TEST_ANY("ONE", -1, false, STEP_WRITE_STEP, FLAG_START_ERROR)
+ TEST_ANY("ONE", -1, false, STEP_WRITE_STEP, FLAG_STEP_ERROR)
};
static void *
client_emulator(void *arg)
{
const TestData *data = &tests_data[test_num];
-
int sock = GPOINTER_TO_INT(arg);
- // send initial message
- write_all(sock, &initial_message, sizeof(initial_message));
-
- // server replies link ack with rsa, etc, similar to above beside
- // fixed fields
- struct {
- SpiceLinkHeader header;
- SpiceLinkReply ack;
- } msg;
- SPICE_VERIFY(sizeof(msg) == sizeof(SpiceLinkHeader) + sizeof(SpiceLinkReply));
- read_all(sock, &msg, sizeof(msg));
- uint32_t num_caps = GUINT32_FROM_LE(msg.ack.num_common_caps) + GUINT32_FROM_LE(msg.ack.num_channel_caps);
- for (; num_caps; --num_caps) {
- uint32_t cap;
- read_all(sock, &cap, sizeof(cap));
- }
-
- // client have to send a SpiceLinkAuthMechanism (just uin32 with
- // mech SPICE_COMMON_CAP_AUTH_SASL)
- write_u32(sock, SPICE_COMMON_CAP_AUTH_SASL);
-
- // sasl finally start, data starts from server (mech list)
- //
- uint32_t mechlen;
- read_u32(sock, &mechlen);
- char buf[300];
- g_assert_cmpint(mechlen, <=, sizeof(buf));
- read_all(sock, buf, mechlen);
+#define STOP_AT(step) if (data->last_step == STEP_ ## step) { break; }
+
+ do {
+ // send initial message
+ write_all(sock, &initial_message, sizeof(initial_message));
+
+ // server replies link ack with rsa, etc, similar to above beside
+ // fixed fields
+ struct {
+ SpiceLinkHeader header;
+ SpiceLinkReply ack;
+ } msg;
+ SPICE_VERIFY(sizeof(msg) == sizeof(SpiceLinkHeader) + sizeof(SpiceLinkReply));
+ read_all(sock, &msg, sizeof(msg));
+ uint32_t num_caps = GUINT32_FROM_LE(msg.ack.num_common_caps) + GUINT32_FROM_LE(msg.ack.num_channel_caps);
+ for (; num_caps; --num_caps) {
+ uint32_t cap;
+ read_all(sock, &cap, sizeof(cap));
+ }
- // mech name
- write_u32(sock, data->mechlen);
- write_all(sock, data->mechname, data->mechlen);
+ // client have to send a SpiceLinkAuthMechanism (just uin32 with
+ // mech SPICE_COMMON_CAP_AUTH_SASL)
+ write_u32(sock, SPICE_COMMON_CAP_AUTH_SASL);
+
+ STOP_AT(NONE);
+
+ // sasl finally start, data starts from server (mech list)
+ //
+ uint32_t mechlen;
+ read_u32(sock, &mechlen);
+ STOP_AT(READ_MECHLIST_LEN);
+
+ char buf[300];
+ g_assert_cmpint(mechlen, <=, sizeof(buf));
+ read_all(sock, buf, mechlen);
+ STOP_AT(READ_MECHLIST);
+
+ // mech name
+ write_u32(sock, data->mechlen);
+ STOP_AT(WRITE_MECHNAME_LEN);
+ write_all(sock, data->mechname, data->mechlen);
+ STOP_AT(WRITE_MECHNAME);
+
+ // first challenge
+ const char *out;
+ unsigned outlen;
+ get_step_out(&out, &outlen, "START", FLAG_CLIENT_NULL_START);
+ write_u32(sock, out ? outlen : 0);
+ STOP_AT(WRITE_START_LEN);
+ if (out) {
+ write_all(sock, out, outlen);
+ }
+ STOP_AT(WRITE_START);
- // first challenge
- write_u32(sock, 5);
- write_all(sock, "START", 5);
+ uint32_t datalen;
+ if (read_u32_err(sock, &datalen) != sizeof(datalen)) {
+ break;
+ }
+ STOP_AT(READ_START_LEN);
+ if (datalen == SPICE_MAGIC) {
+ break;
+ }
+ g_assert_cmpint(datalen, <=, sizeof(buf));
+ read_all(sock, buf, datalen);
+ STOP_AT(READ_START);
+
+ get_step_out(&out, &outlen, "STEP", FLAG_CLIENT_NULL_STEP);
+ write_u32(sock, out ? outlen : 0);
+ STOP_AT(WRITE_STEP_LEN);
+ if (out) {
+ write_all(sock, out, outlen);
+ }
+ STOP_AT(WRITE_STEP);
+ } while(0);
shutdown(sock, SHUT_RDWR);
close(sock);
@@ -450,8 +559,9 @@ idle_next_test(void *arg)
if (data->mechlen < 0) {
data->mechlen = strlen(data->mechname);
}
+ test_flags = data->flags;
int len = data->mechlen;
- printf("\nRunning test %d ('%*.*s' %d)\n", test_num, len, len, data->mechname, len);
+ printf("\nRunning test %d ('%.*s' %d) line %d\n", test_num, len, data->mechname, len, data->line);
int sv[2];
g_assert_cmpint(socketpair(AF_LOCAL, SOCK_STREAM, 0, sv), ==, 0);
--
2.14.3
More information about the Spice-devel
mailing list