Skip to content

Commit beef1d8

Browse files
committed
Add new WiFi tests
The tests try to: * scan for available networks and check whether specified networks are present in the results. * connect to and disconnect from the specified network. * repeats the scan tests while connected to a network. * connect to a network and perform simple HTTP query.
1 parent 7963e8e commit beef1d8

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed

TESTS/network/wifi/main.cpp

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
21+
#include "mbed.h"
22+
23+
using namespace utest::v1;
24+
25+
/**
26+
* WiFi tests require following macros to be defined:
27+
* - WIFI_TEST_SSID - SSID of a network the test will try connecting to
28+
* - WIFI_TEST_PASS - Passphrase that will be used to connecting to the network
29+
* - WIFI_TEST_NETWORKS - List of network that presence will be asserted e.g. "net1", "net2", "net3"
30+
*/
31+
#if !defined(WIFI_TEST_SSID) || !defined(WIFI_TEST_PASS) || !defined(WIFI_TEST_NETWORKS)
32+
#error WIFI_TEST_NETWORKS, WIFI_TEST_PASS and WIFI_TEST_NETWORKS have to be defined for this test.
33+
#endif
34+
35+
const char *networks[] = {WIFI_TEST_NETWORKS, NULL};
36+
37+
/* We use singletons as some of the WiFi modules don't like to be initialized multiple times and using global objects
38+
causes greentea serial to timeout.
39+
*/
40+
#if TARGET_UBLOX_EVK_ODIN_W2
41+
#include "OdinWiFiInterface.h"
42+
SingletonPtr<OdinWiFiInterface> wifi;
43+
#else
44+
#if !TARGET_FF_ARDUINO
45+
#error [NOT_SUPPORTED] Only Arduino form factor devices supported
46+
#endif
47+
#include "ESP8266Interface.h"
48+
ESP8266Interface wifi(D1, D0);
49+
#endif
50+
51+
/* That's a hack to accommodate Odin requiring a singleton */
52+
WiFiInterface *get_wifi()
53+
{
54+
#if TARGET_UBLOX_EVK_ODIN_W2
55+
return wifi.get();
56+
#else
57+
return &wifi;
58+
#endif
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(WIFI_TEST_SSID, WIFI_TEST_PASS, 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(WIFI_TEST_SSID, WIFI_TEST_PASS, 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(WIFI_TEST_SSID, WIFI_TEST_PASS, 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;
178+
}
179+
180+
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),
185+
};
186+
187+
status_t greentea_test_setup(const size_t number_of_cases) {
188+
GREENTEA_SETUP(60, "default_auto");
189+
return greentea_test_setup_handler(number_of_cases);
190+
}
191+
192+
193+
int main() {
194+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
195+
Harness::run(specification);
196+
}

0 commit comments

Comments
 (0)