unit1654.c 4.2 KB

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