Tests for the NetworkSocketAPI
Dependents: BSDInterfaceTests ESP8266InterfaceTests LWIPInterfaceTests SpwfInterface_NSAPI_Tests ... more
Diff: NSAPITests.cpp
- Revision:
- 3:8b595ee6219d
- Parent:
- 2:41bf867fedd2
- Child:
- 4:b52f17273177
diff -r 41bf867fedd2 -r 8b595ee6219d NSAPITests.cpp --- a/NSAPITests.cpp Wed Mar 02 13:24:46 2016 -0600 +++ b/NSAPITests.cpp Wed Mar 02 16:16:25 2016 -0600 @@ -1,15 +1,16 @@ #include "NetworkInterface.h" #include "TCPSocket.h" +#include "UDPSocket.h" #include <stdio.h> #include "string.h" -int nsapi_isConnected_test(NetworkInterface *iface) +int nsapi_ni_isConnected_test(NetworkInterface *iface) { return !(iface->isConnected()); } -int nsapi_getIPAddress_test(NetworkInterface *iface) +int nsapi_ni_getIPAddress_test(NetworkInterface *iface) { if (!iface->getIPAddress()[0]) { @@ -20,7 +21,7 @@ return 0; } -int nsapi_getMACAddress_test(NetworkInterface *iface) +int nsapi_ni_getMACAddress_test(NetworkInterface *iface) { if (!iface->getMACAddress()[0]) { @@ -31,14 +32,14 @@ return 0; } -int nsapi_getHostByName_test(NetworkInterface *iface) +int nsapi_ni_getHostByName_test(NetworkInterface *iface) { char ip_address[NS_IP_SIZE] = "\0"; - int32_t ret = iface->getHostByName("google.com", ip_address); + int32_t ret = iface->getHostByName("developer.mbed.org", ip_address); if (ret) { - printf("'getHostByName' failed\r\n"); + printf("'getHostByName' failed with code %d\r\n", ret); return -1; } else if (!ip_address[0]) { printf("Returned IP address was null\r\n"); @@ -48,35 +49,215 @@ } } -int nsapi_run_test(const char *name, NetworkInterface *iface, int (*test)(NetworkInterface*)) { - int ret; +int nsapi_socket_open_test(Socket *socket, const char *test_address, uint16_t test_port) +{ + int32_t ret = socket->open(test_address, test_port); + + if (ret) { + printf("'open' failed with code %d\r\n", ret); + return -1; + } else { + return 0; + } +} + +int nsapi_socket_isConnected_test(Socket *socket, const char *test_address, uint16_t test_port) +{ + return !socket->isConnected(); +} + + +int nsapi_socket_getIpAddress_test(Socket *socket, const char *test_address, uint16_t test_port) +{ + const char *cur_ip_address = socket->getIPAddress(); - printf("---------------------\r\n"); - printf("%s: running...\r\n", name); + if (!cur_ip_address[0]) { + printf("'getIpAddress' did not return an IP address\r\n"); + return -1; + } else if (strcmp(cur_ip_address, test_address)) { + printf("'getIpAddress' did not reutrurn the expected IP address\r\n"); + printf("Expected: %s\r\n", test_address); + printf("Actual: %s\r\n", cur_ip_address); + return -2; + } + + return 0; +} + +int nsapi_socket_getPort_test(Socket *socket, const char *test_address, uint16_t test_port) +{ + uint16_t cur_port = socket->getPort(); + + if (cur_port != test_port) { + printf("'getPort' did not return the expected port\r\n"); + printf("Expected: %d\r\n", test_port); + printf("Actual: %d\r\n", cur_port); + return -1; + } + + return 0; +} - ret = test(iface); +int nsapi_socket_send_test(Socket *socket, const char *test_address, uint16_t test_port) +{ + char data[] = "{{start}}"; + int32_t ret = socket->send(data, sizeof(data)); + + if (ret) { + printf("'send' failed with code %d\r\n", ret); + return -1; + } else { + return 0; + } +} - printf("%s: ", name); +int nsapi_socket_recv_blocking_test(Socket *socket, const char *test_address, uint16_t test_port) +{ + char expected_data[] = "{{data}}"; + char data[100] = {0}; + + int32_t ret = socket->send(data, sizeof(data)); + + if (ret) { + printf("'send' failed during 'recv' test with code %d\r\n", ret); + return -4; + } + + int32_t bytes_received = socket->recv(data, sizeof(data)); - if (!ret) { - printf("PASS\r\n"); + if (bytes_received >= (int32_t)sizeof(expected_data)) { + if (strstr(data, expected_data) != NULL) { + return 0; + } else { + printf("'recv' returned incorrect data with length %d\r\n", bytes_received); + return -2; + } + } else if (bytes_received < 0) { + printf("'recv' failed with code %d\r\n", bytes_received); + return -3; } else { - printf("FAIL (Return code %d)\r\n", ret); + printf("'recv' returned no data during blocking test\r\n"); + return -1; + } +} + +int nsapi_socket_recv_non_blocking_test(Socket *socket, const char *test_address, uint16_t test_port) +{ + int32_t bytes_received; + int result = -1; + char expected_data[] = "{{data}}"; + char data[100] = {0}; + + int32_t ret = socket->send(expected_data, sizeof(expected_data)); + + if (ret) { + printf("'send' failed during 'recv' test with code %d\r\n", ret); + return -4; } - return ret; + for (int i = 0; i < 100; i++) { + bytes_received = socket->recv(data, sizeof(data), false); + + if (bytes_received >= (int32_t)sizeof(expected_data)) { + if (strstr(data, expected_data) != NULL) { + result = 0; + break; + } else { + printf("'recv' returned incorrect data\r\n"); + result = -2; + break; + } + } else if (bytes_received < 0) { + result = -3; + break; + } + } + + if (result == -1) { + printf("'recv' returned no data during blocking test\r\n"); + } else if (result == -2) { + printf("'recv' returned incorrect data\r\n"); + } else if (result == -3) { + printf("'recv' failed with code %d\r\n", bytes_received); + } + + return result; } -int nsapi_tests(const char *name, NetworkInterface *iface) +int nsapi_socket_close_test(Socket *socket, const char *test_address, uint16_t test_port) { + int32_t ret = socket->close(); + + if (ret) { + printf("'close' failed with code %d\r\n", ret); + return -1; + } else { + return 0; + } +} + +void nspai_print_test_header(const char *name) { + printf("---------------------\r\n"); + printf("%s: running...\r\n", name); +} + +void nspai_print_test_result(const char *name, int result) { + printf("%s: ", name); + + if (!result) { + printf("PASS\r\n"); + } else { + printf("FAIL (Return code %d)\r\n", result); + } +} + +int nsapi_ni_run_test(const char *name, NetworkInterface *iface, int (*test)(NetworkInterface*)) { + int result; + nspai_print_test_header(name); + result = test(iface); + nspai_print_test_result(name, result); + return result; +} + +int nsapi_socket_run_test(const char *name, Socket *socket, const char *test_address, uint16_t test_port, int (*test)(Socket*, const char*, uint16_t)) { + int result; + nspai_print_test_header(name); + result = test(socket, test_address, test_port); + nspai_print_test_result(name, result); + return result; +} + +int nsapi_tests(const char *name, NetworkInterface *iface, const char *test_address, uint16_t test_tcp_port, uint16_t test_udp_port) +{ + TCPSocket tcp_socket(iface); + UDPSocket udp_socket(iface); int ret = 0; - ret |= nsapi_run_test("nsapi_isConnected_test", iface, &nsapi_isConnected_test); - ret |= nsapi_run_test("nsapi_getIPAddress_test", iface, &nsapi_getIPAddress_test); - ret |= nsapi_run_test("nsapi_getMACAddress_test", iface, &nsapi_getMACAddress_test); - ret |= nsapi_run_test("nsapi_getHostByName_test", iface, &nsapi_getHostByName_test); + printf("--- Running NetworkInterface Tests ---\r\n\r\n"); + ret |= nsapi_ni_run_test("nsapi_ni_isConnected_test", iface, &nsapi_ni_isConnected_test); + ret |= nsapi_ni_run_test("nsapi_ni_getIPAddress_test", iface, &nsapi_ni_getIPAddress_test); + ret |= nsapi_ni_run_test("nsapi_ni_getMACAddress_test", iface, &nsapi_ni_getMACAddress_test); + ret |= nsapi_ni_run_test("nsapi_ni_getHostByName_test", iface, &nsapi_ni_getHostByName_test); + + printf("\r\n\r\n--- Running TCPSocket Tests ---\r\n\r\n"); + 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_close_test", &tcp_socket, test_address, test_tcp_port, &nsapi_socket_close_test); - return ret; + printf("\r\n\r\n--- Running UDPSocket Tests ---\r\n\r\n"); + 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_close_test", &udp_socket, test_address, test_udp_port, &nsapi_socket_close_test); + + return ret != 0; }