Skip to content

Commit 62f25bd

Browse files
committed
Added the network tests from mbed OS
1 parent e47620e commit 62f25bd

File tree

20 files changed

+2929
-3
lines changed

20 files changed

+2929
-3
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
# esp8266 mbed-os driver
2-
The driver for the ESP8266 WiFi module
1+
# The ESP8266 WiFi driver for mbed-os
2+
The mbed OS driver for the ESP8266 WiFi module
33

44
## Firmware version
5-
esp8266 modules come in different shapes and forms, but most important difference is which firmware version it is programmed with. To make sure that your module has mbed-os compatible firmware follow update guide: https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update
5+
ESP8266 modules come in different shapes and forms, but most important difference is which firmware version it is programmed with. To make sure that your module has mbed-os compatible firmware follow update guide: https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update
6+
7+
## Testing
8+
The ESP8266 library contains the core network tests taken from mbed OS. After installing mbed CLI and importing the mbed OS library, the tests can be ran with the `mbed test` command:
9+
``` bash
10+
# Runs the ESP8266 network tests, requires a wifi access point
11+
mbed test -t <COMPILER HERE> -m <BOARD HERE> -n tests-net* --compile -DMBED_CFG_ESP8266_SSID='"<SSID HERE>"' -DMBED_CFG_ESP8266_PASS='"<PASS HERE>"'
12+
mbed test -t <COMPILER HERE> -m <BOARD HERE> -n tests-net* --run --verbose
13+
```

