From 497f8c5cd2c26bbf7349aac3f17a8848972888fb Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Tue, 8 Apr 2014 15:31:16 +0100 Subject: [PATCH] Added NET_1 and NET_2 (TCP / UDP client) automated tests to test suite --- .../tests/net/helloworld/tcpclient/main.cpp | 56 ++++++++++++++----- .../tests/net/helloworld/udpclient/main.cpp | 48 +++++++++++----- workspace_tools/tests.py | 10 +++- 3 files changed, 84 insertions(+), 30 deletions(-) diff --git a/libraries/tests/net/helloworld/tcpclient/main.cpp b/libraries/tests/net/helloworld/tcpclient/main.cpp index 5c5f7c79e3..8be303513b 100644 --- a/libraries/tests/net/helloworld/tcpclient/main.cpp +++ b/libraries/tests/net/helloworld/tcpclient/main.cpp @@ -1,31 +1,61 @@ +#include #include "mbed.h" #include "EthernetInterface.h" +#include "test_env.h" + +namespace { + const char *HTTP_SERVER_NAME = "mbed.org"; + const int HTTP_SERVER_PORT = 80; + const int RECV_BUFFER_SIZE = 512; + + // Test related data + const char *HTTP_OK_STR = "200 OK"; + const char *HTTP_HELLO_STR = "Hello world!"; +} + +bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { + const char *f = std::search(first, last, s_first, s_last); + return (f != last); +} int main() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); - printf("IP Address is %s\n", eth.getIPAddress()); - + printf("TCP client IP Address is %s\n", eth.getIPAddress()); + TCPSocketConnection sock; - sock.connect("mbed.org", 80); - + sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT); + char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; sock.send_all(http_cmd, sizeof(http_cmd)); - - char buffer[300]; - int ret; + + char buffer[RECV_BUFFER_SIZE] = {0}; + bool result = true; while (true) { - ret = sock.receive(buffer, sizeof(buffer)-1); + const int ret = sock.receive(buffer, sizeof(buffer) - 1); if (ret <= 0) break; buffer[ret] = '\0'; - printf("Received %d chars from server:\n%s\n", ret, buffer); + + // Find 200 OK HTTP status in reply + bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); + result = result && found_200_ok; + + // Find Hello World! in reply + bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR)); + result = result && found_hello; + + // Print results + printf("HTTP: Received %d chars from server\r\n", ret); + printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); + printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]"); + printf("HTTP: Received massage:\r\n\r\n"); + printf("%s", buffer); } - + sock.close(); - eth.disconnect(); - - while(1) {} + notify_completion(result); + return 0; } diff --git a/libraries/tests/net/helloworld/udpclient/main.cpp b/libraries/tests/net/helloworld/udpclient/main.cpp index a6d3c822ad..0cc446d31e 100644 --- a/libraries/tests/net/helloworld/udpclient/main.cpp +++ b/libraries/tests/net/helloworld/udpclient/main.cpp @@ -1,28 +1,48 @@ #include "mbed.h" #include "EthernetInterface.h" - +#include "test_env.h" + +namespace { + const char *HTTP_SERVER_NAME = "utcnist.colorado.edu"; + const int HTTP_SERVER_PORT = 37; +} + int main() { + bool result = false; EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); - + printf("UDP client IP Address is %s\n", eth.getIPAddress()); + UDPSocket sock; sock.init(); - + Endpoint nist; - nist.set_address("utcnist.colorado.edu", 37); - + nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT); + char out_buffer[] = "plop"; // Does not matter sock.sendTo(nist, out_buffer, sizeof(out_buffer)); - - char in_buffer[4]; - int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); - - unsigned int timeRes = ntohl( *((unsigned int*)in_buffer)); - printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes); - + + union { + char in_buffer_tab[4]; + unsigned int in_buffer_uint; + }; + + const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab)); + if (n > 0) { + const unsigned int timeRes = ntohl(in_buffer_uint); + const float years = timeRes / 60.0 / 60.0 / 24.0 / 365; + printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port()); + printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]"); + printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > 114.0 ? "[OK]" : "[FAIL]"); + result = true; + + if (years < 114.0) { + result = false; + } + } sock.close(); - eth.disconnect(); - while(1) {} + notify_completion(result); + return 0; } diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index aec4ba51c7..54659a8695 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -323,7 +323,7 @@ TESTS = [ "source_dir": join(TEST_DIR, "mbed", "hello"), "dependencies": [MBED_LIBRARIES], "automated": True, - "host_test": "hello_auto", + "host_test": "hello_auto", }, { "id": "MBED_11", "description": "Ticker Int", @@ -572,12 +572,16 @@ TESTS = [ { "id": "NET_1", "description": "TCP client hello world", "source_dir": join(TEST_DIR, "net", "helloworld", "tcpclient"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY, TEST_MBED_LIB], + "duration": 15, + "automated": True, }, { "id": "NET_2", "description": "UDP client hello world", "source_dir": join(TEST_DIR, "net", "helloworld", "udpclient"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY, TEST_MBED_LIB], + "duration": 15, + "automated": True, }, { "id": "NET_3", "description": "TCP echo server",