Tests for the NetworkSocketAPI

Dependents:   BSDInterfaceTests ESP8266InterfaceTests LWIPInterfaceTests SpwfInterface_NSAPI_Tests ... more

Revision:
5:03b0570b4c29
Parent:
4:b52f17273177
Child:
6:d847da0dbf4f
--- a/NSAPITests.cpp	Wed Mar 02 18:02:47 2016 -0600
+++ b/NSAPITests.cpp	Thu Mar 03 13:08:27 2016 -0600
@@ -98,20 +98,7 @@
   return 0;
 }
 
-int nsapi_socket_send_test(Socket *socket, const char *test_address, uint16_t test_port)
-{
-  char data[] = "{{start}}";
-  int32_t ret = socket->send(data, strlen(data));
-
-  if (ret) {
-    printf("'send' failed with code %d\r\n", ret);
-    return -1;
-  } else {
-    return 0;
-  }
-}
-
-int nsapi_socket_recv_blocking_test(Socket *socket, const char *test_address, uint16_t test_port)
+int nsapi_socket_blocking_test(Socket *socket, const char *test_address, uint16_t test_port)
 {
   char expected_data[] = "{{data}}";
   char data[100] = {0};
@@ -119,7 +106,7 @@
   int32_t ret = socket->send(expected_data, strlen(expected_data));
 
   if (ret) {
-    printf("'send' failed during 'recv' test with code %d\r\n", ret);
+    printf("'send' failed during test with code %d\r\n", ret);
     return -4;
   }
 
@@ -130,18 +117,19 @@
       return 0;
     } else {
       printf("'recv' returned incorrect data with length %d\r\n", bytes_received);
+      printf("data: %c%c%c%c\r\n", data[0], data[1], data[2], data[3]);
       return -2;
     }
   } else if (bytes_received < 0) {
     printf("'recv' failed with code %d\r\n", bytes_received);
     return -3;
   } else {
-    printf("'recv' returned no data during blocking test\r\n");
+    printf("'recv' returned no data\r\n");
     return -1;
   }
 }
 
-int nsapi_socket_recv_non_blocking_test(Socket *socket, const char *test_address, uint16_t test_port)
+int nsapi_socket_non_blocking_test(Socket *socket, const char *test_address, uint16_t test_port)
 {
   int32_t bytes_received;
   int result = -1;
@@ -151,13 +139,14 @@
   int32_t ret = socket->send(expected_data, strlen(expected_data));
 
   if (ret) {
-    printf("'send' failed during 'recv' test with code %d\r\n", ret);
+    printf("'send' failed during test with code %d\r\n", ret);
     return -4;
   }
 
-  for (int i = 0; i < 100; i++) {
+  // TODO: Create a better way to "wait" for data besides busy-looping
+  for (int i = 0; i < 10000000; i++) {
     bytes_received = socket->recv(data, sizeof(data), false);
-    
+
     if (bytes_received >= (int32_t)strlen(expected_data)) {
       if (strstr(data, expected_data) != NULL) {
         result = 0;
@@ -174,7 +163,7 @@
   }
 
   if (result == -1) {
-    printf("'recv' returned no data during blocking test\r\n");
+    printf("'recv' returned no data\r\n");
   } else if (result == -2) {
     printf("'recv' returned incorrect data\r\n");
   } else if (result == -3) {
@@ -243,9 +232,8 @@
   ret |= nsapi_socket_run_test("nsapi_socket_open_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_open_test);
   ret |= nsapi_socket_run_test("nsapi_socket_getIpAddress_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_getIpAddress_test);
   ret |= nsapi_socket_run_test("nsapi_socket_getPort_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_getPort_test);
-  ret |= nsapi_socket_run_test("nsapi_socket_send_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_send_test);
-  ret |= nsapi_socket_run_test("nsapi_socket_recv_blocking_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_recv_blocking_test);
-  ret |= nsapi_socket_run_test("nsapi_socket_recv_non_blocking_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_recv_non_blocking_test);
+  ret |= nsapi_socket_run_test("nsapi_socket_blocking_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_blocking_test);
+  ret |= nsapi_socket_run_test("nsapi_socket_non_blocking_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_non_blocking_test);
   ret |= nsapi_socket_run_test("nsapi_socket_close_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_close_test);
 
 
@@ -253,11 +241,16 @@
   ret |= nsapi_socket_run_test("nsapi_socket_open_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_open_test);
   ret |= nsapi_socket_run_test("nsapi_socket_getIpAddress_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_getIpAddress_test);
   ret |= nsapi_socket_run_test("nsapi_socket_getPort_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_getPort_test);
-  ret |= nsapi_socket_run_test("nsapi_socket_send_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_send_test);
-  ret |= nsapi_socket_run_test("nsapi_socket_recv_blocking_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_recv_blocking_test);
-  ret |= nsapi_socket_run_test("nsapi_socket_recv_non_blocking_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_recv_non_blocking_test);
+  ret |= nsapi_socket_run_test("nsapi_socket_blocking_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_blocking_test);
+  ret |= nsapi_socket_run_test("nsapi_socket_non_blocking_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_non_blocking_test);
   ret |= nsapi_socket_run_test("nsapi_socket_close_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_close_test);
 
+  if (ret == 0) {
+    printf("\r\n\r\n--- ALL TESTS PASSING ---\r\n");
+  } else {
+    printf("\r\n\r\n--- TEST FAILURES OCCURRED ---\r\n");
+  }
+
   return ret != 0;
 }