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