unit2601.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "urldata.h"
  26. #include "bufq.h"
  27. #include "curl_log.h"
  28. static CURLcode unit_setup(void)
  29. {
  30. CURLcode res = CURLE_OK;
  31. return res;
  32. }
  33. static void unit_stop(void)
  34. {
  35. }
  36. static const char *tail_err(struct bufq *q)
  37. {
  38. struct buf_chunk *chunk;
  39. if(!q->tail) {
  40. return q->head? "tail is NULL, but head is not" : NULL;
  41. }
  42. chunk = q->head;
  43. while(chunk) {
  44. if(chunk == q->tail) {
  45. if(chunk->next) {
  46. return "tail points to queue, but not at the end";
  47. }
  48. return NULL;
  49. }
  50. chunk = chunk->next;
  51. }
  52. return "tail not part of queue";
  53. }
  54. static void dump_bufq(struct bufq *q, const char *msg)
  55. {
  56. struct buf_chunk *chunk;
  57. const char *terr;
  58. size_t n;
  59. fprintf(stderr, "bufq[chunk_size=%zu, max_chunks=%zu] %s\n",
  60. q->chunk_size, q->max_chunks, msg);
  61. fprintf(stderr, "- queue[\n");
  62. chunk = q->head;
  63. while(chunk) {
  64. fprintf(stderr, " chunk[len=%zu, roff=%zu, woff=%zu]\n",
  65. chunk->dlen, chunk->r_offset, chunk->w_offset);
  66. chunk = chunk->next;
  67. }
  68. fprintf(stderr, " ]\n");
  69. terr = tail_err(q);
  70. fprintf(stderr, "- tail: %s\n", terr? terr : "ok");
  71. n = 0;
  72. chunk = q->spare;
  73. while(chunk) {
  74. ++n;
  75. chunk = chunk->next;
  76. }
  77. fprintf(stderr, "- chunks: %zu\n", q->chunk_count);
  78. fprintf(stderr, "- spares: %zu\n", n);
  79. }
  80. static unsigned char test_data[32*1024];
  81. static void check_bufq(size_t pool_spares,
  82. size_t chunk_size, size_t max_chunks,
  83. size_t wsize, size_t rsize, int opts)
  84. {
  85. struct bufq q;
  86. struct bufc_pool pool;
  87. size_t max_len = chunk_size * max_chunks;
  88. CURLcode result;
  89. ssize_t n, i;
  90. size_t nwritten, nread;
  91. if(pool_spares > 0) {
  92. Curl_bufcp_init(&pool, chunk_size, pool_spares);
  93. Curl_bufq_initp(&q, &pool, max_chunks, opts);
  94. }
  95. else {
  96. Curl_bufq_init2(&q, chunk_size, max_chunks, opts);
  97. }
  98. fail_unless(q.chunk_size == chunk_size, "chunk_size init wrong");
  99. fail_unless(q.max_chunks == max_chunks, "max_chunks init wrong");
  100. fail_unless(q.head == NULL, "init: head not NULL");
  101. fail_unless(q.tail == NULL, "init: tail not NULL");
  102. fail_unless(q.spare == NULL, "init: spare not NULL");
  103. fail_unless(Curl_bufq_len(&q) == 0, "init: bufq length != 0");
  104. n = Curl_bufq_write(&q, test_data, wsize, &result);
  105. fail_unless(n >= 0, "write: negative size returned");
  106. fail_unless((size_t)n <= wsize, "write: wrong size returned");
  107. fail_unless(result == CURLE_OK, "write: wrong result returned");
  108. /* write empty bufq full */
  109. nwritten = 0;
  110. Curl_bufq_reset(&q);
  111. while(!Curl_bufq_is_full(&q)) {
  112. n = Curl_bufq_write(&q, test_data, wsize, &result);
  113. if(n >= 0) {
  114. nwritten += (size_t)n;
  115. }
  116. else if(result != CURLE_AGAIN) {
  117. fail_unless(result == CURLE_AGAIN, "write-loop: unexpected result");
  118. break;
  119. }
  120. }
  121. if(nwritten != max_len) {
  122. fprintf(stderr, "%zu bytes written, but max_len=%zu\n",
  123. nwritten, max_len);
  124. dump_bufq(&q, "after writing full");
  125. fail_if(TRUE, "write: bufq full but nwritten wrong");
  126. }
  127. /* read full bufq empty */
  128. nread = 0;
  129. while(!Curl_bufq_is_empty(&q)) {
  130. n = Curl_bufq_read(&q, test_data, rsize, &result);
  131. if(n >= 0) {
  132. nread += (size_t)n;
  133. }
  134. else if(result != CURLE_AGAIN) {
  135. fail_unless(result == CURLE_AGAIN, "read-loop: unexpected result");
  136. break;
  137. }
  138. }
  139. if(nread != max_len) {
  140. fprintf(stderr, "%zu bytes read, but max_len=%zu\n",
  141. nwritten, max_len);
  142. dump_bufq(&q, "after reading empty");
  143. fail_if(TRUE, "read: bufq empty but nread wrong");
  144. }
  145. if(q.tail) {
  146. dump_bufq(&q, "after reading empty");
  147. fail_if(TRUE, "read empty, but tail is not NULL");
  148. }
  149. for(i = 0; i < 1000; ++i) {
  150. n = Curl_bufq_write(&q, test_data, wsize, &result);
  151. if(n < 0 && result != CURLE_AGAIN) {
  152. fail_unless(result == CURLE_AGAIN, "rw-loop: unexpected write result");
  153. break;
  154. }
  155. n = Curl_bufq_read(&q, test_data, rsize, &result);
  156. if(n < 0 && result != CURLE_AGAIN) {
  157. fail_unless(result == CURLE_AGAIN, "rw-loop: unexpected read result");
  158. break;
  159. }
  160. }
  161. /* Test SOFT_LIMIT option */
  162. Curl_bufq_free(&q);
  163. Curl_bufq_init2(&q, chunk_size, max_chunks, (opts|BUFQ_OPT_SOFT_LIMIT));
  164. nwritten = 0;
  165. while(!Curl_bufq_is_full(&q)) {
  166. n = Curl_bufq_write(&q, test_data, wsize, &result);
  167. if(n < 0 || (size_t)n != wsize) {
  168. fail_unless(n > 0 && (size_t)n == wsize, "write should be complete");
  169. break;
  170. }
  171. nwritten += (size_t)n;
  172. }
  173. if(nwritten < max_len) {
  174. fprintf(stderr, "%zu bytes written, but max_len=%zu\n",
  175. nwritten, max_len);
  176. dump_bufq(&q, "after writing full");
  177. fail_if(TRUE, "write: bufq full but nwritten wrong");
  178. }
  179. /* do one more write on a full bufq, should work */
  180. n = Curl_bufq_write(&q, test_data, wsize, &result);
  181. fail_unless(n > 0 && (size_t)n == wsize, "write should be complete");
  182. nwritten += (size_t)n;
  183. /* see that we get all out again */
  184. nread = 0;
  185. while(!Curl_bufq_is_empty(&q)) {
  186. n = Curl_bufq_read(&q, test_data, rsize, &result);
  187. if(n <= 0) {
  188. fail_unless(n > 0, "read-loop: unexpected fail");
  189. break;
  190. }
  191. nread += (size_t)n;
  192. }
  193. fail_unless(nread == nwritten, "did not get the same out as put in");
  194. dump_bufq(&q, "at end of test");
  195. Curl_bufq_free(&q);
  196. if(pool_spares > 0)
  197. Curl_bufcp_free(&pool);
  198. }
  199. UNITTEST_START
  200. struct bufq q;
  201. ssize_t n;
  202. CURLcode result;
  203. unsigned char buf[16*1024];
  204. Curl_bufq_init(&q, 8*1024, 12);
  205. n = Curl_bufq_read(&q, buf, 128, &result);
  206. fail_unless(n < 0 && result == CURLE_AGAIN, "read empty fail");
  207. Curl_bufq_free(&q);
  208. check_bufq(0, 1024, 4, 128, 128, BUFQ_OPT_NONE);
  209. check_bufq(0, 1024, 4, 129, 127, BUFQ_OPT_NONE);
  210. check_bufq(0, 1024, 4, 2000, 16000, BUFQ_OPT_NONE);
  211. check_bufq(0, 1024, 4, 16000, 3000, BUFQ_OPT_NONE);
  212. check_bufq(0, 8000, 10, 1234, 1234, BUFQ_OPT_NONE);
  213. check_bufq(0, 8000, 10, 8*1024, 4*1024, BUFQ_OPT_NONE);
  214. check_bufq(0, 1024, 4, 128, 128, BUFQ_OPT_NO_SPARES);
  215. check_bufq(0, 1024, 4, 129, 127, BUFQ_OPT_NO_SPARES);
  216. check_bufq(0, 1024, 4, 2000, 16000, BUFQ_OPT_NO_SPARES);
  217. check_bufq(0, 1024, 4, 16000, 3000, BUFQ_OPT_NO_SPARES);
  218. check_bufq(8, 1024, 4, 128, 128, BUFQ_OPT_NONE);
  219. check_bufq(8, 8000, 10, 1234, 1234, BUFQ_OPT_NONE);
  220. check_bufq(8, 1024, 4, 129, 127, BUFQ_OPT_NO_SPARES);
  221. UNITTEST_STOP