[svn-commits] russell: branch russell/dtls r206095 - /team/russell/dtls/tests/test_dtls.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Jul 13 09:14:29 CDT 2009


Author: russell
Date: Mon Jul 13 09:14:25 2009
New Revision: 206095

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=206095
Log:
Expand on the DTLS test module.

Instead of sending a single message in one direction, send a total of 50
messages, 25 in each direction, to do a bit more of a thorough job in
verifying that communication is working as expected.

Modified:
    team/russell/dtls/tests/test_dtls.c

Modified: team/russell/dtls/tests/test_dtls.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/russell/dtls/tests/test_dtls.c?view=diff&rev=206095&r1=206094&r2=206095
==============================================================================
--- team/russell/dtls/tests/test_dtls.c (original)
+++ team/russell/dtls/tests/test_dtls.c Mon Jul 13 09:14:25 2009
@@ -86,25 +86,55 @@
 	return dtls_config;
 }
 
-static const char msg[] = "meepmeep";
-
 struct dtls_recv_args {
+	struct ast_dtls_handler *dtls_handler_client;
+	struct ast_dtls_handler *dtls_handler_server;
+	struct sockaddr_in *sin_remote;
 	struct ast_cli_args *a;
-	int *success;
+	unsigned int *recv_count;
 };
+
+static const unsigned int MSG_COUNT = 50;
 
 static void dtls_recv(const struct sockaddr_in *sin_remote, const void *buf, size_t len,
 		void *user_data)
 {
+	struct ast_dtls_handler *dtls_handler;
 	struct dtls_recv_args *args = user_data;
+	char msg[3];
+	unsigned int res;
 
 	ast_cli(args->a->fd, " --> Ooh, got a message, '%s'\n", (const char *) buf);
 
-	if (!strcmp(buf, msg)) {
-		*args->success = 1;
+	if ((res = atoi(buf)) != *args->recv_count + 1) {
+		ast_cli(args->a->fd, " --> [FAILURE] Received unexpected message.\n");
+		return;
+	}
+
+	*args->recv_count += 1;
+
+	if (res == MSG_COUNT) {
 		ast_mutex_lock(&recv_lock);
 		ast_cond_signal(&recv_cond);
 		ast_mutex_unlock(&recv_lock);
+		return;
+	}
+
+	snprintf(msg, sizeof(msg), "%u", *args->recv_count + 1);
+
+	if (*args->recv_count & 1) {
+		dtls_handler = args->dtls_handler_server;
+		args->sin_remote->sin_port = htons(CLIENT_PORT);
+	} else {
+		dtls_handler = args->dtls_handler_client;
+		args->sin_remote->sin_port = htons(SERVER_PORT);
+	}
+
+	ast_cli(args->a->fd, " --> Sending msg: '%s'\n", msg);
+
+	if (ast_dtls_send(dtls_handler, msg, strlen(msg) + 1, args->sin_remote)) {
+		ast_cli(args->a->fd, " --> [FAILURE] Failed to send msg '%s'\n", msg);
+		return;
 	}
 }
 
@@ -115,7 +145,7 @@
 	struct ast_dtls_handler *dtls_handler_client = NULL;
 	struct ast_dtls_handler *dtls_handler_server = NULL;
 	char *res = CLI_FAILURE;
-	int success = 0;
+	unsigned int recv_count = 0;
 	struct timeval timeout;
 	struct timespec ts;
 	struct sockaddr_in sin_remote = {
@@ -123,7 +153,8 @@
 	};
 	struct dtls_recv_args recv_args = {
 		.a = a,
-		.success = &success,
+		.recv_count = &recv_count,
+		.sin_remote = &sin_remote,
 	};
 
 	ast_cli(a->fd, " --> Beginning DTLS functionality test\n");
@@ -162,6 +193,9 @@
 		goto return_cleanup;
 	}
 
+	recv_args.dtls_handler_client = dtls_handler_client;
+	recv_args.dtls_handler_server = dtls_handler_server;
+
 	inet_aton("127.0.0.1", &sin_remote.sin_addr);
 	sin_remote.sin_port = htons(SERVER_PORT);
 
@@ -171,15 +205,16 @@
 	ts.tv_nsec = timeout.tv_usec * 1000;
 
 	ast_mutex_lock(&recv_lock);
-	ast_cli(a->fd, " --> Sending a secret message: '%s'\n", msg);
-	if (ast_dtls_send(dtls_handler_client, msg, sizeof(msg), &sin_remote)) {
+	ast_cli(a->fd, " --> Attemping to send a total of 50 messages.\n");
+	ast_cli(a->fd, " --> Sending msg: '1'\n");
+	if (ast_dtls_send(dtls_handler_client, "1", 2, &sin_remote)) {
 		ast_cli(a->fd, " --> [FAILURE] Failed to send message.\n");
 	} else {
 		ast_cond_timedwait(&recv_cond, &recv_lock, &ts);
 	}
 	ast_mutex_unlock(&recv_lock);
 
-	if (success) {
+	if (recv_count == 50) {
 		ast_cli(a->fd, " --> [SUCCESS] yay!\n");
 		res = CLI_SUCCESS;
 	} else {




More information about the svn-commits mailing list