1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright 2024 Oxide Computer Company
14 */
15
16 /*
17 * Verify that we generate the appropriate missing field error for the
18 * non-vendor-specific request types.
19 */
20
21 #include <err.h>
22 #include <string.h>
23
24 #include "libnvme_test_common.h"
25
26 static bool
missing_field_err(nvme_ctrl_t * ctrl,const char * desc,nvme_err_t exp_err)27 missing_field_err(nvme_ctrl_t *ctrl, const char *desc, nvme_err_t exp_err)
28 {
29 nvme_err_t err = nvme_ctrl_err(ctrl);
30
31 if (err == exp_err) {
32 return (true);
33 }
34
35 warnx("TEST FAILED: %s returned wrong error %s (0x%x), not %s (0x%x)",
36 desc, nvme_ctrl_errtostr(ctrl, err), err,
37 nvme_ctrl_errtostr(ctrl, exp_err), exp_err);
38 return (false);
39 }
40
41 int
main(void)42 main(void)
43 {
44 int ret = EXIT_SUCCESS;
45 nvme_t *nvme;
46 nvme_ctrl_t *ctrl;
47 nvme_id_req_t *id_req = NULL;
48 nvme_log_req_t *log_req = NULL;
49 nvme_get_feat_req_t *get_feat_req = NULL;
50 nvme_vuc_req_t *vuc_req = NULL;
51 nvme_fw_commit_req_t *fw_commit_req = NULL;
52 nvme_format_req_t *format_req = NULL;
53
54 libnvme_test_init(&nvme, &ctrl);
55
56 if (!nvme_id_req_init_by_cns(ctrl, NVME_CSI_NVM, NVME_IDENTIFY_CTRL,
57 &id_req)) {
58 libnvme_test_ctrl_warn(ctrl, "failed to initialize identify "
59 "request");
60 ret = EXIT_FAILURE;
61 } else if (nvme_id_req_exec(id_req)) {
62 warnx("TEST FAILED: identify request succeeded despite missing "
63 "fields");
64 ret = EXIT_FAILURE;
65 } else if (!missing_field_err(ctrl, "identify request",
66 NVME_ERR_IDENTIFY_REQ_MISSING_FIELDS)) {
67 ret = EXIT_FAILURE;
68 } else {
69 (void) printf("TEST PASSED: identify request generated missing "
70 "fields error\n");
71 }
72 nvme_id_req_fini(id_req);
73
74 if (!nvme_log_req_init(ctrl, &log_req)) {
75 libnvme_test_ctrl_warn(ctrl, "failed to initialize log "
76 "request");
77 ret = EXIT_FAILURE;
78 } else if (nvme_log_req_exec(log_req)) {
79 warnx("TEST FAILED: log request succeeded despite missing "
80 "fields");
81 ret = EXIT_FAILURE;
82 } else if (!missing_field_err(ctrl, "log request",
83 NVME_ERR_LOG_REQ_MISSING_FIELDS)) {
84 ret = EXIT_FAILURE;
85 } else {
86 (void) printf("TEST PASSED: log request generated missing "
87 "fields error\n");
88 }
89 nvme_log_req_fini(log_req);
90
91 if (!nvme_get_feat_req_init(ctrl, &get_feat_req)) {
92 libnvme_test_ctrl_warn(ctrl, "failed to initialize get feature "
93 "request");
94 ret = EXIT_FAILURE;
95 } else if (nvme_get_feat_req_exec(get_feat_req)) {
96 warnx("TEST FAILED: get feature request succeeded despite "
97 "missing fields");
98 ret = EXIT_FAILURE;
99 } else if (!missing_field_err(ctrl, "get feature request",
100 NVME_ERR_GET_FEAT_REQ_MISSING_FIELDS)) {
101 ret = EXIT_FAILURE;
102 } else {
103 (void) printf("TEST PASSED: get feature request generated "
104 "missing fields error\n");
105 }
106 nvme_get_feat_req_fini(get_feat_req);
107
108 if (!nvme_vuc_req_init(ctrl, &vuc_req)) {
109 if (nvme_ctrl_err(ctrl) == NVME_ERR_VUC_UNSUP_BY_DEV) {
110 warnx("TEST SKIPPED: device does not support VUC "
111 "requests");
112 } else {
113 libnvme_test_ctrl_warn(ctrl, "failed to initialize vuc "
114 "request");
115 ret = EXIT_FAILURE;
116 }
117 } else if (nvme_vuc_req_exec(vuc_req)) {
118 warnx("TEST FAILED: vuc request succeeded despite missing "
119 "fields");
120 ret = EXIT_FAILURE;
121 } else if (!missing_field_err(ctrl, "vuc request",
122 NVME_ERR_VUC_REQ_MISSING_FIELDS)) {
123 ret = EXIT_FAILURE;
124 } else {
125 (void) printf("TEST PASSED: vuc request generated missing "
126 "fields error\n");
127 }
128 nvme_vuc_req_fini(vuc_req);
129
130 if (!nvme_fw_commit_req_init(ctrl, &fw_commit_req)) {
131 if (nvme_ctrl_err(ctrl) == NVME_ERR_FW_UNSUP_BY_DEV) {
132 warnx("TEST SKIPPED: device does not support firmware "
133 "requests");
134 } else {
135 libnvme_test_ctrl_warn(ctrl, "failed to initialize fw "
136 "commit request");
137 ret = EXIT_FAILURE;
138 }
139 } else if (nvme_fw_commit_req_exec(fw_commit_req)) {
140 warnx("TEST FAILED: fw commit request succeeded despite "
141 "missing fields");
142 ret = EXIT_FAILURE;
143 } else if (!missing_field_err(ctrl, "fw commit request",
144 NVME_ERR_FW_COMMIT_REQ_MISSING_FIELDS)) {
145 ret = EXIT_FAILURE;
146 } else {
147 (void) printf("TEST PASSED: fw commit request generated "
148 "missing fields error\n");
149 }
150 nvme_fw_commit_req_fini(fw_commit_req);
151
152
153 if (!nvme_format_req_init(ctrl, &format_req)) {
154 if (nvme_ctrl_err(ctrl) == NVME_ERR_FORMAT_UNSUP_BY_DEV) {
155 warnx("TEST SKIPPED: device does not support format "
156 "requests");
157 } else {
158 libnvme_test_ctrl_warn(ctrl, "failed to initialize "
159 "format request");
160 ret = EXIT_FAILURE;
161 }
162 } else if (nvme_format_req_exec(format_req)) {
163 warnx("TEST FAILED: format request succeeded despite missing "
164 "fields");
165 ret = EXIT_FAILURE;
166 } else if (!missing_field_err(ctrl, "format request",
167 NVME_ERR_FORMAT_REQ_MISSING_FIELDS)) {
168 ret = EXIT_FAILURE;
169 } else {
170 (void) printf("TEST PASSED: format request generated missing "
171 "fields error\n");
172 }
173 nvme_format_req_fini(format_req);
174
175 if (ret == EXIT_SUCCESS) {
176 (void) printf("All tests passed successfully\n");
177 }
178
179 nvme_ctrl_fini(ctrl);
180 nvme_fini(nvme);
181 return (ret);
182 }
183