rust: prevent dead_code warning
[nbdkit.git] / tests / test-retry-request-mirror.c
blobb1eb8af02c669362adc5f6d8c3eb1a6e45e15a64
1 /* nbdkit
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
6 * met:
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
30 * SUCH DAMAGE.
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
38 #include <config.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stdbool.h>
43 #include <stdint.h>
44 #include <inttypes.h>
45 #include <string.h>
46 #include <unistd.h>
48 #include <libnbd.h>
50 #include "cleanup.h"
51 #include "web-server.h"
53 #include "test.h"
55 int
56 main (int argc, char *argv[])
58 const char *sockpath;
59 CLEANUP_FREE char *usp_param = NULL;
60 int i, j;
61 char state = 0;
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",
66 argv[0]);
67 exit (77);
68 #endif
70 if (access ("disk", F_OK) == -1) {
71 fprintf (stderr, "%s: 'disk' not built test skipped\n", argv[0]);
72 exit (77);
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]);
78 exit (EXIT_FAILURE);
81 /* Start nbdkit. */
82 if (asprintf (&usp_param, "unix-socket-path=%s", sockpath) == -1) {
83 perror ("asprintf");
84 exit (EXIT_FAILURE);
86 if (test_start_nbdkit ("--filter=retry-request",
87 "curl", usp_param, "http://localhost/mirror",
88 "retry-request-delay=1",
89 NULL) == -1)
90 exit (EXIT_FAILURE);
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. */
101 nbd = nbd_create ();
102 if (nbd == NULL)
103 goto nbd_error;
105 if (nbd_connect_unix (nbd, sock /* NBD socket */) == -1)
106 goto nbd_error;
108 for (i = 0; i < 7 /* not divisible by 2 or 3 */; ++i) {
109 char buf[512];
111 if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1)
112 goto nbd_error;
114 if (state == 0) {
115 /* First time, set the state to 1 or 2 depending on what
116 * we just read.
118 state = buf[0];
120 else {
121 /* Subsequent times, check that the mirror flipped to the
122 * other state.
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]);
127 nbd_close (nbd);
128 exit (EXIT_FAILURE);
132 state++;
133 if (state == 3)
134 state = 1;
137 nbd_close (nbd);
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.
144 state++;
145 if (state == 3)
146 state = 1;
149 exit (EXIT_SUCCESS);
151 nbd_error:
152 fprintf (stderr, "%s: %s\n", argv[0], nbd_get_error ());
153 if (nbd) nbd_close (nbd);
154 exit (EXIT_FAILURE);