2 * Copyright (C) 2013-2021 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* This is a test of recovering from broken redirects to a mirror
34 * service. See the following bug for background:
35 * https://bugzilla.redhat.com/show_bug.cgi?id=2013000
51 #include "web-server.h"
56 main (int argc
, char *argv
[])
59 CLEANUP_FREE
char *usp_param
= NULL
;
62 struct nbd_handle
*nbd
= NULL
;
64 #ifndef HAVE_CURLOPT_UNIX_SOCKET_PATH
65 fprintf (stderr
, "%s: curl does not support CURLOPT_UNIX_SOCKET_PATH\n",
70 if (access ("disk", F_OK
) == -1) {
71 fprintf (stderr
, "%s: 'disk' not built test skipped\n", argv
[0]);
75 sockpath
= web_server ("disk" /* not used but must be set */, NULL
);
76 if (sockpath
== NULL
) {
77 fprintf (stderr
, "%s: could not start web server thread\n", argv
[0]);
82 if (asprintf (&usp_param
, "unix-socket-path=%s", sockpath
) == -1) {
86 if (test_start_nbdkit ("--filter=retry-request",
87 "curl", usp_param
, "http://localhost/mirror",
88 "retry-request-delay=1",
92 /* The way the test works is we fetch the magic "/mirror" path (see
93 * web-server.c). This redirects to /mirror1, /mirror2, /mirror3
94 * round robin on each request. /mirror1 returns all 1's, /mirror2
95 * returns all 2's, and /mirror3 returns a 404 error. The 404 error
96 * should be transparently skipped by the filter, so we should see
97 * alternating 1's and 2's buffers.
99 for (j
= 0; j
< 5; ++j
) {
100 /* Connect to the NBD socket. */
105 if (nbd_connect_unix (nbd
, sock
/* NBD socket */) == -1)
108 for (i
= 0; i
< 7 /* not divisible by 2 or 3 */; ++i
) {
111 if (nbd_pread (nbd
, buf
, sizeof buf
, 0, 0) == -1)
115 /* First time, set the state to 1 or 2 depending on what
121 /* Subsequent times, check that the mirror flipped to the
124 if (buf
[0] != state
|| buf
[1] != state
|| buf
[511] != state
) {
125 fprintf (stderr
, "%s: unexpected state: expecting %d but found %d\n",
126 argv
[0], (int) state
, (int) buf
[0]);
139 /* Reconnection in the next iteration of the loop will read from
140 * the mirror (because the curl plugin always makes a HEAD request
141 * in curl_open to read the size) which will flip the state, so we
142 * have to do it here.
152 fprintf (stderr
, "%s: %s\n", argv
[0], nbd_get_error ());
153 if (nbd
) nbd_close (nbd
);