SAMV71 Xplained Ultra Software Package 1.5

test_tcp.c

00001 #include "test_tcp.h"
00002 
00003 #include "lwip/tcp.h"
00004 #include "lwip/stats.h"
00005 #include "tcp_helper.h"
00006 
00007 #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
00008 #error "This tests needs TCP- and MEMP-statistics enabled"
00009 #endif
00010 
00011 /* Setups/teardown functions */
00012 
00013 static void
00014 tcp_setup(void)
00015 {
00016   tcp_remove_all();
00017 }
00018 
00019 static void
00020 tcp_teardown(void)
00021 {
00022   tcp_remove_all();
00023 }
00024 
00025 
00026 /* Test functions */
00027 
00028 /** Call tcp_new() and tcp_abort() and test memp stats */
00029 START_TEST(test_tcp_new_abort)
00030 {
00031   struct tcp_pcb* pcb;
00032   LWIP_UNUSED_ARG(_i);
00033 
00034   fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
00035 
00036   pcb = tcp_new();
00037   fail_unless(pcb != NULL);
00038   if (pcb != NULL) {
00039     fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
00040     tcp_abort(pcb);
00041     fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
00042   }
00043 }
00044 END_TEST
00045 
00046 /** Create an ESTABLISHED pcb and check if receive callback is called */
00047 START_TEST(test_tcp_recv_inseq)
00048 {
00049   struct test_tcp_counters counters;
00050   struct tcp_pcb* pcb;
00051   struct pbuf* p;
00052   char data[] = {1, 2, 3, 4};
00053   struct ip_addr remote_ip, local_ip;
00054   u16_t data_len;
00055   u16_t remote_port = 0x100, local_port = 0x101;
00056   struct netif netif;
00057   LWIP_UNUSED_ARG(_i);
00058 
00059   /* initialize local vars */
00060   memset(&netif, 0, sizeof(netif));
00061   IP4_ADDR(&local_ip, 192, 168, 1, 1);
00062   IP4_ADDR(&remote_ip, 192, 168, 1, 2);
00063   data_len = sizeof(data);
00064   /* initialize counter struct */
00065   memset(&counters, 0, sizeof(counters));
00066   counters.expected_data_len = data_len;
00067   counters.expected_data = data;
00068 
00069   /* create and initialize the pcb */
00070   pcb = test_tcp_new_counters_pcb(&counters);
00071   EXPECT_RET(pcb != NULL);
00072   tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
00073 
00074   /* create a segment */
00075   p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
00076   EXPECT(p != NULL);
00077   if (p != NULL) {
00078     /* pass the segment to tcp_input */
00079     tcp_input(p, &netif);
00080     /* check if counters are as expected */
00081     EXPECT(counters.close_calls == 0);
00082     EXPECT(counters.recv_calls == 1);
00083     EXPECT(counters.recved_bytes == data_len);
00084     EXPECT(counters.err_calls == 0);
00085   }
00086 
00087   /* make sure the pcb is freed */
00088   EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
00089   tcp_abort(pcb);
00090   EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
00091 }
00092 END_TEST
00093 
00094 
00095 /** Create the suite including all tests for this module */
00096 Suite *
00097 tcp_suite(void)
00098 {
00099   TFun tests[] = {
00100     test_tcp_new_abort,
00101     test_tcp_recv_inseq,
00102   };
00103   return create_suite("TCP", tests, sizeof(tests)/sizeof(TFun), tcp_setup, tcp_teardown);
00104 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines