1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2020, 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 ***************************************************************************/
22 #include "test.h"
23
24 #include "testutil.h"
25 #include "warnless.h"
26 #include "memdebug.h"
27
28 static const char *preload_hosts[] = {
29 "1.example.com",
30 "2.example.com",
31 "3.example.com",
32 "4.example.com",
33 NULL /* end of list marker */
34 };
35
36 struct state {
37 int index;
38 };
39
40 /* "read" is from the point of the library, it wants data from us */
hstsread(CURL * easy,struct curl_hstsentry * e,void * userp)41 static CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e,
42 void *userp)
43 {
44 const char *host;
45 struct state *s = (struct state *)userp;
46 (void)easy;
47 host = preload_hosts[s->index++];
48
49 if(host && (strlen(host) < e->namelen)) {
50 strcpy(e->name, host);
51 e->includeSubDomains = FALSE;
52 strcpy(e->expire, "20300320 01:02:03"); /* curl turns 32 that day */
53 fprintf(stderr, "add '%s'\n", host);
54 }
55 else
56 return CURLSTS_DONE;
57 return CURLSTS_OK;
58 }
59
60 /* check that we get the hosts back in the save */
hstswrite(CURL * easy,struct curl_hstsentry * e,struct curl_index * i,void * userp)61 static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e,
62 struct curl_index *i, void *userp)
63 {
64 (void)easy;
65 (void)userp;
66 printf("[%zu/%zu] %s %s\n", i->index, i->total, e->name, e->expire);
67 return CURLSTS_OK;
68 }
69
70 /*
71 * Read/write HSTS cache entries via callback.
72 */
73
test(char * URL)74 int test(char *URL)
75 {
76 CURLcode ret = CURLE_OK;
77 CURL *hnd;
78 struct state st = {0};
79
80 curl_global_init(CURL_GLOBAL_ALL);
81
82 hnd = curl_easy_init();
83 if(hnd) {
84 curl_easy_setopt(hnd, CURLOPT_URL, URL);
85 curl_easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsread);
86 curl_easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
87 curl_easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
88 curl_easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
89 curl_easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
90 ret = curl_easy_perform(hnd);
91 curl_easy_cleanup(hnd);
92 }
93 curl_global_cleanup();
94 return (int)ret;
95 }
96