Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
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 } 00022 00023 static void 00024 pbuf_teardown(void) 00025 { 00026 } 00027 00028 00029 #define TESTBUFSIZE_1 65535 00030 #define TESTBUFSIZE_2 65530 00031 #define TESTBUFSIZE_3 50050 00032 static u8_t testbuf_1[TESTBUFSIZE_1]; 00033 static u8_t testbuf_1a[TESTBUFSIZE_1]; 00034 static u8_t testbuf_2[TESTBUFSIZE_2]; 00035 static u8_t testbuf_2a[TESTBUFSIZE_2]; 00036 static u8_t testbuf_3[TESTBUFSIZE_3]; 00037 static u8_t testbuf_3a[TESTBUFSIZE_3]; 00038 00039 /* Test functions */ 00040 00041 /** Call pbuf_copy on a pbuf with zero length */ 00042 START_TEST(test_pbuf_copy_zero_pbuf) 00043 { 00044 struct pbuf *p1, *p2, *p3; 00045 err_t err; 00046 LWIP_UNUSED_ARG(_i); 00047 00048 fail_unless(lwip_stats.mem.used == 0); 00049 fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0); 00050 00051 p1 = pbuf_alloc(PBUF_RAW, 1024, PBUF_RAM); 00052 fail_unless(p1 != NULL); 00053 fail_unless(p1->ref == 1); 00054 00055 p2 = pbuf_alloc(PBUF_RAW, 2, PBUF_POOL); 00056 fail_unless(p2 != NULL); 00057 fail_unless(p2->ref == 1); 00058 p2->len = p2->tot_len = 0; 00059 00060 pbuf_cat(p1, p2); 00061 fail_unless(p1->ref == 1); 00062 fail_unless(p2->ref == 1); 00063 00064 p3 = pbuf_alloc(PBUF_RAW, p1->tot_len, PBUF_POOL); 00065 err = pbuf_copy(p3, p1); 00066 fail_unless(err == ERR_VAL); 00067 00068 pbuf_free(p1); 00069 pbuf_free(p3); 00070 fail_unless(lwip_stats.mem.used == 0); 00071 00072 fail_unless(lwip_stats.mem.used == 0); 00073 fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0); 00074 } 00075 END_TEST 00076 00077 START_TEST(test_pbuf_split_64k_on_small_pbufs) 00078 { 00079 struct pbuf *p, *rest=NULL; 00080 LWIP_UNUSED_ARG(_i); 00081 00082 p = pbuf_alloc(PBUF_RAW, 1, PBUF_POOL); 00083 pbuf_split_64k(p, &rest); 00084 fail_unless(p->tot_len == 1); 00085 pbuf_free(p); 00086 } 00087 END_TEST 00088 00089 START_TEST(test_pbuf_queueing_bigger_than_64k) 00090 { 00091 int i; 00092 err_t err; 00093 struct pbuf *p1, *p2, *p3, *rest2=NULL, *rest3=NULL; 00094 LWIP_UNUSED_ARG(_i); 00095 00096 for(i = 0; i < TESTBUFSIZE_1; i++) { 00097 testbuf_1[i] = (u8_t)rand(); 00098 } 00099 for(i = 0; i < TESTBUFSIZE_2; i++) { 00100 testbuf_2[i] = (u8_t)rand(); 00101 } 00102 for(i = 0; i < TESTBUFSIZE_3; i++) { 00103 testbuf_3[i] = (u8_t)rand(); 00104 } 00105 00106 p1 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_1, PBUF_POOL); 00107 fail_unless(p1 != NULL); 00108 p2 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_2, PBUF_POOL); 00109 fail_unless(p2 != NULL); 00110 p3 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_3, PBUF_POOL); 00111 fail_unless(p3 != NULL); 00112 err = pbuf_take(p1, testbuf_1, TESTBUFSIZE_1); 00113 fail_unless(err == ERR_OK); 00114 err = pbuf_take(p2, testbuf_2, TESTBUFSIZE_2); 00115 fail_unless(err == ERR_OK); 00116 err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3); 00117 fail_unless(err == ERR_OK); 00118 00119 pbuf_cat(p1, p2); 00120 pbuf_cat(p1, p3); 00121 00122 pbuf_split_64k(p1, &rest2); 00123 fail_unless(p1->tot_len == TESTBUFSIZE_1); 00124 fail_unless(rest2->tot_len == (u16_t)((TESTBUFSIZE_2+TESTBUFSIZE_3) & 0xFFFF)); 00125 pbuf_split_64k(rest2, &rest3); 00126 fail_unless(rest2->tot_len == TESTBUFSIZE_2); 00127 fail_unless(rest3->tot_len == TESTBUFSIZE_3); 00128 00129 pbuf_copy_partial(p1, testbuf_1a, TESTBUFSIZE_1, 0); 00130 pbuf_copy_partial(rest2, testbuf_2a, TESTBUFSIZE_2, 0); 00131 pbuf_copy_partial(rest3, testbuf_3a, TESTBUFSIZE_3, 0); 00132 for(i = 0; i < TESTBUFSIZE_1; i++) 00133 fail_unless(testbuf_1[i] == testbuf_1a[i]); 00134 for(i = 0; i < TESTBUFSIZE_2; i++) 00135 fail_unless(testbuf_2[i] == testbuf_2a[i]); 00136 for(i = 0; i < TESTBUFSIZE_3; i++) 00137 fail_unless(testbuf_3[i] == testbuf_3a[i]); 00138 00139 pbuf_free(p1); 00140 pbuf_free(rest2); 00141 pbuf_free(rest3); 00142 } 00143 END_TEST 00144 00145 /* Test for bug that writing with pbuf_take_at() did nothing 00146 * and returned ERR_OK when writing at beginning of a pbuf 00147 * in the chain. 00148 */ 00149 START_TEST(test_pbuf_take_at_edge) 00150 { 00151 err_t res; 00152 u8_t *out; 00153 int i; 00154 u8_t testdata[] = { 0x01, 0x08, 0x82, 0x02 }; 00155 struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL); 00156 struct pbuf *q = p->next; 00157 LWIP_UNUSED_ARG(_i); 00158 /* alloc big enough to get a chain of pbufs */ 00159 fail_if(p->tot_len == p->len); 00160 memset(p->payload, 0, p->len); 00161 memset(q->payload, 0, q->len); 00162 00163 /* copy data to the beginning of first pbuf */ 00164 res = pbuf_take_at(p, &testdata, sizeof(testdata), 0); 00165 fail_unless(res == ERR_OK); 00166 00167 out = (u8_t*)p->payload; 00168 for (i = 0; i < (int)sizeof(testdata); i++) { 00169 fail_unless(out[i] == testdata[i], 00170 "Bad data at pos %d, was %02X, expected %02X", i, out[i], testdata[i]); 00171 } 00172 00173 /* copy data to the just before end of first pbuf */ 00174 res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len - 1); 00175 fail_unless(res == ERR_OK); 00176 00177 out = (u8_t*)p->payload; 00178 fail_unless(out[p->len - 1] == testdata[0], 00179 "Bad data at pos %d, was %02X, expected %02X", p->len - 1, out[p->len - 1], testdata[0]); 00180 out = (u8_t*)q->payload; 00181 for (i = 1; i < (int)sizeof(testdata); i++) { 00182 fail_unless(out[i-1] == testdata[i], 00183 "Bad data at pos %d, was %02X, expected %02X", p->len - 1 + i, out[i-1], testdata[i]); 00184 } 00185 00186 /* copy data to the beginning of second pbuf */ 00187 res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len); 00188 fail_unless(res == ERR_OK); 00189 00190 out = (u8_t*)p->payload; 00191 for (i = 0; i < (int)sizeof(testdata); i++) { 00192 fail_unless(out[i] == testdata[i], 00193 "Bad data at pos %d, was %02X, expected %02X", p->len+i, out[i], testdata[i]); 00194 } 00195 } 00196 END_TEST 00197 00198 /* Verify pbuf_put_at()/pbuf_get_at() when using 00199 * offsets equal to beginning of new pbuf in chain 00200 */ 00201 START_TEST(test_pbuf_get_put_at_edge) 00202 { 00203 u8_t *out; 00204 u8_t testdata = 0x01; 00205 u8_t getdata; 00206 struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL); 00207 struct pbuf *q = p->next; 00208 LWIP_UNUSED_ARG(_i); 00209 /* alloc big enough to get a chain of pbufs */ 00210 fail_if(p->tot_len == p->len); 00211 memset(p->payload, 0, p->len); 00212 memset(q->payload, 0, q->len); 00213 00214 /* put byte at the beginning of second pbuf */ 00215 pbuf_put_at(p, p->len, testdata); 00216 00217 out = (u8_t*)q->payload; 00218 fail_unless(*out == testdata, 00219 "Bad data at pos %d, was %02X, expected %02X", p->len, *out, testdata); 00220 00221 getdata = pbuf_get_at(p, p->len); 00222 fail_unless(*out == getdata, 00223 "pbuf_get_at() returned bad data at pos %d, was %02X, expected %02X", p->len, getdata, *out); 00224 } 00225 END_TEST 00226 00227 /** Create the suite including all tests for this module */ 00228 Suite * 00229 pbuf_suite(void) 00230 { 00231 testfunc tests[] = { 00232 TESTFUNC(test_pbuf_copy_zero_pbuf), 00233 TESTFUNC(test_pbuf_split_64k_on_small_pbufs), 00234 TESTFUNC(test_pbuf_queueing_bigger_than_64k), 00235 TESTFUNC(test_pbuf_take_at_edge), 00236 TESTFUNC(test_pbuf_get_put_at_edge) 00237 }; 00238 return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown); 00239 }
Generated on Tue Jul 12 2022 13:16:15 by
