From e10bd603854b6cfe21bbf71c0623aa080e462645 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Wed, 26 Mar 2014 14:48:44 +0000 Subject: [PATCH] NET_7, NET_8 - HTTP and NTP client test automation added to test suite --- libraries/tests/mbed/rpc/main.cpp | 7 +- .../protocols/HTTPClient_HelloWorld/main.cpp | 92 +++++++++++-------- .../protocols/NTPClient_HelloWorld/main.cpp | 48 +++++----- workspace_tools/tests.py | 10 +- 4 files changed, 91 insertions(+), 66 deletions(-) diff --git a/libraries/tests/mbed/rpc/main.cpp b/libraries/tests/mbed/rpc/main.cpp index ffec1f2c05..a8336c3ed0 100644 --- a/libraries/tests/mbed/rpc/main.cpp +++ b/libraries/tests/mbed/rpc/main.cpp @@ -2,16 +2,14 @@ #include "test_env.h" #include "mbed_rpc.h" -char outbuf[RPC_MAX_STRING] = {0}; -float f = 0; - void foo(Arguments *args, Reply *reply) { reply->putData(args->getArg() * 3.3); } bool rpc_test(const char *input, const char *expected) { - printf("RPC: %s -> ", input); + char outbuf[RPC_MAX_STRING] = {0}; bool result = RPC::call(input, outbuf); + printf("RPC: %s -> ", input); if (result == false) { printf("Procedure call ... [FAIL]\r\n"); @@ -27,6 +25,7 @@ bool rpc_test(const char *input, const char *expected) { #define RPC_TEST(INPUT,EXPECTED) result = result && rpc_test(INPUT,EXPECTED); if (result == false) { notify_completion(result); exit(1); } int main() { + float f = 0; bool result = true; // Variable RPCVariable rpc_f(&f, "f"); diff --git a/libraries/tests/net/protocols/HTTPClient_HelloWorld/main.cpp b/libraries/tests/net/protocols/HTTPClient_HelloWorld/main.cpp index b2d4c8dee0..cfdd17f18d 100644 --- a/libraries/tests/net/protocols/HTTPClient_HelloWorld/main.cpp +++ b/libraries/tests/net/protocols/HTTPClient_HelloWorld/main.cpp @@ -1,49 +1,65 @@ #include "mbed.h" +#include "test_env.h" #include "EthernetInterface.h" #include "HTTPClient.h" -EthernetInterface eth; -HTTPClient http; -char str[512]; +#define BUFFER_SIZE 512 -int main() +int main() { + char http_request_buffer[BUFFER_SIZE+1] = {0}; + HTTPClient http; + EthernetInterface eth; eth.init(); //Use DHCP - eth.connect(); - - //GET data - printf("Trying to fetch page...\n"); - int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); - if (!ret) - { - printf("Page fetched successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } - - //POST data - HTTPMap map; - HTTPText text(str, 512); - map.put("Hello", "World"); - map.put("test", "1234"); - printf("Trying to post data...\n"); - ret = http.post("http://httpbin.org/post", map, &text); - if (!ret) - { - printf("Executed POST successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } - - eth.disconnect(); - while(1) { + //GET data + { + bool result = true; + const char *url_hello_txt = "http://mbed.org/media/uploads/donatien/hello.txt"; + printf("HTTP_GET: Trying to fetch page '%s'...\r\n", url_hello_txt); + const int ret = http.get(url_hello_txt, http_request_buffer, BUFFER_SIZE); + if (ret == 0) + { + printf("HTTP_GET: Read %d chars: '%s' ... [OK]\r\n", strlen(http_request_buffer), http_request_buffer); + } + else + { + printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode()); + result = false; + } + + if (result == false) { + notify_completion(false); + exit(ret); + } } + + //POST data + { + bool result = true; + const char *url_httpbin_post = "http://httpbin.org/post"; + HTTPMap map; + HTTPText text(http_request_buffer, BUFFER_SIZE); + map.put("Hello", "World"); + map.put("test", "1234"); + printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post); + const int ret = http.post(url_httpbin_post, map, &text); + if (ret == 0) { + printf("HTTP_POST: Read %d chars ... [OK]\n", strlen(http_request_buffer)); + printf("HTTP_POST: %s\r\n", http_request_buffer); + } + else { + printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode()); + result = false; + } + + if (result == false) { + notify_completion(false); + exit(ret); + } + } + eth.disconnect(); + notify_completion(true); + return 0; } diff --git a/libraries/tests/net/protocols/NTPClient_HelloWorld/main.cpp b/libraries/tests/net/protocols/NTPClient_HelloWorld/main.cpp index 423fce869e..ee24d34d66 100644 --- a/libraries/tests/net/protocols/NTPClient_HelloWorld/main.cpp +++ b/libraries/tests/net/protocols/NTPClient_HelloWorld/main.cpp @@ -1,31 +1,37 @@ #include "mbed.h" +#include "test_env.h" #include "EthernetInterface.h" #include "NTPClient.h" -EthernetInterface eth; -NTPClient ntp; - -int main() +int main() { + EthernetInterface eth; + NTPClient ntp; eth.init(); //Use DHCP - eth.connect(); - - printf("Trying to update time...\r\n"); - if (ntp.setTime("0.pool.ntp.org") == 0) - { - printf("Set time successfully\r\n"); - time_t ctTime; - ctTime = time(NULL); - printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); - } - else - { - printf("Error\r\n"); - } - - eth.disconnect(); - while(1) { + // NTP set time + { + bool result = true; + const char *url_ntp_server = "0.pool.ntp.org"; + printf("NTP_SETTIME: Trying to update time... \r\n"); + const int ret = ntp.setTime(url_ntp_server); + if (ret == 0) { + time_t ctTime = time(NULL); + printf("NTP_SETTIME: UTC Time read successfully ... [OK]\r\n"); + printf("NTP_SETTIME: %s\r\n", ctime(&ctTime)); + } + else { + printf("NTP_SETTIME: Error(%d) ... [FAIL]\r\n", ret); + result = false; + } + + if (result == false) { + notify_completion(false); + exit(ret); + } } + eth.disconnect(); + notify_completion(true); + return 0; } diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index e9b1be999b..94b3ecc17d 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -600,17 +600,21 @@ TESTS = [ "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], "automated": True, "host_test" : "udpecho_client_auto", - "peripherals": ["ethernet"] + "peripherals": ["ethernet"], }, { "id": "NET_7", "description": "HTTP client", "source_dir": join(TEST_DIR, "net", "protocols", "HTTPClient_HelloWorld"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY, TEST_MBED_LIB], + "automated": True, + "peripherals": ["ethernet"], }, { "id": "NET_8", "description": "NTP client", "source_dir": join(TEST_DIR, "net", "protocols", "NTPClient_HelloWorld"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY, TEST_MBED_LIB], + "automated": True, + "peripherals": ["ethernet"], }, { "id": "NET_9", "description": "Multicast Send",