unit1654.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2019 - 2022, 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 "altsvc.h"
  27. static CURLcode
  28. unit_setup(void)
  29. {
  30. return CURLE_OK;
  31. }
  32. static void
  33. unit_stop(void)
  34. {
  35. curl_global_cleanup();
  36. }
  37. #if defined(CURL_DISABLE_HTTP) || defined(CURL_DISABLE_ALTSVC)
  38. UNITTEST_START
  39. {
  40. return 0; /* nothing to do when HTTP or alt-svc is disabled */
  41. }
  42. UNITTEST_STOP
  43. #else
  44. UNITTEST_START
  45. {
  46. char outname[256];
  47. CURL *curl;
  48. CURLcode result;
  49. struct altsvcinfo *asi = Curl_altsvc_init();
  50. if(!asi)
  51. return 1;
  52. result = Curl_altsvc_load(asi, arg);
  53. if(result) {
  54. Curl_altsvc_cleanup(&asi);
  55. return result;
  56. }
  57. curl_global_init(CURL_GLOBAL_ALL);
  58. curl = curl_easy_init();
  59. if(!curl)
  60. goto fail;
  61. fail_unless(asi->list.size == 4, "wrong number of entries");
  62. msnprintf(outname, sizeof(outname), "%s-out", arg);
  63. result = Curl_altsvc_parse(curl, asi, "h2=\"example.com:8080\"\r\n",
  64. ALPN_h1, "example.org", 8080);
  65. if(result) {
  66. fprintf(stderr, "Curl_altsvc_parse() failed!\n");
  67. unitfail++;
  68. }
  69. fail_unless(asi->list.size == 5, "wrong number of entries");
  70. result = Curl_altsvc_parse(curl, asi, "h3=\":8080\"\r\n",
  71. ALPN_h1, "2.example.org", 8080);
  72. if(result) {
  73. fprintf(stderr, "Curl_altsvc_parse(2) failed!\n");
  74. unitfail++;
  75. }
  76. fail_unless(asi->list.size == 6, "wrong number of entries");
  77. result = Curl_altsvc_parse(curl, asi,
  78. "h2=\"example.com:8080\", h3=\"yesyes.com\"\r\n",
  79. ALPN_h1, "3.example.org", 8080);
  80. if(result) {
  81. fprintf(stderr, "Curl_altsvc_parse(3) failed!\n");
  82. unitfail++;
  83. }
  84. /* that one should make two entries */
  85. fail_unless(asi->list.size == 8, "wrong number of entries");
  86. result = Curl_altsvc_parse(curl, asi,
  87. "h2=\"example.com:443\"; ma = 120;\r\n",
  88. ALPN_h2, "example.org", 80);
  89. if(result) {
  90. fprintf(stderr, "Curl_altsvc_parse(4) failed!\n");
  91. unitfail++;
  92. }
  93. fail_unless(asi->list.size == 9, "wrong number of entries");
  94. /* quoted 'ma' value */
  95. result = Curl_altsvc_parse(curl, asi,
  96. "h2=\"example.net:443\"; ma=\"180\";\r\n",
  97. ALPN_h2, "example.net", 80);
  98. if(result) {
  99. fprintf(stderr, "Curl_altsvc_parse(4) failed!\n");
  100. unitfail++;
  101. }
  102. fail_unless(asi->list.size == 10, "wrong number of entries");
  103. result =
  104. Curl_altsvc_parse(curl, asi,
  105. "h2=\":443\", h3=\":443\"; ma = 120; persist = 1\r\n",
  106. ALPN_h1, "curl.se", 80);
  107. if(result) {
  108. fprintf(stderr, "Curl_altsvc_parse(5) failed!\n");
  109. unitfail++;
  110. }
  111. fail_unless(asi->list.size == 12, "wrong number of entries");
  112. /* clear that one again and decrease the counter */
  113. result = Curl_altsvc_parse(curl, asi, "clear;\r\n",
  114. ALPN_h1, "curl.se", 80);
  115. if(result) {
  116. fprintf(stderr, "Curl_altsvc_parse(6) failed!\n");
  117. unitfail++;
  118. }
  119. fail_unless(asi->list.size == 10, "wrong number of entries");
  120. Curl_altsvc_save(curl, asi, outname);
  121. curl_easy_cleanup(curl);
  122. curl_global_cleanup();
  123. fail:
  124. Curl_altsvc_cleanup(&asi);
  125. curl_global_cleanup();
  126. return unitfail;
  127. }
  128. UNITTEST_STOP
  129. #endif