test_pbuf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. for(i = 0; i < TESTBUFSIZE_1; i++)
  137. fail_unless(testbuf_1[i] == testbuf_1a[i]);
  138. for(i = 0; i < TESTBUFSIZE_2; i++)
  139. fail_unless(testbuf_2[i] == testbuf_2a[i]);
  140. for(i = 0; i < TESTBUFSIZE_3; i++)
  141. fail_unless(testbuf_3[i] == testbuf_3a[i]);
  142. pbuf_free(p1);
  143. pbuf_free(rest2);
  144. pbuf_free(rest3);
  145. }
  146. END_TEST
  147. /* Test for bug that writing with pbuf_take_at() did nothing
  148. * and returned ERR_OK when writing at beginning of a pbuf
  149. * in the chain.
  150. */
  151. START_TEST(test_pbuf_take_at_edge)
  152. {
  153. err_t res;
  154. u8_t *out;
  155. int i;
  156. u8_t testdata[] = { 0x01, 0x08, 0x82, 0x02 };
  157. struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
  158. struct pbuf *q = p->next;
  159. LWIP_UNUSED_ARG(_i);
  160. /* alloc big enough to get a chain of pbufs */
  161. fail_if(p->tot_len == p->len);
  162. memset(p->payload, 0, p->len);
  163. memset(q->payload, 0, q->len);
  164. /* copy data to the beginning of first pbuf */
  165. res = pbuf_take_at(p, &testdata, sizeof(testdata), 0);
  166. fail_unless(res == ERR_OK);
  167. out = (u8_t*)p->payload;
  168. for (i = 0; i < (int)sizeof(testdata); i++) {
  169. fail_unless(out[i] == testdata[i],
  170. "Bad data at pos %d, was %02X, expected %02X", i, out[i], testdata[i]);
  171. }
  172. /* copy data to the just before end of first pbuf */
  173. res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len - 1);
  174. fail_unless(res == ERR_OK);
  175. out = (u8_t*)p->payload;
  176. fail_unless(out[p->len - 1] == testdata[0],
  177. "Bad data at pos %d, was %02X, expected %02X", p->len - 1, out[p->len - 1], testdata[0]);
  178. out = (u8_t*)q->payload;
  179. for (i = 1; i < (int)sizeof(testdata); i++) {
  180. fail_unless(out[i-1] == testdata[i],
  181. "Bad data at pos %d, was %02X, expected %02X", p->len - 1 + i, out[i-1], testdata[i]);
  182. }
  183. /* copy data to the beginning of second pbuf */
  184. res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len);
  185. fail_unless(res == ERR_OK);
  186. out = (u8_t*)p->payload;
  187. for (i = 0; i < (int)sizeof(testdata); i++) {
  188. fail_unless(out[i] == testdata[i],
  189. "Bad data at pos %d, was %02X, expected %02X", p->len+i, out[i], testdata[i]);
  190. }
  191. pbuf_free(p);
  192. }
  193. END_TEST
  194. /* Verify pbuf_put_at()/pbuf_get_at() when using
  195. * offsets equal to beginning of new pbuf in chain
  196. */
  197. START_TEST(test_pbuf_get_put_at_edge)
  198. {
  199. u8_t *out;
  200. u8_t testdata = 0x01;
  201. u8_t getdata;
  202. struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
  203. struct pbuf *q = p->next;
  204. LWIP_UNUSED_ARG(_i);
  205. /* alloc big enough to get a chain of pbufs */
  206. fail_if(p->tot_len == p->len);
  207. memset(p->payload, 0, p->len);
  208. memset(q->payload, 0, q->len);
  209. /* put byte at the beginning of second pbuf */
  210. pbuf_put_at(p, p->len, testdata);
  211. out = (u8_t*)q->payload;
  212. fail_unless(*out == testdata,
  213. "Bad data at pos %d, was %02X, expected %02X", p->len, *out, testdata);
  214. getdata = pbuf_get_at(p, p->len);
  215. fail_unless(*out == getdata,
  216. "pbuf_get_at() returned bad data at pos %d, was %02X, expected %02X", p->len, getdata, *out);
  217. pbuf_free(p);
  218. }
  219. END_TEST
  220. /** Create the suite including all tests for this module */
  221. Suite *
  222. pbuf_suite(void)
  223. {
  224. testfunc tests[] = {
  225. TESTFUNC(test_pbuf_alloc_zero_pbufs),
  226. TESTFUNC(test_pbuf_copy_zero_pbuf),
  227. TESTFUNC(test_pbuf_split_64k_on_small_pbufs),
  228. TESTFUNC(test_pbuf_queueing_bigger_than_64k),
  229. TESTFUNC(test_pbuf_take_at_edge),
  230. TESTFUNC(test_pbuf_get_put_at_edge)
  231. };
  232. return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown);
  233. }