unit1660.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2020 - 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 "hsts.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_HSTS)
  36. UNITTEST_START
  37. {
  38. return 0; /* nothing to do when HTTP or HSTS are disabled */
  39. }
  40. UNITTEST_STOP
  41. #else
  42. struct testit {
  43. const char *host;
  44. const char *chost; /* if non-NULL, use to lookup with */
  45. const char *hdr; /* if NULL, just do the lookup */
  46. const CURLcode result; /* parse result */
  47. };
  48. static const struct testit headers[] = {
  49. /* two entries read from disk cache, verify first */
  50. { "-", "readfrom.example", NULL, CURLE_OK},
  51. { "-", "old.example", NULL, CURLE_OK},
  52. /* delete the remaining one read from disk */
  53. { "readfrom.example", NULL, "max-age=\"0\"", CURLE_OK},
  54. { "example.com", NULL, "max-age=\"31536000\"\r\n", CURLE_OK },
  55. { "example.com", NULL, "max-age=\"21536000\"\r\n", CURLE_OK },
  56. { "example.com", NULL, "max-age=\"21536000\"; \r\n", CURLE_OK },
  57. { "example.com", NULL, "max-age=\"21536000\"; includeSubDomains\r\n",
  58. CURLE_OK },
  59. { "example.org", NULL, "max-age=\"31536000\"\r\n", CURLE_OK },
  60. { "this.example", NULL, "max=\"31536\";", CURLE_BAD_FUNCTION_ARGUMENT },
  61. { "this.example", NULL, "max-age=\"31536", CURLE_BAD_FUNCTION_ARGUMENT },
  62. { "this.example", NULL, "max-age=31536\"", CURLE_OK },
  63. /* max-age=0 removes the entry */
  64. { "this.example", NULL, "max-age=0", CURLE_OK },
  65. { "another.example", NULL, "includeSubDomains; ",
  66. CURLE_BAD_FUNCTION_ARGUMENT },
  67. /* Two max-age is illegal */
  68. { "example.com", NULL,
  69. "max-age=\"21536000\"; includeSubDomains; max-age=\"3\";",
  70. CURLE_BAD_FUNCTION_ARGUMENT },
  71. /* Two includeSubDomains is illegal */
  72. { "2.example.com", NULL,
  73. "max-age=\"21536000\"; includeSubDomains; includeSubDomains;",
  74. CURLE_BAD_FUNCTION_ARGUMENT },
  75. /* use a unknown directive "include" that should be ignored */
  76. { "3.example.com", NULL, "max-age=\"21536000\"; include; includeSubDomains;",
  77. CURLE_OK },
  78. /* remove the "3.example.com" one, should still match the example.com */
  79. { "3.example.com", NULL, "max-age=\"0\"; includeSubDomains;",
  80. CURLE_OK },
  81. { "-", "foo.example.com", NULL, CURLE_OK},
  82. { "-", "foo.xample.com", NULL, CURLE_OK},
  83. /* should not match */
  84. { "example.net", "forexample.net", "max-age=\"31536000\"\r\n", CURLE_OK },
  85. /* should not match either, since forexample.net is not in the example.net
  86. domain */
  87. { "example.net", "forexample.net",
  88. "max-age=\"31536000\"; includeSubDomains\r\n", CURLE_OK },
  89. /* remove example.net again */
  90. { "example.net", NULL, "max-age=\"0\"; includeSubDomains\r\n", CURLE_OK },
  91. /* make this live for 7 seconds */
  92. { "expire.example", NULL, "max-age=\"7\"\r\n", CURLE_OK },
  93. { NULL, NULL, NULL, 0 }
  94. };
  95. static void showsts(struct stsentry *e, const char *chost)
  96. {
  97. if(!e)
  98. printf("'%s' is not HSTS\n", chost);
  99. else {
  100. printf("%s [%s]: %" CURL_FORMAT_CURL_OFF_T "%s\n",
  101. chost, e->host, e->expires,
  102. e->includeSubDomains ? " includeSubDomains" : "");
  103. }
  104. }
  105. UNITTEST_START
  106. {
  107. CURLcode result;
  108. struct stsentry *e;
  109. struct hsts *h = Curl_hsts_init();
  110. int i;
  111. const char *chost;
  112. CURL *easy;
  113. if(!h)
  114. return 1;
  115. curl_global_init(CURL_GLOBAL_ALL);
  116. easy = curl_easy_init();
  117. if(!easy) {
  118. Curl_hsts_cleanup(&h);
  119. curl_global_cleanup();
  120. return 1;
  121. }
  122. Curl_hsts_loadfile(easy, h, "log/input1660");
  123. for(i = 0; headers[i].host ; i++) {
  124. if(headers[i].hdr) {
  125. result = Curl_hsts_parse(h, headers[i].host, headers[i].hdr);
  126. if(result != headers[i].result) {
  127. fprintf(stderr, "Curl_hsts_parse(%s) failed: %d\n",
  128. headers[i].hdr, result);
  129. unitfail++;
  130. continue;
  131. }
  132. else if(result) {
  133. printf("Input %u: error %d\n", i, (int) result);
  134. continue;
  135. }
  136. }
  137. chost = headers[i].chost ? headers[i].chost : headers[i].host;
  138. e = Curl_hsts(h, chost, TRUE);
  139. showsts(e, chost);
  140. }
  141. printf("Number of entries: %zu\n", h->list.size);
  142. /* verify that it is exists for 7 seconds */
  143. chost = "expire.example";
  144. for(i = 100; i < 110; i++) {
  145. e = Curl_hsts(h, chost, TRUE);
  146. showsts(e, chost);
  147. deltatime++; /* another second passed */
  148. }
  149. (void)Curl_hsts_save(easy, h, "log/hsts1660");
  150. Curl_hsts_cleanup(&h);
  151. curl_easy_cleanup(easy);
  152. curl_global_cleanup();
  153. return unitfail;
  154. }
  155. UNITTEST_STOP
  156. #endif