unit2602.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "dynbuf.h"
  27. #include "dynhds.h"
  28. #include "curl_trc.h"
  29. static CURLcode unit_setup(void)
  30. {
  31. return CURLE_OK;
  32. }
  33. static void unit_stop(void)
  34. {
  35. }
  36. UNITTEST_START
  37. struct dynhds hds;
  38. struct dynbuf dbuf;
  39. CURLcode result;
  40. size_t i;
  41. /* add 1 more header than allowed */
  42. Curl_dynhds_init(&hds, 2, 128);
  43. fail_if(Curl_dynhds_count(&hds), "should be empty");
  44. fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
  45. fail_if(Curl_dynhds_add(&hds, "test2", 5, "456", 3), "add failed");
  46. /* remove and add without exceeding limits */
  47. for(i = 0; i < 100; ++i) {
  48. if(Curl_dynhds_remove(&hds, "test2", 5) != 1) {
  49. fail_if(TRUE, "should");
  50. break;
  51. }
  52. if(Curl_dynhds_add(&hds, "test2", 5, "456", 3)) {
  53. fail_if(TRUE, "add failed");
  54. break;
  55. }
  56. }
  57. fail_unless(Curl_dynhds_count(&hds) == 2, "should hold 2");
  58. /* set, replacing previous entry without exceeding limits */
  59. for(i = 0; i < 100; ++i) {
  60. if(Curl_dynhds_set(&hds, "test2", 5, "456", 3)) {
  61. fail_if(TRUE, "add failed");
  62. break;
  63. }
  64. }
  65. fail_unless(Curl_dynhds_count(&hds) == 2, "should hold 2");
  66. /* exceed limit on # of entries */
  67. result = Curl_dynhds_add(&hds, "test3", 5, "789", 3);
  68. fail_unless(result, "add should have failed");
  69. fail_unless(Curl_dynhds_count_name(&hds, "test", 4) == 0, "false positive");
  70. fail_unless(Curl_dynhds_count_name(&hds, "test1", 4) == 0, "false positive");
  71. fail_if(Curl_dynhds_get(&hds, "test1", 4), "false positive");
  72. fail_unless(Curl_dynhds_get(&hds, "test1", 5), "false negative");
  73. fail_unless(Curl_dynhds_count_name(&hds, "test1", 5) == 1, "should");
  74. fail_unless(Curl_dynhds_ccount_name(&hds, "test2") == 1, "should");
  75. fail_unless(Curl_dynhds_cget(&hds, "test2"), "should");
  76. fail_unless(Curl_dynhds_ccount_name(&hds, "TEST2") == 1, "should");
  77. fail_unless(Curl_dynhds_ccontains(&hds, "TesT2"), "should");
  78. fail_unless(Curl_dynhds_contains(&hds, "TeSt2", 5), "should");
  79. Curl_dynhds_free(&hds);
  80. /* add header exceeding max overall length */
  81. Curl_dynhds_init(&hds, 128, 10);
  82. fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
  83. fail_unless(Curl_dynhds_add(&hds, "test2", 5, "456", 3), "should fail");
  84. fail_if(Curl_dynhds_add(&hds, "t", 1, "1", 1), "add failed");
  85. Curl_dynhds_reset(&hds);
  86. Curl_dynhds_free(&hds);
  87. Curl_dynhds_init(&hds, 128, 4*1024);
  88. fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
  89. fail_if(Curl_dynhds_add(&hds, "test1", 5, "123", 3), "add failed");
  90. fail_if(Curl_dynhds_cadd(&hds, "blablabla", "thingies"), "add failed");
  91. fail_if(Curl_dynhds_h1_cadd_line(&hds, "blablabla: thingies"), "add failed");
  92. fail_unless(Curl_dynhds_ccount_name(&hds, "blablabla") == 2, "should");
  93. fail_unless(Curl_dynhds_cremove(&hds, "blablabla") == 2, "should");
  94. fail_if(Curl_dynhds_ccontains(&hds, "blablabla"), "should not");
  95. result = Curl_dynhds_h1_cadd_line(&hds, "blablabla thingies");
  96. fail_unless(result, "add should have failed");
  97. if(!result) {
  98. fail_unless(Curl_dynhds_ccount_name(&hds, "bLABlaBlA") == 0, "should");
  99. fail_if(Curl_dynhds_cadd(&hds, "Bla-Bla", "thingies"), "add failed");
  100. Curl_dyn_init(&dbuf, 32*1024);
  101. fail_if(Curl_dynhds_h1_dprint(&hds, &dbuf), "h1 print failed");
  102. if(Curl_dyn_ptr(&dbuf)) {
  103. fail_if(strcmp(Curl_dyn_ptr(&dbuf),
  104. "test1: 123\r\ntest1: 123\r\nBla-Bla: thingies\r\n"),
  105. "h1 format differs");
  106. }
  107. Curl_dyn_free(&dbuf);
  108. }
  109. Curl_dynhds_free(&hds);
  110. Curl_dynhds_init(&hds, 128, 4*1024);
  111. /* continuation without previous header fails */
  112. result = Curl_dynhds_h1_cadd_line(&hds, " indented value");
  113. fail_unless(result, "add should have failed");
  114. /* continuation with previous header must succeed */
  115. fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti1: val1"), "add");
  116. fail_if(Curl_dynhds_h1_cadd_line(&hds, " val2"), "add indent");
  117. fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti2: val1"), "add");
  118. fail_if(Curl_dynhds_h1_cadd_line(&hds, "\tval2"), "add indent");
  119. fail_if(Curl_dynhds_h1_cadd_line(&hds, "ti3: val1"), "add");
  120. fail_if(Curl_dynhds_h1_cadd_line(&hds, " val2"), "add indent");
  121. Curl_dyn_init(&dbuf, 32*1024);
  122. fail_if(Curl_dynhds_h1_dprint(&hds, &dbuf), "h1 print failed");
  123. if(Curl_dyn_ptr(&dbuf)) {
  124. fprintf(stderr, "indent concat: %s\n", Curl_dyn_ptr(&dbuf));
  125. fail_if(strcmp(Curl_dyn_ptr(&dbuf),
  126. "ti1: val1 val2\r\nti2: val1 val2\r\nti3: val1 val2\r\n"),
  127. "wrong format");
  128. }
  129. Curl_dyn_free(&dbuf);
  130. Curl_dynhds_free(&hds);
  131. UNITTEST_STOP