1 #include <stdio.h>
2 #include <string.h>
3 #include "unity.h"
4 #include "unity_fixture.h"
5 #include "esp_netif.h"
6 #include "esp_wifi.h"
7 #include "nvs_flash.h"
8 #include "esp_wifi_netif.h"
9 #include "sdkconfig.h"
10 #include "lwip/sockets.h"
11 #include "test_utils.h"
12
13
14 TEST_GROUP(esp_netif);
15
TEST_SETUP(esp_netif)16 TEST_SETUP(esp_netif)
17 {
18 }
19
TEST_TEAR_DOWN(esp_netif)20 TEST_TEAR_DOWN(esp_netif)
21 {
22 }
23
TEST(esp_netif,init_and_destroy)24 TEST(esp_netif, init_and_destroy)
25 {
26 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
27 esp_netif_t *esp_netif = esp_netif_new(NULL);
28
29 TEST_ASSERT_EQUAL(NULL, esp_netif);
30 esp_netif = esp_netif_new(&cfg);
31 TEST_ASSERT_NOT_EQUAL(NULL, esp_netif);
32
33 esp_netif_destroy(esp_netif);
34 }
35
36
TEST(esp_netif,get_from_if_key)37 TEST(esp_netif, get_from_if_key)
38 {
39 // init default netif
40 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
41 esp_netif_t *esp_netif = esp_netif_new(&cfg);
42 TEST_ASSERT_NOT_NULL(esp_netif);
43
44 // check it's accessible by key
45 TEST_ASSERT_EQUAL(esp_netif, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
46
47 // destroy it
48 esp_netif_destroy(esp_netif);
49
50 // check it's also destroyed in list
51 TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
52
53 }
54
55
TEST(esp_netif,create_delete_multiple_netifs)56 TEST(esp_netif, create_delete_multiple_netifs)
57 {
58 // interface key has to be a unique identifier
59 const char* if_keys[] = { "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9" };
60 const int nr_of_netifs = sizeof(if_keys)/sizeof(char*);
61 esp_netif_t *netifs[nr_of_netifs];
62
63 // create 10 wifi stations
64 for (int i=0; i<nr_of_netifs; ++i) {
65 esp_netif_inherent_config_t base_netif_config = { .if_key = if_keys[i]};
66 esp_netif_config_t cfg = { .base = &base_netif_config, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
67 netifs[i] = esp_netif_new(&cfg);
68 TEST_ASSERT_NOT_NULL(netifs[i]);
69 }
70
71 // there's no AP within created stations
72 TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"));
73
74 // destroy
75 for (int i=0; i<nr_of_netifs; ++i) {
76 esp_netif_destroy(netifs[i]);
77 }
78
79 }
80
TEST(esp_netif,dhcp_client_state_transitions_wifi_sta)81 TEST(esp_netif, dhcp_client_state_transitions_wifi_sta)
82 {
83 // init default wifi netif
84 test_case_uses_tcpip();
85 TEST_ESP_OK(nvs_flash_init());
86 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
87 esp_netif_t *sta = esp_netif_new(&cfg);
88 TEST_ASSERT_NOT_NULL(sta);
89 esp_netif_attach_wifi_station(sta);
90 wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
91 TEST_ESP_OK(esp_wifi_init(&wifi_cfg));
92
93 esp_netif_dhcp_status_t state;
94
95 // testing DHCP states per netif state transitions
96 esp_netif_action_start(sta, NULL, 0, NULL);
97 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
98
99 TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state);
100 esp_netif_action_connected(sta, NULL, 0, NULL);
101 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
102
103 TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
104 esp_netif_action_stop(sta, NULL, 0, NULL);
105 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
106
107 TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state);
108
109 // destroy default wifi netif
110 esp_netif_destroy(sta);
111 TEST_ASSERT(esp_wifi_stop() == ESP_OK);
112 TEST_ASSERT(esp_wifi_deinit() == ESP_OK);
113 nvs_flash_deinit();
114 }
115
TEST(esp_netif,dhcp_server_state_transitions_wifi_ap)116 TEST(esp_netif, dhcp_server_state_transitions_wifi_ap)
117 {
118 // init default wifi netif
119 test_case_uses_tcpip();
120 TEST_ESP_OK(nvs_flash_init());
121 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
122 esp_netif_t *ap = esp_netif_new(&cfg);
123 TEST_ASSERT_NOT_NULL(ap);
124 esp_netif_attach_wifi_station(ap);
125 wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
126 TEST_ESP_OK(esp_wifi_init(&wifi_cfg));
127
128 esp_netif_dhcp_status_t state;
129
130 // testing DHCP server states per netif state transitions
131 esp_netif_action_start(ap, NULL, 0, NULL);
132 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state));
133 TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
134
135 esp_netif_action_stop(ap, NULL, 0, NULL);
136 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state));
137 TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state);
138
139 // destroy default wifi netif
140 esp_netif_destroy(ap);
141 TEST_ASSERT(esp_wifi_stop() == ESP_OK);
142 TEST_ASSERT(esp_wifi_deinit() == ESP_OK);
143 nvs_flash_deinit();
144 }
145
TEST(esp_netif,dhcp_server_state_transitions_mesh)146 TEST(esp_netif, dhcp_server_state_transitions_mesh)
147 {
148 esp_netif_t *ap = NULL;
149 esp_netif_t *sta = NULL;
150 esp_netif_dhcp_status_t state;
151
152 // init two mesh network interfaces
153 test_case_uses_tcpip();
154 TEST_ESP_OK(nvs_flash_init());
155 TEST_ESP_OK(esp_event_loop_create_default());
156 TEST_ESP_OK(esp_netif_create_default_wifi_mesh_netifs(&sta, &ap));
157 TEST_ASSERT_NOT_NULL(sta);
158 TEST_ASSERT_NOT_NULL(ap);
159 wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
160 TEST_ESP_OK(esp_wifi_init(&wifi_cfg));
161
162 // test both server and client are *not* STARTED after interfaces created
163 TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
164 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
165 TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state));
166 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
167
168 // test both server and client are still *not* STARTED after start
169 esp_netif_action_start(ap, NULL, 0, NULL);
170 esp_netif_action_start(sta, NULL, 0, NULL);
171 TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
172 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
173 TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state));
174 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
175
176 // test both server and client are still *not* STARTED even after connect
177 esp_netif_action_connected(ap, NULL, 0, NULL);
178 esp_netif_action_connected(sta, NULL, 0, NULL);
179 TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
180 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
181 TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state));
182 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
183
184 // test station gets promoted to be a root (so DHCP client started manually) and client is in STATED state
185 esp_netif_dhcpc_start(sta);
186 esp_netif_action_connected(sta, NULL, 0, NULL);
187 TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
188 TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
189 esp_netif_dhcpc_stop(sta);
190
191 // test both server and client are still *not* STARTED even after stop
192 esp_netif_action_stop(sta, NULL, 0, NULL);
193 esp_netif_action_stop(ap, NULL, 0, NULL);
194 TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
195 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
196 TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state));
197 TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
198
199 // destroy event_loop, netifs, wifi, nvs
200 TEST_ESP_OK(esp_event_loop_delete_default());
201 esp_netif_destroy(ap);
202 esp_netif_destroy(sta);
203 TEST_ASSERT(esp_wifi_stop() == ESP_OK);
204 TEST_ASSERT(esp_wifi_deinit() == ESP_OK);
205 nvs_flash_deinit();
206 }
207
TEST(esp_netif,create_custom_wifi_interfaces)208 TEST(esp_netif, create_custom_wifi_interfaces)
209 {
210 esp_netif_t *ap = NULL;
211 esp_netif_t *sta = NULL;
212 uint8_t configured_mac[6] = {1, 2, 3, 4, 5, 6};
213 uint8_t actual_mac[6] = { 0 };
214
215 // create customized station
216 esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
217 esp_netif_config.if_desc = "custom wifi station";
218 esp_netif_config.route_prio = 1;
219 sta = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
220 TEST_ASSERT_NOT_NULL(sta);
221 TEST_ASSERT_EQUAL_STRING("custom wifi station", esp_netif_get_desc(sta));
222 TEST_ASSERT_EQUAL(1, esp_netif_get_route_prio(sta));
223
224 // create customized access point
225 esp_netif_inherent_config_t esp_netif_config2 = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
226 esp_netif_config2.if_desc = "custom wifi ap";
227 esp_netif_config2.route_prio = 10;
228 memcpy(esp_netif_config2.mac, configured_mac, 6);
229
230 ap = esp_netif_create_wifi(WIFI_IF_AP, &esp_netif_config2);
231 TEST_ASSERT_NOT_NULL(ap);
232 TEST_ASSERT_EQUAL_STRING( "custom wifi ap", esp_netif_get_desc(ap));
233 TEST_ASSERT_EQUAL(10, esp_netif_get_route_prio(ap));
234 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_mac(ap, actual_mac));
235 TEST_ASSERT_EQUAL_HEX8_ARRAY(configured_mac, actual_mac, 6);
236
237 esp_wifi_destroy_if_driver(esp_netif_get_io_driver(ap));
238 esp_wifi_destroy_if_driver(esp_netif_get_io_driver(sta));
239 esp_netif_destroy(ap);
240 esp_netif_destroy(sta);
241 }
242
243
TEST(esp_netif,get_set_hostname)244 TEST(esp_netif, get_set_hostname)
245 {
246 const char *hostname;
247 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
248
249 test_case_uses_tcpip();
250 esp_netif_t *esp_netif = esp_netif_new(&cfg);
251
252 // specific hostname not set yet, get_hostname should fail
253 TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname));
254
255 TEST_ASSERT_NOT_NULL(esp_netif);
256 esp_netif_attach_wifi_station(esp_netif);
257
258 esp_netif_action_start(esp_netif, NULL, 0, NULL);
259
260 // specific hostname not set yet, but if started, get_hostname to return default config value
261 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname));
262 TEST_ASSERT_EQUAL_STRING(hostname, CONFIG_LWIP_LOCAL_HOSTNAME);
263
264 // specific hostname set and get
265 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_set_hostname(esp_netif, "new_name"));
266 TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname));
267 TEST_ASSERT_EQUAL_STRING(hostname, "new_name");
268
269 esp_netif_destroy(esp_netif);
270 }
271
TEST_GROUP_RUNNER(esp_netif)272 TEST_GROUP_RUNNER(esp_netif)
273 {
274 RUN_TEST_CASE(esp_netif, init_and_destroy)
275 RUN_TEST_CASE(esp_netif, get_from_if_key)
276 RUN_TEST_CASE(esp_netif, create_delete_multiple_netifs)
277 RUN_TEST_CASE(esp_netif, dhcp_client_state_transitions_wifi_sta)
278 RUN_TEST_CASE(esp_netif, dhcp_server_state_transitions_wifi_ap)
279 RUN_TEST_CASE(esp_netif, dhcp_server_state_transitions_mesh)
280 RUN_TEST_CASE(esp_netif, create_custom_wifi_interfaces)
281 RUN_TEST_CASE(esp_netif, get_set_hostname)
282 }
283
app_main(void)284 void app_main(void)
285 {
286 UNITY_MAIN(esp_netif);
287 }
288