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_ip4.c Source File

test_ip4.c

00001 #include "test_ip4.h"
00002 
00003 #include "lwip/ip4.h"
00004 #include "lwip/inet_chksum.h"
00005 #include "lwip/stats.h"
00006 #include "lwip/prot/ip.h"
00007 #include "lwip/prot/ip4.h"
00008 
00009 #include "lwip/tcpip.h"
00010 
00011 #if !LWIP_IPV4 || !IP_REASSEMBLY || !MIB2_STATS || !IPFRAG_STATS
00012 #error "This tests needs LWIP_IPV4, IP_REASSEMBLY; MIB2- and IPFRAG-statistics enabled"
00013 #endif
00014 
00015 /* Helper functions */
00016 static void
00017 create_ip4_input_fragment(u16_t ip_id, u16_t start, u16_t len, int last)
00018 {
00019   struct pbuf *p;
00020   struct netif *input_netif = netif_list; /* just use any netif */
00021   fail_unless((start & 7) == 0);
00022   fail_unless(((len & 7) == 0) || last);
00023   fail_unless(input_netif != NULL);
00024 
00025   p = pbuf_alloc(PBUF_RAW, len + sizeof(struct ip_hdr), PBUF_RAM);
00026   fail_unless(p != NULL);
00027   if (p != NULL) {
00028     err_t err;
00029     struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
00030     IPH_VHL_SET(iphdr, 4, sizeof(struct ip_hdr) / 4);
00031     IPH_TOS_SET(iphdr, 0);
00032     IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
00033     IPH_ID_SET(iphdr, lwip_htons(ip_id));
00034     if (last) {
00035       IPH_OFFSET_SET(iphdr, lwip_htons(start / 8));
00036     } else {
00037       IPH_OFFSET_SET(iphdr, lwip_htons((start / 8) | IP_MF));
00038     }
00039     IPH_TTL_SET(iphdr, 5);
00040     IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
00041     IPH_CHKSUM_SET(iphdr, 0);
00042     ip4_addr_copy(iphdr->src, *netif_ip4_addr(input_netif));
00043     iphdr->src.addr = lwip_htonl(lwip_htonl(iphdr->src.addr) + 1);
00044     ip4_addr_copy(iphdr->dest, *netif_ip4_addr(input_netif));
00045     IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, sizeof(struct ip_hdr)));
00046 
00047     err = ip4_input(p, input_netif);
00048     if (err != ERR_OK) {
00049       pbuf_free(p);
00050     }
00051     fail_unless(err == ERR_OK);
00052   }
00053 }
00054 
00055 /* Setups/teardown functions */
00056 
00057 static void
00058 ip4_setup(void)
00059 {
00060   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00061 }
00062 
00063 static void
00064 ip4_teardown(void)
00065 {
00066   if (netif_list->loop_first != NULL) {
00067     pbuf_free(netif_list->loop_first);
00068     netif_list->loop_first = NULL;
00069   }
00070   netif_list->loop_last = NULL;
00071   /* poll until all memory is released... */
00072   tcpip_thread_poll_one();
00073   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00074 }
00075 
00076 
00077 /* Test functions */
00078 
00079 START_TEST(test_ip4_reass)
00080 {
00081   const u16_t ip_id = 128;
00082   LWIP_UNUSED_ARG(_i);
00083 
00084   memset(&lwip_stats.mib2, 0, sizeof(lwip_stats.mib2));
00085 
00086   create_ip4_input_fragment(ip_id, 8*200, 200, 1);
00087   fail_unless(lwip_stats.ip_frag.recv == 1);
00088   fail_unless(lwip_stats.ip_frag.err == 0);
00089   fail_unless(lwip_stats.ip_frag.memerr == 0);
00090   fail_unless(lwip_stats.ip_frag.drop == 0);
00091   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00092 
00093   create_ip4_input_fragment(ip_id, 0*200, 200, 0);
00094   fail_unless(lwip_stats.ip_frag.recv == 2);
00095   fail_unless(lwip_stats.ip_frag.err == 0);
00096   fail_unless(lwip_stats.ip_frag.memerr == 0);
00097   fail_unless(lwip_stats.ip_frag.drop == 0);
00098   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00099 
00100   create_ip4_input_fragment(ip_id, 1*200, 200, 0);
00101   fail_unless(lwip_stats.ip_frag.recv == 3);
00102   fail_unless(lwip_stats.ip_frag.err == 0);
00103   fail_unless(lwip_stats.ip_frag.memerr == 0);
00104   fail_unless(lwip_stats.ip_frag.drop == 0);
00105   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00106 
00107   create_ip4_input_fragment(ip_id, 2*200, 200, 0);
00108   fail_unless(lwip_stats.ip_frag.recv == 4);
00109   fail_unless(lwip_stats.ip_frag.err == 0);
00110   fail_unless(lwip_stats.ip_frag.memerr == 0);
00111   fail_unless(lwip_stats.ip_frag.drop == 0);
00112   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00113 
00114   create_ip4_input_fragment(ip_id, 3*200, 200, 0);
00115   fail_unless(lwip_stats.ip_frag.recv == 5);
00116   fail_unless(lwip_stats.ip_frag.err == 0);
00117   fail_unless(lwip_stats.ip_frag.memerr == 0);
00118   fail_unless(lwip_stats.ip_frag.drop == 0);
00119   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00120 
00121   create_ip4_input_fragment(ip_id, 4*200, 200, 0);
00122   fail_unless(lwip_stats.ip_frag.recv == 6);
00123   fail_unless(lwip_stats.ip_frag.err == 0);
00124   fail_unless(lwip_stats.ip_frag.memerr == 0);
00125   fail_unless(lwip_stats.ip_frag.drop == 0);
00126   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00127 
00128   create_ip4_input_fragment(ip_id, 7*200, 200, 0);
00129   fail_unless(lwip_stats.ip_frag.recv == 7);
00130   fail_unless(lwip_stats.ip_frag.err == 0);
00131   fail_unless(lwip_stats.ip_frag.memerr == 0);
00132   fail_unless(lwip_stats.ip_frag.drop == 0);
00133   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00134 
00135   create_ip4_input_fragment(ip_id, 6*200, 200, 0);
00136   fail_unless(lwip_stats.ip_frag.recv == 8);
00137   fail_unless(lwip_stats.ip_frag.err == 0);
00138   fail_unless(lwip_stats.ip_frag.memerr == 0);
00139   fail_unless(lwip_stats.ip_frag.drop == 0);
00140   fail_unless(lwip_stats.mib2.ipreasmoks == 0);
00141 
00142   create_ip4_input_fragment(ip_id, 5*200, 200, 0);
00143   fail_unless(lwip_stats.ip_frag.recv == 9);
00144   fail_unless(lwip_stats.ip_frag.err == 0);
00145   fail_unless(lwip_stats.ip_frag.memerr == 0);
00146   fail_unless(lwip_stats.ip_frag.drop == 0);
00147   fail_unless(lwip_stats.mib2.ipreasmoks == 1);
00148 }
00149 END_TEST
00150 
00151 
00152 /** Create the suite including all tests for this module */
00153 Suite *
00154 ip4_suite(void)
00155 {
00156   testfunc tests[] = {
00157     TESTFUNC(test_ip4_reass),
00158   };
00159   return create_suite("IPv4", tests, sizeof(tests)/sizeof(testfunc), ip4_setup, ip4_teardown);
00160 }