-set socket options before “socket.bind” -listen for incoming SOCK_STREAM connections only before “socket.accept” resolves ubuntu errors.
Fork of NSAPITests by
NSAPITests.cpp
- Committer:
- Christopher Haster
- Date:
- 2016-03-02
- Revision:
- 4:b52f17273177
- Parent:
- 3:8b595ee6219d
- Child:
- 5:03b0570b4c29
File content as of revision 4:b52f17273177:
#include "NetworkInterface.h" #include "TCPSocket.h" #include "UDPSocket.h" #include <stdio.h> #include "string.h" int nsapi_ni_isConnected_test(NetworkInterface *iface) { return !(iface->isConnected()); } int nsapi_ni_getIPAddress_test(NetworkInterface *iface) { if (!iface->getIPAddress()[0]) { printf("'getIpAddress' did not return an IP address\r\n"); return -1; } return 0; } int nsapi_ni_getMACAddress_test(NetworkInterface *iface) { if (!iface->getMACAddress()[0]) { printf("'getMacAddress' did not return a MAC address\r\n"); return -1; } return 0; } int nsapi_ni_getHostByName_test(NetworkInterface *iface) { char ip_address[NS_IP_SIZE] = "\0"; int32_t ret = iface->getHostByName("developer.mbed.org", ip_address); if (ret) { printf("'getHostByName' failed with code %d\r\n", ret); return -1; } else if (!ip_address[0]) { printf("Returned IP address was null\r\n"); return -2; } else { return 0; } } 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(); 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; } 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) { char expected_data[] = "{{data}}"; char data[100] = {0}; int32_t ret = socket->send(expected_data, strlen(expected_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 (bytes_received >= (int32_t)strlen(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("'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, strlen(expected_data)); if (ret) { printf("'send' failed during 'recv' test with code %d\r\n", ret); return -4; } for (int i = 0; i < 100; 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; 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_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; 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); 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; }