Skip to content

Commit

Permalink
Make network tests properly support boards with wifi again (#317)
Browse files Browse the repository at this point in the history
* Make network tests properly support boards with wifi again

* Style fixes

* Style fix again
  • Loading branch information
multiplemonomials authored Aug 3, 2024
1 parent 5354079 commit 42df56c
Show file tree
Hide file tree
Showing 80 changed files with 301 additions and 582 deletions.
19 changes: 19 additions & 0 deletions connectivity/netsocket/tests/TESTS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,24 @@ if(DEFAULT_IFC_IDX EQUAL -1)
set(TEST_SKIPPED "No default network interface on this target")
endif()

# Set up variables for wi-fi SSID and password
if("MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE=WIFI" IN_LIST MBED_CONFIG_DEFINITIONS)
set(MBED_GREENTEA_WIFI_SECURE_SSID "" CACHE STRING "SSID of a secured wi-fi network with internet access, for greentea tests which need to connect to wifi")
set(MBED_GREENTEA_WIFI_SECURE_PASSWORD "" CACHE STRING "Password to the network given by MBED_GREENTEA_WIFI_SECURE_SSID")
set(MBED_GREENTEA_WIFI_SECURE_PROTOCOL "WPA2" CACHE STRING "WiFi security protocol, valid values are WEP, WPA, WPA2, WPA_WPA2, WPA3, WPA3_WPA2, NONE")

if("${MBED_GREENTEA_WIFI_SECURE_SSID}" STREQUAL "" OR "${MBED_GREENTEA_WIFI_SECURE_PASSWORD}" STREQUAL "")
message(WARNING "MBED_GREENTEA_WIFI_SECURE_SSID and MBED_GREENTEA_WIFI_SECURE_PASSWORD must be set in order for wi-fi greentea tests to pass")
else()
add_compile_definitions(
"MBED_GREENTEA_WIFI_SECURE_SSID=\"${MBED_GREENTEA_WIFI_SECURE_SSID}\""
"MBED_GREENTEA_WIFI_SECURE_PASSWORD=\"${MBED_GREENTEA_WIFI_SECURE_PASSWORD}\""
MBED_GREENTEA_WIFI_SECURE_PROTOCOL=${MBED_GREENTEA_WIFI_SECURE_PROTOCOL})
endif()
endif()

# Pull in common util header
include_directories(common)

add_subdirectory(netsocket)
add_subdirectory(network)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024 Jamie Smith, 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 MBED_OS_GREENTEA_GET_NETWORK_INTERFACE_H
#define MBED_OS_GREENTEA_GET_NETWORK_INTERFACE_H

#include "WiFiInterface.h"
#include "NetworkInterface.h"

#define WIFI 2
#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \
(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_GREENTEA_WIFI_SECURE_SSID))
#error [NOT_SUPPORTED] No network configuration found for this target.
#endif

/*
* Read the MBED_GREENTEA_WIFI_SECURE_PROTOCOL define from CMake and get the type of wifi
* security in use
*/
inline nsapi_security get_wifi_security()
{
#define JOIN(x, y) JOIN_AGAIN(x, y)
#define JOIN_AGAIN(x, y) x ## y
return JOIN(NSAPI_SECURITY_, MBED_GREENTEA_WIFI_SECURE_PROTOCOL);
#undef JOIN
#undef JOIN_AGAIN
}

/*
* Get the wifi interface for this board, or nullptr if there is none
*/
inline WiFiInterface *get_wifi_interface()
{
auto *const wifi_interface = WiFiInterface::get_default_instance();
if (wifi_interface == nullptr) {
return nullptr;
}

// Set the credentials based on CMake settings so it actually can connect to the internet
wifi_interface->set_credentials(MBED_GREENTEA_WIFI_SECURE_SSID,
MBED_GREENTEA_WIFI_SECURE_PASSWORD,
get_wifi_security());

return wifi_interface;
}

/*
* Get the default network interface for this board.
* For wifi, this also configures in the credentials passed to CMake.
*/
inline NetworkInterface *get_network_interface()
{
#if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI
return get_wifi_interface();
#else
return NetworkInterface::get_default_instance();
#endif

}

