1 /* t-http-basic.c - Basic regression tests for http.c
2 * Copyright (C) 2018 g10 Code GmbH
3 *
4 * This file is part of GnuPG.
5 *
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://gnu.org/licenses/>.
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include <config.h>
22 #include <stdlib.h>
23
24 #include "../common/util.h"
25 #include "t-support.h"
26 #include "http.h"
27
28 #define PGM "t-http-basic"
29
30
31 static void
test_http_prepare_redirect(void)32 test_http_prepare_redirect (void)
33 {
34 static struct {
35 const char *url;
36 const char *location;
37 const char *expect_url;
38 gpg_error_t expect_err;
39 } tests[] = {
40 {
41 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
42 NULL,
43 "",
44 GPG_ERR_NO_DATA
45 },
46 {
47 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
48 "",
49 "",
50 GPG_ERR_NO_DATA
51 },
52 {
53 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
54 "foo//bla",
55 "",
56 GPG_ERR_BAD_URI
57 },
58 {
59 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
60 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
61 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
62 0
63 },
64 {
65 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
66 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
67 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
68 0
69 },
70 {
71 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
72 "http://foo.gnupg.org:8080/.not-so-well-known/openpgpkey/hu/12345678",
73 "http://foo.gnupg.org:8080/.well-known/openpgpkey/hu/12345678",
74 0
75 },
76 {
77 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
78 "http:///.no-so-well-known/openpgpkey/hu/12345678",
79 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
80 GPG_ERR_BAD_URI
81 },
82 {
83 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
84 "http://gnupg.org:8080/.not-so-well-known/openpgpkey/hu/12345678",
85 "http://gnupg.org:8080/.not-so-well-known/openpgpkey/hu/12345678",
86 0
87 },
88 {
89 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
90 "http://gnupg.org:8/.not-so-well-known/openpgpkey/hu/12345678",
91 "http://gnupg.org:8/.not-so-well-known/openpgpkey/hu/12345678",
92 0
93 },
94 {
95 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
96 "http://gnupg.org:/.no-so-well-known/openpgpkey/hu/12345678",
97 "http://gnupg.org:/.no-so-well-known/openpgpkey/hu/12345678",
98 0
99 },
100 {
101 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
102 "http://gnupg.org/",
103 "http://gnupg.org/",
104 0
105 },
106 {
107 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
108 "http://gnupg.net",
109 "http://gnupg.net/.well-known/openpgpkey/hu/12345678",
110 0
111 },
112 {
113 "http://gnupg.org",
114 "http://gnupg.org",
115 "http://gnupg.org",
116 0
117 },
118 {
119 "http://gnupg.org",
120 "http://foo.gnupg.org",
121 "http://foo.gnupg.org",
122 0
123 },
124 {
125 "http://gnupg.org/",
126 "http://foo.gnupg.org",
127 "http://foo.gnupg.org/",
128 0
129 },
130 {
131 "http://gnupg.org",
132 "http://foo.gnupg.org/",
133 "http://foo.gnupg.org",
134 0
135 },
136 {
137 "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
138 "http://gnupg.org/something-else",
139 "http://gnupg.org/something-else",
140 0
141 },
142 };
143 int tidx;
144 http_redir_info_t ri;
145 gpg_error_t err;
146 char *newurl;
147
148 err = http_prepare_redirect (NULL, 301, tests[0].location, &newurl);
149 if (gpg_err_code (err) != GPG_ERR_INV_ARG)
150 fail (0);
151 memset (&ri, 0, sizeof ri);
152 err = http_prepare_redirect (&ri, 301, tests[0].location, &newurl);
153 if (gpg_err_code (err) != GPG_ERR_INV_ARG)
154 fail (0);
155 memset (&ri, 0, sizeof ri);
156 ri.silent = 1;
157 ri.orig_url = "http://example.org";
158 err = http_prepare_redirect (&ri, 301, tests[0].location, &newurl);
159 if (gpg_err_code (err) != GPG_ERR_NO_DATA)
160 fail (0);
161
162 for (tidx = 0; tidx < DIM (tests); tidx++)
163 {
164 memset (&ri, 0, sizeof ri);
165 ri.silent = 1;
166 ri.redirects_left = 1;
167 ri.orig_url = tests[tidx].url;
168
169 err = http_prepare_redirect (&ri, 301, tests[tidx].location, &newurl);
170 if (err && newurl)
171 fail (tidx);
172 if (err && gpg_err_code (err) != tests[tidx].expect_err)
173 fail (tidx);
174 if (err)
175 continue;
176 if (!newurl)
177 fail (tidx);
178 if (strcmp (tests[tidx].expect_url, newurl))
179 {
180 fprintf (stderr, "want: '%s'\n", tests[tidx].expect_url);
181 fprintf (stderr, "got : '%s'\n", newurl);
182 fail (tidx);
183 }
184
185 xfree (newurl);
186 }
187 }
188
189
190 int
main(int argc,char ** argv)191 main (int argc, char **argv)
192 {
193 (void)argc;
194 (void)argv;
195
196 test_http_prepare_redirect ();
197
198 return 0;
199 }
200