• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2019 - 2021, 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 "curlcheck.h"
23 
24 #include "urldata.h"
25 #include "altsvc.h"
26 
27 static CURLcode
unit_setup(void)28 unit_setup(void)
29 {
30   return CURLE_OK;
31 }
32 
33 static void
unit_stop(void)34 unit_stop(void)
35 {
36   curl_global_cleanup();
37 }
38 
39 #if defined(CURL_DISABLE_HTTP) || defined(CURL_DISABLE_ALTSVC)
40 UNITTEST_START
41 {
42   return 0; /* nothing to do when HTTP or alt-svc is disabled */
43 }
44 UNITTEST_STOP
45 #else
46 UNITTEST_START
47 {
48   char outname[256];
49   CURL *curl;
50   CURLcode result;
51   struct altsvcinfo *asi = Curl_altsvc_init();
52   if(!asi)
53     return 1;
54   result = Curl_altsvc_load(asi, arg);
55   if(result) {
56     Curl_altsvc_cleanup(&asi);
57     return result;
58   }
59   curl_global_init(CURL_GLOBAL_ALL);
60   curl = curl_easy_init();
61   if(!curl)
62     goto fail;
63   fail_unless(asi->list.size == 4, "wrong number of entries");
64   msnprintf(outname, sizeof(outname), "%s-out", arg);
65 
66   result = Curl_altsvc_parse(curl, asi, "h2=\"example.com:8080\"\r\n",
67                              ALPN_h1, "example.org", 8080);
68   if(result) {
69     fprintf(stderr, "Curl_altsvc_parse() failed!\n");
70     unitfail++;
71   }
72   fail_unless(asi->list.size == 5, "wrong number of entries");
73 
74   result = Curl_altsvc_parse(curl, asi, "h3=\":8080\"\r\n",
75                              ALPN_h1, "2.example.org", 8080);
76   if(result) {
77     fprintf(stderr, "Curl_altsvc_parse(2) failed!\n");
78     unitfail++;
79   }
80   fail_unless(asi->list.size == 6, "wrong number of entries");
81 
82   result = Curl_altsvc_parse(curl, asi,
83                              "h2=\"example.com:8080\", h3=\"yesyes.com\"\r\n",
84                              ALPN_h1, "3.example.org", 8080);
85   if(result) {
86     fprintf(stderr, "Curl_altsvc_parse(3) failed!\n");
87     unitfail++;
88   }
89   /* that one should make two entries */
90   fail_unless(asi->list.size == 8, "wrong number of entries");
91 
92   result = Curl_altsvc_parse(curl, asi,
93                              "h2=\"example.com:443\"; ma = 120;\r\n",
94                              ALPN_h2, "example.org", 80);
95   if(result) {
96     fprintf(stderr, "Curl_altsvc_parse(4) failed!\n");
97     unitfail++;
98   }
99   fail_unless(asi->list.size == 9, "wrong number of entries");
100 
101   /* quoted 'ma' value */
102   result = Curl_altsvc_parse(curl, asi,
103                              "h2=\"example.net:443\"; ma=\"180\";\r\n",
104                              ALPN_h2, "example.net", 80);
105   if(result) {
106     fprintf(stderr, "Curl_altsvc_parse(4) failed!\n");
107     unitfail++;
108   }
109   fail_unless(asi->list.size == 10, "wrong number of entries");
110 
111   result =
112     Curl_altsvc_parse(curl, asi,
113                       "h2=\":443\", h3=\":443\"; ma = 120; persist = 1\r\n",
114                       ALPN_h1, "curl.se", 80);
115   if(result) {
116     fprintf(stderr, "Curl_altsvc_parse(5) failed!\n");
117     unitfail++;
118   }
119   fail_unless(asi->list.size == 12, "wrong number of entries");
120 
121   /* clear that one again and decrease the counter */
122   result = Curl_altsvc_parse(curl, asi, "clear;\r\n",
123                              ALPN_h1, "curl.se", 80);
124   if(result) {
125     fprintf(stderr, "Curl_altsvc_parse(6) failed!\n");
126     unitfail++;
127   }
128   fail_unless(asi->list.size == 10, "wrong number of entries");
129 
130   Curl_altsvc_save(curl, asi, outname);
131 
132   curl_easy_cleanup(curl);
133   curl_global_cleanup();
134   fail:
135   Curl_altsvc_cleanup(&asi);
136   curl_global_cleanup();
137   return unitfail;
138 }
139 UNITTEST_STOP
140 #endif
141