Skip to content

Commit 3d87507

Browse files
author
Seppo Takalo
committed
Implement functional Wifi tests
Implement 100% function coverage for WifiInterface as specified in "Wifi test plan"
1 parent 373e6ab commit 3d87507

19 files changed

+435
-184
lines changed

TESTS/network/wifi/get_interface.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "mbed.h"
2+
#include "ESP8266Interface.h"
3+
4+
WiFiInterface *get_interface()
5+
{
6+
static WiFiInterface *interface = NULL;
7+
8+
if (interface)
9+
delete interface;
10+
11+
#if TARGET_UBLOX_EVK_ODIN_W2
12+
#include "OdinWiFiInterface.h"
13+
interface = new OdinWiFiInterface();
14+
#elif TARGET_REALTEK_RTL8195AM
15+
#include "RTWInterface.h"
16+
interface = new RTWInterface();
17+
#else
18+
interface = new ESP8266Interface(D1, D0);
19+
#endif
20+
return interface;
21+
}

TESTS/network/wifi/main.cpp

Lines changed: 27 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,196 +1,39 @@
1-
/* mbed Microcontroller Library
2-
* Copyright (c) 2016 ARM Limited
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
#include "utest/utest.h"
18-
#include "unity/unity.h"
19-
#include "greentea-client/test_env.h"
20-
211
#include "mbed.h"
22-
23-
#if TARGET_UBLOX_EVK_ODIN_W2
24-
#include "OdinWiFiInterface.h"
25-
#else
26-
#error [NOT_SUPPORTED] Only built in WiFi modules are supported at this time.
27-
#endif
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
286

297
using namespace utest::v1;
308

31-
/**
32-
* WiFi tests require following macros to be defined:
33-
* - MBED_CONF_APP_WIFI_SSID - SSID of a network the test will try connecting to
34-
* - MBED_CONF_APP_WIFI_PASSWORD - Passphrase that will be used to connecting to the network
35-
* - WIFI_TEST_NETWORKS - List of network that presence will be asserted e.g. "net1", "net2", "net3"
36-
*/
37-
#if !defined(MBED_CONF_APP_WIFI_SSID) || !defined(MBED_CONF_APP_WIFI_PASSWORD) || !defined(MBED_CONF_APP_WIFI_NETWORKS)
38-
#error [NOT_SUPPORTED] MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD and MBED_CONF_APP_WIFI_NETWORKS have to be defined for this test.
39-
#endif
40-
41-
const char *networks[] = {MBED_CONF_APP_WIFI_NETWORKS, NULL};
42-
43-
WiFiInterface *wifi;
44-
45-
/* In normal circumstances the WiFi object could be global, but the delay introduced by WiFi initialization is an issue
46-
for the tests. It causes Greentea to timeout on syncing with the board. To solve it we defer the actual object
47-
creation till we actually need it.
48-
*/
49-
WiFiInterface *get_wifi()
50-
{
51-
if (wifi == NULL) {
52-
/* We don't really care about freeing this, as its lifetime is through the full test suit run. */
53-
#if TARGET_UBLOX_EVK_ODIN_W2
54-
wifi = new OdinWiFiInterface;
55-
#endif
56-
}
57-
58-
return wifi;
59-
}
60-
61-
void check_wifi(const char *ssid, bool *net_stat)
62-
{
63-
int i = 0;
64-
while(networks[i]) {
65-
if (strcmp(networks[i], ssid) == 0) {
66-
net_stat[i] = true;
67-
break;
68-
}
69-
i++;
70-
}
71-
}
72-
73-
void wifi_scan()
74-
{
75-
int count;
76-
WiFiAccessPoint *aps;
77-
const int net_len = sizeof(networks)/sizeof(networks[0]);
78-
bool net_stat[net_len - 1];
79-
80-
memset(net_stat, 0, sizeof(net_stat));
81-
82-
count = get_wifi()->scan(NULL, 0);
83-
TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error");
84-
TEST_ASSERT_MESSAGE(count > 0, "Scan result empty");
85-
86-
aps = new WiFiAccessPoint[count];
87-
count = get_wifi()->scan(aps, count);
88-
for(int i = 0; i < count; i++) {
89-
check_wifi(aps[i].get_ssid(), net_stat);
90-
}
91-
92-
delete[] aps;
93-
94-
for (unsigned i = 0; i < sizeof(net_stat); i++) {
95-
TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected");
96-
}
97-
}
98-
99-
void wifi_connect()
100-
{
101-
int ret;
102-
103-
ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
104-
TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
105-
106-
ret = get_wifi()->disconnect();
107-
TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
108-
}
109-
110-
void wifi_connect_scan()
111-
{
112-
int ret;
113-
int count;
114-
WiFiAccessPoint *aps;
115-
const int net_len = sizeof(networks)/sizeof(networks[0]);
116-
bool net_stat[net_len - 1];
117-
118-
memset(net_stat, 0, sizeof(net_stat));
119-
120-
ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
121-
TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
122-
123-
count = get_wifi()->scan(NULL, 0);
124-
TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error");
125-
TEST_ASSERT_MESSAGE(count > 0, "Scan result empty");
126-
127-
aps = new WiFiAccessPoint[count];
128-
count = get_wifi()->scan(aps, count);
129-
for(int i = 0; i < count; i++) {
130-
check_wifi(aps[i].get_ssid(), net_stat);
131-
}
132-
133-
delete[] aps;
134-
135-
ret = get_wifi()->disconnect();
136-
TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
137-
138-
for (unsigned i = 0; i < sizeof(net_stat); i++) {
139-
TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected");
140-
}
141-
}
142-
143-
void wifi_http()
144-
{
145-
TCPSocket socket;
146-
int ret;
147-
148-
ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
149-
TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
150-
151-
// Open a socket on the network interface, and create a TCP connection to www.arm.com
152-
ret = socket.open(get_wifi());
153-
TEST_ASSERT_MESSAGE(ret == 0, "Socket open failed");
154-
ret = socket.connect("www.arm.com", 80);
155-
TEST_ASSERT_MESSAGE(ret == 0, "Socket connect failed");
156-
157-
// Send a simple http request
158-
char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
159-
int scount = socket.send(sbuffer, sizeof sbuffer);
160-
TEST_ASSERT_MESSAGE(scount >= 0, "Socket send failed");
161-
162-
// Recieve a simple http response and check if it's not empty
163-
char rbuffer[64];
164-
int rcount = socket.recv(rbuffer, sizeof rbuffer);
165-
TEST_ASSERT_MESSAGE(rcount >= 0, "Socket recv error");
166-
TEST_ASSERT_MESSAGE(rcount > 0, "No data received");
167-
168-
ret = socket.close();
169-
TEST_ASSERT_MESSAGE(ret == 0, "Socket close failed");
170-
171-
ret = get_wifi()->disconnect();
172-
TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
173-
}
174-
175-
status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
176-
greentea_case_failure_abort_handler(source, reason);
177-
return STATUS_CONTINUE;
9+
utest::v1::status_t test_setup(const size_t number_of_cases) {
10+
GREENTEA_SETUP(120, "default_auto");
11+
return verbose_test_setup_handler(number_of_cases);
17812
}
17913