#endif //MBED_OS_GREENTEA_GET_NETWORK_INTERFACE_H
75 changes: 4 additions & 71 deletions connectivity/netsocket/tests/TESTS/netsocket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ git checkout master
cd ..
```

Also, building socket test cases requires a special macro to enable all tests, so create an `mbed_app.json` file with the following content at minimum:
To configure the echo server to a non-default IP and port, you can create an mbed_app.json with the following content:

```
{
Expand Down Expand Up @@ -336,77 +336,10 @@ Also, building socket test cases requires a special macro to enable all tests, s
}
```

Wi-Fi tests require some more configuration, so for Wi-Fi purposes, the `mbed_app.json` might look like this:

Wi-Fi tests require configuration of your wifi network, so you might run CMake like this:
```
{
"config": {
"wifi-secure-ssid": {
"help": "WiFi SSID for WPA2 secured network",
"value": "\"test-network\""
},
"wifi-unsecure-ssid": {
"help": "WiFi SSID for unsecure netwrok",
"value": "\"unsecure-test-net\""
},
"wifi-password": {
"help": "WiFi Password",
"value": "\"password\""
},
"wifi-secure-protocol": {
"help": "WiFi security protocol, valid values are WEP, WPA, WPA2, WPA_WPA2",
"value": "\"WPA2\""
},
"wifi-ch-secure": {
"help": "Channel number of secure SSID",
"value": 6
},
"wifi-ch-unsecure": {
"help": "Channel number of unsecure SSID",
"value": 6
},
"ap-mac-secure": {
"help": "BSSID of secure AP in form of AA:BB:CC:DD:EE:FF",
"value": "\"58:8b:f3:99:f2:9c\""
},
"ap-mac-unsecure": {
"help": "BSSID of unsecure AP in form of \"AA:BB:CC:DD:EE:FF\"",
"value": "\"58:8b:f3:99:c2:08\""
},
"max-scan-size": {
"help": "How many networks may appear in Wifi scan result",
"value": 30
},
"echo-server-addr" : {
"help" : "IP address of echo server",
"value" : "\"echo.mbedcloudtesting.com\""
},
"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" : "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": {
"*": {
"target.network-default-interface-type": "WIFI",
"nsapi.default-wifi-ssid": "\"WIFI_SSID\"",
"nsapi.default-wifi-password": "\"WIFI_PASSWORD\"",
"nsapi.default-wifi-security": "WPA_WPA2"
}
}
}
cmake "-DMBED_GREENTEA_WIFI_SECURE_SSID=My Wifi Network Name" -DMBED_GREENTEA_WIFI_SECURE_PASSWORD=MyWifiNetworkPassword
-DMBED_GREENTEA_WIFI_SECURE_CHANNEL=5
```

Cellular tests require some more configuration, so for cellular purposes, the `mbed_app.json` might look like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void do_getaddrinfo_async(const char hosts[][DNS_TEST_HOST_LEN], unsigned int op
unsigned int count = 0;
for (unsigned int i = 0; i < op_count; i++) {
data[i].semaphore = &semaphore;
nsapi_error_t err = NetworkInterface::get_default_instance()->getaddrinfo_async(hosts[i], &hints, mbed::Callback<void(nsapi_error_t, SocketAddress *)>(getaddrinfo_cb, (void *) &data[i]));
nsapi_error_t err = get_network_interface()->getaddrinfo_async(hosts[i], &hints, mbed::Callback<void(nsapi_error_t, SocketAddress *)>(getaddrinfo_cb, (void *) &data[i]));
TEST_ASSERT(err >= 0 || err == NSAPI_ERROR_NO_MEMORY || err == NSAPI_ERROR_BUSY);
if (err >= 0) {
// Callback will be called
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void ASYNCHRONOUS_DNS_TIMEOUTS()
nsapi_error_t result;
int count = MAX_TRIAL_ATTEMPTS;
do {
result = NetworkInterface::get_default_instance()->gethostbyname(dns_test_hosts[0], &address);
result = get_network_interface()->gethostbyname(dns_test_hosts[0], &address);
if (result == NSAPI_ERROR_OK) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "nsapi_dns.h"
#include "mbed_trace.h"
#include "greentea_get_network_interface.h"

#define TRACE_GROUP "GRNT"

Expand Down
12 changes: 2 additions & 10 deletions connectivity/netsocket/tests/TESTS/netsocket/dns/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
#if !defined(MBED_CONF_RTOS_PRESENT)
#error [NOT_SUPPORTED] dns test cases require a RTOS to run.
#else

#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.
#else

#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
Expand Down Expand Up @@ -167,7 +160,7 @@ static void net_bringup()
nsapi_dns_reset();
MBED_ASSERT(MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_NSAPI_DNS_CACHE_SIZE && MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_APP_DNS_SIMULT_QUERIES + 1);

net = NetworkInterface::get_default_instance();
net = get_network_interface();
TEST_ASSERT_NOT_NULL_MESSAGE(net, "No NetworkInterface configured");
nsapi_error_t err = net->connect();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
Expand All @@ -186,7 +179,7 @@ static void net_bringup()

static void net_bringdown()
{
NetworkInterface::get_default_instance()->disconnect();
get_network_interface()->disconnect();
tr_info("MBED: ifdown");
}

Expand Down Expand Up @@ -238,5 +231,4 @@ int main()
return !Harness::run(specification);
}

#endif // !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
#endif // !defined(MBED_CONF_RTOS_PRESENT)
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void do_getaddrinfo(const char hosts[][DNS_TEST_HOST_LEN], unsigned int op_count
SocketAddress hints{{NSAPI_UNSPEC}, 80};
for (unsigned int i = 0; i < op_count; i++) {
SocketAddress *result;
nsapi_error_t err = NetworkInterface::get_default_instance()->get_default_instance()->getaddrinfo(hosts[i], &hints, &result);
nsapi_error_t err = get_network_interface()->getaddrinfo(hosts[i], &hints, &result);

if (err == NSAPI_ERROR_DNS_FAILURE) {
(*exp_dns_failure)++;
Expand Down
19 changes: 6 additions & 13 deletions connectivity/netsocket/tests/TESTS/netsocket/tcp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
#error [NOT_SUPPORTED] tcp test cases require a RTOS to run
#else

#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.
#else

#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity/unity.h"
Expand Down Expand Up @@ -66,15 +60,15 @@ void drop_bad_packets(TCPSocket &sock, int orig_timeout)
nsapi_version_t get_ip_version()
{
SocketAddress test;
if (NetworkInterface::get_default_instance()->get_ip_address(&test) != NSAPI_ERROR_OK) {
if (get_network_interface()->get_ip_address(&test) != NSAPI_ERROR_OK) {
return NSAPI_UNSPEC;
}
return test.get_ip_version();
}

static void _ifup()
{
NetworkInterface *net = NetworkInterface::get_default_instance();
NetworkInterface *net = get_network_interface();
TEST_ASSERT_NOT_NULL_MESSAGE(net, "No NetworkInterface configured");
nsapi_error_t err = net->connect();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
Expand All @@ -93,20 +87,20 @@ static void _ifup()

static void _ifdown()
{
NetworkInterface::get_default_instance()->disconnect();
get_network_interface()->disconnect();
tr_info("MBED: ifdown");
}

nsapi_error_t tcpsocket_connect_to_srv(TCPSocket &sock, uint16_t port)
{
SocketAddress tcp_addr;

NetworkInterface::get_default_instance()->gethostbyname(ECHO_SERVER_ADDR, &tcp_addr);
get_network_interface()->gethostbyname(ECHO_SERVER_ADDR, &tcp_addr);
tcp_addr.set_port(port);

tr_info("MBED: Server '%s', port %d", tcp_addr.get_ip_address(), tcp_addr.get_port());

nsapi_error_t err = sock.open(NetworkInterface::get_default_instance());
nsapi_error_t err = sock.open(get_network_interface());
if (err != NSAPI_ERROR_OK) {
tr_error("Error from sock.open: %d", err);
return err;
Expand Down Expand Up @@ -137,7 +131,7 @@ bool is_tcp_supported()
static bool tested = false;
if (!tested) {
TCPSocket socket;
supported = socket.open(NetworkInterface::get_default_instance()) == NSAPI_ERROR_OK;
supported = socket.open(get_network_interface()) == NSAPI_ERROR_OK;
}
return supported;
}
Expand Down Expand Up @@ -253,5 +247,4 @@ int main()
}

#endif // ECHO_SERVER_ADDR
#endif // !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
#endif // !defined(MBED_CONF_RTOS_PRESENT)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "../test_params.h"
#include "mbed_trace.h"
#include "greentea_get_network_interface.h"

#define TRACE_GROUP "GRNT"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void TCPSOCKET_BIND_ADDRESS()
TEST_FAIL();
return;
}
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(get_network_interface()));
SocketAddress sockAddr;
NetworkInterface::get_default_instance()->get_ip_address(&sockAddr);
get_network_interface()->get_ip_address(&sockAddr);
sockAddr.set_port(80);
nsapi_error_t bind_result = sock->bind(sockAddr);
if (bind_result == NSAPI_ERROR_UNSUPPORTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void TCPSOCKET_BIND_ADDRESS_INVALID()
SKIP_IF_TCP_UNSUPPORTED();
TCPSocket sock;

TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_network_interface()));
nsapi_error_t bind_result = NSAPI_ERROR_OK;
if (get_ip_version() == NSAPI_IPv4) {
SocketAddress a("190.2.3.4", 1024);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void TCPSOCKET_BIND_PORT()
TEST_FAIL();
return;
}
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(get_network_interface()));
nsapi_error_t bind_result = sock->bind(1024);
if (bind_result == NSAPI_ERROR_UNSUPPORTED) {
TEST_IGNORE_MESSAGE("bind() not supported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void TCPSOCKET_BIND_PORT_FAIL()
TEST_FAIL();
return;
}
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(get_network_interface()));
nsapi_error_t bind_result = sock->bind(1024);
if (bind_result == NSAPI_ERROR_UNSUPPORTED) {
TEST_IGNORE_MESSAGE("bind() not supported");
Expand All @@ -47,7 +47,7 @@ void TCPSOCKET_BIND_PORT_FAIL()
if (!sock2) {
TEST_FAIL();
}
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock2->open(NetworkInterface::get_default_instance()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock2->open(get_network_interface()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, sock2->bind(1024));

delete sock;
Expand Down
Loading

0 comments on commit 42df56c

Please sign in to comment.