TESTS/net/.mbedignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
host_tests/*

TESTS/net/connectivity/main.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#if !FEATURE_LWIP
2+
#error [NOT_SUPPORTED] LWIP not supported for this target
3+
#endif
4+
#if DEVICE_EMAC
5+
#error [NOT_SUPPORTED] Not supported for WiFi targets
6+
#endif
7+
8+
#include "mbed.h"
9+
#include "greentea-client/test_env.h"
10+
#include "unity.h"
11+
#include "utest.h"
12+
13+
#include "ESP8266Interface.h"
14+
15+
using namespace utest::v1;
16+
17+
#ifndef MBED_CFG_ESP8266_TX
18+
#define MBED_CFG_ESP8266_TX D1
19+
#endif
20+
21+
#ifndef MBED_CFG_ESP8266_RX
22+
#define MBED_CFG_ESP8266_RX D0
23+
#endif
24+
25+
#ifndef MBED_CFG_ESP8266_DEBUG
26+
#define MBED_CFG_ESP8266_DEBUG false
27+
#endif
28+
29+
30+
// Bringing the network up and down
31+
template <int COUNT>
32+
void test_bring_up_down() {
33+
ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
34+
net.set_credentials(MBED_CFG_ESP8266_SSID, MBED_CFG_ESP8266_PASS);
35+
36+
for (int i = 0; i < COUNT; i++) {
37+
int err = net.connect();
38+
TEST_ASSERT_EQUAL(0, err);
39+
40+
printf("MBED: IP Address %s\r\n", net.get_ip_address());
41+
printf("MBED: Netmask %s\r\n", net.get_netmask());
42+
printf("MBED: Gateway %s\r\n", net.get_gateway());
43+
TEST_ASSERT(net.get_ip_address());
44+
TEST_ASSERT(net.get_netmask());
45+
TEST_ASSERT(net.get_gateway());
46+
47+
UDPSocket udp;
48+
err = udp.open(&net);
49+
TEST_ASSERT_EQUAL(0, err);
50+
err = udp.close();
51+
TEST_ASSERT_EQUAL(0, err);
52+
53+
TCPSocket tcp;
54+
err = tcp.open(&net);
55+
TEST_ASSERT_EQUAL(0, err);
56+
err = tcp.close();
57+
TEST_ASSERT_EQUAL(0, err);
58+
59+
err = net.disconnect();
60+
TEST_ASSERT_EQUAL(0, err);
61+
}
62+
}
63+
64+
65+
// Test setup
66+
utest::v1::status_t test_setup(const size_t number_of_cases) {
67+
char uuid[48] = {0};
68+
GREENTEA_SETUP_UUID(120, "default_auto", uuid, sizeof(uuid));
69+
70+
// create mac address based on uuid
71+
uint64_t mac = 0;
72+
for (int i = 0; i < sizeof(uuid); i++) {
73+
mac += uuid[i];
74+
}
75+
mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1);
76+
77+
return verbose_test_setup_handler(number_of_cases);
78+
}
79+
80+
Case cases[] = {
81+
Case("Bringing the network up and down", test_bring_up_down<1>),
82+
Case("Bringing the network up and down twice", test_bring_up_down<2>),
83+
};
84+
85+
Specification specification(test_setup, cases);
86+
87+
int main() {
88+
return !Harness::run(specification);
89+
}

TESTS/net/gethostbyname/main.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#if !FEATURE_LWIP
2+
#error [NOT_SUPPORTED] LWIP not supported for this target
3+
#endif
4+
#if DEVICE_EMAC
5+
#error [NOT_SUPPORTED] Not supported for WiFi targets
6+
#endif
7+
8+
#include "mbed.h"
9+
#include "greentea-client/test_env.h"
10+
#include "unity.h"
11+
#include "utest.h"
12+
#include "ESP8266Interface.h"
13+
14+
using namespace utest::v1;
15+
16+
// Hostname for testing against
17+
// Must have A and AAAA records
18+
#ifndef MBED_DNS_TEST_HOST
19+
#define MBED_DNS_TEST_HOST "connector.mbed.com"
20+
#endif
21+
22+
#ifndef MBED_CFG_ESP8266_TX
23+
#define MBED_CFG_ESP8266_TX D1
24+
#endif
25+
26+
#ifndef MBED_CFG_ESP8266_RX
27+
#define MBED_CFG_ESP8266_RX D0
28+
#endif
29+
30+
#ifndef MBED_CFG_ESP8266_DEBUG
31+
#define MBED_CFG_ESP8266_DEBUG false
32+
#endif
33+
34+
// Address info from stack
35+
const char *ip_literal;
36+
nsapi_version_t ip_pref;
37+
const char *ip_pref_repr;
38+
39+
// Network setup
40+
ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
41+
void net_bringup() {
42+
int err = net.connect(MBED_CFG_ESP8266_SSID, MBED_CFG_ESP8266_PASS);
43+
TEST_ASSERT_EQUAL(0, err);
44+
printf("MBED: Connected to network\n");
45+
printf("MBED: IP Address: %s\n", net.get_ip_address());
46+
47+
ip_literal = net.get_ip_address();
48+
ip_pref = SocketAddress(ip_literal).get_ip_version();
49+
ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" :
50+
(ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec";
51+
}
52+
53+
54+
// DNS tests
55+
void test_dns_query() {
56+
SocketAddress addr;
57+
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr);
58+
printf("DNS: query \"%s\" => \"%s\"\n",
59+
MBED_DNS_TEST_HOST, addr.get_ip_address());
60+
61+
TEST_ASSERT_EQUAL(0, err);
62+
TEST_ASSERT((bool)addr);
63+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
64+
}
65+
66+
void test_dns_query_pref() {
67+
SocketAddress addr;
68+
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr, ip_pref);
69+
printf("DNS: query %s \"%s\" => \"%s\"\n",
70+
ip_pref_repr, MBED_DNS_TEST_HOST, addr.get_ip_address());
71+
72+
TEST_ASSERT_EQUAL(0, err);
73+
TEST_ASSERT((bool)addr);
74+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
75+
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
76+
}
77+
78+
void test_dns_literal() {
79+
SocketAddress addr;
80+
int err = net.gethostbyname(ip_literal, &addr);
81+
printf("DNS: literal \"%s\" => \"%s\"\n",
82+
ip_literal, addr.get_ip_address());
83+
84+
TEST_ASSERT_EQUAL(0, err);
85+
TEST_ASSERT((bool)addr);
86+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
87+
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
88+
}
89+
90+
void test_dns_literal_pref() {
91+
SocketAddress addr;
92+
int err = net.gethostbyname(ip_literal, &addr, ip_pref);
93+
printf("DNS: literal %s \"%s\" => \"%s\"\n",
94+
ip_pref_repr, ip_literal, addr.get_ip_address());
95+
96+
TEST_ASSERT_EQUAL(0, err);
97+
TEST_ASSERT((bool)addr);
98+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
99+
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
100+
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
101+
}
102+
103+
104+
// Test setup
105+
utest::v1::status_t test_setup(const size_t number_of_cases) {
106+
char uuid[48] = {0};
107+
GREENTEA_SETUP_UUID(120, "default_auto", uuid, 48);
108+
109+
// create mac address based on uuid
110+
uint64_t mac = 0;
111+
for (int i = 0; i < sizeof(uuid); i++) {
112+
mac += uuid[i];
113+
}
114+
mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1);
115+
net_bringup();
116+
117+
return verbose_test_setup_handler(number_of_cases);
118+
}
119+
120+
Case cases[] = {
121+
Case("DNS query", test_dns_query),
122+
Case("DNS preference query", test_dns_query_pref),
123+
Case("DNS literal", test_dns_literal),
124+
Case("DNS preference literal", test_dns_literal_pref),
125+
};
126+
127+
Specification specification(test_setup, cases);
128+
129+
int main() {
130+
return !Harness::run(specification);
131+
}

0 commit comments

Comments
 (0)