summaryrefslogtreecommitdiffstats
path: root/dirmngr/t-http-basic.c
blob: edf82efb9836fd29abeae1a367325898892cd0fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* t-http-basic.c - Basic regression tests for http.c
 * Copyright (C) 2018  g10 Code GmbH
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://gnu.org/licenses/>.
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <config.h>
#include <stdlib.h>

#include "../common/util.h"
#include "t-support.h"
#include "http.h"

#define PGM "t-http-basic"


static void
test_http_prepare_redirect (void)
{
  static struct {
    const char *url;
    const char *location;
    const char *expect_url;
    gpg_error_t expect_err;
  } tests[] = {
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      NULL,
      "",
      GPG_ERR_NO_DATA
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "",
      "",
      GPG_ERR_NO_DATA
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "foo//bla",
      "",
      GPG_ERR_BAD_URI
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://foo.gnupg.org:8080/.not-so-well-known/openpgpkey/hu/12345678",
      "http://foo.gnupg.org:8080/.well-known/openpgpkey/hu/12345678",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http:///.no-so-well-known/openpgpkey/hu/12345678",
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      GPG_ERR_BAD_URI
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org:8080/.not-so-well-known/openpgpkey/hu/12345678",
      "http://gnupg.org:8080/.not-so-well-known/openpgpkey/hu/12345678",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org:8/.not-so-well-known/openpgpkey/hu/12345678",
      "http://gnupg.org:8/.not-so-well-known/openpgpkey/hu/12345678",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org:/.no-so-well-known/openpgpkey/hu/12345678",
      "http://gnupg.org:/.no-so-well-known/openpgpkey/hu/12345678",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org/",
      "http://gnupg.org/",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.net",
      "http://gnupg.net/.well-known/openpgpkey/hu/12345678",
      0
    },
    {
      "http://gnupg.org",
      "http://gnupg.org",
      "http://gnupg.org",
      0
    },
    {
      "http://gnupg.org",
      "http://foo.gnupg.org",
      "http://foo.gnupg.org",
      0
    },
    {
      "http://gnupg.org/",
      "http://foo.gnupg.org",
      "http://foo.gnupg.org/",
      0
    },
    {
      "http://gnupg.org",
      "http://foo.gnupg.org/",
      "http://foo.gnupg.org",
      0
    },
    {
      "http://gnupg.org/.well-known/openpgpkey/hu/12345678",
      "http://gnupg.org/something-else",
      "http://gnupg.org/something-else",
      0
    },
  };
  int tidx;
  http_redir_info_t ri;
  gpg_error_t err;
  char *newurl;

  err = http_prepare_redirect (NULL, 301, tests[0].location, &newurl);
  if (gpg_err_code (err) != GPG_ERR_INV_ARG)
    fail (0);
  memset (&ri, 0, sizeof ri);
  err = http_prepare_redirect (&ri, 301, tests[0].location, &newurl);
  if (gpg_err_code (err) != GPG_ERR_INV_ARG)
    fail (0);
  memset (&ri, 0, sizeof ri);
  ri.silent = 1;
  ri.orig_url = "http://example.org";
  err = http_prepare_redirect (&ri, 301, tests[0].location, &newurl);
  if (gpg_err_code (err) != GPG_ERR_NO_DATA)
    fail (0);

  for (tidx = 0; tidx < DIM (tests); tidx++)
    {
      memset (&ri, 0, sizeof ri);
      ri.silent = 1;
      ri.redirects_left = 1;
      ri.orig_url = tests[tidx].url;

      err = http_prepare_redirect (&ri, 301, tests[tidx].location, &newurl);
      if (err && newurl)
        fail (tidx);
      if (err && gpg_err_code (err) != tests[tidx].expect_err)
        fail (tidx);
      if (err)
        continue;
      if (!newurl)
        fail (tidx);
      if (strcmp (tests[tidx].expect_url, newurl))
        {
          fprintf (stderr, "want: '%s'\n", tests[tidx].expect_url);
          fprintf (stderr, "got : '%s'\n", newurl);
          fail (tidx);
        }

      xfree (newurl);
    }
}


int
main (int argc, char **argv)
{
  (void)argc;
  (void)argv;

  test_http_prepare_redirect ();

  return 0;
}