test_pbuf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "test_pbuf.h"
  2. #include "lwip/pbuf.h"
  3. #include "lwip/stats.h"
  4. #if !LWIP_STATS || !MEM_STATS ||!MEMP_STATS
  5. #error "This tests needs MEM- and MEMP-statistics enabled"
  6. #endif
  7. #if LWIP_DNS
  8. #error "This test needs DNS turned off (as it mallocs on init)"
  9. #endif
  10. #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !LWIP_WND_SCALE
  11. #error "This test needs TCP OOSEQ queueing and window scaling enabled"
  12. #endif
  13. /* Setups/teardown functions */
  14. static void
  15. pbuf_setup(void)
  16. {
  17. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  18. }
  19. static void
  20. pbuf_teardown(void)
  21. {
  22. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  23. }
  24. #define TESTBUFSIZE_1 65535
  25. #define TESTBUFSIZE_2 65530
  26. #define TESTBUFSIZE_3 50050
  27. static u8_t testbuf_1[TESTBUFSIZE_1];
  28. static u8_t testbuf_1a[TESTBUFSIZE_1];
  29. static u8_t testbuf_2[TESTBUFSIZE_2];
  30. static u8_t testbuf_2a[TESTBUFSIZE_2];
  31. static u8_t testbuf_3[TESTBUFSIZE_3];
  32. static u8_t testbuf_3a[TESTBUFSIZE_3];
  33. /* Test functions */
  34. START_TEST(test_pbuf_alloc_zero_pbufs)
  35. {
  36. struct pbuf *p;
  37. LWIP_UNUSED_ARG(_i);
  38. p = pbuf_alloc(PBUF_RAW, 0, PBUF_ROM);
  39. fail_unless(p != NULL);
  40. if (p != NULL) {
  41. pbuf_free(p);
  42. }
  43. p = pbuf_alloc(PBUF_RAW, 0, PBUF_RAM);
  44. fail_unless(p != NULL);
  45. if (p != NULL) {
  46. pbuf_free(p);
  47. }
  48. p = pbuf_alloc(PBUF_RAW, 0, PBUF_REF);
  49. fail_unless(p != NULL);
  50. if (p != NULL) {
  51. pbuf_free(p);
  52. }
  53. p = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
  54. fail_unless(p != NULL);
  55. if (p != NULL) {
  56. pbuf_free(p);
  57. }
  58. }
  59. END_TEST
  60. /** Call pbuf_copy on a pbuf with zero length */
  61. START_TEST(test_pbuf_copy_zero_pbuf)
  62. {
  63. struct pbuf *p1, *p2, *p3;
  64. err_t err;
  65. LWIP_UNUSED_ARG(_i);
  66. fail_unless(lwip_stats.mem.used == 0);
  67. fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
  68. p1 = pbuf_alloc(PBUF_RAW, 1024, PBUF_RAM);
  69. fail_unless(p1 != NULL);
  70. fail_unless(p1->ref == 1);
  71. p2 = pbuf_alloc(PBUF_RAW, 2, PBUF_POOL);
  72. fail_unless(p2 != NULL);
  73. fail_unless(p2->ref == 1);
  74. p2->len = p2->tot_len = 0;
  75. pbuf_cat(p1, p2);
  76. fail_unless(p1->ref == 1);
  77. fail_unless(p2->ref == 1);
  78. p3 = pbuf_alloc(PBUF_RAW, p1->tot_len, PBUF_POOL);
  79. err = pbuf_copy(p3, p1);
  80. fail_unless(err == ERR_VAL);
  81. pbuf_free(p1);
  82. pbuf_free(p3);
  83. fail_unless(lwip_stats.mem.used == 0);
  84. fail_unless(lwip_stats.mem.used == 0);
  85. fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
  86. }
  87. END_TEST
  88. START_TEST(test_pbuf_split_64k_on_small_pbufs)
  89. {
  90. struct pbuf *p, *rest=NULL;
  91. LWIP_UNUSED_ARG(_i);
  92. p = pbuf_alloc(PBUF_RAW, 1, PBUF_POOL);
  93. pbuf_split_64k(p, &rest);
  94. fail_unless(p->tot_len == 1);
  95. pbuf_free(p);
  96. }
  97. END_TEST
  98. START_TEST(test_pbuf_queueing_bigger_than_64k)
  99. {
  100. int i;
  101. err_t err;
  102. struct pbuf *p1, *p2, *p3, *rest2=NULL, *rest3=NULL;
  103. LWIP_UNUSED_ARG(_i);
  104. for(i = 0; i < TESTBUFSIZE_1; i++) {
  105. testbuf_1[i] = (u8_t)rand();
  106. }
  107. for(i = 0; i < TESTBUFSIZE_2; i++) {
  108. testbuf_2[i] = (u8_t)rand();
  109. }
  110. for(i = 0; i < TESTBUFSIZE_3; i++) {
  111. testbuf_3[i] = (u8_t)rand();
  112. }
  113. p1 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_1, PBUF_POOL);
  114. fail_unless(p1 != NULL);
  115. p2 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_2, PBUF_POOL);
  116. fail_unless(p2 != NULL);
  117. p3 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_3, PBUF_POOL);
  118. fail_unless(p3 != NULL);
  119. err = pbuf_take(p1, testbuf_1, TESTBUFSIZE_1);
  120. fail_unless(err == ERR_OK);
  121. err = pbuf_take(p2, testbuf_2, TESTBUFSIZE_2);
  122. fail_unless(err == ERR_OK);
  123. err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3);
  124. fail_unless(err == ERR_OK);
  125. pbuf_cat(p1, p2);
  126. pbuf_cat(p1, p3);
  127. pbuf_split_64k(p1, &rest2);
  128. fail_unless(p1->tot_len == TESTBUFSIZE_1);
  129. fail_unless(rest2->tot_len == (u16_t)((TESTBUFSIZE_2+TESTBUFSIZE_3) & 0xFFFF));
  130. pbuf_split_64k(rest2, &rest3);
  131. fail_unless(rest2->tot_len == TESTBUFSIZE_2);
  132. fail_unless(rest3->tot_len == TESTBUFSIZE_3);
  133. pbuf_copy_partial(p1, testbuf_1a, TESTBUFSIZE_1, 0);
  134. pbuf_copy_partial(rest2, testbuf_2a, TESTBUFSIZE_2, 0);
  135. pbuf_copy_partial(rest3, testbuf_3a, TESTBUFSIZE_3, 0);
  136. fail_if(memcmp(testbuf_1, testbuf_1a, TESTBUFSIZE_1));
  137. fail_if(memcmp(testbuf_2, testbuf_2a, TESTBUFSIZE_2));
  138. fail_if(memcmp(testbuf_3, testbuf_3a, TESTBUFSIZE_3));
  139. pbuf_free(p1);
  140. pbuf_free(rest2);
  141. pbuf_free(rest3);
  142. }
  143. END_TEST
  144. /* Test for bug that writing with pbuf_take_at() did nothing
  145. * and returned ERR_OK when writing at beginning of a pbuf
  146. * in the chain.
  147. */
  148. START_TEST(test_pbuf_take_at_edge)
  149. {
  150. err_t res;
  151. u8_t *out;
  152. int i;
  153. u8_t testdata[] = { 0x01, 0x08, 0x82, 0x02 };
  154. struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
  155. struct pbuf *q = p->next;
  156. LWIP_UNUSED_ARG(_i);
  157. /* alloc big enough to get a chain of pbufs */
  158. fail_if(p->tot_len == p->len);
  159. memset(p->payload, 0, p->len);
  160. memset(q->payload, 0, q->len);
  161. /* copy data to the beginning of first pbuf */
  162. res = pbuf_take_at(p, &testdata, sizeof(testdata), 0);
  163. fail_unless(res == ERR_OK);
  164. out = (u8_t*)p->payload;
  165. for (i = 0; i < (int)sizeof(testdata); i++) {
  166. fail_unless(out[i] == testdata[i],
  167. "Bad data at pos %d, was %02X, expected %02X", i, out[i], testdata[i]);
  168. }
  169. /* copy data to the just before end of first pbuf */
  170. res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len - 1);
  171. fail_unless(res == ERR_OK);
  172. out = (u8_t*)p->payload;
  173. fail_unless(out[p->len - 1] == testdata[0],
  174. "Bad data at pos %d, was %02X, expected %02X", p->len - 1, out[p->len - 1], testdata[0]);
  175. out = (u8_t*)q->payload;
  176. for (i = 1; i < (int)sizeof(testdata); i++) {
  177. fail_unless(out[i-1] == testdata[i],
  178. "Bad data at pos %d, was %02X, expected %02X", p->len - 1 + i, out[i-1], testdata[i]);
  179. }
  180. /* copy data to the beginning of second pbuf */
  181. res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len);
  182. fail_unless(res == ERR_OK);
  183. out = (u8_t*)p->payload;
  184. for (i = 0; i < (int)sizeof(testdata); i++) {
  185. fail_unless(out[i] == testdata[i],
  186. "Bad data at pos %d, was %02X, expected %02X", p->len+i, out[i], testdata[i]);
  187. }
  188. pbuf_free(p);
  189. }
  190. END_TEST
  191. /* Verify pbuf_put_at()/pbuf_get_at() when using
  192. * offsets equal to beginning of new pbuf in chain
  193. */
  194. START_TEST(test_pbuf_get_put_at_edge)
  195. {
  196. u8_t *out;
  197. u8_t testdata = 0x01;
  198. u8_t getdata;
  199. struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
  200. struct pbuf *q = p->next;
  201. LWIP_UNUSED_ARG(_i);
  202. /* alloc big enough to get a chain of pbufs */
  203. fail_if(p->tot_len == p->len);
  204. memset(p->payload, 0, p->len);
  205. memset(q->payload, 0, q->len);
  206. /* put byte at the beginning of second pbuf */
  207. pbuf_put_at(p, p->len, testdata);
  208. out = (u8_t*)q->payload;
  209. fail_unless(*out == testdata,
  210. "Bad data at pos %d, was %02X, expected %02X", p->len, *out, testdata);
  211. getdata = pbuf_get_at(p, p->len);
  212. fail_unless(*out == getdata,
  213. "pbuf_get_at() returned bad data at pos %d, was %02X, expected %02X", p->len, getdata, *out);
  214. pbuf_free(p);
  215. }
  216. END_TEST
  217. /** Create the suite including all tests for this module */
  218. Suite *
  219. pbuf_suite(void)
  220. {
  221. testfunc tests[] = {
  222. TESTFUNC(test_pbuf_alloc_zero_pbufs),
  223. TESTFUNC(test_pbuf_copy_zero_pbuf),
  224. TESTFUNC(test_pbuf_split_64k_on_small_pbufs),
  225. TESTFUNC(test_pbuf_queueing_bigger_than_64k),
  226. TESTFUNC(test_pbuf_take_at_edge),
  227. TESTFUNC(test_pbuf_get_put_at_edge)
  228. };
  229. return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown);
  230. }