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