test_pbuf.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  18. static void
  19. pbuf_teardown(void)
  20. {
  21. }
  22. #define TESTBUFSIZE_1 65535
  23. #define TESTBUFSIZE_2 65530
  24. #define TESTBUFSIZE_3 50050
  25. static u8_t testbuf_1[TESTBUFSIZE_1];
  26. static u8_t testbuf_1a[TESTBUFSIZE_1];
  27. static u8_t testbuf_2[TESTBUFSIZE_2];
  28. static u8_t testbuf_2a[TESTBUFSIZE_2];
  29. static u8_t testbuf_3[TESTBUFSIZE_3];
  30. static u8_t testbuf_3a[TESTBUFSIZE_3];
  31. /* Test functions */
  32. /** Call pbuf_copy on a pbuf with zero length */
  33. START_TEST(test_pbuf_copy_zero_pbuf)
  34. {
  35. struct pbuf *p1, *p2, *p3;
  36. err_t err;
  37. LWIP_UNUSED_ARG(_i);
  38. fail_unless(lwip_stats.mem.used == 0);
  39. fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
  40. p1 = pbuf_alloc(PBUF_RAW, 1024, PBUF_RAM);
  41. fail_unless(p1 != NULL);
  42. fail_unless(p1->ref == 1);
  43. p2 = pbuf_alloc(PBUF_RAW, 2, PBUF_POOL);
  44. fail_unless(p2 != NULL);
  45. fail_unless(p2->ref == 1);
  46. p2->len = p2->tot_len = 0;
  47. pbuf_cat(p1, p2);
  48. fail_unless(p1->ref == 1);
  49. fail_unless(p2->ref == 1);
  50. p3 = pbuf_alloc(PBUF_RAW, p1->tot_len, PBUF_POOL);
  51. err = pbuf_copy(p3, p1);
  52. fail_unless(err == ERR_VAL);
  53. pbuf_free(p1);
  54. pbuf_free(p3);
  55. fail_unless(lwip_stats.mem.used == 0);
  56. fail_unless(lwip_stats.mem.used == 0);
  57. fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
  58. }
  59. END_TEST
  60. START_TEST(test_pbuf_split_64k_on_small_pbufs)
  61. {
  62. struct pbuf *p, *rest=NULL;
  63. LWIP_UNUSED_ARG(_i);
  64. p = pbuf_alloc(PBUF_RAW, 1, PBUF_POOL);
  65. pbuf_split_64k(p, &rest);
  66. fail_unless(p->tot_len == 1);
  67. pbuf_free(p);
  68. }
  69. END_TEST
  70. START_TEST(test_pbuf_queueing_bigger_than_64k)
  71. {
  72. int i;
  73. err_t err;
  74. struct pbuf *p1, *p2, *p3, *rest2=NULL, *rest3=NULL;
  75. LWIP_UNUSED_ARG(_i);
  76. for(i = 0; i < TESTBUFSIZE_1; i++) {
  77. testbuf_1[i] = (u8_t)rand();
  78. }
  79. for(i = 0; i < TESTBUFSIZE_2; i++) {
  80. testbuf_2[i] = (u8_t)rand();
  81. }
  82. for(i = 0; i < TESTBUFSIZE_3; i++) {
  83. testbuf_3[i] = (u8_t)rand();
  84. }
  85. p1 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_1, PBUF_POOL);
  86. fail_unless(p1 != NULL);
  87. p2 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_2, PBUF_POOL);
  88. fail_unless(p2 != NULL);
  89. p3 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_3, PBUF_POOL);
  90. fail_unless(p3 != NULL);
  91. err = pbuf_take(p1, testbuf_1, TESTBUFSIZE_1);
  92. fail_unless(err == ERR_OK);
  93. err = pbuf_take(p2, testbuf_2, TESTBUFSIZE_2);
  94. fail_unless(err == ERR_OK);
  95. err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3);
  96. fail_unless(err == ERR_OK);
  97. pbuf_cat(p1, p2);
  98. pbuf_cat(p1, p3);
  99. pbuf_split_64k(p1, &rest2);
  100. fail_unless(p1->tot_len == TESTBUFSIZE_1);
  101. fail_unless(rest2->tot_len == (u16_t)((TESTBUFSIZE_2+TESTBUFSIZE_3) & 0xFFFF));
  102. pbuf_split_64k(rest2, &rest3);
  103. fail_unless(rest2->tot_len == TESTBUFSIZE_2);
  104. fail_unless(rest3->tot_len == TESTBUFSIZE_3);
  105. pbuf_copy_partial(p1, testbuf_1a, TESTBUFSIZE_1, 0);
  106. pbuf_copy_partial(rest2, testbuf_2a, TESTBUFSIZE_2, 0);
  107. pbuf_copy_partial(rest3, testbuf_3a, TESTBUFSIZE_3, 0);
  108. for(i = 0; i < TESTBUFSIZE_1; i++)
  109. fail_unless(testbuf_1[i] == testbuf_1a[i]);
  110. for(i = 0; i < TESTBUFSIZE_2; i++)
  111. fail_unless(testbuf_2[i] == testbuf_2a[i]);
  112. for(i = 0; i < TESTBUFSIZE_3; i++)
  113. fail_unless(testbuf_3[i] == testbuf_3a[i]);
  114. pbuf_free(p1);
  115. pbuf_free(rest2);
  116. pbuf_free(rest3);
  117. }
  118. END_TEST
  119. /* Test for bug that writing with pbuf_take_at() did nothing
  120. * and returned ERR_OK when writing at beginning of a pbuf
  121. * in the chain.
  122. */
  123. START_TEST(test_pbuf_take_at_edge)
  124. {
  125. err_t res;
  126. u8_t *out;
  127. int i;
  128. u8_t testdata[] = { 0x01, 0x08, 0x82, 0x02 };
  129. struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
  130. struct pbuf *q = p->next;
  131. LWIP_UNUSED_ARG(_i);
  132. /* alloc big enough to get a chain of pbufs */
  133. fail_if(p->tot_len == p->len);
  134. memset(p->payload, 0, p->len);
  135. memset(q->payload, 0, q->len);
  136. /* copy data to the beginning of first pbuf */
  137. res = pbuf_take_at(p, &testdata, sizeof(testdata), 0);
  138. fail_unless(res == ERR_OK);
  139. out = (u8_t*)p->payload;
  140. for (i = 0; i < (int)sizeof(testdata); i++) {
  141. fail_unless(out[i] == testdata[i],
  142. "Bad data at pos %d, was %02X, expected %02X", i, out[i], testdata[i]);
  143. }
  144. /* copy data to the just before end of first pbuf */
  145. res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len - 1);
  146. fail_unless(res == ERR_OK);
  147. out = (u8_t*)p->payload;
  148. fail_unless(out[p->len - 1] == testdata[0],
  149. "Bad data at pos %d, was %02X, expected %02X", p->len - 1, out[p->len - 1], testdata[0]);
  150. out = (u8_t*)q->payload;
  151. for (i = 1; i < (int)sizeof(testdata); i++) {
  152. fail_unless(out[i-1] == testdata[i],
  153. "Bad data at pos %d, was %02X, expected %02X", p->len - 1 + i, out[i-1], testdata[i]);
  154. }
  155. /* copy data to the beginning of second pbuf */
  156. res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len);
  157. fail_unless(res == ERR_OK);
  158. out = (u8_t*)p->payload;
  159. for (i = 0; i < (int)sizeof(testdata); i++) {
  160. fail_unless(out[i] == testdata[i],
  161. "Bad data at pos %d, was %02X, expected %02X", p->len+i, out[i], testdata[i]);
  162. }
  163. }
  164. END_TEST
  165. /* Verify pbuf_put_at()/pbuf_get_at() when using
  166. * offsets equal to beginning of new pbuf in chain
  167. */
  168. START_TEST(test_pbuf_get_put_at_edge)
  169. {
  170. u8_t *out;
  171. u8_t testdata = 0x01;
  172. u8_t getdata;
  173. struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
  174. struct pbuf *q = p->next;
  175. LWIP_UNUSED_ARG(_i);
  176. /* alloc big enough to get a chain of pbufs */
  177. fail_if(p->tot_len == p->len);
  178. memset(p->payload, 0, p->len);
  179. memset(q->payload, 0, q->len);
  180. /* put byte at the beginning of second pbuf */
  181. pbuf_put_at(p, p->len, testdata);
  182. out = (u8_t*)q->payload;
  183. fail_unless(*out == testdata,
  184. "Bad data at pos %d, was %02X, expected %02X", p->len, *out, testdata);
  185. getdata = pbuf_get_at(p, p->len);
  186. fail_unless(*out == getdata,
  187. "pbuf_get_at() returned bad data at pos %d, was %02X, expected %02X", p->len, getdata, *out);
  188. }
  189. END_TEST
  190. /** Create the suite including all tests for this module */
  191. Suite *
  192. pbuf_suite(void)
  193. {
  194. testfunc tests[] = {
  195. TESTFUNC(test_pbuf_copy_zero_pbuf),
  196. TESTFUNC(test_pbuf_split_64k_on_small_pbufs),
  197. TESTFUNC(test_pbuf_queueing_bigger_than_64k),
  198. TESTFUNC(test_pbuf_take_at_edge),
  199. TESTFUNC(test_pbuf_get_put_at_edge)
  200. };
  201. return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown);
  202. }