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

test_pbuf.c

00001 #include "test_pbuf.h"
00002 
00003 #include "lwip/pbuf.h"
00004 #include "lwip/stats.h"
00005 
00006 #if !LWIP_STATS || !MEM_STATS ||!MEMP_STATS
00007 #error "This tests needs MEM- and MEMP-statistics enabled"
00008 #endif
00009 #if LWIP_DNS
00010 #error "This test needs DNS turned off (as it mallocs on init)"
00011 #endif
00012 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !LWIP_WND_SCALE
00013 #error "This test needs TCP OOSEQ queueing and window scaling enabled"
00014 #endif
00015 
00016 /* Setups/teardown functions */
00017 
00018 static void
00019 pbuf_setup(void)
00020 {
00021   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00022 }
00023 
00024 static void
00025 pbuf_teardown(void)
00026 {
00027   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00028 }
00029 
00030 
00031 #define TESTBUFSIZE_1 65535
00032 #define TESTBUFSIZE_2 65530
00033 #define TESTBUFSIZE_3 50050
00034 static u8_t testbuf_1[TESTBUFSIZE_1];
00035 static u8_t testbuf_1a[TESTBUFSIZE_1];
00036 static u8_t testbuf_2[TESTBUFSIZE_2];
00037 static u8_t testbuf_2a[TESTBUFSIZE_2];
00038 static u8_t testbuf_3[TESTBUFSIZE_3];
00039 static u8_t testbuf_3a[TESTBUFSIZE_3];
00040 
00041 /* Test functions */
00042 START_TEST(test_pbuf_alloc_zero_pbufs)
00043 {
00044   struct pbuf *p;
00045   LWIP_UNUSED_ARG(_i);
00046 
00047   p = pbuf_alloc(PBUF_RAW, 0, PBUF_ROM);
00048   fail_unless(p != NULL);
00049   if (p != NULL) {
00050     pbuf_free(p);
00051   }
00052 
00053   p = pbuf_alloc(PBUF_RAW, 0, PBUF_RAM);
00054   fail_unless(p != NULL);
00055   if (p != NULL) {
00056     pbuf_free(p);
00057   }
00058 
00059   p = pbuf_alloc(PBUF_RAW, 0, PBUF_REF);
00060   fail_unless(p != NULL);
00061   if (p != NULL) {
00062     pbuf_free(p);
00063   }
00064 
00065   p = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
00066   fail_unless(p != NULL);
00067   if (p != NULL) {
00068     pbuf_free(p);
00069   }
00070 }
00071 END_TEST
00072 
00073 /** Call pbuf_copy on a pbuf with zero length */
00074 START_TEST(test_pbuf_copy_zero_pbuf)
00075 {
00076   struct pbuf *p1, *p2, *p3;
00077   err_t err;
00078   LWIP_UNUSED_ARG(_i);
00079 
00080   fail_unless(lwip_stats.mem.used == 0);
00081   fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
00082 
00083   p1 = pbuf_alloc(PBUF_RAW, 1024, PBUF_RAM);
00084   fail_unless(p1 != NULL);
00085   fail_unless(p1->ref == 1);
00086 
00087   p2 = pbuf_alloc(PBUF_RAW, 2, PBUF_POOL);
00088   fail_unless(p2 != NULL);
00089   fail_unless(p2->ref == 1);
00090   p2->len = p2->tot_len = 0;
00091 
00092   pbuf_cat(p1, p2);
00093   fail_unless(p1->ref == 1);
00094   fail_unless(p2->ref == 1);
00095 
00096   p3 = pbuf_alloc(PBUF_RAW, p1->tot_len, PBUF_POOL);
00097   err = pbuf_copy(p3, p1);
00098   fail_unless(err == ERR_VAL);
00099 
00100   pbuf_free(p1);
00101   pbuf_free(p3);
00102   fail_unless(lwip_stats.mem.used == 0);
00103 
00104   fail_unless(lwip_stats.mem.used == 0);
00105   fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
00106 }
00107 END_TEST
00108 
00109 START_TEST(test_pbuf_split_64k_on_small_pbufs)
00110 {
00111   struct pbuf *p, *rest=NULL;
00112   LWIP_UNUSED_ARG(_i);
00113 
00114   p = pbuf_alloc(PBUF_RAW, 1, PBUF_POOL);
00115   pbuf_split_64k(p, &rest);
00116   fail_unless(p->tot_len == 1);
00117   pbuf_free(p);
00118 }
00119 END_TEST
00120 
00121 START_TEST(test_pbuf_queueing_bigger_than_64k)
00122 {
00123   int i;
00124   err_t err;
00125   struct pbuf *p1, *p2, *p3, *rest2=NULL, *rest3=NULL;
00126   LWIP_UNUSED_ARG(_i);
00127 
00128   for(i = 0; i < TESTBUFSIZE_1; i++) {
00129     testbuf_1[i] = (u8_t)rand();
00130   }
00131   for(i = 0; i < TESTBUFSIZE_2; i++) {
00132     testbuf_2[i] = (u8_t)rand();
00133   }
00134   for(i = 0; i < TESTBUFSIZE_3; i++) {
00135     testbuf_3[i] = (u8_t)rand();
00136   }
00137 
00138   p1 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_1, PBUF_POOL);
00139   fail_unless(p1 != NULL);
00140   p2 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_2, PBUF_POOL);
00141   fail_unless(p2 != NULL);
00142   p3 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_3, PBUF_POOL);
00143   fail_unless(p3 != NULL);
00144   err = pbuf_take(p1, testbuf_1, TESTBUFSIZE_1);
00145   fail_unless(err == ERR_OK);
00146   err = pbuf_take(p2, testbuf_2, TESTBUFSIZE_2);
00147   fail_unless(err == ERR_OK);
00148   err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3);
00149   fail_unless(err == ERR_OK);
00150 
00151   pbuf_cat(p1, p2);
00152   pbuf_cat(p1, p3);
00153 
00154   pbuf_split_64k(p1, &rest2);
00155   fail_unless(p1->tot_len == TESTBUFSIZE_1);
00156   fail_unless(rest2->tot_len == (u16_t)((TESTBUFSIZE_2+TESTBUFSIZE_3) & 0xFFFF));
00157   pbuf_split_64k(rest2, &rest3);
00158   fail_unless(rest2->tot_len == TESTBUFSIZE_2);
00159   fail_unless(rest3->tot_len == TESTBUFSIZE_3);
00160 
00161   pbuf_copy_partial(p1, testbuf_1a, TESTBUFSIZE_1, 0);
00162   pbuf_copy_partial(rest2, testbuf_2a, TESTBUFSIZE_2, 0);
00163   pbuf_copy_partial(rest3, testbuf_3a, TESTBUFSIZE_3, 0);
00164   fail_if(memcmp(testbuf_1, testbuf_1a, TESTBUFSIZE_1));
00165   fail_if(memcmp(testbuf_2, testbuf_2a, TESTBUFSIZE_2));
00166   fail_if(memcmp(testbuf_3, testbuf_3a, TESTBUFSIZE_3));
00167 
00168   pbuf_free(p1);
00169   pbuf_free(rest2);
00170   pbuf_free(rest3);
00171 }
00172 END_TEST
00173 
00174 /* Test for bug that writing with pbuf_take_at() did nothing
00175  * and returned ERR_OK when writing at beginning of a pbuf
00176  * in the chain.
00177  */
00178 START_TEST(test_pbuf_take_at_edge)
00179 {
00180   err_t res;
00181   u8_t *out;
00182   int i;
00183   u8_t testdata[] = { 0x01, 0x08, 0x82, 0x02 };
00184   struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
00185   struct pbuf *q = p->next;
00186   LWIP_UNUSED_ARG(_i);
00187   /* alloc big enough to get a chain of pbufs */
00188   fail_if(p->tot_len == p->len);
00189   memset(p->payload, 0, p->len);
00190   memset(q->payload, 0, q->len);
00191 
00192   /* copy data to the beginning of first pbuf */
00193   res = pbuf_take_at(p, &testdata, sizeof(testdata), 0);
00194   fail_unless(res == ERR_OK);
00195 
00196   out = (u8_t*)p->payload;
00197   for (i = 0; i < (int)sizeof(testdata); i++) {
00198     fail_unless(out[i] == testdata[i],
00199       "Bad data at pos %d, was %02X, expected %02X", i, out[i], testdata[i]);
00200   }
00201 
00202   /* copy data to the just before end of first pbuf */
00203   res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len - 1);
00204   fail_unless(res == ERR_OK);
00205 
00206   out = (u8_t*)p->payload;
00207   fail_unless(out[p->len - 1] == testdata[0],
00208     "Bad data at pos %d, was %02X, expected %02X", p->len - 1, out[p->len - 1], testdata[0]);
00209   out = (u8_t*)q->payload;
00210   for (i = 1; i < (int)sizeof(testdata); i++) {
00211     fail_unless(out[i-1] == testdata[i],
00212       "Bad data at pos %d, was %02X, expected %02X", p->len - 1 + i, out[i-1], testdata[i]);
00213   }
00214 
00215   /* copy data to the beginning of second pbuf */
00216   res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len);
00217   fail_unless(res == ERR_OK);
00218 
00219   out = (u8_t*)p->payload;
00220   for (i = 0; i < (int)sizeof(testdata); i++) {
00221     fail_unless(out[i] == testdata[i],
00222       "Bad data at pos %d, was %02X, expected %02X", p->len+i, out[i], testdata[i]);
00223   }
00224   pbuf_free(p);
00225 }
00226 END_TEST
00227 
00228 /* Verify pbuf_put_at()/pbuf_get_at() when using
00229  * offsets equal to beginning of new pbuf in chain
00230  */
00231 START_TEST(test_pbuf_get_put_at_edge)
00232 {
00233   u8_t *out;
00234   u8_t testdata = 0x01;
00235   u8_t getdata;
00236   struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
00237   struct pbuf *q = p->next;
00238   LWIP_UNUSED_ARG(_i);
00239   /* alloc big enough to get a chain of pbufs */
00240   fail_if(p->tot_len == p->len);
00241   memset(p->payload, 0, p->len);
00242   memset(q->payload, 0, q->len);
00243 
00244   /* put byte at the beginning of second pbuf */
00245   pbuf_put_at(p, p->len, testdata);
00246 
00247   out = (u8_t*)q->payload;
00248   fail_unless(*out == testdata,
00249     "Bad data at pos %d, was %02X, expected %02X", p->len, *out, testdata);
00250 
00251   getdata = pbuf_get_at(p, p->len);
00252   fail_unless(*out == getdata,
00253     "pbuf_get_at() returned bad data at pos %d, was %02X, expected %02X", p->len, getdata, *out);
00254   pbuf_free(p);
00255 }
00256 END_TEST
00257 
00258 /** Create the suite including all tests for this module */
00259 Suite *
00260 pbuf_suite(void)
00261 {
00262   testfunc tests[] = {
00263     TESTFUNC(test_pbuf_alloc_zero_pbufs),
00264     TESTFUNC(test_pbuf_copy_zero_pbuf),
00265     TESTFUNC(test_pbuf_split_64k_on_small_pbufs),
00266     TESTFUNC(test_pbuf_queueing_bigger_than_64k),
00267     TESTFUNC(test_pbuf_take_at_edge),
00268     TESTFUNC(test_pbuf_get_put_at_edge)
00269   };
00270   return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown);
00271 }