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 "curlcheck.h"
25
26 #include "urldata.h"
27 #include "altsvc.h"
28
29 static CURLcode
unit_setup(void)30 unit_setup(void)
31 {
32 return CURLE_OK;
33 }
34
35 static void
unit_stop(void)36 unit_stop(void)
37 {
38 curl_global_cleanup();
39 }
40
41 UNITTEST_START
42 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_ALTSVC)
43 {
44 char outname[256];
45 CURL *curl;
46 CURLcode result;
47 struct altsvcinfo *asi = Curl_altsvc_init();
48 abort_if(!asi, "Curl_altsvc_i");
49 result = Curl_altsvc_load(asi, arg);
50 if(result) {
51 fail_if(result, "Curl_altsvc_load");
52 goto fail;
53 }
54 curl_global_init(CURL_GLOBAL_ALL);
55 curl = curl_easy_init();
56 if(!curl) {
57 fail_if(!curl, "curl_easy_init");
58 goto fail;
59 }
60 fail_unless(asi->list.size == 4, "wrong number of entries");
61 msnprintf(outname, sizeof(outname), "%s-out", arg);
62
63 result = Curl_altsvc_parse(curl, asi, "h2=\"example.com:8080\"\r\n",
64 ALPN_h1, "example.org", 8080);
65 fail_if(result, "Curl_altsvc_parse() failed!");
66 fail_unless(asi->list.size == 5, "wrong number of entries");
67
68 result = Curl_altsvc_parse(curl, asi, "h3=\":8080\"\r\n",
69 ALPN_h1, "2.example.org", 8080);
70 fail_if(result, "Curl_altsvc_parse(2) failed!");
71 fail_unless(asi->list.size == 6, "wrong number of entries");
72
73 result = Curl_altsvc_parse(curl, asi,
74 "h2=\"example.com:8080\", h3=\"yesyes.com\"\r\n",
75 ALPN_h1, "3.example.org", 8080);
76 fail_if(result, "Curl_altsvc_parse(3) failed!");
77 /* that one should make two entries */
78 fail_unless(asi->list.size == 8, "wrong number of entries");
79
80 result = Curl_altsvc_parse(curl, asi,
81 "h2=\"example.com:443\"; ma = 120;\r\n",
82 ALPN_h2, "example.org", 80);
83 fail_if(result, "Curl_altsvc_parse(4) failed!");
84 fail_unless(asi->list.size == 9, "wrong number of entries");
85
86 /* quoted 'ma' value */
87 result = Curl_altsvc_parse(curl, asi,
88 "h2=\"example.net:443\"; ma=\"180\";\r\n",
89 ALPN_h2, "example.net", 80);
90 fail_if(result, "Curl_altsvc_parse(4) failed!");
91 fail_unless(asi->list.size == 10, "wrong number of entries");
92
93 result =
94 Curl_altsvc_parse(curl, asi,
95 "h2=\":443\", h3=\":443\"; ma = 120; persist = 1\r\n",
96 ALPN_h1, "curl.se", 80);
97 fail_if(result, "Curl_altsvc_parse(5) failed!");
98 fail_unless(asi->list.size == 12, "wrong number of entries");
99
100 /* clear that one again and decrease the counter */
101 result = Curl_altsvc_parse(curl, asi, "clear;\r\n",
102 ALPN_h1, "curl.se", 80);
103 fail_if(result, "Curl_altsvc_parse(6) failed!");
104 fail_unless(asi->list.size == 10, "wrong number of entries");
105
106 Curl_altsvc_save(curl, asi, outname);
107
108 curl_easy_cleanup(curl);
109 fail:
110 Curl_altsvc_cleanup(&asi);
111 }
112 #endif
113 UNITTEST_STOP
114