test_esp_netif.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include <string.h>
  2. #include "unity.h"
  3. #include "test_utils.h"
  4. #include "esp_netif.h"
  5. #include "esp_wifi.h"
  6. #include "nvs_flash.h"
  7. #include "esp_wifi_netif.h"
  8. #include "lwip/netif.h"
  9. #include "esp_netif_net_stack.h"
  10. TEST_CASE("esp_netif: init and destroy", "[esp_netif]")
  11. {
  12. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
  13. esp_netif_t *esp_netif = esp_netif_new(NULL);
  14. TEST_ASSERT_EQUAL(NULL, esp_netif);
  15. esp_netif = esp_netif_new(&cfg);
  16. TEST_ASSERT_NOT_EQUAL(NULL, esp_netif);
  17. esp_netif_destroy(esp_netif);
  18. }
  19. TEST_CASE("esp_netif: get from if_key", "[esp_netif][leaks=0]")
  20. {
  21. // init default netif
  22. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
  23. esp_netif_t *esp_netif = esp_netif_new(&cfg);
  24. TEST_ASSERT_NOT_NULL(esp_netif);
  25. // check it's accessible by key
  26. TEST_ASSERT_EQUAL(esp_netif, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
  27. // destroy it
  28. esp_netif_destroy(esp_netif);
  29. // check it's also destroyed in list
  30. TEST_ASSERT_EQUAL(NULL, esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
  31. }
  32. // This is a private esp-netif API, but include here to test it
  33. bool esp_netif_is_netif_listed(esp_netif_t *esp_netif);
  34. TEST_CASE("esp_netif: create and delete multiple netifs", "[esp_netif][leaks=0]")
  35. {
  36. // interface key has to be a unique identifier
  37. const char* if_keys[] = { "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9" };
  38. const int nr_of_netifs = sizeof(if_keys)/sizeof(char*);
  39. esp_netif_t *netifs[nr_of_netifs];
  40. // create 10 wifi stations
  41. for (int i=0; i<nr_of_netifs; ++i) {
  42. esp_netif_inherent_config_t base_netif_config = { .if_key = if_keys[i]};
  43. esp_netif_config_t cfg = { .base = &base_netif_config, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
  44. netifs[i] = esp_netif_new(&cfg);
  45. TEST_ASSERT_NOT_NULL(netifs[i]);
  46. }
  47. // there's no AP within created netifs
  48. TEST_ASSERT_NULL(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"));
  49. // check that the created netifs are correctly found by their interface keys and globally listed
  50. for (int i=0; i<nr_of_netifs; ++i) {
  51. TEST_ASSERT_EQUAL(netifs[i], esp_netif_get_handle_from_ifkey(if_keys[i]));
  52. TEST_ASSERT_TRUE(esp_netif_is_netif_listed(netifs[i]));
  53. }
  54. // destroy one by one and check it's been removed
  55. for (int i=0; i<nr_of_netifs; ++i) {
  56. esp_netif_destroy(netifs[i]);
  57. TEST_ASSERT_FALSE(esp_netif_is_netif_listed(netifs[i]));
  58. }
  59. }
  60. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  61. //IDF-5047
  62. TEST_CASE("esp_netif: test dhcp client state transitions for wifi station", "[esp_netif]")
  63. {
  64. // init default wifi netif
  65. test_case_uses_tcpip();
  66. TEST_ESP_OK(nvs_flash_init());
  67. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
  68. esp_netif_t *sta = esp_netif_new(&cfg);
  69. TEST_ASSERT_NOT_NULL(sta);
  70. esp_netif_attach_wifi_station(sta);
  71. wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
  72. TEST_ESP_OK(esp_wifi_init(&wifi_cfg));
  73. esp_netif_dhcp_status_t state;
  74. // testing DHCP states per netif state transitions
  75. esp_netif_action_start(sta, NULL, 0, NULL);
  76. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
  77. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state);
  78. esp_netif_action_connected(sta, NULL, 0, NULL);
  79. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
  80. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  81. // test manual DHCP state transitions using dhcpc-start/stop API
  82. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_stop(sta));
  83. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
  84. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STOPPED, state);
  85. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_start(sta));
  86. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
  87. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  88. TEST_ASSERT_EQUAL(ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED, esp_netif_dhcpc_start(sta));
  89. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
  90. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  91. // stop the netif and test dhcp state update
  92. esp_netif_action_stop(sta, NULL, 0, NULL);
  93. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcpc_get_status(sta, &state));
  94. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state);
  95. // destroy default wifi netif
  96. esp_netif_destroy(sta);
  97. TEST_ASSERT(esp_wifi_stop() == ESP_OK);
  98. TEST_ASSERT(esp_wifi_deinit() == ESP_OK);
  99. nvs_flash_deinit();
  100. }
  101. TEST_CASE("esp_netif: test dhcp server state transitions for wifi soft AP", "[esp_netif]")
  102. {
  103. // init default wifi netif
  104. test_case_uses_tcpip();
  105. TEST_ESP_OK(nvs_flash_init());
  106. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_AP();
  107. esp_netif_t *ap = esp_netif_new(&cfg);
  108. TEST_ASSERT_NOT_NULL(ap);
  109. esp_netif_attach_wifi_station(ap);
  110. wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
  111. TEST_ESP_OK(esp_wifi_init(&wifi_cfg));
  112. esp_netif_dhcp_status_t state;
  113. // testing DHCP server states per netif state transitions
  114. esp_netif_action_start(ap, NULL, 0, NULL);
  115. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state));
  116. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  117. // test manual DHCP state transitions using dhcps-start/stop API
  118. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_stop(ap));
  119. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state));
  120. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STOPPED, state);
  121. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_start(ap));
  122. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state));
  123. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  124. TEST_ASSERT_EQUAL(ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED, esp_netif_dhcps_start(ap));
  125. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state));
  126. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  127. // stop the netif and test dhcp state update
  128. esp_netif_action_stop(ap, NULL, 0, NULL);
  129. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_dhcps_get_status(ap, &state));
  130. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_INIT, state);
  131. // destroy default wifi netif
  132. esp_netif_destroy(ap);
  133. TEST_ASSERT(esp_wifi_stop() == ESP_OK);
  134. TEST_ASSERT(esp_wifi_deinit() == ESP_OK);
  135. nvs_flash_deinit();
  136. }
  137. TEST_CASE("esp_netif: test dhcp state transitions for mesh netifs", "[esp_netif]")
  138. {
  139. esp_netif_t *ap = NULL;
  140. esp_netif_t *sta = NULL;
  141. esp_netif_dhcp_status_t state;
  142. // init two mesh network interfaces
  143. test_case_uses_tcpip();
  144. TEST_ESP_OK(nvs_flash_init());
  145. TEST_ESP_OK(esp_event_loop_create_default());
  146. TEST_ESP_OK(esp_netif_create_default_wifi_mesh_netifs(&sta, &ap));
  147. TEST_ASSERT_NOT_NULL(sta);
  148. TEST_ASSERT_NOT_NULL(ap);
  149. wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
  150. TEST_ESP_OK(esp_wifi_init(&wifi_cfg));
  151. // test both server and client are *not* STARTED after interfaces created
  152. TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
  153. TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  154. TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state));
  155. TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  156. // test both server and client are still *not* STARTED after start
  157. esp_netif_action_start(ap, NULL, 0, NULL);
  158. esp_netif_action_start(sta, NULL, 0, NULL);
  159. TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
  160. TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  161. TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state));
  162. TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  163. // test both server and client are still *not* STARTED even after connect
  164. esp_netif_action_connected(ap, NULL, 0, NULL);
  165. esp_netif_action_connected(sta, NULL, 0, NULL);
  166. TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
  167. TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  168. TEST_ESP_OK(esp_netif_dhcps_get_status(ap, &state));
  169. TEST_ASSERT_NOT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  170. // test station gets promoted to be a root (so DHCP client started manually) and client is in STATED state
  171. esp_netif_dhcpc_start(sta);
  172. esp_netif_action_connected(sta, NULL, 0, NULL);
  173. TEST_ESP_OK(esp_netif_dhcpc_get_status(sta, &state));
  174. TEST_ASSERT_EQUAL(ESP_NETIF_DHCP_STARTED, state);
  175. esp_netif_dhcpc_stop(sta);
  176. // test both server and client are still *not* STARTED even after stop
  177. esp_netif_action_stop(sta, NULL, 0, NULL);
  178. esp_netif_action_stop(ap, 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. // destroy event_loop, netifs, wifi, nvs
  184. TEST_ESP_OK(esp_event_loop_delete_default());
  185. esp_netif_destroy(ap);
  186. esp_netif_destroy(sta);
  187. TEST_ASSERT(esp_wifi_stop() == ESP_OK);
  188. TEST_ASSERT(esp_wifi_deinit() == ESP_OK);
  189. nvs_flash_deinit();
  190. }
  191. TEST_CASE("esp_netif: create custom wifi interfaces", "[esp_netif][leaks=0]")
  192. {
  193. esp_netif_t *ap = NULL;
  194. esp_netif_t *sta = NULL;
  195. uint8_t configured_mac[6] = {1, 2, 3, 4, 5, 6};
  196. uint8_t actual_mac[6] = { 0 };
  197. // create customized station
  198. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
  199. esp_netif_config.if_desc = "custom wifi station";
  200. esp_netif_config.route_prio = 1;
  201. sta = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
  202. TEST_ASSERT_NOT_NULL(sta);
  203. TEST_ASSERT_EQUAL_STRING("custom wifi station", esp_netif_get_desc(sta));
  204. TEST_ASSERT_EQUAL(1, esp_netif_get_route_prio(sta));
  205. // create customized access point
  206. esp_netif_inherent_config_t esp_netif_config2 = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
  207. esp_netif_config2.if_desc = "custom wifi ap";
  208. esp_netif_config2.route_prio = 10;
  209. memcpy(esp_netif_config2.mac, configured_mac, 6);
  210. ap = esp_netif_create_wifi(WIFI_IF_AP, &esp_netif_config2);
  211. TEST_ASSERT_NOT_NULL(ap);
  212. TEST_ASSERT_EQUAL_STRING( "custom wifi ap", esp_netif_get_desc(ap));
  213. TEST_ASSERT_EQUAL(10, esp_netif_get_route_prio(ap));
  214. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_mac(ap, actual_mac));
  215. TEST_ASSERT_EQUAL_HEX8_ARRAY(configured_mac, actual_mac, 6);
  216. esp_wifi_destroy_if_driver(esp_netif_get_io_driver(ap));
  217. esp_wifi_destroy_if_driver(esp_netif_get_io_driver(sta));
  218. esp_netif_destroy(ap);
  219. esp_netif_destroy(sta);
  220. }
  221. TEST_CASE("esp_netif: get/set hostname", "[esp_netif]")
  222. {
  223. const char *hostname;
  224. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
  225. test_case_uses_tcpip();
  226. esp_netif_t *esp_netif = esp_netif_new(&cfg);
  227. // specific hostname not set yet, get_hostname should fail
  228. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname));
  229. TEST_ASSERT_NOT_NULL(esp_netif);
  230. esp_netif_attach_wifi_station(esp_netif);
  231. esp_netif_action_start(esp_netif, NULL, 0, NULL);
  232. // specific hostname not set yet, but if started, get_hostname to return default config value
  233. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname));
  234. TEST_ASSERT_EQUAL_STRING(hostname, CONFIG_LWIP_LOCAL_HOSTNAME);
  235. // specific hostname set and get
  236. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_set_hostname(esp_netif, "new_name"));
  237. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname));
  238. TEST_ASSERT_EQUAL_STRING(hostname, "new_name");
  239. // test that setting the long name is refused and the previously set value retained
  240. #define ESP_NETIF_HOSTNAME_MAX_SIZE 32
  241. char long_name[ESP_NETIF_HOSTNAME_MAX_SIZE + 2] = { 0 };
  242. memset(long_name, 'A', ESP_NETIF_HOSTNAME_MAX_SIZE+1); // construct the long name
  243. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_set_hostname(esp_netif, long_name));
  244. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_get_hostname(esp_netif, &hostname));
  245. TEST_ASSERT_EQUAL_STRING(hostname, "new_name");
  246. esp_netif_destroy(esp_netif);
  247. }
  248. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  249. TEST_CASE("esp_netif: convert ip address from string", "[esp_netif]")
  250. {
  251. const char *ipv4_src[] = {"127.168.1.1", "255.255.255.0", "305.500.721.801", "127.168.1..", "abc.def.***.ddd"};
  252. esp_ip4_addr_t ipv4;
  253. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[0], &ipv4));
  254. TEST_ASSERT_EQUAL(ipv4.addr, ESP_IP4TOADDR(127, 168, 1, 1));
  255. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[1], &ipv4));
  256. TEST_ASSERT_EQUAL(ipv4.addr, ESP_IP4TOADDR(255, 255, 255, 0));
  257. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[2], &ipv4));
  258. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[3], &ipv4));
  259. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip4(ipv4_src[4], &ipv4));
  260. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip4(NULL, &ipv4));
  261. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip4(ipv4_src[0], NULL));
  262. const char *ipv6_src[] = {"127:168:6:8:188:65:1:0", "255:255:255:0:0:0:65:56", "305:500:721:888:777:458:555:666", "EFGH.127:168::55"};
  263. esp_ip6_addr_t ipv6;
  264. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[0], &ipv6));
  265. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[1], &ipv6));
  266. TEST_ASSERT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[2], &ipv6));
  267. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_str_to_ip6(ipv6_src[3], &ipv6));
  268. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip6(NULL, &ipv6));
  269. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_netif_str_to_ip6(ipv6_src[0], NULL));
  270. }
  271. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  272. //IDF-5047
  273. TEST_CASE("esp_netif: create and destroy default wifi interfaces", "[esp_netif][leaks=0]")
  274. {
  275. // Helper constants to refer default STA and AP's params
  276. static const esp_netif_inherent_config_t default_sta_cfg = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
  277. static const esp_netif_inherent_config_t default_ap_cfg = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
  278. // create default station
  279. esp_netif_t *sta = esp_netif_create_default_wifi_sta();
  280. // check it gets created and has default params
  281. TEST_ASSERT_NOT_NULL(sta);
  282. TEST_ASSERT_EQUAL_STRING(default_sta_cfg.if_desc, esp_netif_get_desc(sta));
  283. TEST_ASSERT_EQUAL(default_sta_cfg.route_prio, esp_netif_get_route_prio(sta));
  284. // create default access point
  285. esp_netif_t *ap = esp_netif_create_default_wifi_ap();
  286. // check it gets created and has default params
  287. TEST_ASSERT_NOT_NULL(ap);
  288. TEST_ASSERT_EQUAL_STRING(default_ap_cfg.if_desc, esp_netif_get_desc(ap));
  289. TEST_ASSERT_EQUAL(default_ap_cfg.route_prio, esp_netif_get_route_prio(ap));
  290. // destroy the station
  291. esp_netif_destroy_default_wifi(sta);
  292. // destroy the AP
  293. esp_netif_destroy_default_wifi(ap);
  294. // quick check on create-destroy cycle of the default station again
  295. sta = NULL;
  296. sta = esp_netif_create_default_wifi_sta();
  297. TEST_ASSERT_NOT_NULL(sta);
  298. esp_netif_destroy_default_wifi(sta);
  299. }
  300. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  301. static esp_err_t dummy_transmit(void* hd, void *buf, size_t length)
  302. {
  303. return ESP_OK;
  304. }
  305. TEST_CASE("esp_netif: test routing priority", "[esp_netif]")
  306. {
  307. test_case_uses_tcpip();
  308. // interface key has to be a unique identifier
  309. const char *if_keys[] = {"if0", "if1", "if2", "if3", "if4", "if5", "if6", "if7", "if8", "if9"};
  310. const int nr_of_netifs = sizeof(if_keys) / sizeof(char *);
  311. esp_netif_t *netifs[nr_of_netifs];
  312. esp_netif_driver_ifconfig_t driver_config = { .handle = (void*)1, .transmit = dummy_transmit };
  313. // create 10 netifs with different route prio
  314. int max_prio_i = nr_of_netifs / 2; // index of netif with maximum route-prio
  315. for (int i = 0; i < nr_of_netifs; ++i) {
  316. esp_netif_inherent_config_t base_netif_config = { .if_key = if_keys[i],
  317. .route_prio = i > max_prio_i ? 0 : i };
  318. esp_netif_config_t cfg = { .base = &base_netif_config,
  319. .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA,
  320. .driver = &driver_config };
  321. netifs[i] = esp_netif_new(&cfg);
  322. TEST_ASSERT_NOT_NULL(netifs[i]);
  323. // set the interface up and connected -- to enable the default netif based on route_prio
  324. esp_netif_action_start(netifs[i], 0, 0, 0);
  325. esp_netif_action_connected(netifs[i], 0, 0, 0);
  326. }
  327. // route_prio increases with index until max_prio_i -> check this is the default netif
  328. TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i]), netif_default);
  329. // now we stop the max_prio netif and check the default is on the previous index (max_prio-1)
  330. esp_netif_action_stop(netifs[max_prio_i], 0, 0, 0);
  331. TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i - 1]), netif_default);
  332. // now we override the default netif with API (which has route_prio == 0)
  333. int override_prio_i = nr_of_netifs - 1; // last netif to be set-default manually
  334. esp_netif_set_default_netif(netifs[override_prio_i]);
  335. // check the configured netif is default
  336. TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[override_prio_i]), netif_default);
  337. // try to start/connect the previously stopped netif with max_prio
  338. esp_netif_action_start(netifs[max_prio_i], 0, 0, 0);
  339. esp_netif_action_connected(netifs[max_prio_i], 0, 0, 0);
  340. // and check the configured netif is still the default
  341. TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[override_prio_i]), netif_default);
  342. // we destroy the configured default netif
  343. esp_netif_destroy(netifs[override_prio_i]);
  344. // ...and check the max-prio netif is default now
  345. TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i]), netif_default);
  346. // stop the max_prio netif, to see the auto-default still works
  347. esp_netif_action_stop(netifs[max_prio_i], 0, 0, 0);
  348. // ...so the current default is on (max_prio-1)
  349. TEST_ASSERT_EQUAL_PTR(esp_netif_get_netif_impl(netifs[max_prio_i - 1]), netif_default);
  350. // destroy one by one and check it's been removed
  351. for (int i=0; i < override_prio_i; ++i) {
  352. esp_netif_destroy(netifs[i]);
  353. TEST_ASSERT_FALSE(esp_netif_is_netif_listed(netifs[i]));
  354. }
  355. }