Skip to content

Commit d4cafb5

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

File tree

20 files changed

+2851
-3
lines changed

20 files changed

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

TESTS/net/gethostbyname/main.cpp

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

0 commit comments

Comments
 (0)