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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <errno.h>
#include <stdlib.h>
#include <libnvme.h>
#include "mock.h"
#include "util.h"
#define TEST_FD 0xFD
#define TEST_NSID 0x12345678
#define TEST_NVMSETID 0xABCD
#define TEST_UUID 123
#define TEST_CSI NVME_CSI_KV
#define TEST_CNTID 0x4321
#define TEST_DOMID 0xFEDC
#define TEST_ENDGID 0x0123
#define TEST_SC NVME_SC_INVALID_FIELD
static void test_ns(void)
{
struct nvme_id_ns expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_NS,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_ns(TEST_FD, TEST_NSID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_ctrl(void)
{
struct nvme_id_ctrl expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CTRL,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_ctrl(TEST_FD, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_active_ns_list(void)
{
struct nvme_ns_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_NS_ACTIVE_LIST,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_active_ns_list(TEST_FD, TEST_NSID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_ns_descs(void)
{
uint8_t expected_id[NVME_IDENTIFY_DATA_SIZE];
struct nvme_ns_id_desc *id;
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_NS_DESC_LIST,
.out_data = &expected_id,
};
int err;
arbitrary(expected_id, sizeof(expected_id));
id = calloc(1, NVME_IDENTIFY_DATA_SIZE);
check(id, "memory allocation failed");
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_ns_descs(TEST_FD, TEST_NSID, id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(id, expected_id, sizeof(expected_id), "incorrect identify data");
free(id);
}
static void test_nvmset_list(void)
{
struct nvme_id_nvmset_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_NVMSET_LIST,
.cdw11 = TEST_NVMSETID,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_nvmset_list(TEST_FD, TEST_NVMSETID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_ns_csi(void)
{
uint8_t expected_id[NVME_IDENTIFY_DATA_SIZE];
uint8_t id[NVME_IDENTIFY_DATA_SIZE] = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CSI_NS,
.cdw11 = TEST_CSI << 24,
.cdw14 = TEST_UUID,
.out_data = expected_id,
};
int err;
arbitrary(expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_ns_csi(TEST_FD, TEST_NSID, TEST_UUID, TEST_CSI, id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(id, expected_id, sizeof(id), "incorrect identify data");
}
static void test_zns_identify_ns(void)
{
struct nvme_zns_id_ns expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CSI_NS,
.cdw11 = NVME_CSI_ZNS << 24,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_zns_identify_ns(TEST_FD, TEST_NSID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_nvm_identify_ctrl(void)
{
struct nvme_id_ctrl_nvm expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CSI_CTRL,
.cdw11 = NVME_CSI_NVM << 24,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_nvm_identify_ctrl(TEST_FD, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_zns_identify_ctrl(void)
{
struct nvme_zns_id_ctrl expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CSI_CTRL,
.cdw11 = NVME_CSI_ZNS << 24,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_zns_identify_ctrl(TEST_FD, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_active_ns_list_csi(void)
{
struct nvme_ns_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CSI_NS_ACTIVE_LIST,
.cdw11 = TEST_CSI << 24,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_active_ns_list_csi(
TEST_FD, TEST_NSID, TEST_CSI, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_independent_identify_ns(void)
{
struct nvme_id_independent_id_ns expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CSI_INDEPENDENT_ID_NS,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
/* That's a mouthful! */
err = nvme_identify_independent_identify_ns(TEST_FD, TEST_NSID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_allocated_ns_list(void)
{
struct nvme_ns_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_ALLOCATED_NS_LIST,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_allocated_ns_list(TEST_FD, TEST_NSID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_allocated_ns(void)
{
struct nvme_id_ns expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_ALLOCATED_NS,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_allocated_ns(TEST_FD, TEST_NSID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_nsid_ctrl_list(void)
{
struct nvme_ctrl_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = TEST_CNTID << 16
| NVME_IDENTIFY_CNS_NS_CTRL_LIST,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_nsid_ctrl_list(TEST_FD, TEST_NSID, TEST_CNTID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_ctrl_list(void)
{
struct nvme_ctrl_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = TEST_CNTID << 16
| NVME_IDENTIFY_CNS_CTRL_LIST,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_ctrl_list(TEST_FD, TEST_CNTID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_primary_ctrl(void)
{
struct nvme_primary_ctrl_cap expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = TEST_CNTID << 16
| NVME_IDENTIFY_CNS_PRIMARY_CTRL_CAP,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_primary_ctrl(TEST_FD, TEST_CNTID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_secondary_ctrl_list(void)
{
struct nvme_secondary_ctrl_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = TEST_CNTID << 16
| NVME_IDENTIFY_CNS_SECONDARY_CTRL_LIST,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_secondary_ctrl_list(TEST_FD, TEST_CNTID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_ns_granularity(void)
{
struct nvme_id_ns_granularity_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_NS_GRANULARITY,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_ns_granularity(TEST_FD, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_uuid(void)
{
struct nvme_id_uuid_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_UUID_LIST,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_uuid(TEST_FD, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_domain_list(void)
{
struct nvme_id_domain_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_DOMAIN_LIST,
.cdw11 = TEST_DOMID,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_domain_list(TEST_FD, TEST_DOMID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_endurance_group_list(void)
{
struct nvme_id_endurance_group_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_ENDURANCE_GROUP_ID,
.cdw11 = TEST_ENDGID,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_endurance_group_list(TEST_FD, TEST_ENDGID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_allocated_ns_list_csi(void)
{
struct nvme_ns_list expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(expected_id),
.cdw10 = NVME_IDENTIFY_CNS_CSI_ALLOCATED_NS_LIST,
.cdw11 = TEST_CSI << 24,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_allocated_ns_list_csi(
TEST_FD, TEST_NSID, TEST_CSI, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
static void test_iocs(void)
{
struct nvme_id_iocs expected_id, id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(expected_id),
.cdw10 = TEST_CNTID << 16
| NVME_IDENTIFY_CNS_COMMAND_SET_STRUCTURE,
.out_data = &expected_id,
};
int err;
arbitrary(&expected_id, sizeof(expected_id));
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_iocs(TEST_FD, TEST_CNTID, &id);
end_mock_cmds();
check(err == 0, "identify returned error %d, errno %m", err);
cmp(&id, &expected_id, sizeof(id), "incorrect identify data");
}
/*
* All identify functions tail-call nvme_identify(),
* so testing errors in any of them will do
*/
static void test_status_code_error(void)
{
struct nvme_id_nvmset_list id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.data_len = sizeof(id),
.cdw10 = NVME_IDENTIFY_CNS_NVMSET_LIST,
.cdw11 = TEST_NVMSETID,
.err = TEST_SC,
};
int err;
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_nvmset_list(TEST_FD, TEST_NVMSETID, &id);
end_mock_cmds();
check(err == TEST_SC, "got error %d, expected %d", err, TEST_SC);
}
static void test_kernel_error(void)
{
struct nvme_id_ns id = {};
struct mock_cmd mock_admin_cmd = {
.opcode = nvme_admin_identify,
.nsid = TEST_NSID,
.data_len = sizeof(id),
.cdw10 = NVME_IDENTIFY_CNS_NS,
.err = -EIO,
};
int err;
set_mock_admin_cmds(&mock_admin_cmd, 1);
err = nvme_identify_ns(TEST_FD, TEST_NSID, &id);
end_mock_cmds();
check(err == -1, "got error %d, expected -1", err);
check(errno == EIO, "unexpected error %m");
}
static void run_test(const char *test_name, void (*test_fn)(void))
{
printf("Running test %s...", test_name);
fflush(stdout);
test_fn();
puts(" OK");
}
#define RUN_TEST(name) run_test(#name, test_ ## name)
int main(void)
{
set_mock_fd(TEST_FD);
RUN_TEST(ns);
RUN_TEST(ctrl);
RUN_TEST(active_ns_list);
RUN_TEST(ns_descs);
RUN_TEST(nvmset_list);
RUN_TEST(ns_csi);
RUN_TEST(zns_identify_ns);
RUN_TEST(nvm_identify_ctrl);
RUN_TEST(zns_identify_ctrl);
RUN_TEST(active_ns_list_csi);
RUN_TEST(independent_identify_ns);
RUN_TEST(allocated_ns_list);
RUN_TEST(allocated_ns);
RUN_TEST(nsid_ctrl_list);
RUN_TEST(ctrl_list);
RUN_TEST(primary_ctrl);
RUN_TEST(secondary_ctrl_list);
RUN_TEST(ns_granularity);
RUN_TEST(uuid);
RUN_TEST(domain_list);
RUN_TEST(endurance_group_list);
RUN_TEST(allocated_ns_list_csi);
RUN_TEST(iocs);
RUN_TEST(status_code_error);
RUN_TEST(kernel_error);
}
|