lib1915.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "test.h"
  25. #include "testtrace.h"
  26. #include "testutil.h"
  27. #include "warnless.h"
  28. #include "memdebug.h"
  29. struct entry {
  30. const char *name;
  31. const char *exp;
  32. };
  33. static const struct entry preload_hosts[] = {
  34. #if (SIZEOF_TIME_T < 5)
  35. { "1.example.com", "20370320 01:02:03" },
  36. { "2.example.com", "20370320 03:02:01" },
  37. { "3.example.com", "20370319 01:02:03" },
  38. #else
  39. { "1.example.com", "25250320 01:02:03" },
  40. { "2.example.com", "25250320 03:02:01" },
  41. { "3.example.com", "25250319 01:02:03" },
  42. #endif
  43. { "4.example.com", "" },
  44. { NULL, NULL } /* end of list marker */
  45. };
  46. struct state {
  47. int index;
  48. };
  49. /* "read" is from the point of the library, it wants data from us */
  50. static CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e,
  51. void *userp)
  52. {
  53. const char *host;
  54. const char *expire;
  55. struct state *s = (struct state *)userp;
  56. (void)easy;
  57. host = preload_hosts[s->index].name;
  58. expire = preload_hosts[s->index++].exp;
  59. if(host && (strlen(host) < e->namelen)) {
  60. strcpy(e->name, host);
  61. e->includeSubDomains = FALSE;
  62. strcpy(e->expire, expire);
  63. fprintf(stderr, "add '%s'\n", host);
  64. }
  65. else
  66. return CURLSTS_DONE;
  67. return CURLSTS_OK;
  68. }
  69. /* verify error from callback */
  70. static CURLSTScode hstsreadfail(CURL *easy, struct curl_hstsentry *e,
  71. void *userp)
  72. {
  73. (void)easy;
  74. (void)e;
  75. (void)userp;
  76. return CURLSTS_FAIL;
  77. }
  78. /* check that we get the hosts back in the save */
  79. static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e,
  80. struct curl_index *i, void *userp)
  81. {
  82. (void)easy;
  83. (void)userp;
  84. printf("[%zu/%zu] %s %s\n", i->index, i->total, e->name, e->expire);
  85. return CURLSTS_OK;
  86. }
  87. /*
  88. * Read/write HSTS cache entries via callback.
  89. */
  90. CURLcode test(char *URL)
  91. {
  92. CURLcode res = CURLE_OK;
  93. CURL *hnd;
  94. struct state st = {0};
  95. global_init(CURL_GLOBAL_ALL);
  96. libtest_debug_config.nohex = 1;
  97. libtest_debug_config.tracetime = 1;
  98. easy_init(hnd);
  99. easy_setopt(hnd, CURLOPT_URL, URL);
  100. easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT, 1L);
  101. easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsread);
  102. easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
  103. easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
  104. easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
  105. easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
  106. easy_setopt(hnd, CURLOPT_DEBUGDATA, &libtest_debug_config);
  107. easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
  108. easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  109. res = curl_easy_perform(hnd);
  110. curl_easy_cleanup(hnd);
  111. hnd = NULL;
  112. if(res == CURLE_OPERATION_TIMEDOUT) /* we expect that on Windows */
  113. res = CURLE_COULDNT_CONNECT;
  114. printf("First request returned %d\n", res);
  115. res = CURLE_OK;
  116. easy_init(hnd);
  117. easy_setopt(hnd, CURLOPT_URL, URL);
  118. easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT, 1L);
  119. easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsreadfail);
  120. easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
  121. easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
  122. easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
  123. easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
  124. easy_setopt(hnd, CURLOPT_DEBUGDATA, &libtest_debug_config);
  125. easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
  126. easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
  127. res = curl_easy_perform(hnd);
  128. curl_easy_cleanup(hnd);
  129. hnd = NULL;
  130. printf("Second request returned %d\n", res);
  131. test_cleanup:
  132. curl_easy_cleanup(hnd);
  133. curl_global_cleanup();
  134. return res;
  135. }