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
|
/* 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 behavior of nbd_opt_list. */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <libnbd.h>
#include "requires.h"
struct progress {
int id;
int visit;
bool freed;
};
static int
check (void *user_data, const char *name, const char *description)
{
struct progress *p = user_data;
if (p->freed) {
fprintf (stderr, "use after free callback");
exit (EXIT_FAILURE);
}
if (*description) {
fprintf (stderr, "unexpected description for id %d visit %d: %s\n",
p->id, p->visit, description);
exit (EXIT_FAILURE);
}
switch (p->id) {
case 0:
fprintf (stderr, "callback shouldn't be reached when server has error\n");
exit (EXIT_FAILURE);
case 1:
switch (p->visit) {
case 0:
if (strcmp (name, "a") != 0) {
fprintf (stderr, "unexpected name '%s', expecting 'a'\n", name);
exit (EXIT_FAILURE);
}
break;
case 1:
if (strcmp (name, "b") != 0) {
fprintf (stderr, "unexpected name '%s', expecting 'b'\n", name);
exit (EXIT_FAILURE);
}
break;
default:
fprintf (stderr, "callback reached too many times\n");
exit (EXIT_FAILURE);
}
break;
case 2:
fprintf (stderr, "callback shouldn't be reached when list is empty\n");
exit (EXIT_FAILURE);
case 3:
if (p->visit != 0) {
fprintf (stderr, "callback reached too many times\n");
exit (EXIT_FAILURE);
}
if (strcmp (name, "a") != 0) {
fprintf (stderr, "unexpected name '%s', expecting 'a'\n", name);
exit (EXIT_FAILURE);
}
break;
default:
fprintf (stderr, "callback reached with unexpected id %d\n", p->id);
exit (EXIT_FAILURE);
}
p->visit++;
return 0;
}
static void
cb_free (void *user_data)
{
struct progress *p = user_data;
if (p->freed) {
fprintf (stderr, "too many free callbacks");
exit (EXIT_FAILURE);
}
p->freed = true;
}
static struct nbd_handle*
prepare (int i)
{
char mode[] = "mode=X";
char *args[] = { NBDKIT, "-s", "--exit-with-parent", "-v",
"sh", SCRIPT, mode, NULL };
struct nbd_handle *nbd;
/* Get into negotiating state. */
mode[5] = '0' + i;
nbd = nbd_create ();
if (nbd == NULL ||
nbd_set_opt_mode (nbd, true) == -1 ||
nbd_connect_command (nbd, args) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
return nbd;
}
static void
cleanup (struct nbd_handle *nbd)
{
nbd_opt_abort (nbd);
nbd_close (nbd);
}
int
main (int argc, char *argv[])
{
struct nbd_handle *nbd;
int64_t r;
struct progress p;
/* Quick check that nbdkit is new enough */
requires (NBDKIT " sh --dump-plugin | grep -q has_list_exports=1");
/* First pass: server fails NBD_OPT_LIST. */
nbd = prepare (0);
p = (struct progress) { .id = 0 };
r = nbd_opt_list (nbd, (nbd_list_callback) { .callback = check,
.user_data = &p,
.free = cb_free});
if (r != -1) {
fprintf (stderr, "expected error after opt_list\n");
exit (EXIT_FAILURE);
}
if (p.visit != 0) {
fprintf (stderr, "callback called unexpectedly\n");
exit (EXIT_FAILURE);
}
if (!p.freed) {
fprintf (stderr, "callback not freed by libnbd\n");
exit (EXIT_FAILURE);
}
cleanup (nbd);
/* Second pass: server advertises 'a' and 'b'. */
nbd = prepare (1);
p = (struct progress) { .id = 1 };
r = nbd_opt_list (nbd, (nbd_list_callback) { .callback = check,
.user_data = &p,
.free = cb_free});
if (r == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
else if (r != 2 || p.visit != r) {
fprintf (stderr, "wrong number of exports, got %" PRId64 " expecting 2\n",
r);
exit (EXIT_FAILURE);
}
if (!p.freed) {
fprintf (stderr, "callback not freed by libnbd\n");
exit (EXIT_FAILURE);
}
cleanup (nbd);
/* Third pass: server advertises empty list. */
nbd = prepare (2);
p = (struct progress) { .id = 2 };
r = nbd_opt_list (nbd, (nbd_list_callback) { .callback = check,
.user_data = &p,
.free = cb_free});
if (r == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
else if (r != 0 || p.visit != r) {
fprintf (stderr, "wrong number of exports, got %" PRId64 " expecting 0\n",
r);
exit (EXIT_FAILURE);
}
if (!p.freed) {
fprintf (stderr, "callback not freed by libnbd\n");
exit (EXIT_FAILURE);
}
cleanup (nbd);
/* Final pass: server advertises 'a'. */
nbd = prepare (3);
p = (struct progress) { .id = 3 };
r = nbd_opt_list (nbd, (nbd_list_callback) { .callback = check,
.user_data = &p,
.free = cb_free});
if (r == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
else if (r != 1 || p.visit != r) {
fprintf (stderr, "wrong number of exports, got %" PRId64 " expecting 1\n",
r);
exit (EXIT_FAILURE);
}
if (!p.freed) {
fprintf (stderr, "callback not freed by libnbd\n");
exit (EXIT_FAILURE);
}
cleanup (nbd);
exit (EXIT_SUCCESS);
}
|