File: interop.c

package info (click to toggle)
libnbd 1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,956 kB
  • sloc: ansic: 55,158; ml: 12,325; sh: 8,811; python: 4,757; makefile: 3,038; perl: 165; cpp: 24
file content (338 lines) | stat: -rw-r--r-- 8,892 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* NBD client library in userspace
 * Copyright Red Hat
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* Test interop with other servers. */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>

#ifdef HAVE_GNUTLS
#include <gnutls/gnutls.h>
#endif

#include <libnbd.h>

#include "../tests/requires.h"

#define SIZE (1024*1024)

#if defined(CERTS) || defined(PSK)
#define TLS 1
#ifndef TLS_MODE
#error "TLS_MODE must be defined when using CERTS || PSK"
#endif
#endif

#define TMPFILE tmp
static char tmp[] = "/tmp/nbdXXXXXX";

static void
unlink_tmpfile (void)
{
  unlink (TMPFILE);
}

#ifdef HAVE_GNUTLS
#if TLS
static void
tls_log (int level, const char *msg)
{
  /* Messages from GnuTLS are always \n-terminated. */
  fprintf (stderr, "gnutls[%d]: %s", level, msg);
}
#endif
#endif

int
main (int argc, char *argv[])
{
  struct nbd_handle *nbd;
  char *args[] = { SERVER, SERVER_PARAMS, NULL };
  int64_t actual_size;
  char buf[512], buf2[512];
  size_t i;
  int r;

  /* Check requirements or skip the test. */
#ifdef REQUIRES
  REQUIRES
#endif

  /* Ignore SIGPIPE.  We only need this for GnuTLS that lacks the
   * GNUTLS_NO_SIGNAL flag, either because it predates GnuTLS 3.4.2 or
   * because the OS lacks MSG_NOSIGNAL support.
   */
#if TLS && !defined (HAVE_GNUTLS_NO_SIGNAL)
  signal (SIGPIPE, SIG_IGN);
#endif

  /* Create a large sparse temporary file. */
  int fd = mkstemp (TMPFILE);
  if (fd == -1 ||
      ftruncate (fd, SIZE) == -1 ||
      close (fd) == -1) {
    perror (TMPFILE);
    exit (EXIT_FAILURE);
  }
  atexit (unlink_tmpfile);

  nbd = nbd_create ();
  if (nbd == NULL) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }

