113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "test.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include "testutil.h"
2713498266Sopenharmony_ci#include "warnless.h"
2813498266Sopenharmony_ci#include "memdebug.h"
2913498266Sopenharmony_ci
3013498266Sopenharmony_cistruct entry {
3113498266Sopenharmony_ci  const char *name;
3213498266Sopenharmony_ci  const char *exp;
3313498266Sopenharmony_ci};
3413498266Sopenharmony_ci
3513498266Sopenharmony_cistatic const struct entry preload_hosts[] = {
3613498266Sopenharmony_ci#if (SIZEOF_TIME_T < 5)
3713498266Sopenharmony_ci  { "1.example.com", "20370320 01:02:03" },
3813498266Sopenharmony_ci  { "2.example.com", "20370320 03:02:01" },
3913498266Sopenharmony_ci  { "3.example.com", "20370319 01:02:03" },
4013498266Sopenharmony_ci#else
4113498266Sopenharmony_ci  { "1.example.com", "25250320 01:02:03" },
4213498266Sopenharmony_ci  { "2.example.com", "25250320 03:02:01" },
4313498266Sopenharmony_ci  { "3.example.com", "25250319 01:02:03" },
4413498266Sopenharmony_ci#endif
4513498266Sopenharmony_ci  { "4.example.com", "" },
4613498266Sopenharmony_ci  { NULL, NULL } /* end of list marker */
4713498266Sopenharmony_ci};
4813498266Sopenharmony_ci
4913498266Sopenharmony_cistruct state {
5013498266Sopenharmony_ci  int index;
5113498266Sopenharmony_ci};
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci/* "read" is from the point of the library, it wants data from us */
5413498266Sopenharmony_cistatic CURLSTScode hstsread(CURL *easy, struct curl_hstsentry *e,
5513498266Sopenharmony_ci                            void *userp)
5613498266Sopenharmony_ci{
5713498266Sopenharmony_ci  const char *host;
5813498266Sopenharmony_ci  const char *expire;
5913498266Sopenharmony_ci  struct state *s = (struct state *)userp;
6013498266Sopenharmony_ci  (void)easy;
6113498266Sopenharmony_ci  host = preload_hosts[s->index].name;
6213498266Sopenharmony_ci  expire = preload_hosts[s->index++].exp;
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci  if(host && (strlen(host) < e->namelen)) {
6513498266Sopenharmony_ci    strcpy(e->name, host);
6613498266Sopenharmony_ci    e->includeSubDomains = FALSE;
6713498266Sopenharmony_ci    strcpy(e->expire, expire);
6813498266Sopenharmony_ci    fprintf(stderr, "add '%s'\n", host);
6913498266Sopenharmony_ci  }
7013498266Sopenharmony_ci  else
7113498266Sopenharmony_ci    return CURLSTS_DONE;
7213498266Sopenharmony_ci  return CURLSTS_OK;
7313498266Sopenharmony_ci}
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci/* verify error from callback */
7613498266Sopenharmony_cistatic CURLSTScode hstsreadfail(CURL *easy, struct curl_hstsentry *e,
7713498266Sopenharmony_ci                                void *userp)
7813498266Sopenharmony_ci{
7913498266Sopenharmony_ci  (void)easy;
8013498266Sopenharmony_ci  (void)e;
8113498266Sopenharmony_ci  (void)userp;
8213498266Sopenharmony_ci  return CURLSTS_FAIL;
8313498266Sopenharmony_ci}
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci/* check that we get the hosts back in the save */
8613498266Sopenharmony_cistatic CURLSTScode hstswrite(CURL *easy, struct curl_hstsentry *e,
8713498266Sopenharmony_ci                             struct curl_index *i, void *userp)
8813498266Sopenharmony_ci{
8913498266Sopenharmony_ci  (void)easy;
9013498266Sopenharmony_ci  (void)userp;
9113498266Sopenharmony_ci  printf("[%zu/%zu] %s %s\n", i->index, i->total, e->name, e->expire);
9213498266Sopenharmony_ci  return CURLSTS_OK;
9313498266Sopenharmony_ci}
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci/*
9613498266Sopenharmony_ci * Read/write HSTS cache entries via callback.
9713498266Sopenharmony_ci */
9813498266Sopenharmony_ci
9913498266Sopenharmony_ciint test(char *URL)
10013498266Sopenharmony_ci{
10113498266Sopenharmony_ci  CURLcode res = CURLE_OK;
10213498266Sopenharmony_ci  CURL *hnd;
10313498266Sopenharmony_ci  struct state st = {0};
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci  easy_init(hnd);
10813498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_URL, URL);
10913498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsread);
11013498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
11113498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
11213498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
11313498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
11413498266Sopenharmony_ci  res = curl_easy_perform(hnd);
11513498266Sopenharmony_ci  curl_easy_cleanup(hnd);
11613498266Sopenharmony_ci  hnd = NULL;
11713498266Sopenharmony_ci  printf("First request returned %d\n", (int)res);
11813498266Sopenharmony_ci  res = CURLE_OK;
11913498266Sopenharmony_ci
12013498266Sopenharmony_ci  easy_init(hnd);
12113498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_URL, URL);
12213498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSREADFUNCTION, hstsreadfail);
12313498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSREADDATA, &st);
12413498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSWRITEFUNCTION, hstswrite);
12513498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTSWRITEDATA, &st);
12613498266Sopenharmony_ci  easy_setopt(hnd, CURLOPT_HSTS_CTRL, CURLHSTS_ENABLE);
12713498266Sopenharmony_ci  res = curl_easy_perform(hnd);
12813498266Sopenharmony_ci  curl_easy_cleanup(hnd);
12913498266Sopenharmony_ci  hnd = NULL;
13013498266Sopenharmony_ci  printf("Second request returned %d\n", (int)res);
13113498266Sopenharmony_ci
13213498266Sopenharmony_citest_cleanup:
13313498266Sopenharmony_ci  curl_easy_cleanup(hnd);
13413498266Sopenharmony_ci  curl_global_cleanup();
13513498266Sopenharmony_ci  return (int)res;
13613498266Sopenharmony_ci}
137