14+
// Test cases
18015
Case cases[] = {
181-
Case("Scan test", wifi_scan, greentea_failure_handler),
182-
Case("Connect test", wifi_connect, greentea_failure_handler),
183-
Case("Scan while connected test", wifi_connect_scan, greentea_failure_handler),
184-
Case("HTTP test", wifi_http, greentea_failure_handler),
16+
Case("WIFI-CONSTRUCTOR", wifi_constructor),
17+
Case("WIFI-SET-CREDENTIAL", wifi_set_credential),
18+
Case("WIFI-SET-CHANNEL", wifi_set_channel),
19+
Case("WIFI-GET-RSSI", wifi_get_rssi),
20+
Case("WIFI-CONNECT-PARAMS-NULL", wifi_connect_params_null),
21+
Case("WIFI-CONNECT-PARAMS-VALID-UNSECURE", wifi_connect_params_valid_unsecure),
22+
Case("WIFI-CONNECT-PARAMS-VALID-SECURE", wifi_connect_params_valid_secure),
23+
Case("WIFI-CONNECT-PARAMS-CHANNEL", wifi_connect_params_channel),
24+
Case("WIFI-CONNECT-PARAMS-CHANNEL-FAIL", wifi_connect_params_channel_fail),
25+
Case("WIFI-CONNECT-NOCREDENTIALS", wifi_connect_nocredentials),
26+
Case("WIFI-CONNECT", wifi_connect),
27+
Case("WIFI-CONNECT-SECURE", wifi_connect_secure),
28+
Case("WIFI-CONNECT-SECURE-FAIL", wifi_connect_secure_fail),
29+
Case("WIFI-CONNECT-DISCONNECT-REPEAT", wifi_connect_disconnect_repeat),
30+
Case("WIFI-SCAN-NULL", wifi_scan_null),
31+
Case("WIFI-SCAN", wifi_scan),
18532
};
18633