#ifdef EXPORT_NAME
  if (nbd_set_export_name (nbd, EXPORT_NAME) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
#endif

#if TLS
  if (nbd_supports_tls (nbd) != 1) {
    fprintf (stderr, "skip: compiled without TLS support\n");
    exit (77);
  }

#ifdef HAVE_GNUTLS
  /* This is kind of ugly but GnuTLS only allows us to set these
   * globally (so they are not appropriate for libnbd).
   *
   * Also by default GnuTLS throws away log messages even if you
   * called gnutls_global_set_log_level.  It doesn't install the
   * default log handler unless you set $GNUTLS_DEBUG_LEVEL.  So we
   * need to have our own log handler.
   *
   * Also the log levels are quite random.  Level 2 doesn't show the
   * negotiated ciphersuite, but level 3+ shows excessive detail.
   */
  gnutls_global_set_log_level (2);
  gnutls_global_set_log_function (tls_log);
#endif

  if (nbd_set_tls (nbd, TLS_MODE) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
  if (nbd_set_tls_username (nbd, "alice") == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
#ifdef TLS_HOSTNAME
  if (nbd_set_tls_hostname (nbd, TLS_HOSTNAME) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
#endif
#endif /* TLS */

#if defined(CERTS)
  const char *certs = CERTS;
  if (certs && nbd_set_tls_certificates (nbd, certs) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
#elif defined(PSK)
  if (nbd_set_tls_psk_file (nbd, PSK) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
#endif

  /* Print the server parameters. */
  fprintf (stderr, "server: %s", args[0]);
  for (i = 1; args[i] != NULL; ++i)
    fprintf (stderr, " %s", args[i]);
  fprintf (stderr, "\n");

  /* Start the server. */
#if SOCKET_ACTIVATION
#define NBD_CONNECT nbd_connect_systemd_socket_activation
#else
#define NBD_CONNECT nbd_connect_command
#endif
  r = NBD_CONNECT (nbd, args);
#if EXPECT_FAIL
  if (r != -1) {
    fprintf (stderr, "%s: expected connection to fail but it did not\n",
             argv[0]);
    exit (EXIT_FAILURE);
  }
  exit (EXIT_SUCCESS);
  /*NOTREACHED*/
#else
  if (r == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
#endif

#if TLS
  if (TLS_MODE == LIBNBD_TLS_REQUIRE) {
    if (nbd_get_tls_negotiated (nbd) != 1) {
      fprintf (stderr,
               "%s: TLS required, but not negotiated on the connection\n",
               argv[0]);
      exit (EXIT_FAILURE);
    }
  }
  else if (TLS_MODE == LIBNBD_TLS_ALLOW) {
#if TLS_FALLBACK
    if (nbd_get_tls_negotiated (nbd) != 0) {
      fprintf (stderr,
               "%s: TLS disabled, but connection didn't fall back to "
               "plaintext\n",
               argv[0]);
      exit (EXIT_FAILURE);
    }
#else // !TLS_FALLBACK
    if (nbd_get_tls_negotiated (nbd) != 1) {
      fprintf (stderr,
               "%s: TLS allowed, but not negotiated on the connection\n",
               argv[0]);
      exit (EXIT_FAILURE);
    }
#endif
  }
#endif

  /* The apparent size reported over NBD should match the real size of
   * the temporary file.
   */
  actual_size = nbd_get_size (nbd);
  if (actual_size == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }
  if (actual_size != SIZE) {
    fprintf (stderr, "%s: actual size %" PRIi64 " <> expected size %d",
             argv[0], actual_size, SIZE);
    exit (EXIT_FAILURE);
  }

  /* Test reading. */
  if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }

  if (nbd_is_read_only (nbd) == 0) {
    /* Test writing. */
    memset (buf, 0x5a, sizeof buf);
    if (nbd_pwrite (nbd, buf, sizeof buf, 0, 0) == -1) {
      fprintf (stderr, "%s\n", nbd_get_error ());
      exit (EXIT_FAILURE);
    }

    if (nbd_can_flush (nbd) > 0) {
      /* Test flush. */
      if (nbd_flush (nbd, 0) == -1) {
        fprintf (stderr, "%s\n", nbd_get_error ());
        exit (EXIT_FAILURE);
      }
      /* Test the temporary file is updated after flush. */
      fd = open (TMPFILE, O_RDONLY);
      if (fd == -1) {
        perror (TMPFILE);
        exit (EXIT_FAILURE);
      }
      if (read (fd, buf2, sizeof buf2) == -1) {
        perror ("read");
        exit (EXIT_FAILURE);
      }
      close (fd);
      if (memcmp (buf, buf2, sizeof buf) != 0) {
        fprintf (stderr, "%s: error: "
                 "backing file was not updated after write + flush\n",
                 argv[0]);
        exit (EXIT_FAILURE);
      }
    } /* nbd_can_flush */
    else {
      printf ("%s: warning: server does not support flush\n", argv[0]);
    }
  } /* !nbd_is_read_only */
  else {
    printf ("%s: warning: server does not support writes\n", argv[0]);
  }

  if (nbd_can_zero (nbd) > 0) {
    /* Test writing zeroes. */
    if (nbd_zero (nbd, SIZE, 0, 0) == -1) {
      fprintf (stderr, "%s\n", nbd_get_error ());
      exit (EXIT_FAILURE);
    }

    if (nbd_can_flush (nbd) > 0) {
      /* Test flush. */
      if (nbd_flush (nbd, 0) == -1) {
        fprintf (stderr, "%s\n", nbd_get_error ());
        exit (EXIT_FAILURE);
      }
      /* Test the temporary file is all zeroes after flush. */
      fd = open (TMPFILE, O_RDONLY);
      if (fd == -1) {
        perror (TMPFILE);
        exit (EXIT_FAILURE);
      }
      memset (buf, 0, sizeof buf);
      for (i = 0; i < SIZE; i += sizeof buf2) {
        if (read (fd, buf2, sizeof buf2) == -1) {
          perror ("read");
          exit (EXIT_FAILURE);
        }
        if (memcmp (buf, buf2, sizeof buf) != 0) {
          fprintf (stderr, "%s: error: "
                   "backing file was not updated after zero + flush\n",
                   argv[0]);
          exit (EXIT_FAILURE);
        }
      }
      close (fd);
    } /* nbd_can_flush */
    else {
      printf ("%s: warning: server does not support flush\n", argv[0]);
    }
  } /* nbd_can_zero */
  else {
    printf ("%s: warning: server does not support zeroing\n", argv[0]);
  }

  /* XXX Test trim and block_status. */

  if (nbd_shutdown (nbd, 0) == -1) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    exit (EXIT_FAILURE);
  }

  nbd_close (nbd);

  exit (EXIT_SUCCESS);
}