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
|
/* 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_info. */
/* See also unit test 230 in the various language ports. */
#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>
int
main (int argc, char *argv[])
{
struct nbd_handle *nbd;
int64_t r;
char *s;
char *args[] = { NBDKIT, "-s", "--exit-with-parent", "-v",
"sh", SCRIPT, NULL };
/* Get into negotiating state. */
nbd = nbd_create ();
if (nbd == NULL ||
nbd_set_opt_mode (nbd, true) == -1 ||
nbd_connect_command (nbd, args) == -1 ||
nbd_add_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* No size, flags, or meta-contexts yet */
if (nbd_get_size (nbd) != -1) {
fprintf (stderr, "expecting error for get_size\n");
exit (EXIT_FAILURE);
}
if (nbd_is_read_only (nbd) != -1) {
fprintf (stderr, "expecting error for is_read_only\n");
exit (EXIT_FAILURE);
}
if (nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION) != -1) {
fprintf (stderr, "expecting error for can_meta_context\n");
exit (EXIT_FAILURE);
}
/* info with no prior name gets info on "" */
if (nbd_opt_info (nbd) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if ((r = nbd_get_size (nbd)) != 0) {
fprintf (stderr, "expecting size of 0, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if ((r = nbd_is_read_only (nbd)) != 1) {
fprintf (stderr, "expecting read-only export, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if ((r = nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION)) != 1) {
fprintf (stderr, "expecting can_meta_context true, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
/* changing export wipes out prior info */
if (nbd_set_export_name (nbd, "b") == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_get_size (nbd) != -1) {
fprintf (stderr, "expecting error for get_size\n");
exit (EXIT_FAILURE);
}
if (nbd_is_read_only (nbd) != -1) {
fprintf (stderr, "expecting error for is_read_only\n");
exit (EXIT_FAILURE);
}
if (nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION) != -1) {
fprintf (stderr, "expecting error for can_meta_context\n");
exit (EXIT_FAILURE);
}
/* info on something not present fails */
if (nbd_set_export_name (nbd, "a") == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_opt_info (nbd) != -1) {
fprintf (stderr, "expecting error for opt_info\n");
exit (EXIT_FAILURE);
}
/* info for a different export, with automatic meta_context disabled */
if (nbd_set_export_name (nbd, "b") == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_set_request_meta_context (nbd, 0) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_opt_info (nbd) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* idempotent name change is no-op */
if (nbd_set_export_name (nbd, "b") == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if ((r = nbd_get_size (nbd)) != 1) {
fprintf (stderr, "expecting size of 1, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if ((r = nbd_is_read_only (nbd)) != 0) {
fprintf (stderr, "expecting read-write export, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if (nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION) != -1) {
fprintf (stderr, "expecting error for can_meta_context\n");
exit (EXIT_FAILURE);
}
if (nbd_set_request_meta_context (nbd, 1) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* go on something not present */
if (nbd_set_export_name (nbd, "a") == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_opt_go (nbd) != -1) {
fprintf (stderr, "expecting error for opt_go\n");
exit (EXIT_FAILURE);
}
if (nbd_get_size (nbd) != -1) {
fprintf (stderr, "expecting error for get_size\n");
exit (EXIT_FAILURE);
}
if (nbd_is_read_only (nbd) != -1) {
fprintf (stderr, "expecting error for is_read_only\n");
exit (EXIT_FAILURE);
}
if (nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION) != -1) {
fprintf (stderr, "expecting error for can_meta_context\n");
exit (EXIT_FAILURE);
}
/* go on a valid export */
if (nbd_set_export_name (nbd, "good") == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_opt_go (nbd) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if ((r = nbd_get_size (nbd)) != 4) {
fprintf (stderr, "expecting size of 4, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if ((r = nbd_is_read_only (nbd)) != 1) {
fprintf (stderr, "expecting read-only export, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if ((r = nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION)) != 1) {
fprintf (stderr, "expecting can_meta_context true, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
/* now info is no longer valid, but does not wipe data */
if (nbd_set_export_name (nbd, "a") != -1) {
fprintf (stderr, "expecting error for set_export_name\n");
exit (EXIT_FAILURE);
}
if ((s = nbd_get_export_name (nbd)) == NULL) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (strcmp (s, "good") != 0) {
fprintf (stderr, "expecting export name 'good', got '%s'\n", s);
exit (EXIT_FAILURE);
}
free (s);
if (nbd_opt_info (nbd) != -1) {
fprintf (stderr, "expecting error for opt_info\n");
exit (EXIT_FAILURE);
}
if ((r = nbd_get_size (nbd)) != 4) {
fprintf (stderr, "expecting size of 4, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if ((r = nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION)) != 1) {
fprintf (stderr, "expecting can_meta_context true, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
nbd_shutdown (nbd, 0);
nbd_close (nbd);
/* Another connection. This time, check that SET_META triggered by opt_info
* persists through nbd_opt_go with set_request_meta_context disabled.
*/
nbd = nbd_create ();
if (nbd == NULL ||
nbd_set_opt_mode (nbd, true) == -1 ||
nbd_connect_command (nbd, args) == -1 ||
nbd_add_meta_context (nbd, "x-unexpected:bogus") == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION) != -1) {
fprintf (stderr, "expecting error for can_meta_context\n");
exit (EXIT_FAILURE);
}
if (nbd_opt_info (nbd) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if ((r = nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION)) != 0) {
fprintf (stderr, "expecting can_meta_context false, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
if (nbd_set_request_meta_context (nbd, 0) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Adding to the request list now won't matter */
if (nbd_add_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION) != 0) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if (nbd_opt_go (nbd) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
if ((r = nbd_can_meta_context (nbd, LIBNBD_CONTEXT_BASE_ALLOCATION)) != 0) {
fprintf (stderr, "expecting can_meta_context false, got %" PRId64 "\n", r);
exit (EXIT_FAILURE);
}
nbd_shutdown (nbd, 0);
nbd_close (nbd);
exit (EXIT_SUCCESS);
}
|