Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / interop / list-exports.c
blob8b15c815ecabd79e6bcf92a40ae8987a2b98d411
1 /* NBD client library in userspace
2 * Copyright Red Hat
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 /* Test nbd_set_list_exports against an NBD server. */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include <libnbd.h>
30 #include "array-size.h"
32 #include "../tests/requires.h"
34 #ifdef NEEDS_TMPFILE
35 #define TMPFILE tmp
36 static char tmp[] = "/tmp/nbdXXXXXX";
38 static void
39 unlink_tmpfile (void)
41 unlink (TMPFILE);
43 #endif /* NEEDS_TMPFILE */
45 static const char *exports[] = { EXPORTS };
46 static const char *descriptions[ARRAY_SIZE (exports)] = { DESCRIPTIONS };
47 static char *actual[ARRAY_SIZE (exports)][2]; /* (name, description) recvd */
49 static char *progname;
51 static int
52 append (void *opaque, const char *name, const char *description)
54 size_t *ip = opaque;
55 size_t i = *ip;
57 if (i >= ARRAY_SIZE (exports)) {
58 fprintf (stderr, "%s: server returned more exports than expected",
59 progname);
60 exit (EXIT_FAILURE);
63 printf ("append: i=%zu name=\"%s\" description=\"%s\"\n",
64 i, name, description);
65 fflush (stdout);
67 actual[i][0] = strdup (name);
68 actual[i][1] = strdup (description);
69 if (!actual[i][0] || !actual[i][1]) abort ();
71 (*ip)++;
72 return 0;
75 static int
76 compare_actuals (const void *vp1, const void *vp2)
78 return strcmp (* (char * const *) vp1, * (char * const *) vp2);
81 static void
82 free_actuals (void)
84 size_t i;
86 for (i = 0; i < ARRAY_SIZE (exports); ++i) {
87 free (actual[i][0]);
88 free (actual[i][1]);
92 int
93 main (int argc, char *argv[])
95 struct nbd_handle *nbd;
96 size_t i = 0;
98 progname = argv[0];
100 /* Check requirements or skip the test. */
101 #ifdef REQUIRES
102 REQUIRES
103 #endif
105 #ifdef NEEDS_TMPFILE
106 /* Create a sparse temporary file. */
107 int fd = mkstemp (TMPFILE);
108 if (fd == -1 ||
109 ftruncate (fd, 1024) == -1 ||
110 close (fd) == -1) {
111 perror (TMPFILE);
112 exit (EXIT_FAILURE);
114 atexit (unlink_tmpfile);
115 #endif
117 nbd = nbd_create ();
118 if (nbd == NULL) {
119 fprintf (stderr, "%s\n", nbd_get_error ());
120 exit (EXIT_FAILURE);
123 /* Set option mode in the handle. */
124 nbd_set_opt_mode (nbd, true);
126 /* Run the NBD server. */
127 char *args[] = { SERVER, SERVER_PARAMS, NULL };
128 #if SOCKET_ACTIVATION
129 #define NBD_CONNECT nbd_connect_systemd_socket_activation
130 #else
131 #define NBD_CONNECT nbd_connect_command
132 #endif
133 if (NBD_CONNECT (nbd, args) == -1 ||
134 nbd_opt_list (nbd, (nbd_list_callback) {
135 .callback = append, .user_data = &i}) == -1) {
136 fprintf (stderr, "%s\n", nbd_get_error ());
137 exit (EXIT_FAILURE);
140 /* Check for expected number of exports. */
141 if (i != ARRAY_SIZE (exports)) {
142 fprintf (stderr, "%s: expected %zu export, but got %zu\n",
143 argv[0], ARRAY_SIZE (exports), i);
144 exit (EXIT_FAILURE);
147 /* Servers won't always return the list of exports in a particular
148 * order. In particular nbdkit-file-plugin returns them in the
149 * order they are read from the directory by readdir. Sort before
150 * comparing.
152 qsort (actual, ARRAY_SIZE (exports), sizeof actual[0], compare_actuals);
154 for (i = 0; i < ARRAY_SIZE (exports); ++i) {
155 if (strcmp (actual[i][0], exports[i]) != 0) {
156 fprintf (stderr, "%s: expected export \"%s\", but got \"%s\"\n",
157 progname, exports[i], actual[i][0]);
158 exit (EXIT_FAILURE);
160 if (strcmp (actual[i][1], descriptions[i]) != 0) {
161 fprintf (stderr, "%s: expected description \"%s\", but got \"%s\"\n",
162 progname, descriptions[i], actual[i][1]);
163 exit (EXIT_FAILURE);
167 nbd_opt_abort (nbd);
168 nbd_close (nbd);
169 free_actuals ();
170 exit (EXIT_SUCCESS);