187-
status_t greentea_test_setup(const size_t number_of_cases) {
188-
GREENTEA_SETUP(90, "default_auto");
189-
return greentea_test_setup_handler(number_of_cases);
190-
}
191-
34+
Specification specification(test_setup, cases);
19235

36+
// Entry point into the tests
19337
int main() {
194-
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
195-
Harness::run(specification);
38+
return !Harness::run(specification);
19639
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_constructor() {
10+
WiFiInterface *wifi = get_interface();
11+
TEST_ASSERT(wifi);
12+
}

TESTS/network/wifi/wifi_connect.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_connect(void)
10+
{
11+
WiFiInterface *wifi = get_interface();
12+
13+
TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, wifi->set_credentials(MBED_CONF_APP_WIFI_UNSECURE_SSID, NULL));
14+
15+
TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, wifi->connect());
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_connect_disconnect_repeat(void)
10+
{
11+
WiFiInterface *wifi = get_interface();
12+
nsapi_error_t error;
13+
14+
error = wifi->set_credentials(MBED_CONF_APP_WIFI_UNSECURE_SSID, NULL);
15+
TEST_ASSERT(error == NSAPI_ERROR_OK);
16+
17+
for(int i=0; i<10; i++) {
18+
error = wifi->connect();
19+
TEST_ASSERT(error == NSAPI_ERROR_OK);
20+
error = wifi->disconnect();
21+
TEST_ASSERT(error == NSAPI_ERROR_OK);
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_connect_nocredentials(void)
10+
{
11+
WiFiInterface *wifi = get_interface();
12+
nsapi_error_t error;
13+
error = wifi->connect();
14+
wifi->disconnect();
15+
TEST_ASSERT(error == NSAPI_ERROR_PARAMETER);
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_connect_params_channel(void)
10+
{
11+
WiFiInterface *wifi = get_interface();
12+
13+
if (wifi->set_channel(1) == NSAPI_ERROR_UNSUPPORTED && wifi->set_channel(36) == NSAPI_ERROR_UNSUPPORTED) {
14+
TEST_IGNORE_MESSAGE("set_channel() not supported");
15+
return;
16+
}
17+
18+
nsapi_error_t error = wifi->connect(MBED_CONF_APP_WIFI_SECURE_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA2, MBED_CONF_APP_WIFI_CH_SECURE);
19+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, error);
20+
}
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_connect_params_channel_fail(void)
10+
{
11+
WiFiInterface *wifi = get_interface();
12+
13+
if (wifi->set_channel(1) == NSAPI_ERROR_UNSUPPORTED && wifi->set_channel(36) == NSAPI_ERROR_UNSUPPORTED) {
14+
TEST_IGNORE_MESSAGE("set_channel() not supported");
15+
return;
16+
}
17+
18+
nsapi_error_t error = wifi->connect(MBED_CONF_APP_WIFI_SECURE_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA2, MBED_CONF_APP_WIFI_CH_UNSECURE);
19+
TEST_ASSERT(error==NSAPI_ERROR_CONNECTION_TIMEOUT || error==NSAPI_ERROR_NO_CONNECTION);
20+
}
21+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_connect_params_null(void)
10+
{
11+
WiFiInterface *wifi = get_interface();
12+
TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_PARAMETER, wifi->connect(NULL, NULL));
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "wifi_tests.h"
6+
7+
using namespace utest::v1;
8+
9+
void wifi_connect_params_valid_secure(void)
10+
{
11+
WiFiInterface *wifi = get_interface();
12+
TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, wifi->connect(MBED_CONF_APP_WIFI_SECURE_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA2));
13+
}

0 commit comments

Comments
 (0)