From 99fc26ecdf0ceae3bdb20d05552be57161bc972a Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Fri, 1 Feb 2019 14:23:22 +0200 Subject: [PATCH] Greentea Socket bind tests made ipv6-aware --- TESTS/netsocket/tcp/main.cpp | 9 +++++++++ TESTS/netsocket/tcp/tcp_tests.h | 1 + .../netsocket/tcp/tcpsocket_bind_address_invalid.cpp | 9 ++++++++- TESTS/netsocket/tcp/tcpsocket_bind_wrong_type.cpp | 9 ++++++++- TESTS/netsocket/udp/main.cpp | 9 +++++++++ TESTS/netsocket/udp/udp_tests.h | 1 + .../netsocket/udp/udpsocket_bind_address_invalid.cpp | 11 ++++++++++- TESTS/netsocket/udp/udpsocket_bind_wrong_type.cpp | 9 ++++++++- 8 files changed, 54 insertions(+), 4 deletions(-) diff --git a/TESTS/netsocket/tcp/main.cpp b/TESTS/netsocket/tcp/main.cpp index 36f453532a..b49e9a9689 100644 --- a/TESTS/netsocket/tcp/main.cpp +++ b/TESTS/netsocket/tcp/main.cpp @@ -57,6 +57,15 @@ void drop_bad_packets(TCPSocket &sock, int orig_timeout) sock.set_timeout(orig_timeout); } +nsapi_version_t get_ip_version() +{ + SocketAddress test; + if (!test.set_ip_address(NetworkInterface::get_default_instance()->get_ip_address())) { + return NSAPI_UNSPEC; + } + return test.get_ip_version(); +} + static void _ifup() { NetworkInterface *net = NetworkInterface::get_default_instance(); diff --git a/TESTS/netsocket/tcp/tcp_tests.h b/TESTS/netsocket/tcp/tcp_tests.h index 8314a30da5..e03f8414ef 100644 --- a/TESTS/netsocket/tcp/tcp_tests.h +++ b/TESTS/netsocket/tcp/tcp_tests.h @@ -20,6 +20,7 @@ NetworkInterface *get_interface(); void drop_bad_packets(TCPSocket &sock, int orig_timeout); +nsapi_version_t get_ip_version(); void fill_tx_buffer_ascii(char *buff, size_t len); nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket &sock); nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket &sock); diff --git a/TESTS/netsocket/tcp/tcpsocket_bind_address_invalid.cpp b/TESTS/netsocket/tcp/tcpsocket_bind_address_invalid.cpp index 4e0c568ce7..933926ede4 100644 --- a/TESTS/netsocket/tcp/tcpsocket_bind_address_invalid.cpp +++ b/TESTS/netsocket/tcp/tcpsocket_bind_address_invalid.cpp @@ -39,7 +39,14 @@ void TCPSOCKET_BIND_ADDRESS_INVALID() return; } TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); - nsapi_error_t bind_result = sock->bind("190.2.3.4", 1024); + nsapi_error_t bind_result = NSAPI_ERROR_OK; + if (get_ip_version() == NSAPI_IPv4) { + bind_result = sock->bind("190.2.3.4", 1024); + } else if (get_ip_version() == NSAPI_IPv6) { + bind_result = sock->bind("fe80::ff01", 1024); + } else { + TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6"); + } if (bind_result == NSAPI_ERROR_UNSUPPORTED) { TEST_IGNORE_MESSAGE("bind() not supported"); } else { diff --git a/TESTS/netsocket/tcp/tcpsocket_bind_wrong_type.cpp b/TESTS/netsocket/tcp/tcpsocket_bind_wrong_type.cpp index be223b0f2c..6a388b5ca6 100644 --- a/TESTS/netsocket/tcp/tcpsocket_bind_wrong_type.cpp +++ b/TESTS/netsocket/tcp/tcpsocket_bind_wrong_type.cpp @@ -40,7 +40,14 @@ void TCPSOCKET_BIND_WRONG_TYPE() } TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); char addr_bytes[16] = {0xfe, 0x80, 0xff, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - SocketAddress sockAddr = SocketAddress(addr_bytes, NSAPI_IPv4, 80); + SocketAddress sockAddr; + if (get_ip_version() == NSAPI_IPv4) { + sockAddr = SocketAddress(addr_bytes, NSAPI_IPv4, 80); + } else if (get_ip_version() == NSAPI_IPv6) { + sockAddr = SocketAddress(addr_bytes, NSAPI_IPv6, 80); + } else { + TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6"); + } nsapi_error_t bind_result = sock->bind(sockAddr); if (bind_result == NSAPI_ERROR_UNSUPPORTED) { TEST_IGNORE_MESSAGE("bind() not supported"); diff --git a/TESTS/netsocket/udp/main.cpp b/TESTS/netsocket/udp/main.cpp index 9f9b7e950a..f8b87e30b7 100644 --- a/TESTS/netsocket/udp/main.cpp +++ b/TESTS/netsocket/udp/main.cpp @@ -64,6 +64,15 @@ void drop_bad_packets(UDPSocket &sock, int orig_timeout) sock.set_timeout(orig_timeout); } +nsapi_version_t get_ip_version() +{ + SocketAddress test; + if (!test.set_ip_address(NetworkInterface::get_default_instance()->get_ip_address())) { + return NSAPI_UNSPEC; + } + return test.get_ip_version(); +} + void fill_tx_buffer_ascii(char *buff, size_t len) { for (size_t i = 0; i < len; ++i) { diff --git a/TESTS/netsocket/udp/udp_tests.h b/TESTS/netsocket/udp/udp_tests.h index b740ada298..34a06670a3 100644 --- a/TESTS/netsocket/udp/udp_tests.h +++ b/TESTS/netsocket/udp/udp_tests.h @@ -20,6 +20,7 @@ NetworkInterface *get_interface(); void drop_bad_packets(UDPSocket &sock, int orig_timeout); +nsapi_version_t get_ip_version(); void fill_tx_buffer_ascii(char *buff, size_t len); #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE diff --git a/TESTS/netsocket/udp/udpsocket_bind_address_invalid.cpp b/TESTS/netsocket/udp/udpsocket_bind_address_invalid.cpp index e0cee8a360..44b0717685 100644 --- a/TESTS/netsocket/udp/udpsocket_bind_address_invalid.cpp +++ b/TESTS/netsocket/udp/udpsocket_bind_address_invalid.cpp @@ -38,7 +38,16 @@ void UDPSOCKET_BIND_ADDRESS_INVALID() TEST_FAIL(); } TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); - nsapi_error_t bind_result = sock->bind("190.2.3.4", 1024); + + nsapi_error_t bind_result = NSAPI_ERROR_OK; + if (get_ip_version() == NSAPI_IPv4) { + bind_result = sock->bind("190.2.3.4", 1024); + } else if (get_ip_version() == NSAPI_IPv6) { + bind_result = sock->bind("fe80::ff01", 1024); + } else { + TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6"); + } + if (bind_result == NSAPI_ERROR_UNSUPPORTED) { TEST_IGNORE_MESSAGE("bind() not supported"); } else { diff --git a/TESTS/netsocket/udp/udpsocket_bind_wrong_type.cpp b/TESTS/netsocket/udp/udpsocket_bind_wrong_type.cpp index 5925af6254..f6a6e7928b 100644 --- a/TESTS/netsocket/udp/udpsocket_bind_wrong_type.cpp +++ b/TESTS/netsocket/udp/udpsocket_bind_wrong_type.cpp @@ -39,7 +39,14 @@ void UDPSOCKET_BIND_WRONG_TYPE() } TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance())); char addr_bytes[16] = {0xfe, 0x80, 0xff, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - SocketAddress sockAddr = SocketAddress(addr_bytes, NSAPI_IPv4, 80); + SocketAddress sockAddr; + if (get_ip_version() == NSAPI_IPv4) { + sockAddr = SocketAddress(addr_bytes, NSAPI_IPv4, 80); + } else if (get_ip_version() == NSAPI_IPv6) { + sockAddr = SocketAddress(addr_bytes, NSAPI_IPv6, 80); + } else { + TEST_FAIL_MESSAGE("This stack is neither IPv4 nor IPv6"); + } nsapi_error_t bind_result = sock->bind(sockAddr); if (bind_result == NSAPI_ERROR_UNSUPPORTED) { TEST_IGNORE_MESSAGE("bind() not supported");