From d568e2160bc874ccefc39e6bef186b314b6a2f87 Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Mon, 7 Jan 2019 16:41:00 +0100 Subject: [PATCH 1/2] Add TLSSocket greentea tests. TLSSocket tests based on existing TCPSocket greentea tests and TLSSocket icetea tests. --- TESTS/netsocket/README.md | 68 +++--- TESTS/netsocket/tls/main.cpp | 214 ++++++++++++++++++ TESTS/netsocket/tls/tls_tests.h | 70 ++++++ .../tls/tlssocket_connect_invalid.cpp | 41 ++++ TESTS/netsocket/tls/tlssocket_echotest.cpp | 209 +++++++++++++++++ .../tls/tlssocket_echotest_burst.cpp | 154 +++++++++++++ .../tls/tlssocket_endpoint_close.cpp | 91 ++++++++ .../tls/tlssocket_handshake_invalid.cpp | 35 +++ TESTS/netsocket/tls/tlssocket_no_cert.cpp | 34 +++ .../netsocket/tls/tlssocket_open_destruct.cpp | 50 ++++ TESTS/netsocket/tls/tlssocket_open_limit.cpp | 97 ++++++++ TESTS/netsocket/tls/tlssocket_open_twice.cpp | 50 ++++ .../netsocket/tls/tlssocket_recv_timeout.cpp | 84 +++++++ TESTS/netsocket/tls/tlssocket_send_closed.cpp | 36 +++ TESTS/netsocket/tls/tlssocket_send_repeat.cpp | 44 ++++ .../netsocket/tls/tlssocket_send_timeout.cpp | 52 +++++ .../tls/tlssocket_send_unconnected.cpp | 34 +++ .../netsocket/tls/tlssocket_simultaneous.cpp | 66 ++++++ tools/test_configs/6lowpanInterface_host.json | 8 + .../test_configs/6lowpanInterface_router.json | 8 + tools/test_configs/ESP8266Interface.json | 8 + tools/test_configs/EthernetInterface.json | 8 + .../HeapBlockDeviceAndEthernetInterface.json | 8 + .../HeapBlockDeviceAndWifiInterface.json | 8 + tools/test_configs/ISM43362Interface.json | 8 + tools/test_configs/SpwfSAInterface.json | 8 + .../ThreadInterface_end_device.json | 8 + .../test_configs/ThreadInterface_router.json | 8 + tools/test_configs/no_network.json | 8 + 29 files changed, 1491 insertions(+), 26 deletions(-) create mode 100644 TESTS/netsocket/tls/main.cpp create mode 100644 TESTS/netsocket/tls/tls_tests.h create mode 100644 TESTS/netsocket/tls/tlssocket_connect_invalid.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_echotest.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_echotest_burst.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_endpoint_close.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_no_cert.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_open_destruct.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_open_limit.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_open_twice.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_recv_timeout.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_send_closed.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_send_repeat.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_send_timeout.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_send_unconnected.cpp create mode 100644 TESTS/netsocket/tls/tlssocket_simultaneous.cpp diff --git a/TESTS/netsocket/README.md b/TESTS/netsocket/README.md index 0874f18b9a9..f4d9bb2dc75 100644 --- a/TESTS/netsocket/README.md +++ b/TESTS/netsocket/README.md @@ -17,7 +17,7 @@ The target for this plan is to test: - [Socket](https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/Socket.h). - [UDPSocket](https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/UDPSocket.h). - [TCPSocket](https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/TCPSocket.h). -- [TCPServer](https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/TCPServer.h). +- [TLSSocket](https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/TLSSocket.h). Reference documentation: https://os.mbed.com/docs/latest/reference/network-socket.html @@ -102,35 +102,41 @@ time stream tcp6 nowait root internal You can connect to the test server with an NMAP tool like this: ```.sh -$ nmap -sT -p7,9,13,37 echo.mbedcloudtesting.com +$ nmap -sT -p7,9,13,37,2007,2009,2013 echo.mbedcloudtesting.com -Starting Nmap 7.12 ( https://nmap.org ) at 2018-04-05 16:17 EEST +Starting Nmap 7.01 ( https://nmap.org ) at 2019-01-07 16:45 CET Nmap scan report for echo.mbedcloudtesting.com (52.215.34.155) -Host is up (0.065s latency). -Other addresses for echo.mbedcloudtesting.com (not scanned): 2a05:d018:21f:3800:8584:60f8:bc9f:e614 +Host is up (0.027s latency). +Other addresses for echo.mbedcloudtesting.com (not scanned): 2a05:d018:21f:3800:3164:2a5c:75b3:970b rDNS record for 52.215.34.155: ec2-52-215-34-155.eu-west-1.compute.amazonaws.com -PORT STATE SERVICE -7/tcp open echo -9/tcp open discard -13/tcp open daytime -37/tcp open time +PORT STATE SERVICE +7/tcp open echo +9/tcp open discard +13/tcp open daytime +37/tcp open time +2007/tcp open dectalk +2009/tcp open news +2013/tcp open raid-am -Nmap done: 1 IP address (1 host up) scanned in 0.17 seconds +Nmap done: 1 IP address (1 host up) scanned in 0.10 seconds -$ sudo nmap -sT -p7,9,13,37 echo.mbedcloudtesting.com +$ sudo nmap -sU -p7,9,13,37,2007,2009,2013 echo.mbedcloudtesting.com -Starting Nmap 7.12 ( https://nmap.org ) at 2018-04-05 16:16 EEST +Starting Nmap 7.01 ( https://nmap.org ) at 2019-01-08 15:05 CET Nmap scan report for echo.mbedcloudtesting.com (52.215.34.155) -Host is up (0.068s latency). -Other addresses for echo.mbedcloudtesting.com (not scanned): 2a05:d018:21f:3800:8584:60f8:bc9f:e614 +Host is up (0.044s latency). +Other addresses for echo.mbedcloudtesting.com (not scanned): 2a05:d018:21f:3800:3164:2a5c:75b3:970b rDNS record for 52.215.34.155: ec2-52-215-34-155.eu-west-1.compute.amazonaws.com -PORT STATE SERVICE -7/tcp open echo -9/tcp open discard -13/tcp open daytime -37/tcp open time - -Nmap done: 1 IP address (1 host up) scanned in 0.20 seconds +PORT STATE SERVICE +7/udp open echo +9/udp open|filtered discard +13/udp closed daytime +37/udp closed time +2007/udp closed raid-am +2009/udp closed whosockami +2013/udp closed raid-cd + +Nmap done: 1 IP address (1 host up) scanned in 1.78 seconds ``` ### Ethernet test environment @@ -242,10 +248,18 @@ content at minimum: "echo-server-port" : { "help" : "Port of echo server", "value" : "7" - } + }, "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Echo port of echo server", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server", + "value" : "2009" } } } @@ -301,6 +315,10 @@ the `mbed_app.json` might look like this: "help" : "Port of echo server", "value" : "7" } + "echo-server-port-tls" : { + "help" : "Port of echo server TLS", + "value" : "2007" + } }, "target_overrides": { "*": { @@ -1651,8 +1669,6 @@ Subset for driver test ### For physical layer driver (emac, PPP): -- TCPSERVER_ACCEPT -- TCPSERVER_LISTEN - TCPSOCKET_ECHOTEST - TCPSOCKET_ECHOTEST_BURST - TCPSOCKET_ECHOTEST_BURST_NONBLOCK @@ -1671,4 +1687,4 @@ Subset for driver test ### For socket layer driver (AT-driven, external IP stack): -All Socket, UDPSocket, TCPSocket and TCPServer testcases. +All Socket, UDPSocket, TCPSocket and TLSSocket testcases. diff --git a/TESTS/netsocket/tls/main.cpp b/TESTS/netsocket/tls/main.cpp new file mode 100644 index 00000000000..e4aab9a005f --- /dev/null +++ b/TESTS/netsocket/tls/main.cpp @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define WIFI 2 +#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \ + (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID)) +#error [NOT_SUPPORTED] No network configuration found for this target. +#endif +#ifndef MBED_CONF_APP_ECHO_SERVER_ADDR +#error [NOT_SUPPORTED] Requires parameters from mbed_app.json +#endif + +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "utest/utest_stack_trace.h" +#include "tls_tests.h" + +using namespace utest::v1; + +namespace { +Timer tc_bucket; // Timer to limit a test cases run time +} + +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE +mbed_stats_socket_t tls_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT]; +#endif + +char tls_global::rx_buffer[RX_BUFF_SIZE]; +char tls_global::tx_buffer[TX_BUFF_SIZE]; + +const char *tls_global::cert = \ + "-----BEGIN CERTIFICATE-----\n" + "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" + "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" + "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" + "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" + "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" + "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" + "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" + "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" + "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" + "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" + "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" + "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" + "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" + "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" + "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" + "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" + "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" + "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" + "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" + "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" + "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" + "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" + "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" + "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" + "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" + "-----END CERTIFICATE-----\n"; + +void drop_bad_packets(TLSSocket &sock, int orig_timeout) +{ + nsapi_error_t err; + sock.set_timeout(0); + while (true) { + err = sock.recv(NULL, 0); + if (err == NSAPI_ERROR_WOULD_BLOCK) { + break; + } + } + sock.set_timeout(orig_timeout); +} + +static void _ifup() +{ + NetworkInterface *net = NetworkInterface::get_default_instance(); + nsapi_error_t err = net->connect(); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); + printf("MBED: TLSClient IP address is '%s'\n", net->get_ip_address()); +} + +static void _ifdown() +{ + NetworkInterface::get_default_instance()->disconnect(); + printf("MBED: ifdown\n"); +} + +nsapi_error_t tlssocket_connect_to_srv(TLSSocket &sock, uint16_t port) +{ + SocketAddress tls_addr; + + NetworkInterface::get_default_instance()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tls_addr); + tls_addr.set_port(port); + + printf("MBED: Server '%s', port %d\n", tls_addr.get_ip_address(), tls_addr.get_port()); + + nsapi_error_t err = sock.set_root_ca_cert(tls_global::cert); + if (err != NSAPI_ERROR_OK) { + printf("Error from sock.set_root_ca_cert: %d\n", err); + return err; + } + + err = sock.open(NetworkInterface::get_default_instance()); + if (err != NSAPI_ERROR_OK) { + printf("Error from sock.open: %d\n", err); + return err; + } + + err = sock.connect(tls_addr); + if (err != NSAPI_ERROR_OK) { + printf("Error from sock.connect: %d\n", err); + return err; + } + + return NSAPI_ERROR_OK; +} + +nsapi_error_t tlssocket_connect_to_echo_srv(TLSSocket &sock) +{ + return tlssocket_connect_to_srv(sock, MBED_CONF_APP_ECHO_SERVER_PORT_TLS); +} + +nsapi_error_t tlssocket_connect_to_discard_srv(TLSSocket &sock) +{ + return tlssocket_connect_to_srv(sock, MBED_CONF_APP_ECHO_SERVER_DISCARD_PORT_TLS); +} + +void fill_tx_buffer_ascii(char *buff, size_t len) +{ + for (size_t i = 0; i < len; ++i) { + buff[i] = (rand() % 43) + '0'; + } +} + +int split2half_rmng_tls_test_time() +{ + return (tls_global::TESTS_TIMEOUT - tc_bucket.read()) / 2; +} + +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE +int fetch_stats() +{ + return SocketStats::mbed_stats_socket_get_each(&tls_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT); +} +#endif + +// Test setup +utest::v1::status_t greentea_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(tls_global::TESTS_TIMEOUT, "default_auto"); + _ifup(); + tc_bucket.start(); + return greentea_test_setup_handler(number_of_cases); +} + +void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure) +{ + tc_bucket.stop(); + _ifdown(); + return greentea_test_teardown_handler(passed, failed, failure); +} + + +Case cases[] = { + Case("TLSSOCKET_ECHOTEST", TLSSOCKET_ECHOTEST), + Case("TLSSOCKET_ECHOTEST_NONBLOCK", TLSSOCKET_ECHOTEST_NONBLOCK), + Case("TLSSOCKET_CONNECT_INVALID", TLSSOCKET_CONNECT_INVALID), + Case("TLSSOCKET_ECHOTEST_BURST", TLSSOCKET_ECHOTEST_BURST), + Case("TLSSOCKET_ECHOTEST_BURST_NONBLOCK", TLSSOCKET_ECHOTEST_BURST_NONBLOCK), + Case("TLSSOCKET_RECV_TIMEOUT", TLSSOCKET_RECV_TIMEOUT), + Case("TLSSOCKET_ENDPOINT_CLOSE", TLSSOCKET_ENDPOINT_CLOSE), + Case("TLSSOCKET_HANDSHAKE_INVALID", TLSSOCKET_HANDSHAKE_INVALID), + Case("TLSSOCKET_OPEN_TWICE", TLSSOCKET_OPEN_TWICE), + Case("TLSSOCKET_OPEN_LIMIT", TLSSOCKET_OPEN_LIMIT), + Case("TLSSOCKET_OPEN_DESTRUCT", TLSSOCKET_OPEN_DESTRUCT), + Case("TLSSOCKET_SEND_UNCONNECTED", TLSSOCKET_SEND_UNCONNECTED), + Case("TLSSOCKET_SEND_CLOSED", TLSSOCKET_SEND_CLOSED), + Case("TLSSOCKET_SEND_REPEAT", TLSSOCKET_SEND_REPEAT), + Case("TLSSOCKET_SEND_TIMEOUT", TLSSOCKET_SEND_TIMEOUT), + Case("TLSSOCKET_NO_CERT", TLSSOCKET_NO_CERT), + Case("TLSSOCKET_SIMULTANEOUS", TLSSOCKET_SIMULTANEOUS) +}; + +Specification specification(greentea_setup, cases, greentea_teardown, greentea_continue_handlers); + +int retval; +void run_test(void) +{ + retval = !Harness::run(specification); +} + +static unsigned char stack_mem[8192]; +int main() +{ + Thread *th = new Thread(osPriorityNormal, 8192, stack_mem, "tls_gt_thread"); + th->start(callback(run_test)); + th->join(); + return retval; +} diff --git a/TESTS/netsocket/tls/tls_tests.h b/TESTS/netsocket/tls/tls_tests.h new file mode 100644 index 00000000000..b29c065b40f --- /dev/null +++ b/TESTS/netsocket/tls/tls_tests.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TLS_TESTS_H +#define TLS_TESTS_H + +NetworkInterface *get_interface(); +void drop_bad_packets(TLSSocket &sock, int orig_timeout); +void fill_tx_buffer_ascii(char *buff, size_t len); +nsapi_error_t tlssocket_connect_to_echo_srv(TLSSocket &sock); +nsapi_error_t tlssocket_connect_to_discard_srv(TLSSocket &sock); + +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE +extern mbed_stats_socket_t tls_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT]; +int fetch_stats(void); +#endif + +/** + * Single testcase might take only half of the remaining execution time + */ +int split2half_rmng_tls_test_time(); // [s] + +namespace tls_global { +static const int TESTS_TIMEOUT = 480; +static const int TLS_OS_STACK_SIZE = 2048; + +static const int RX_BUFF_SIZE = 1220; +static const int TX_BUFF_SIZE = 1220; + +extern char rx_buffer[RX_BUFF_SIZE]; +extern char tx_buffer[TX_BUFF_SIZE]; +extern const char *cert; +} + +/* + * Test cases + */ +void TLSSOCKET_CONNECT_INVALID(); +void TLSSOCKET_HANDSHAKE_INVALID(); +void TLSSOCKET_ECHOTEST(); +void TLSSOCKET_ECHOTEST_NONBLOCK(); +void TLSSOCKET_ECHOTEST_BURST(); +void TLSSOCKET_ECHOTEST_BURST_NONBLOCK(); +void TLSSOCKET_ENDPOINT_CLOSE(); +void TLSSOCKET_RECV_TIMEOUT(); +void TLSSOCKET_OPEN_TWICE(); +void TLSSOCKET_OPEN_LIMIT(); +void TLSSOCKET_OPEN_DESTRUCT(); +void TLSSOCKET_SEND_UNCONNECTED(); +void TLSSOCKET_SEND_CLOSED(); +void TLSSOCKET_SEND_REPEAT(); +void TLSSOCKET_NO_CERT(); +void TLSSOCKET_SIMULTANEOUS(); +void TLSSOCKET_SEND_TIMEOUT(); + +#endif //TLS_TESTS_H diff --git a/TESTS/netsocket/tls/tlssocket_connect_invalid.cpp b/TESTS/netsocket/tls/tlssocket_connect_invalid.cpp new file mode 100644 index 00000000000..433d82007aa --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_connect_invalid.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_CONNECT_INVALID() +{ + TLSSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert(tls_global::cert)); + + TEST_ASSERT(sock.connect(NULL, MBED_CONF_APP_ECHO_SERVER_DISCARD_PORT_TLS) < 0); + TEST_ASSERT(sock.connect("", MBED_CONF_APP_ECHO_SERVER_DISCARD_PORT_TLS) < 0); + TEST_ASSERT(sock.connect("", 0) < 0); + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, + sock.connect(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_DISCARD_PORT_TLS)); + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_echotest.cpp b/TESTS/netsocket/tls/tlssocket_echotest.cpp new file mode 100644 index 00000000000..b49afb41d6a --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_echotest.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +namespace { +static const int SIGNAL_SIGIO = 0x1; +static const int SIGIO_TIMEOUT = 20000; //[ms] + +static const int BUFF_SIZE = 1200; +static const int PKTS = 22; +static const int pkt_sizes[PKTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \ + 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, \ + 1100, 1200 + }; +TLSSocket *sock; +Semaphore tx_sem(0, 1); +events::EventQueue *event_queue; +int bytes2recv; +int bytes2recv_total; + +Timer tc_exec_time; +int time_allotted; +bool receive_error; +} + +void tlssocket_echotest_nonblock_receive(); + +static void _sigio_handler(osThreadId id) +{ + osSignalSet(id, SIGNAL_SIGIO); + if (event_queue != NULL) { + event_queue->call(tlssocket_echotest_nonblock_receive); + } +} + +void TLSSOCKET_ECHOTEST() +{ + sock = new TLSSocket; + if (tlssocket_connect_to_echo_srv(*sock) != NSAPI_ERROR_OK) { + printf("Error from tlssocket_connect_to_echo_srv\n"); + TEST_FAIL(); + return; + delete sock; + } + + int recvd; + int sent; + int x = 0; + for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) { + fill_tx_buffer_ascii(tls_global::tx_buffer, BUFF_SIZE); + + sent = sock->send(tls_global::tx_buffer, pkt_s); + if (sent < 0) { + printf("[Round#%02d] network error %d\n", x, sent); + TEST_FAIL(); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); + delete sock; + return; + } + + int bytes2recv = sent; + while (bytes2recv) { + recvd = sock->recv(&(tls_global::rx_buffer[sent - bytes2recv]), bytes2recv); + if (recvd < 0) { + printf("[Round#%02d] network error %d\n", x, recvd); + TEST_FAIL(); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); + delete sock; + return; + } + bytes2recv -= recvd; + } + TEST_ASSERT_EQUAL(0, memcmp(tls_global::tx_buffer, tls_global::rx_buffer, sent)); + } + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); + delete sock; +} + +void tlssocket_echotest_nonblock_receive() +{ + int recvd = sock->recv(&(tls_global::rx_buffer[bytes2recv_total - bytes2recv]), bytes2recv); + if (recvd == NSAPI_ERROR_WOULD_BLOCK) { + if (tc_exec_time.read() >= time_allotted) { + receive_error = true; + } + return; + } else if (recvd < 0) { + receive_error = true; + } else { + bytes2recv -= recvd; + } + + if (bytes2recv == 0) { + TEST_ASSERT_EQUAL(0, memcmp(tls_global::tx_buffer, tls_global::rx_buffer, bytes2recv_total)); + + static int round = 0; + printf("[Recevr#%02d] bytes received: %d\n", round++, bytes2recv_total); + tx_sem.release(); + } + else if (receive_error || bytes2recv < 0) { + TEST_FAIL(); + tx_sem.release(); + } + // else - no error, not all bytes were received yet. +} + +void TLSSOCKET_ECHOTEST_NONBLOCK() +{ +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE + int j = 0; + int count = fetch_stats(); + for (; j < count; j++) { + TEST_ASSERT_EQUAL(SOCK_CLOSED, tls_stats[j].state); + } +#endif + sock = new TLSSocket; + tc_exec_time.start(); + time_allotted = split2half_rmng_tls_test_time(); // [s] + + tlssocket_connect_to_echo_srv(*sock); + sock->set_blocking(false); + sock->sigio(callback(_sigio_handler, ThisThread::get_id())); + + int bytes2send; + int sent; + int s_idx = 0; + receive_error = false; + unsigned char *stack_mem = (unsigned char *)malloc(tls_global::TLS_OS_STACK_SIZE); + TEST_ASSERT_NOT_NULL(stack_mem); + Thread *receiver_thread = new Thread(osPriorityNormal, + tls_global::TLS_OS_STACK_SIZE, + stack_mem, + "receiver"); + EventQueue queue(2 * EVENTS_EVENT_SIZE); + event_queue = &queue; + TEST_ASSERT_EQUAL(osOK, receiver_thread->start(callback(&queue, &EventQueue::dispatch_forever))); + + for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) { + pkt_s = pkt_sizes[s_idx]; + bytes2recv = pkt_s; + bytes2recv_total = pkt_s; + + fill_tx_buffer_ascii(tls_global::tx_buffer, pkt_s); + + bytes2send = pkt_s; + while (bytes2send > 0) { + sent = sock->send(&(tls_global::tx_buffer[pkt_s - bytes2send]), bytes2send); + if (sent == NSAPI_ERROR_WOULD_BLOCK) { + if (tc_exec_time.read() >= time_allotted || + osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { + TEST_FAIL(); + goto END; + } + continue; + } else if (sent <= 0) { + printf("[Sender#%02d] network error %d\n", s_idx, sent); + + TEST_FAIL(); + goto END; + } + bytes2send -= sent; + } + printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s); +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE + count = fetch_stats(); + for (j = 0; j < count; j++) { + if ((tls_stats[j].state == SOCK_OPEN) && (tls_stats[j].proto == NSAPI_TLS)) { + break; + } + } + TEST_ASSERT_EQUAL(bytes2send, tls_stats[j].sent_bytes); +#endif + tx_sem.wait(); + if (receive_error) { + break; + } + } +END: + sock->sigio(NULL); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); + receiver_thread->terminate(); + delete receiver_thread; + receiver_thread = NULL; + tc_exec_time.stop(); + free(stack_mem); + delete sock; +} diff --git a/TESTS/netsocket/tls/tlssocket_echotest_burst.cpp b/TESTS/netsocket/tls/tlssocket_echotest_burst.cpp new file mode 100644 index 00000000000..7797c45272c --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_echotest_burst.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +namespace { +static const int SIGNAL_SIGIO = 0x1; +static const int SIGIO_TIMEOUT = 20000; //[ms] + +static const int BURST_CNT = 100; +static const int BURST_SIZE = 1220; +} + +static void _sigio_handler(osThreadId id) +{ + osSignalSet(id, SIGNAL_SIGIO); +} + +void TLSSOCKET_ECHOTEST_BURST() +{ + TLSSocket *sock = new TLSSocket; + tlssocket_connect_to_echo_srv(*sock); + sock->sigio(callback(_sigio_handler, ThisThread::get_id())); + + // TX buffer to be preserved for comparison + fill_tx_buffer_ascii(tls_global::tx_buffer, BURST_SIZE); + + int recvd; + int bt_left; + int sent; + for (int i = 0; i < BURST_CNT; i++) { + bt_left = BURST_SIZE; + while (bt_left > 0) { + sent = sock->send(&(tls_global::tx_buffer[BURST_SIZE - bt_left]), bt_left); + if (sent == NSAPI_ERROR_WOULD_BLOCK) { + if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { + TEST_FAIL(); + goto END; + } + continue; + } else if (sent < 0) { + printf("[%02d] network error %d\n", i, sent); + TEST_FAIL(); + goto END; + } + bt_left -= sent; + } + + bt_left = BURST_SIZE; + while (bt_left > 0) { + recvd = sock->recv(&(tls_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE); + if (recvd < 0) { + printf("[%02d] network error %d\n", i, recvd); + break; + } + bt_left -= recvd; + } + + if (bt_left != 0) { + TEST_FAIL_MESSAGE("bt_left != 0"); + goto END; + } + + TEST_ASSERT_EQUAL(0, memcmp(tls_global::tx_buffer, tls_global::rx_buffer, BURST_SIZE)); + } +END: + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); + delete sock; +} + +void TLSSOCKET_ECHOTEST_BURST_NONBLOCK() +{ + TLSSocket *sock = new TLSSocket; + tlssocket_connect_to_echo_srv(*sock); + sock->set_blocking(false); + sock->sigio(callback(_sigio_handler, ThisThread::get_id())); + + // TX buffer to be preserved for comparison + fill_tx_buffer_ascii(tls_global::tx_buffer, BURST_SIZE); + + int sent; + int recvd; + int bt_left = 0; + for (int i = 0; i < BURST_CNT; i++) { + bt_left = BURST_SIZE; + while (bt_left > 0) { + sent = sock->send(&(tls_global::tx_buffer[BURST_SIZE - bt_left]), bt_left); + if (sent == NSAPI_ERROR_WOULD_BLOCK) { + if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { + TEST_FAIL(); + goto END; + } + continue; + } else if (sent < 0) { + printf("[%02d] network error %d\n", i, sent); + TEST_FAIL(); + goto END; + } + bt_left -= sent; + } + if (bt_left != 0) { + TEST_FAIL(); + goto END; + } + + bt_left = BURST_SIZE; + while (bt_left > 0) { + recvd = sock->recv(&(tls_global::rx_buffer[BURST_SIZE - bt_left]), BURST_SIZE); + if (recvd == NSAPI_ERROR_WOULD_BLOCK) { + if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { + printf("[bt#%02d] packet timeout...", i); + break; + } + continue; + } else if (recvd < 0) { + printf("[%02d] network error %d\n", i, recvd); + break; + } + bt_left -= recvd; + } + + if (bt_left != 0) { + printf("network error %d, missing %d bytes from a burst\n", recvd, bt_left); + TEST_FAIL(); + goto END; + } + + TEST_ASSERT_EQUAL(0, memcmp(tls_global::tx_buffer, tls_global::rx_buffer, BURST_SIZE)); + } +END: + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); + delete sock; +} diff --git a/TESTS/netsocket/tls/tlssocket_endpoint_close.cpp b/TESTS/netsocket/tls/tlssocket_endpoint_close.cpp new file mode 100644 index 00000000000..31ab1517c88 --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_endpoint_close.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +namespace { +static const int SIGNAL_SIGIO = 0x1; +static const int SIGIO_TIMEOUT = 20000; //[ms] +} + +static void _sigio_handler(osThreadId id) +{ + osSignalSet(id, SIGNAL_SIGIO); +} + +static nsapi_error_t _tlssocket_connect_to_daytime_srv(TLSSocket &sock) +{ + SocketAddress tls_addr; + + NetworkInterface::get_default_instance()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tls_addr); + tls_addr.set_port(2013); + + nsapi_error_t err = sock.open(NetworkInterface::get_default_instance()); + if (err != NSAPI_ERROR_OK) { + return err; + } + + return sock.connect(tls_addr); +} + + +void TLSSOCKET_ENDPOINT_CLOSE() +{ + static const int MORE_THAN_AVAILABLE = 30; + char buff[MORE_THAN_AVAILABLE]; + int time_allotted = split2half_rmng_tls_test_time(); // [s] + Timer tc_exec_time; + tc_exec_time.start(); + + TLSSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert(tls_global::cert)); + if (_tlssocket_connect_to_daytime_srv(sock) != NSAPI_ERROR_OK) { + TEST_FAIL(); + return; + } + sock.sigio(callback(_sigio_handler, ThisThread::get_id())); + + int recvd = 0; + int recvd_total = 0; + while (true) { + recvd = sock.recv(&(buff[recvd_total]), MORE_THAN_AVAILABLE); + if (recvd_total > 0 && recvd == 0) { + break; // Endpoint closed socket, success + } else if (recvd <= 0) { + TEST_FAIL(); + break; + } else if (recvd == NSAPI_ERROR_WOULD_BLOCK) { + if (tc_exec_time.read() >= time_allotted || + osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) { + TEST_FAIL(); + break; + } + continue; + } + recvd_total += recvd; + TEST_ASSERT(recvd_total < MORE_THAN_AVAILABLE); + } + tc_exec_time.stop(); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp b/TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp new file mode 100644 index 00000000000..93772fcfaba --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_HANDSHAKE_INVALID() +{ + TLSSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert(tls_global::cert)); + TEST_ASSERT_EQUAL(NSAPI_ERROR_NO_CONNECTION, + sock.connect("os.mbed.com", MBED_CONF_APP_ECHO_SERVER_DISCARD_PORT_TLS)); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_no_cert.cpp b/TESTS/netsocket/tls/tlssocket_no_cert.cpp new file mode 100644 index 00000000000..af9d56b515a --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_no_cert.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_NO_CERT() +{ + TLSSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_AUTH_FAILURE, + sock.connect(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT_TLS)); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_open_destruct.cpp b/TESTS/netsocket/tls/tlssocket_open_destruct.cpp new file mode 100644 index 00000000000..2dcc6acbd49 --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_open_destruct.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "greentea-client/test_env.h" +#include "mbed.h" +#include "TLSSocket.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_OPEN_DESTRUCT() +{ +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE + int count = fetch_stats(); + for (int j = 0; j < count; j++) { + TEST_ASSERT_EQUAL(SOCK_CLOSED, tls_stats[j].state); + } +#endif + + for (int i = 0; i < 1000; i++) { + TLSSocket *sock = new TLSSocket; + if (!sock) { + TEST_FAIL(); + } + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); + delete sock; + } +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE + count = fetch_stats(); + for (int j = 0; j < count; j++) { + TEST_ASSERT_EQUAL(SOCK_CLOSED, tls_stats[j].state); + } +#endif +} diff --git a/TESTS/netsocket/tls/tlssocket_open_limit.cpp b/TESTS/netsocket/tls/tlssocket_open_limit.cpp new file mode 100644 index 00000000000..aea5983ae7f --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_open_limit.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "greentea-client/test_env.h" +#include "mbed.h" +#include "TLSSocket.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +namespace { +typedef struct TLSSocketItem { + TLSSocket *sock; + TLSSocketItem *next; +} SocketItem; +} + +void TLSSOCKET_OPEN_LIMIT() +{ + int open_sockets[2] = {0}; + + for (int i = 0; i < 2; i++) { + TLSSocketItem *socket_list_head = NULL; + TLSSocketItem *it; + + TLSSocket *sock; + int ret; + while (true) { + sock = new TLSSocket; + if (!sock) { + break; + } + ret = sock->open(NetworkInterface::get_default_instance()); + if (ret == NSAPI_ERROR_NO_MEMORY || ret == NSAPI_ERROR_NO_SOCKET) { + printf("[round#%02d] unable to open new socket, error: %d\n", i, ret); + delete sock; + break; + } + + // Hopefully this doesn't interfere when trying to allocate more sockets + it = new TLSSocketItem; + if (!it) { + delete sock; + break; + } + + it->sock = sock; + // Order of items in the list doesn't matter + it->next = socket_list_head; + socket_list_head = it; + } + + if (!socket_list_head) { + break; + } + +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE + int count = fetch_stats(); + int open_count = 0; + for (int j = 0; j < count; j++) { + if ((tls_stats[j].state == SOCK_OPEN) && (tls_stats[j].proto == NSAPI_TLS)) { + open_count++; + } + } + TEST_ASSERT(open_count >= 4); +#endif + + TLSSocketItem *tmp; + for (TLSSocketItem *it = socket_list_head; it;) { + ++open_sockets[i]; + tmp = it; + it = it->next; + socket_list_head = it; + delete tmp->sock; + delete tmp; + } + printf("[round#%02d] %d sockets opened\n", i, open_sockets[i]); + } + TEST_ASSERT_EQUAL(open_sockets[0], open_sockets[1]); + TEST_ASSERT(open_sockets[0] >= 4); +} diff --git a/TESTS/netsocket/tls/tlssocket_open_twice.cpp b/TESTS/netsocket/tls/tlssocket_open_twice.cpp new file mode 100644 index 00000000000..ae8e069a5ec --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_open_twice.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "greentea-client/test_env.h" +#include "mbed.h" +#include "TLSSocket.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_OPEN_TWICE() +{ +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE + int count = fetch_stats(); + for (int j = 0; j < count; j++) { + TEST_ASSERT_EQUAL(SOCK_CLOSED, tls_stats[j].state); + } +#endif + TLSSocket *sock = new TLSSocket; + if (!sock) { + TEST_FAIL(); + } + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, sock->open(NetworkInterface::get_default_instance())); + + delete sock; +#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE + count = fetch_stats(); + for (int j = 0; j < count; j++) { + TEST_ASSERT_EQUAL(SOCK_CLOSED, tls_stats[j].state); + } +#endif +} diff --git a/TESTS/netsocket/tls/tlssocket_recv_timeout.cpp b/TESTS/netsocket/tls/tlssocket_recv_timeout.cpp new file mode 100644 index 00000000000..b3e0f5b74d6 --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_recv_timeout.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +namespace { +static const int SIGNAL_SIGIO = 0x1; +static const int SIGIO_TIMEOUT = 20000; //[ms] +} + +static void _sigio_handler(osThreadId id) +{ + osSignalSet(id, SIGNAL_SIGIO); +} + +void TLSSOCKET_RECV_TIMEOUT() +{ + static const int DATA_LEN = 100; + char buff[DATA_LEN] = {0}; + int time_allotted = split2half_rmng_tls_test_time(); // [s] + Timer tc_exec_time; + tc_exec_time.start(); + + TLSSocket sock; + tlssocket_connect_to_echo_srv(sock); + sock.set_timeout(100); + sock.sigio(callback(_sigio_handler, ThisThread::get_id())); + + int recvd = 0; + int pkt_unrecvd; + Timer timer; + for (int i = 0; i < 5; i++) { + pkt_unrecvd = DATA_LEN; + TEST_ASSERT_EQUAL(DATA_LEN, sock.send(buff, DATA_LEN)); + + while (pkt_unrecvd) { + timer.reset(); + timer.start(); + recvd = sock.recv(&(buff[DATA_LEN - pkt_unrecvd]), pkt_unrecvd); + timer.stop(); + + if (recvd == NSAPI_ERROR_WOULD_BLOCK) { + if (tc_exec_time.read() >= time_allotted || + (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout)) { + TEST_FAIL(); + goto CLEANUP; + } + printf("MBED: recv() took: %dus\n", timer.read_us()); + TEST_ASSERT_INT_WITHIN(51, 150, (timer.read_us() + 500) / 1000); + continue; + } else if (recvd < 0) { + printf("[pkt#%02d] network error %d\n", i, recvd); + TEST_FAIL(); + goto CLEANUP; + } + pkt_unrecvd -= recvd; + } + } + +CLEANUP: + tc_exec_time.stop(); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_send_closed.cpp b/TESTS/netsocket/tls/tlssocket_send_closed.cpp new file mode 100644 index 00000000000..a67c6def4ff --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_send_closed.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_SEND_CLOSED() +{ + TLSSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert(tls_global::cert)); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, + sock.connect(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT_TLS)); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); + TEST_ASSERT_EQUAL(NSAPI_ERROR_NO_SOCKET, sock.send("12345", 5)); +} diff --git a/TESTS/netsocket/tls/tlssocket_send_repeat.cpp b/TESTS/netsocket/tls/tlssocket_send_repeat.cpp new file mode 100644 index 00000000000..f7582f8e54b --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_send_repeat.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_SEND_REPEAT() +{ + TLSSocket sock; + tlssocket_connect_to_discard_srv(sock); + + int snd; + Timer timer; + static const char tx_buffer[] = {'h', 'e', 'l', 'l', 'o'}; + for (int i = 0; i < 1000; i++) { + snd = sock.send(tx_buffer, sizeof(tx_buffer)); + if (snd != sizeof(tx_buffer)) { + TEST_FAIL(); + break; + } + } + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_send_timeout.cpp b/TESTS/netsocket/tls/tlssocket_send_timeout.cpp new file mode 100644 index 00000000000..00b807db824 --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_send_timeout.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_SEND_TIMEOUT() +{ + TLSSocket sock; + if (tlssocket_connect_to_discard_srv(sock) != NSAPI_ERROR_OK) { + TEST_FAIL(); + return; + } + + int err; + Timer timer; + static const char tx_buffer[] = {'h', 'e', 'l', 'l', 'o'}; + for (int i = 0; i < 10; i++) { + timer.reset(); + timer.start(); + err = sock.send(tx_buffer, sizeof(tx_buffer)); + timer.stop(); + if ((err == sizeof(tx_buffer)) && + (timer.read_ms() <= 800)) { + continue; + } + TEST_FAIL(); + break; + } + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_send_unconnected.cpp b/TESTS/netsocket/tls/tlssocket_send_unconnected.cpp new file mode 100644 index 00000000000..ba7f53e04f2 --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_send_unconnected.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_SEND_UNCONNECTED() +{ + TLSSocket sock; + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance())); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.set_root_ca_cert(tls_global::cert)); + TEST_ASSERT_EQUAL(NSAPI_ERROR_NO_CONNECTION, sock.send("12345", 5)); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); +} diff --git a/TESTS/netsocket/tls/tlssocket_simultaneous.cpp b/TESTS/netsocket/tls/tlssocket_simultaneous.cpp new file mode 100644 index 00000000000..a902e188ef8 --- /dev/null +++ b/TESTS/netsocket/tls/tlssocket_simultaneous.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "TLSSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest.h" +#include "tls_tests.h" + +using namespace utest::v1; + +void TLSSOCKET_SIMULTANEOUS() +{ + TLSSocket sock1; + TLSSocket sock2; + tlssocket_connect_to_echo_srv(sock1); + tlssocket_connect_to_echo_srv(sock2); + + int snd; + int rcvd; + static const char tx_buffer1[] = {'h', 'e', 'l', 'l', 'o', '1'}; + static const char tx_buffer2[] = {'h', 'e', 'l', 'l', 'o', '2'}; + char rx_buffer1[6]; + char rx_buffer2[6]; + for (int i = 0; i < 10; i++) { + snd = sock1.send(tx_buffer1, sizeof(tx_buffer1)); + if (snd != sizeof(tx_buffer1)) { + TEST_FAIL(); + break; + } + snd = sock2.send(tx_buffer2, sizeof(tx_buffer2)); + if (snd != sizeof(tx_buffer2)) { + TEST_FAIL(); + break; + } + + rcvd = sock1.recv(&rx_buffer1, 6); + if ((rcvd != 6) || (memcmp(tx_buffer1, rx_buffer1, 6) != 0)) { + TEST_FAIL(); + break; + } + rcvd = sock2.recv(&rx_buffer2, 6); + if ((rcvd != 6) || (memcmp(tx_buffer2, rx_buffer2, 6) != 0)) { + TEST_FAIL(); + break; + } + } + + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock1.close()); + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock2.close()); +} diff --git a/tools/test_configs/6lowpanInterface_host.json b/tools/test_configs/6lowpanInterface_host.json index 8fa01b90c4d..7070c716afb 100644 --- a/tools/test_configs/6lowpanInterface_host.json +++ b/tools/test_configs/6lowpanInterface_host.json @@ -11,6 +11,14 @@ "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" } }, "target_overrides": { diff --git a/tools/test_configs/6lowpanInterface_router.json b/tools/test_configs/6lowpanInterface_router.json index 8744b75bf82..8c4f48dd1f8 100644 --- a/tools/test_configs/6lowpanInterface_router.json +++ b/tools/test_configs/6lowpanInterface_router.json @@ -11,6 +11,14 @@ "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" } }, "target_overrides": { diff --git a/tools/test_configs/ESP8266Interface.json b/tools/test_configs/ESP8266Interface.json index 12765af4964..d7152960f4f 100644 --- a/tools/test_configs/ESP8266Interface.json +++ b/tools/test_configs/ESP8266Interface.json @@ -11,6 +11,14 @@ "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" } }, "target_overrides": { diff --git a/tools/test_configs/EthernetInterface.json b/tools/test_configs/EthernetInterface.json index 0a3684eb771..0288d289fda 100644 --- a/tools/test_configs/EthernetInterface.json +++ b/tools/test_configs/EthernetInterface.json @@ -11,6 +11,14 @@ "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" } }, "target_overrides": { diff --git a/tools/test_configs/HeapBlockDeviceAndEthernetInterface.json b/tools/test_configs/HeapBlockDeviceAndEthernetInterface.json index 4662fb2cdbe..345a360ae7a 100644 --- a/tools/test_configs/HeapBlockDeviceAndEthernetInterface.json +++ b/tools/test_configs/HeapBlockDeviceAndEthernetInterface.json @@ -12,6 +12,14 @@ "help" : "Discard port of echo server", "value" : "9" }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" + }, "sim-blockdevice": { "help": "Simulated block device, requires sufficient heap", "macro_name": "MBED_TEST_SIM_BLOCKDEVICE", diff --git a/tools/test_configs/HeapBlockDeviceAndWifiInterface.json b/tools/test_configs/HeapBlockDeviceAndWifiInterface.json index 123d8e74c7d..cce8abfb576 100644 --- a/tools/test_configs/HeapBlockDeviceAndWifiInterface.json +++ b/tools/test_configs/HeapBlockDeviceAndWifiInterface.json @@ -12,6 +12,14 @@ "help" : "Discard port of echo server", "value" : "9" }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" + }, "sim-blockdevice": { "help": "Simulated block device, requires sufficient heap", "macro_name": "MBED_TEST_SIM_BLOCKDEVICE", diff --git a/tools/test_configs/ISM43362Interface.json b/tools/test_configs/ISM43362Interface.json index 13934953a70..bb7e8d546fb 100644 --- a/tools/test_configs/ISM43362Interface.json +++ b/tools/test_configs/ISM43362Interface.json @@ -12,6 +12,14 @@ "help" : "Discard port of echo server", "value" : "9" }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" + }, "wifi-secure-ssid": { "help": "WiFi SSID for WPA2 secured network", "value": "\"SSID-SECURE\"" diff --git a/tools/test_configs/SpwfSAInterface.json b/tools/test_configs/SpwfSAInterface.json index 526d1ab0507..132209a8ca8 100644 --- a/tools/test_configs/SpwfSAInterface.json +++ b/tools/test_configs/SpwfSAInterface.json @@ -12,6 +12,14 @@ "help" : "Discard port of echo server", "value" : "9" }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" + }, "WIFI-TX" : { "help" : "Wifi TX pin", "value" : "D8" diff --git a/tools/test_configs/ThreadInterface_end_device.json b/tools/test_configs/ThreadInterface_end_device.json index a708365aaec..48d29960870 100644 --- a/tools/test_configs/ThreadInterface_end_device.json +++ b/tools/test_configs/ThreadInterface_end_device.json @@ -11,6 +11,14 @@ "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" } }, "target_overrides": { diff --git a/tools/test_configs/ThreadInterface_router.json b/tools/test_configs/ThreadInterface_router.json index 2e712932997..28cca298447 100644 --- a/tools/test_configs/ThreadInterface_router.json +++ b/tools/test_configs/ThreadInterface_router.json @@ -11,6 +11,14 @@ "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : "9" + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : "2007" + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : "2009" } }, "target_overrides": { diff --git a/tools/test_configs/no_network.json b/tools/test_configs/no_network.json index 7a32b299173..9d56bc81ffa 100644 --- a/tools/test_configs/no_network.json +++ b/tools/test_configs/no_network.json @@ -11,6 +11,14 @@ "echo-server-discard-port" : { "help" : "Discard port of echo server", "value" : null + }, + "echo-server-port-tls" : { + "help" : "Port of echo server for TLS", + "value" : null + }, + "echo-server-discard-port-tls" : { + "help" : "Discard port of echo server for TLS", + "value" : null } }, "target_overrides": { From af655226524da7f44df56be318a8292161aa1ea3 Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Fri, 11 Jan 2019 13:07:52 +0100 Subject: [PATCH 2/2] Publish TLSSocket test documentation The whole README.md had to be updated to match the internal Confluence documentation, which can now be locked. In the process I also updated any spotted mistakes in tests documentation, removed the obsolete TCPServer tests documentation and added a test which was missing from UDPSocket's main. --- TESTS/netsocket/README.md | 406 +++++++++--------- TESTS/netsocket/tls/main.cpp | 8 + TESTS/netsocket/tls/tls_tests.h | 6 + .../tls/tlssocket_connect_invalid.cpp | 4 + TESTS/netsocket/tls/tlssocket_echotest.cpp | 13 +- .../tls/tlssocket_echotest_burst.cpp | 4 + .../tls/tlssocket_endpoint_close.cpp | 4 + .../tls/tlssocket_handshake_invalid.cpp | 4 + TESTS/netsocket/tls/tlssocket_no_cert.cpp | 4 + .../netsocket/tls/tlssocket_open_destruct.cpp | 4 + TESTS/netsocket/tls/tlssocket_open_limit.cpp | 4 + TESTS/netsocket/tls/tlssocket_open_twice.cpp | 4 + .../netsocket/tls/tlssocket_recv_timeout.cpp | 4 + TESTS/netsocket/tls/tlssocket_send_closed.cpp | 4 + TESTS/netsocket/tls/tlssocket_send_repeat.cpp | 4 + .../netsocket/tls/tlssocket_send_timeout.cpp | 4 + .../tls/tlssocket_send_unconnected.cpp | 4 + .../netsocket/tls/tlssocket_simultaneous.cpp | 4 + TESTS/netsocket/udp/main.cpp | 1 + 19 files changed, 277 insertions(+), 213 deletions(-) diff --git a/TESTS/netsocket/README.md b/TESTS/netsocket/README.md index f4d9bb2dc75..7c45460677b 100644 --- a/TESTS/netsocket/README.md +++ b/TESTS/netsocket/README.md @@ -58,14 +58,14 @@ echo.mbedcloudtesting.com has IPv6 address 2a05:d018:21f:3800:8584:60f8:bc9f:e61 **Open services in the test server** - Echo protocol, [RFC 862](https://tools.ietf.org/html/rfc862) is - enabled in both TCP and UDP. Port 7. + enabled in both TCP and UDP. Port 7. Port 2007 for TLS. - Discard protocol, [RFC 863](https://tools.ietf.org/html/rfc863) is - enabled in both TCP and UDP. Port 9. + enabled in both TCP and UDP. Port 9. Port 2009 for TLS. - Character generator protocol, [RFC 864](https://tools.ietf.org/html/rfc864) is enabled in both TCP and UDP. Port 19. Output pattern should follow the proposed example pattern in RFC. - Daytime protocol, [RFC 867](https://tools.ietf.org/html/rfc867) in - both TCP and UDP. Port 13. + both TCP and UDP. Port 13. Port 2013 for TLS. - Time protocol, [RFC 868](https://tools.ietf.org/html/rfc868) in both TCP and UDP. Port 37. @@ -179,43 +179,55 @@ pass the test if the driver implements the feature in question. | 12 | UDPSOCKET_BIND_UNOPENED | SHOULD | | 13 | UDPSOCKET_SENDTO_INVALID | MUST | | 14 | UDPSOCKET_SENDTO_REPEAT | MUST | -| 15 | UDPSOCKET_BIND_SENDTO | SHOULD | -| 16 | UDPSOCKET_ECHOTEST | MUST | -| 17 | UDPSOCKET_ECHOTEST_NONBLOCK | MUST | -| 18 | UDPSOCKET_RECV_TIMEOUT | SHOULD | -| 19 | UDPSOCKET_SENDTO_TIMEOUT | SHOULD | -| 20 | TCPSOCKET_OPEN_DESTRUCT | MUST | -| 21 | TCPSOCKET_OPEN_LIMIT | MUST | -| 22 | TCPSOCKET_OPEN_TWICE | MUST | -| 23 | TCPSOCKET_OPEN_CLOSE_REPEAT | MUST | -| 24 | TCPSOCKET_BIND_PORT | SHOULD | -| 25 | TCPSOCKET_BIND_PORT_FAIL | SHOULD | -| 26 | TCPSOCKET_BIND_ADDRESS_PORT | SHOULD | -| 27 | TCPSOCKET_BIND_ADDRESS_NULL | SHOULD | -| 28 | TCPSOCKET_BIND_ADDRESS_INVALID | SHOULD | -| 29 | TCPSOCKET_BIND_WRONG_TYPE | SHOULD | -| 30 | TCPSOCKET_BIND_ADDRESS | SHOULD | -| 31 | TCPSOCKET_BIND_UNOPENED | SHOULD | -| 32 | TCPSOCKET_CONNECT_INVALID | MUST | -| 33 | TCPSOCKET_SEND_REPEAT | MUST | -| 34 | TCPSOCKET_ECHOTEST | MUST | -| 35 | TCPSOCKET_ECHOTEST_NONBLOCK | MUST | -| 36 | TCPSOCKET_RECV_TIMEOUT | SHOULD | -| 37 | TCPSOCKET_SEND_TIMEOUT | SHOULD | -| 38 | TCPSOCKET_ENDPOINT_CLOSE | MUST | -| 39 | TCPSERVER_ACCEPT | SHOULD | -| 40 | TCPSERVER_LISTEN | SHOULD | -| 41 | TCPSERVER_LISTEN_WITHOUT_BIND | SHOULD | -| 42 | TCPSERVER_ACCEPT_WITHOUT_LISTEN | SHOULD | -| 43 | UDPSOCKET_ECHOTEST_BURST | MUST | -| 44 | UDPSOCKET_ECHOTEST_BURST_NONBLOCK | MUST | -| 45 | TCPSOCKET_ECHOTEST_BURST | MUST | -| 46 | TCPSOCKET_ECHOTEST_BURST_NONBLOCK | MUST | -| 47 | TCPSOCKET_RECV_100K | MUST | -| 48 | TCPSOCKET_RECV_100K_NONBLOCK | MUST | -| 49 | TCPSOCKET_THREAD_PER_SOCKET_SAFETY | MUST | -| 50 | TCPSOCKET_SETSOCKOPT_KEEPALIVE_VALID | SHOULD | -| 51 | TCPSOCKET_SETSOCKOPT_KEEPALIVE_INVALID | SHOULD | +| 15 | UDPSOCKET_ECHOTEST | MUST | +| 16 | UDPSOCKET_ECHOTEST_NONBLOCK | MUST | +| 17 | UDPSOCKET_RECV_TIMEOUT | SHOULD | +| 18 | UDPSOCKET_SENDTO_TIMEOUT | SHOULD | +| 19 | TCPSOCKET_OPEN_DESTRUCT | MUST | +| 20 | TCPSOCKET_OPEN_LIMIT | MUST | +| 21 | TCPSOCKET_OPEN_TWICE | MUST | +| 22 | TCPSOCKET_OPEN_CLOSE_REPEAT | MUST | +| 23 | TCPSOCKET_BIND_PORT | SHOULD | +| 24 | TCPSOCKET_BIND_PORT_FAIL | SHOULD | +| 25 | TCPSOCKET_BIND_ADDRESS_PORT | SHOULD | +| 26 | TCPSOCKET_BIND_ADDRESS_NULL | SHOULD | +| 27 | TCPSOCKET_BIND_ADDRESS_INVALID | SHOULD | +| 28 | TCPSOCKET_BIND_WRONG_TYPE | SHOULD | +| 29 | TCPSOCKET_BIND_ADDRESS | SHOULD | +| 30 | TCPSOCKET_BIND_UNOPENED | SHOULD | +| 31 | TCPSOCKET_CONNECT_INVALID | MUST | +| 32 | TCPSOCKET_SEND_REPEAT | MUST | +| 33 | TCPSOCKET_ECHOTEST | MUST | +| 34 | TCPSOCKET_ECHOTEST_NONBLOCK | MUST | +| 35 | TCPSOCKET_RECV_TIMEOUT | SHOULD | +| 36 | TCPSOCKET_SEND_TIMEOUT | SHOULD | +| 37 | TCPSOCKET_ENDPOINT_CLOSE | MUST | +| 38 | UDPSOCKET_ECHOTEST_BURST | MUST | +| 39 | UDPSOCKET_ECHOTEST_BURST_NONBLOCK | MUST | +| 40 | TCPSOCKET_ECHOTEST_BURST | MUST | +| 41 | TCPSOCKET_ECHOTEST_BURST_NONBLOCK | MUST | +| 42 | TCPSOCKET_RECV_100K | MUST | +| 43 | TCPSOCKET_RECV_100K_NONBLOCK | MUST | +| 44 | TCPSOCKET_THREAD_PER_SOCKET_SAFETY | MUST | +| 45 | TCPSOCKET_SETSOCKOPT_KEEPALIVE_VALID | SHOULD | +| 46 | TLSSOCKET_OPEN_DESTRUCT | SHOULD | +| 47 | TLSSOCKET_OPEN_LIMIT | SHOULD | +| 48 | TLSSOCKET_OPEN_TWICE | SHOULD | +| 49 | TLSSOCKET_CONNECT_INVALID | SHOULD | +| 50 | TLSSOCKET_HANDSHAKE_INVALID | SHOULD | +| 51 | TLSSOCKET_SEND_CLOSED | SHOULD | +| 52 | TLSSOCKET_SEND_UNCONNECTED | SHOULD | +| 53 | TLSSOCKET_SEND_REPEAT | SHOULD | +| 54 | TLSSOCKET_SEND_TIMEOUT | SHOULD | +| 55 | TLSSOCKET_ECHOTEST | SHOULD | +| 56 | TLSSOCKET_ECHOTEST_NONBLOCK | SHOULD | +| 57 | TLSSOCKET_ENDPOINT_CLOSE | SHOULD | +| 58 | TLSSOCKET_NO_CERT | SHOULD | +| 59 | TLSSOCKET_RECV_TIMEOUT | SHOULD | +| 60 | TLSSOCKET_SIMULTANEOUS_TEST | SHOULD | +| 61 | TLSSOCKET_ECHOTEST_BURST | SHOULD | +| 62 | TLSSOCKET_ECHOTEST_BURST_NONBLOCK | SHOULD | + Building test binaries @@ -354,7 +366,10 @@ Test cases for Socket class These test are equal for UDPSocket and TCPSocket but are described here because of identical API and behaviour. Socket class is abstract so it cannot be instantiated, therefore these test cases are implemented using -both TCPSocket and UDPSocket. +both TCPSocket and UDPSocket. Some of these tests are also implemented +for TLSSocket class. In such case certificate has to be set for the Socket +before calling `open()`, unless specified otherwise in the test's +description. ### SOCKET_OPEN_DESTRUCT @@ -733,19 +748,16 @@ Call `UDPSocket::sendto()` with invalid parameters. 1. Call `UDPSocket:sendto( NULL, 9, NULL, 0);` 2. Call `UDPSocket:sendto( "", 9, NULL, 0);` 3. Call `UDPSocket:sendto( "", 0, NULL, 0);` -4. Call `UDPSocket:sendto(NULL, 9, "hello", 5);` -5. Call `UDPSocket:sendto(NULL, 0, "hello", 5);` -6. Call `UDPSocket:sendto("echo.mbedcloudtesting.com", 9,NULL, 0);` -7. Call `UDPSocket:sendto("echo.mbedcloudtesting.com", 9, "hello", 5);` -8. destroy the socket +4. Call `UDPSocket:sendto("echo.mbedcloudtesting.com", 9,NULL, 0);` +5. Call `UDPSocket:sendto("echo.mbedcloudtesting.com", 9, "hello", 5);` +6. destroy the socket **Expected result:** All sendto() calls should return some error code except: -- step 6 should return 5 -- step 7 should return 0 -- step 8 should return 5 +- step 4 should return 0 +- step 5 should return 5 @@ -775,41 +787,6 @@ All sendto() calls should return 5. -### UDPSOCKET_BIND_SENDTO - -**Description:** - - Bind the socket to specific port before sending. Verify from DUT2 that -packet was coming from correct port. - -Requires two devices with LAN connectivity (Eth, WiFi or mesh). For -Cellular or WAN connectivity, skip this test. - -**Preconditions:** - -1. Network interface and stack are initialised -2. Network connection is up -3. UDPSocket is open - -**Test steps:** - -1. DUT1&2: Call `UDPSocket::bind();` -2. DUT2: Get devices IP address -3. DUT1: Call `UDPSocket::sendto( dut2, port, "hello", 5);` -4. DUT2: Call `UDPSocket::recvfrom();` -5. destroy the sockets - -**Expected result:** - -`UDPSocket::bind()` should return NSAPI_ERROR_OK. - -`UDPSocket::sendto()` call should return 5. - -`UDPSocket::recvfrom()` should return 5 and port number should match the -one used in bind() call. Data should contain "hello" - - - ### UDPSOCKET_ECHOTEST **Description:** @@ -1014,7 +991,7 @@ Call `TCPSocket::connect()` with invalid parameters. **Expected result:** -All connect() calls should return some error code except the number 5 +All connect() calls should return some error code except the number 4 should return NSAPI_ERROR_OK. @@ -1238,52 +1215,52 @@ Test we are able to request setting valid TCP keepalive values `TCPSocket::getsockopt(keepalive)` returns same value as was set with `TCPSocket::setsockopt()` or NSAPI_ERROR_UNSUPPORTED -### TCPSOCKET_SETSOCKOPT_KEEPALIVE_INVALID +Test cases for TLSSocket class +------------------------------ -**Description:** +### TLSSOCKET_OPEN_DESTRUCT -Test we are able to detect if an invalid TCP keepalive value is tried to -be set +**Description:** Run SOCKET_OPEN_DESTRUCT for TLSSocket -**Preconditions:** +### TLSSOCKET_OPEN_LIMIT -1. Network interface and stack are initialised -2. Network connection is up +**Description:** Run SOCKET_OPEN_LIMIT for TLSSocket -**Test steps:** +### TLSSOCKET_OPEN_TWICE -1. Call `TCPSocket::setsockopt(keepalive, [-1 or 7201]);` -2. Call `TCPSocket::getsockopt(keepalive);` +**Description:** Run SOCKET_OPEN_TWICE for TLSSocket -**Postconditions:** +### TLSSOCKET_CONNECT_INVALID -1. Call `TCPSocket::close();` -2. delete socket +**Description:** Run SOCKET_CONNECT_INVALID for TLSSocket -**Expected result:** +### TLSSOCKET_HANDSHAKE_INVALID -`TCPSocket::setsockopt(keepalive)` returns error code or -NSAPI_ERROR_UNSUPPORTED +**Description:** -`TCPSocket::getsockopt()` returns 0 or NSAPI_ERROR_UNSUPPORTED +Execute TLS handshake by calling `TLSSocket::connect()` - server must not match to the certificate used by to os.mbed.com +**Preconditions:** -Test cases for TCPServer class ------------------------------- +1. Network interface and stack are initialised +2. Network connection is up -These tests require two devices under test and connectivity between -them. Therefore they can only be ran with LAN connectivity (Eth, Wifi or -Mesh) or if there is no firewall between devices. +**Test steps:** +1. Create TLSSocket +2. Call `TLSSocket::open()` +3. Call `TLSSocket::connect("os.mbed.com", 2009)` +4. Call `TLSSocket::close()` -### TCPSERVER_ACCEPT +**Expected result:** -**Description:** +TLSSocket::connect must return an error + +### TLSSOCKET_SEND_CLOSED - Test that `TCPServer::bind()`, `TCPServer::listen()` -and `TCPServer::accept()` works. +**Description:** -Requires 2 devices. +Make a HTTP request to a closed socket **Preconditions:** @@ -1292,34 +1269,29 @@ Requires 2 devices. **Test steps:** -1. DUT1: `TCPServer::bind()` -2. DUT1: `TCPServer::listen()` -3. DUT1: Create a new `TCPSocket` -4. DUT1: `TCPServer::accept()` -5. DUT2: Create a new `TCPSocket` -6. DUT2: `TCPSocket::connect(, )` -7. DUT1: should receive new socket from accept(), - call `TCPSocket::send("hello",5)` for it -8. DUT2: call `TCPSocket::recv(buffer, 5)` -9. DUT2: Verify that it received "hello" -10. destroy all sockets. +1. Create TLSSocket +2. Call `TLSSocket::open()` +3. Call `TLSSocket::connect("echo.mbedcloudtesting.com", 2007)` +4. Call `TLSSocket::close()` +5. Call `TLSSocket::send("12345", 5)` **Expected result:** -On DUT1 accept() call blocks until connection is received. Other calls -should return NSAPI_ERROR_OK +TLSSocket::send must return an error -On DUT2 all calls should return NSAPI_ERROR_OK +### TLSSOCKET_SEND_REPEAT +**Description:** Run SOCKET_SEND_REPEAT for TLSSOCKET by using port number 2009. +### TLSSOCKET_SEND_TIMEOUT -### TCPSERVER_LISTEN +**Description:** Run SOCKET_SEND_TIMEOUT for TLSSOCKET by using port number 2009. -**Description:** +### TLSSOCKET_SEND_UNCONNECTED - Test that `TCPServer::listen()` has the backlog functionality. +**Description:** -Requires 2 devices. +Make a HTTP request to an unconnected socket **Preconditions:** @@ -1328,34 +1300,32 @@ Requires 2 devices. **Test steps:** -1. DUT1: `TCPServer::bind()` -2. DUT1: `TCPServer::listen(2)` -3. loop 2 times: - 1. DUT2: Create a new TCPSocket - 2. DUT2: `TCPSocket::connect(, )` +1. Create TLSSocket +2. Call `TLSSocket::open()` +3. Call `TLSSocket::send("12345", 5)` +4. Call `TLSSocket::connect("echo.mbedcloudtesting.com", 2007)` + +**Expected result:** -4. loop 2 times: - 1. DUT1: Create a new TCPSocket - 2. DUT1: `TCPServer::accept()` - 3. DUT1: should receive new socket from accept(), - call `TCPSocket::send("hello",5)` for it +TLSSocket::send must return an error -5. DUT2: call `TCPSocket::recv(buffer, 5)` for both socket. -6. DUT2: Verify that it received "hello" -7. destroy all sockets. +### TLSSOCKET_ECHOTEST -**Expected result:** +**Description:** Run SOCKET_ECHOTEST for TLSSOCKET by using port number 2007. + +### TLSSOCKET_ECHOTEST_NONBLOCK -DUT2 should receive connection before the server have called accept(), -because backlog must be minimum of 2. +**Description:** Run SOCKET_ECHOTEST_NONBLOCK for TLSSOCKET by using port number 2007. +### TLSSOCKET_ENDPOINT_CLOSE -### TCPSERVER_LISTEN_WITHOUT_BIND +**Description:** Run SOCKET_ENDPOINT_CLOSE for TLSSOCKET by using port number 2013. + +### TLSSOCKET_NO_CERT **Description:** - Call `TCPServer::listen()` without calling bind() first. Should fail, -because no listening port have been defined. +Verify that TLS Socket fails to connect without certificate. **Preconditions:** @@ -1364,40 +1334,60 @@ because no listening port have been defined. **Test steps:** -1. Create TCPServer -2. Call `TCPServer::listen()` +1. Create TLSSocket, without adding a default certificate. +2. Call `TLSSocket::open()` +3. Call `TLSSocket::connect("echo.mbedcloudtesting.com", 2009)` **Expected result:** -Should -return NSAPI_ERROR_UNSUPPORTED, NSAPI_ERROR_PARAMETER, NSAPI_ERROR_NO_ADDRESS -or NSAPI_ERROR_DEVICE_ERROR +TLSSocket::connect must return an error if the certificate is not present. + +### TLSSOCKET_RECV_TIMEOUT +**Description:** -### TCPSERVER_ACCEPT_WITHOUT_LISTEN +Run TCPSOCKET_RECV_TIMEOUT for TLSSOCKET by using port number 2007.  + +### TLSSOCKET_SIMULTANEOUS_TEST **Description:** - Call `TCPServer::accept()` without calling listen() first. Should fail, -because socket is not listening for connections. +Simultaneously send packets to echo server on two opened sockets and read incoming packets back. Verify working of two TLS sockets open and operate simultaneously. **Preconditions:** 1. Network interface and stack are initialised 2. Network connection is up +3. TLSSockets are open and one additional thread has been created + +4. Both threads get their own socket instance **Test steps:** -1. Create TCPServer -2. Call `TCPServer::bind()` -3. Create new TCPSocket. -4. Call `TCPServer::accept()` +1. On main thread: + 1. Call `TLSSocket::connect("echo.mbedcloudtesting.com", 2007);` + 2. Call `TLSSocket::send(, );` + 1. If less than was returned, size = sent bytes -**Expected result:** + 3. `Call TLSSocket::recv(buffer, );` + 4. Verify incomming content was the same that was sent + 5. Repeat 100 times + +2. Simultaneously with the earlier step do on the additional thread: + 1. Call `TLSSocket::connect("echo.mbedcloudtesting.com", 2007);` + 2. Call `TLSSocket::send(, );` + 1. If less than was returned, size = sent bytes -Should return NSAPI_ERROR_UNSUPPORTED, NSAPI_ERROR_PARAMETER -or NSAPI_ERROR_DEVICE_ERROR + 3. Call `TLSSocket::recv(buffer, );` + 4. Verify incomming content was the same that was sent + 5. Repeat 100 times + +3. Wait for end additional thread +4. Close and destroy the sockets + +**Expected result:** +All send() calls should return the packet size or less. All recv() calls on main thread should return the same sized packet that was send with same content. All recv() calls on additional thread should return the valid daytime string .  Performance tests ----------------- @@ -1411,9 +1401,9 @@ Send burst of packets to echo server and read incoming packets back. **Preconditions:** -1. Network interface and stack are initialised -2. Network connection is up -3. UDPSocket is open +1. Network interface and stack are initialised. +2. Network connection is up. +3. UDPSocket is open. **Test steps:** @@ -1425,8 +1415,8 @@ Send burst of packets to echo server and read incoming packets back. 6. Wait for incomming packets for five second. 7. Verify incomming content was the same that was sent. Allow packet reordering. -8. Repeat 100 times -9. destroy the socket +8. Repeat 100 times. +9. Destroy the socket. **Expected result:** @@ -1435,9 +1425,9 @@ All sendto() calls should return the packet size. All recvfrom() calls should return the same sized packet that was send with same content. Allow packet reordering. -Calculate packet loss rate, maximum tolerated packet loss rate is 30% +Calculate packet loss rate, maximum tolerated packet loss rate is 30%. -Calculate number of succesfull rounds, it should be higher than 70 +Calculate number of succesfull rounds, it should be higher than 70. ### UDPSOCKET_ECHOTEST_BURST_NONBLOCK @@ -1445,13 +1435,13 @@ Calculate number of succesfull rounds, it should be higher than 70 **Description:** Send burst of packets to echo server and read incoming packets back. Use -socket in non-blocking mode +socket in non-blocking mode. **Preconditions:** -1. Network interface and stack are initialised -2. Network connection is up -3. UDPSocket is open +1. Network interface and stack are initialised. +2. Network connection is up. +3. UDPSocket is open. **Test steps:** @@ -1467,8 +1457,8 @@ socket in non-blocking mode 9. Wait for incomming packets for five second. 10. Verify incomming content was the same that was sent. Allow packet reordering. -11. Repeat 100 times -12. destroy the socket +11. Repeat 100 times. +12. Destroy the socket. **Expected result:** @@ -1477,9 +1467,9 @@ All sendto() calls should return the packet size. All recvfrom() calls should return the same sized packet that was send with same content. Allow packet reordering. -Calculate packet loss rate, maximum tolerated packet loss rate is 30% +Calculate packet loss rate, maximum tolerated packet loss rate is 30%. -Calculate number of succesfull rounds, it should be higher than 70 +Calculate number of succesfull rounds, it should be higher than 70. @@ -1491,9 +1481,9 @@ Send burst of packets to echo server and read incoming packets back. **Preconditions:** -1. Network interface and stack are initialised -2. Network connection is up -3. TCPSocket is open +1. Network interface and stack are initialised. +2. Network connection is up. +3. TCPSocket is open. **Test steps:** @@ -1505,8 +1495,8 @@ Send burst of packets to echo server and read incoming packets back. 6. Call `TCPSocket::send(, 500);` 7. Call `TCPSocket::recv(buf, 1220)` 8. Verify incomming content was the same that was sent. -9. Repeat 100 times -10. destroy the socket +9. Repeat 100 times. +10. Destroy the socket. **Expected result:** @@ -1525,13 +1515,13 @@ send. Total amount of returned must match 1220. **Description:** Send burst of packets to echo server and read incoming packets back. Use -socket in non-blocking mode +socket in non-blocking mode. **Preconditions:** -1. Network interface and stack are initialised -2. Network connection is up -3. TCPSocket is open +1. Network interface and stack are initialised. +2. Network connection is up. +3. TCPSocket is open. **Test steps:** @@ -1543,13 +1533,13 @@ socket in non-blocking mode 5. For randomly generated packets, sized 100, 200, 300, 120 and 500 do 1. Call `TCPSocket::send(packet, size);` 2. If less than size is sent, repeat with remaining. - 3. If NSAPI_ERROR_WOULD_BLOCK returned, wait for next sigio() + 3. If NSAPI_ERROR_WOULD_BLOCK returned, wait for next sigio(). 6. Wait for incomming packets for five second. 7. Verify incomming content was the same that was sent. Allow recv() to return smaller piezes. -8. Repeat 100 times -9. destroy the socket +8. Repeat 100 times. +9. Destroy the socket. **Expected result:** @@ -1571,9 +1561,9 @@ Download 100kB of data **Preconditions:** -1. Network interface and stack are initialised -2. Network connection is up -3. TCPSocket is open +1. Network interface and stack are initialised. +2. Network connection is up. +3. TCPSocket is open. **Test steps:** @@ -1581,7 +1571,7 @@ Download 100kB of data 2. Call `TCPSocket::recv(buffer, 100);` 3. Verify input according to known pattern. 4. Loop until 100kB of data received. -5. close socket. +5. Close socket. **Expected result:** @@ -1598,9 +1588,9 @@ Download 100kB of data **Preconditions:** -1. Network interface and stack are initialised -2. Network connection is up -3. TCPSocket is open +1. Network interface and stack are initialised. +2. Network connection is up. +3. TCPSocket is open. **Test steps:** @@ -1612,7 +1602,7 @@ Download 100kB of data 2. Verify input according to known pattern. 4. Wait until 100kB of data received. -5. close socket. +5. Close socket. **Expected result:** @@ -1620,7 +1610,7 @@ Each recv() call should return equal or less than 100 bytes of data or NSAPI_ERROR_WOULD_BLOCK in which case thread should wait for another sigio(). No errors should be returned. -Measure time taken for receiving, report speed +Measure time taken for receiving, report speed. ### TCPSOCKET_THREAD_PER_SOCKET_SAFETY @@ -1631,16 +1621,16 @@ through a dedicated socket **Preconditions:** -1. Network interface and stack are initialised -2. Network connection is up -3. 2 TCPSockets are open and one additional thread has been created -4. Both threads get their own socket instance +1. Network interface and stack are initialised. +2. Network connection is up. +3. 2 TCPSockets are open and one additional thread has been created. +4. Both threads get their own socket instance. **Test steps:** 1. Call `TCPSocket::connect("echo.mbedcloudtesting.com", 7)` in both threads - in the main thread executing the test case and on - the additional one; + the additional one. 2. On main thread 1. For randomly generated packets, sized 1001, 901, 801,...,101,1 do @@ -1656,13 +1646,13 @@ through a dedicated socket 3. stop the thread if inconsistensies were found and report it to main thread -4. Kill the additional thread -5. Close and destroy the sockets +4. Kill the additional thread. +5. Close and destroy the sockets. **Expected result:** Echo server returns data to both threads and received data matches to -send data. The additional thread isn't stopped prematurely +send data. The additional thread isn't stopped prematurely. Subset for driver test ---------------------- diff --git a/TESTS/netsocket/tls/main.cpp b/TESTS/netsocket/tls/main.cpp index e4aab9a005f..0e7316a9e3c 100644 --- a/TESTS/netsocket/tls/main.cpp +++ b/TESTS/netsocket/tls/main.cpp @@ -31,6 +31,8 @@ #include "utest/utest_stack_trace.h" #include "tls_tests.h" +#if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) + using namespace utest::v1; namespace { @@ -193,7 +195,9 @@ Case cases[] = { Case("TLSSOCKET_SEND_REPEAT", TLSSOCKET_SEND_REPEAT), Case("TLSSOCKET_SEND_TIMEOUT", TLSSOCKET_SEND_TIMEOUT), Case("TLSSOCKET_NO_CERT", TLSSOCKET_NO_CERT), +#ifndef __IAR_SYSTEMS_ICC__ Case("TLSSOCKET_SIMULTANEOUS", TLSSOCKET_SIMULTANEOUS) +#endif }; Specification specification(greentea_setup, cases, greentea_teardown, greentea_continue_handlers); @@ -212,3 +216,7 @@ int main() th->join(); return retval; } + +#else +#error [NOT_SUPPORTED] This device does not support SSL library +#endif // defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) diff --git a/TESTS/netsocket/tls/tls_tests.h b/TESTS/netsocket/tls/tls_tests.h index b29c065b40f..f5555c6be63 100644 --- a/TESTS/netsocket/tls/tls_tests.h +++ b/TESTS/netsocket/tls/tls_tests.h @@ -18,6 +18,10 @@ #ifndef TLS_TESTS_H #define TLS_TESTS_H +#include "TLSSocket.h" + +#if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) + NetworkInterface *get_interface(); void drop_bad_packets(TLSSocket &sock, int orig_timeout); void fill_tx_buffer_ascii(char *buff, size_t len); @@ -67,4 +71,6 @@ void TLSSOCKET_NO_CERT(); void TLSSOCKET_SIMULTANEOUS(); void TLSSOCKET_SEND_TIMEOUT(); +#endif // defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) + #endif //TLS_TESTS_H diff --git a/TESTS/netsocket/tls/tlssocket_connect_invalid.cpp b/TESTS/netsocket/tls/tlssocket_connect_invalid.cpp index 433d82007aa..61e3375fa87 100644 --- a/TESTS/netsocket/tls/tlssocket_connect_invalid.cpp +++ b/TESTS/netsocket/tls/tlssocket_connect_invalid.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_CONNECT_INVALID() { TLSSocket sock; @@ -39,3 +41,5 @@ void TLSSOCKET_CONNECT_INVALID() TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_echotest.cpp b/TESTS/netsocket/tls/tlssocket_echotest.cpp index b49afb41d6a..b7b91b1ef45 100644 --- a/TESTS/netsocket/tls/tlssocket_echotest.cpp +++ b/TESTS/netsocket/tls/tlssocket_echotest.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + namespace { static const int SIGNAL_SIGIO = 0x1; static const int SIGIO_TIMEOUT = 20000; //[ms] @@ -118,8 +120,7 @@ void tlssocket_echotest_nonblock_receive() static int round = 0; printf("[Recevr#%02d] bytes received: %d\n", round++, bytes2recv_total); tx_sem.release(); - } - else if (receive_error || bytes2recv < 0) { + } else if (receive_error || bytes2recv < 0) { TEST_FAIL(); tx_sem.release(); } @@ -150,9 +151,9 @@ void TLSSOCKET_ECHOTEST_NONBLOCK() unsigned char *stack_mem = (unsigned char *)malloc(tls_global::TLS_OS_STACK_SIZE); TEST_ASSERT_NOT_NULL(stack_mem); Thread *receiver_thread = new Thread(osPriorityNormal, - tls_global::TLS_OS_STACK_SIZE, - stack_mem, - "receiver"); + tls_global::TLS_OS_STACK_SIZE, + stack_mem, + "receiver"); EventQueue queue(2 * EVENTS_EVENT_SIZE); event_queue = &queue; TEST_ASSERT_EQUAL(osOK, receiver_thread->start(callback(&queue, &EventQueue::dispatch_forever))); @@ -207,3 +208,5 @@ void TLSSOCKET_ECHOTEST_NONBLOCK() free(stack_mem); delete sock; } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_echotest_burst.cpp b/TESTS/netsocket/tls/tlssocket_echotest_burst.cpp index 7797c45272c..330c4c8bb74 100644 --- a/TESTS/netsocket/tls/tlssocket_echotest_burst.cpp +++ b/TESTS/netsocket/tls/tlssocket_echotest_burst.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + namespace { static const int SIGNAL_SIGIO = 0x1; static const int SIGIO_TIMEOUT = 20000; //[ms] @@ -152,3 +154,5 @@ void TLSSOCKET_ECHOTEST_BURST_NONBLOCK() TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close()); delete sock; } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_endpoint_close.cpp b/TESTS/netsocket/tls/tlssocket_endpoint_close.cpp index 31ab1517c88..326bf3079c8 100644 --- a/TESTS/netsocket/tls/tlssocket_endpoint_close.cpp +++ b/TESTS/netsocket/tls/tlssocket_endpoint_close.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + namespace { static const int SIGNAL_SIGIO = 0x1; static const int SIGIO_TIMEOUT = 20000; //[ms] @@ -89,3 +91,5 @@ void TLSSOCKET_ENDPOINT_CLOSE() tc_exec_time.stop(); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp b/TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp index 93772fcfaba..1c985eed6d7 100644 --- a/TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp +++ b/TESTS/netsocket/tls/tlssocket_handshake_invalid.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_HANDSHAKE_INVALID() { TLSSocket sock; @@ -33,3 +35,5 @@ void TLSSOCKET_HANDSHAKE_INVALID() sock.connect("os.mbed.com", MBED_CONF_APP_ECHO_SERVER_DISCARD_PORT_TLS)); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_no_cert.cpp b/TESTS/netsocket/tls/tlssocket_no_cert.cpp index af9d56b515a..3ad393977ad 100644 --- a/TESTS/netsocket/tls/tlssocket_no_cert.cpp +++ b/TESTS/netsocket/tls/tlssocket_no_cert.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_NO_CERT() { TLSSocket sock; @@ -32,3 +34,5 @@ void TLSSOCKET_NO_CERT() sock.connect(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT_TLS)); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_open_destruct.cpp b/TESTS/netsocket/tls/tlssocket_open_destruct.cpp index 2dcc6acbd49..5df6c901817 100644 --- a/TESTS/netsocket/tls/tlssocket_open_destruct.cpp +++ b/TESTS/netsocket/tls/tlssocket_open_destruct.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_OPEN_DESTRUCT() { #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE @@ -48,3 +50,5 @@ void TLSSOCKET_OPEN_DESTRUCT() } #endif } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_open_limit.cpp b/TESTS/netsocket/tls/tlssocket_open_limit.cpp index aea5983ae7f..58ff5bd70ad 100644 --- a/TESTS/netsocket/tls/tlssocket_open_limit.cpp +++ b/TESTS/netsocket/tls/tlssocket_open_limit.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + namespace { typedef struct TLSSocketItem { TLSSocket *sock; @@ -95,3 +97,5 @@ void TLSSOCKET_OPEN_LIMIT() TEST_ASSERT_EQUAL(open_sockets[0], open_sockets[1]); TEST_ASSERT(open_sockets[0] >= 4); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_open_twice.cpp b/TESTS/netsocket/tls/tlssocket_open_twice.cpp index ae8e069a5ec..4710898ef68 100644 --- a/TESTS/netsocket/tls/tlssocket_open_twice.cpp +++ b/TESTS/netsocket/tls/tlssocket_open_twice.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_OPEN_TWICE() { #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE @@ -48,3 +50,5 @@ void TLSSOCKET_OPEN_TWICE() } #endif } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_recv_timeout.cpp b/TESTS/netsocket/tls/tlssocket_recv_timeout.cpp index b3e0f5b74d6..277d2c094ff 100644 --- a/TESTS/netsocket/tls/tlssocket_recv_timeout.cpp +++ b/TESTS/netsocket/tls/tlssocket_recv_timeout.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + namespace { static const int SIGNAL_SIGIO = 0x1; static const int SIGIO_TIMEOUT = 20000; //[ms] @@ -82,3 +84,5 @@ void TLSSOCKET_RECV_TIMEOUT() tc_exec_time.stop(); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_send_closed.cpp b/TESTS/netsocket/tls/tlssocket_send_closed.cpp index a67c6def4ff..3fff8414fb0 100644 --- a/TESTS/netsocket/tls/tlssocket_send_closed.cpp +++ b/TESTS/netsocket/tls/tlssocket_send_closed.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_SEND_CLOSED() { TLSSocket sock; @@ -34,3 +36,5 @@ void TLSSOCKET_SEND_CLOSED() TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); TEST_ASSERT_EQUAL(NSAPI_ERROR_NO_SOCKET, sock.send("12345", 5)); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_send_repeat.cpp b/TESTS/netsocket/tls/tlssocket_send_repeat.cpp index f7582f8e54b..9b5d465de14 100644 --- a/TESTS/netsocket/tls/tlssocket_send_repeat.cpp +++ b/TESTS/netsocket/tls/tlssocket_send_repeat.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_SEND_REPEAT() { TLSSocket sock; @@ -42,3 +44,5 @@ void TLSSOCKET_SEND_REPEAT() TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_send_timeout.cpp b/TESTS/netsocket/tls/tlssocket_send_timeout.cpp index 00b807db824..46f391bfa9b 100644 --- a/TESTS/netsocket/tls/tlssocket_send_timeout.cpp +++ b/TESTS/netsocket/tls/tlssocket_send_timeout.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_SEND_TIMEOUT() { TLSSocket sock; @@ -50,3 +52,5 @@ void TLSSOCKET_SEND_TIMEOUT() TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_send_unconnected.cpp b/TESTS/netsocket/tls/tlssocket_send_unconnected.cpp index ba7f53e04f2..7a7cafe1c1f 100644 --- a/TESTS/netsocket/tls/tlssocket_send_unconnected.cpp +++ b/TESTS/netsocket/tls/tlssocket_send_unconnected.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_SEND_UNCONNECTED() { TLSSocket sock; @@ -32,3 +34,5 @@ void TLSSOCKET_SEND_UNCONNECTED() TEST_ASSERT_EQUAL(NSAPI_ERROR_NO_CONNECTION, sock.send("12345", 5)); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/tls/tlssocket_simultaneous.cpp b/TESTS/netsocket/tls/tlssocket_simultaneous.cpp index a902e188ef8..b2c5c41e13e 100644 --- a/TESTS/netsocket/tls/tlssocket_simultaneous.cpp +++ b/TESTS/netsocket/tls/tlssocket_simultaneous.cpp @@ -24,6 +24,8 @@ using namespace utest::v1; +#if defined(MBEDTLS_SSL_CLI_C) + void TLSSOCKET_SIMULTANEOUS() { TLSSocket sock1; @@ -64,3 +66,5 @@ void TLSSOCKET_SIMULTANEOUS() TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock1.close()); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock2.close()); } + +#endif // defined(MBEDTLS_SSL_CLI_C) diff --git a/TESTS/netsocket/udp/main.cpp b/TESTS/netsocket/udp/main.cpp index 14fac54a568..9f9b7e950ae 100644 --- a/TESTS/netsocket/udp/main.cpp +++ b/TESTS/netsocket/udp/main.cpp @@ -97,6 +97,7 @@ Case cases[] = { Case("UDPSOCKET_ECHOTEST_NONBLOCK", UDPSOCKET_ECHOTEST_NONBLOCK), Case("UDPSOCKET_OPEN_CLOSE_REPEAT", UDPSOCKET_OPEN_CLOSE_REPEAT), Case("UDPSOCKET_OPEN_LIMIT", UDPSOCKET_OPEN_LIMIT), + Case("UDPSOCKET_RECV_TIMEOUT", UDPSOCKET_RECV_TIMEOUT), Case("UDPSOCKET_SENDTO_TIMEOUT", UDPSOCKET_SENDTO_TIMEOUT), Case("UDPSOCKET_OPEN_DESTRUCT", UDPSOCKET_OPEN_DESTRUCT), Case("UDPSOCKET_OPEN_TWICE", UDPSOCKET_OPEN_TWICE),