Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_netif.c Source File

test_netif.c

00001 #include "test_netif.h"
00002 
00003 #include "lwip/netif.h"
00004 #include "lwip/stats.h"
00005 #include "lwip/etharp.h"
00006 #include "netif/ethernet.h"
00007 
00008 #if !LWIP_NETIF_EXT_STATUS_CALLBACK
00009 #error "This tests needs LWIP_NETIF_EXT_STATUS_CALLBACK enabled"
00010 #endif
00011 
00012 struct netif net_test;
00013 
00014 
00015 /* Setups/teardown functions */
00016 
00017 static void
00018 netif_setup(void)
00019 {
00020   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00021 }
00022 
00023 static void
00024 netif_teardown(void)
00025 {
00026   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00027 }
00028 
00029 /* test helper functions */
00030 
00031 static err_t
00032 testif_tx_func(struct netif *netif, struct pbuf *p)
00033 {
00034   LWIP_UNUSED_ARG(netif);
00035   LWIP_UNUSED_ARG(p);
00036   return ERR_OK;
00037 }
00038 
00039 static err_t
00040 testif_init(struct netif *netif)
00041 {
00042   netif->name[0] = 'c';
00043   netif->name[1] = 'h';
00044   netif->output = etharp_output;
00045   netif->linkoutput = testif_tx_func;
00046   netif->mtu = 1500;
00047   netif->hwaddr_len = 6;
00048   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
00049 
00050   netif->hwaddr[0] = 0x02;
00051   netif->hwaddr[1] = 0x03;
00052   netif->hwaddr[2] = 0x04;
00053   netif->hwaddr[3] = 0x05;
00054   netif->hwaddr[4] = 0x06;
00055   netif->hwaddr[5] = 0x07;
00056 
00057   return ERR_OK;
00058 }
00059 
00060 #define MAX_NSC_REASON_IDX 10
00061 static netif_nsc_reason_t expected_reasons;
00062 static int callback_ctr;
00063 
00064 static int dummy_active;
00065 
00066 static void
00067 test_netif_ext_callback_dummy(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
00068 {
00069   LWIP_UNUSED_ARG(netif);
00070   LWIP_UNUSED_ARG(reason);
00071   LWIP_UNUSED_ARG(args);
00072 
00073   fail_unless(dummy_active);
00074 }
00075 
00076 static void
00077 test_netif_ext_callback(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
00078 {
00079   LWIP_UNUSED_ARG(args); /* @todo */
00080   callback_ctr++;
00081 
00082   fail_unless(netif == &net_test);
00083 
00084   fail_unless(expected_reasons == reason);
00085 }
00086 
00087 /* Test functions */
00088 
00089 NETIF_DECLARE_EXT_CALLBACK(netif_callback_1)
00090 NETIF_DECLARE_EXT_CALLBACK(netif_callback_2)
00091 NETIF_DECLARE_EXT_CALLBACK(netif_callback_3)
00092 
00093 START_TEST(test_netif_extcallbacks)
00094 {
00095   ip4_addr_t addr;
00096   ip4_addr_t netmask;
00097   ip4_addr_t gw;
00098   LWIP_UNUSED_ARG(_i);
00099 
00100   IP4_ADDR(&addr, 0, 0, 0, 0);
00101   IP4_ADDR(&netmask, 0, 0, 0, 0);
00102   IP4_ADDR(&gw, 0, 0, 0, 0);
00103 
00104   netif_add_ext_callback(&netif_callback_3, test_netif_ext_callback_dummy);
00105   netif_add_ext_callback(&netif_callback_2, test_netif_ext_callback);
00106   netif_add_ext_callback(&netif_callback_1, test_netif_ext_callback_dummy);
00107 
00108   dummy_active = 1;
00109 
00110   /* positive tests: check that single events come as expected */
00111 
00112   expected_reasons = LWIP_NSC_NETIF_ADDED;
00113   callback_ctr = 0;
00114   netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
00115   fail_unless(callback_ctr == 1);
00116 
00117   expected_reasons = LWIP_NSC_LINK_CHANGED;
00118   callback_ctr = 0;
00119   netif_set_link_up(&net_test);
00120   fail_unless(callback_ctr == 1);
00121 
00122   expected_reasons = LWIP_NSC_STATUS_CHANGED;
00123   callback_ctr = 0;
00124   netif_set_up(&net_test);
00125   fail_unless(callback_ctr == 1);
00126 
00127   IP4_ADDR(&addr, 1, 2, 3, 4);
00128   expected_reasons = LWIP_NSC_IPV4_ADDRESS_CHANGED;
00129   callback_ctr = 0;
00130   netif_set_ipaddr(&net_test, &addr);
00131   fail_unless(callback_ctr == 1);
00132 
00133   IP4_ADDR(&netmask, 255, 255, 255, 0);
00134   expected_reasons = LWIP_NSC_IPV4_NETMASK_CHANGED;
00135   callback_ctr = 0;
00136   netif_set_netmask(&net_test, &netmask);
00137   fail_unless(callback_ctr == 1);
00138 
00139   IP4_ADDR(&gw, 1, 2, 3, 254);
00140   expected_reasons = LWIP_NSC_IPV4_GATEWAY_CHANGED;
00141   callback_ctr = 0;
00142   netif_set_gw(&net_test, &gw);
00143   fail_unless(callback_ctr == 1);
00144 
00145   IP4_ADDR(&addr, 0, 0, 0, 0);
00146   expected_reasons = LWIP_NSC_IPV4_ADDRESS_CHANGED;
00147   callback_ctr = 0;
00148   netif_set_ipaddr(&net_test, &addr);
00149   fail_unless(callback_ctr == 1);
00150 
00151   IP4_ADDR(&netmask, 0, 0, 0, 0);
00152   expected_reasons = LWIP_NSC_IPV4_NETMASK_CHANGED;
00153   callback_ctr = 0;
00154   netif_set_netmask(&net_test, &netmask);
00155   fail_unless(callback_ctr == 1);
00156 
00157   IP4_ADDR(&gw, 0, 0, 0, 0);
00158   expected_reasons = LWIP_NSC_IPV4_GATEWAY_CHANGED;
00159   callback_ctr = 0;
00160   netif_set_gw(&net_test, &gw);
00161   fail_unless(callback_ctr == 1);
00162 
00163   /* check for multi-events (only one combined callback expected) */
00164 
00165   IP4_ADDR(&addr, 1, 2, 3, 4);
00166   IP4_ADDR(&netmask, 255, 255, 255, 0);
00167   IP4_ADDR(&gw, 1, 2, 3, 254);
00168   expected_reasons = (netif_nsc_reason_t)(LWIP_NSC_IPV4_ADDRESS_CHANGED | LWIP_NSC_IPV4_NETMASK_CHANGED |
00169                                           LWIP_NSC_IPV4_GATEWAY_CHANGED | LWIP_NSC_IPV4_SETTINGS_CHANGED);
00170   callback_ctr = 0;
00171   netif_set_addr(&net_test, &addr, &netmask, &gw);
00172   fail_unless(callback_ctr == 1);
00173 
00174   /* check that for no-change, no callback is expected */
00175   expected_reasons = LWIP_NSC_NONE;
00176   callback_ctr = 0;
00177   netif_set_ipaddr(&net_test, &addr);
00178   fail_unless(callback_ctr == 0);
00179 
00180   netif_set_netmask(&net_test, &netmask);
00181   callback_ctr = 0;
00182   fail_unless(callback_ctr == 0);
00183 
00184   callback_ctr = 0;
00185   netif_set_gw(&net_test, &gw);
00186   fail_unless(callback_ctr == 0);
00187 
00188   callback_ctr = 0;
00189   netif_set_addr(&net_test, &addr, &netmask, &gw);
00190   fail_unless(callback_ctr == 0);
00191 
00192   /* check for single-events */
00193   IP4_ADDR(&addr, 1, 2, 3, 5);
00194   expected_reasons = (netif_nsc_reason_t)(LWIP_NSC_IPV4_ADDRESS_CHANGED | LWIP_NSC_IPV4_SETTINGS_CHANGED);
00195   callback_ctr = 0;
00196   netif_set_addr(&net_test, &addr, &netmask, &gw);
00197   fail_unless(callback_ctr == 1);
00198 
00199   expected_reasons = LWIP_NSC_STATUS_CHANGED;
00200   callback_ctr = 0;
00201   netif_set_down(&net_test);
00202   fail_unless(callback_ctr == 1);
00203 
00204   expected_reasons = LWIP_NSC_NETIF_REMOVED;
00205   callback_ctr = 0;
00206   netif_remove(&net_test);
00207   fail_unless(callback_ctr == 1);
00208 
00209   expected_reasons = LWIP_NSC_NONE;
00210 
00211   netif_remove_ext_callback(&netif_callback_2);
00212   netif_remove_ext_callback(&netif_callback_3);
00213   netif_remove_ext_callback(&netif_callback_1);
00214   dummy_active = 0;
00215 }
00216 END_TEST
00217 
00218 
00219 /** Create the suite including all tests for this module */
00220 Suite *
00221 netif_suite(void)
00222 {
00223   testfunc tests[] = {
00224     TESTFUNC(test_netif_extcallbacks)
00225   };
00226   return create_suite("NETIF", tests, sizeof(tests)/sizeof(testfunc), netif_setup, netif_teardown);
00227 }