1 /* NBD client library in userspace
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. */
30 #include "array-size.h"
32 #include "../tests/requires.h"
36 static char tmp
[] = "/tmp/nbdXXXXXX";
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
;
52 append (void *opaque
, const char *name
, const char *description
)
57 if (i
>= ARRAY_SIZE (exports
)) {
58 fprintf (stderr
, "%s: server returned more exports than expected",
63 printf ("append: i=%zu name=\"%s\" description=\"%s\"\n",
64 i
, name
, description
);
67 actual
[i
][0] = strdup (name
);
68 actual
[i
][1] = strdup (description
);
69 if (!actual
[i
][0] || !actual
[i
][1]) abort ();
76 compare_actuals (const void *vp1
, const void *vp2
)
78 return strcmp (* (char * const *) vp1
, * (char * const *) vp2
);
86 for (i
= 0; i
< ARRAY_SIZE (exports
); ++i
) {
93 main (int argc
, char *argv
[])
95 struct nbd_handle
*nbd
;
100 /* Check requirements or skip the test. */
106 /* Create a sparse temporary file. */
107 int fd
= mkstemp (TMPFILE
);
109 ftruncate (fd
, 1024) == -1 ||
114 atexit (unlink_tmpfile
);
119 fprintf (stderr
, "%s\n", nbd_get_error ());
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
131 #define NBD_CONNECT nbd_connect_command
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 ());
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
);
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
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]);
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]);