• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 struct entry {
31   const char *name;
32   const char *exp;
33 };
34 
35 static const struct entry preload_hosts[] = {
36   /* curl turns 39 that day just before 31-bit time_t overflow */
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   { "4.example.com", "" },
41   { NULL, NULL } /* end of list marker */
42 };
43 
44 struct state {
45   int index;
46 };
47 
48 /* "read" is from the point of the library, it wants data from us */
hstsread(CURL * easy,struct curl_hstsentry * e,void * userp)49 static CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e,
50                             void *userp)
51 {
52   const char *host;
53   const char *expire;
54   struct state *s = (struct state *)userp;
55   (void)easy;
56   host = preload_hosts[s->index].name;
57   expire = preload_hosts[s->index++].exp;
58 
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 
70 /* verify error from callback */
hstsreadfail(CURL * easy,struct curl_hstsentry * e,void * userp)71 static CURLSTScode hstsreadfail(CURL *easy, struct curl_hstsentry *e,
72                                 void *userp)
73 {
74   (void)easy;
75   (void)e;
76   (void)userp;
77   return CURLSTS_FAIL;
78 }
79 
80 /* check that we get the hosts back in the save */
hstswrite(CURL * easy,struct curl_hstsentry * e,struct curl_index * i,void * userp)81 static CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e,
82                              struct curl_index *i, void *userp)
83 {
84   (void)easy;
85   (void)userp;
86   printf("[%zu/%zu] %s %s\n", i->index, i->total, e->name, e->expire);
87   return CURLSTS_OK;
88 }
89 
90 /*
91  * Read/write HSTS cache entries via callback.
92  */
93 
test(char * URL)94 int test(char *URL)
95 {
96   CURLcode ret = CURLE_OK;
97   CURL *hnd;
98   struct state st = {0};
99 
100   curl_global_init(CURL_GLOBAL_ALL);
101 
102   hnd = curl_easy_init();
103   if(hnd) {
104     curl_easy_setopt(hnd, CURLOPT_URL, URL);
105     curl_easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsread);
106     curl_easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
107     curl_easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
108     curl_easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
109     curl_easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
110     ret = curl_easy_perform(hnd);
111     curl_easy_cleanup(hnd);
112     printf("First request returned %d\n", (int)ret);
113   }
114   hnd = curl_easy_init();
115   if(hnd) {
116     curl_easy_setopt(hnd, CURLOPT_URL, URL);
117     curl_easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsreadfail);
118     curl_easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
119     curl_easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
120     curl_easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
121     curl_easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
122     ret = curl_easy_perform(hnd);
123     curl_easy_cleanup(hnd);
124     printf("Second request returned %d\n", (int)ret);
125   }
126   curl_global_cleanup();
127   return (int)ret;
128 }
129