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