]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/nvmf/subsystem/subsystem_ut.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / spdk / test / lib / nvmf / subsystem / subsystem_ut.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <stdio.h>
38
39 #include "spdk_cunit.h"
40 #include "subsystem.h"
41
42 const struct spdk_nvmf_ctrlr_ops spdk_nvmf_direct_ctrlr_ops;
43 const struct spdk_nvmf_ctrlr_ops spdk_nvmf_virtual_ctrlr_ops;
44 const struct spdk_nvmf_ctrlr_ops spdk_nvmf_discovery_ctrlr_ops;
45
46 #include "subsystem.c"
47
48 SPDK_LOG_REGISTER_TRACE_FLAG("nvmf", SPDK_TRACE_NVMF)
49
50 struct spdk_nvmf_tgt g_nvmf_tgt;
51
52 struct spdk_nvmf_listen_addr *
53 spdk_nvmf_listen_addr_create(const char *trname, const char *traddr, const char *trsvcid)
54 {
55 struct spdk_nvmf_listen_addr *listen_addr;
56
57 listen_addr = calloc(1, sizeof(*listen_addr));
58 if (!listen_addr) {
59 return NULL;
60 }
61
62 listen_addr->traddr = strdup(traddr);
63 if (!listen_addr->traddr) {
64 free(listen_addr);
65 return NULL;
66 }
67
68 listen_addr->trsvcid = strdup(trsvcid);
69 if (!listen_addr->trsvcid) {
70 free(listen_addr->traddr);
71 free(listen_addr);
72 return NULL;
73 }
74
75 listen_addr->trname = strdup(trname);
76 if (!listen_addr->trname) {
77 free(listen_addr->traddr);
78 free(listen_addr->trsvcid);
79 free(listen_addr);
80 return NULL;
81 }
82
83 return listen_addr;
84 }
85
86 void
87 spdk_nvmf_listen_addr_destroy(struct spdk_nvmf_listen_addr *addr)
88 {
89 free(addr->trname);
90 free(addr->trsvcid);
91 free(addr->traddr);
92 free(addr);
93 }
94
95 void
96 spdk_nvmf_listen_addr_cleanup(struct spdk_nvmf_listen_addr *addr)
97 {
98 return;
99 }
100
101 static int
102 test_transport1_listen_addr_add(struct spdk_nvmf_listen_addr *listen_addr)
103 {
104 return 0;
105 }
106
107 static void
108 test_transport1_listen_addr_discover(struct spdk_nvmf_listen_addr *listen_addr,
109 struct spdk_nvmf_discovery_log_page_entry *entry)
110 {
111 entry->trtype = 42;
112 }
113
114 static const struct spdk_nvmf_transport test_transport1 = {
115 .listen_addr_add = test_transport1_listen_addr_add,
116 .listen_addr_discover = test_transport1_listen_addr_discover,
117 };
118
119 const struct spdk_nvmf_transport *
120 spdk_nvmf_transport_get(const char *trname)
121 {
122 if (!strcasecmp(trname, "test_transport1")) {
123 return &test_transport1;
124 }
125
126 return NULL;
127 }
128
129 int32_t
130 spdk_nvme_ctrlr_process_admin_completions(struct spdk_nvme_ctrlr *ctrlr)
131 {
132 return -1;
133 }
134
135 int32_t
136 spdk_nvme_qpair_process_completions(struct spdk_nvme_qpair *qpair, uint32_t max_completions)
137 {
138 return -1;
139 }
140
141 int
142 spdk_nvme_detach(struct spdk_nvme_ctrlr *ctrlr)
143 {
144 return -1;
145 }
146
147 void
148 spdk_nvmf_session_destruct(struct spdk_nvmf_session *session)
149 {
150 }
151
152 int
153 spdk_nvmf_session_poll(struct spdk_nvmf_session *session)
154 {
155 return -1;
156 }
157
158 bool
159 spdk_bdev_claim(struct spdk_bdev *bdev, spdk_bdev_remove_cb_t remove_cb,
160 void *remove_ctx)
161 {
162 return true;
163 }
164
165 static void
166 nvmf_test_create_subsystem(void)
167 {
168 char nqn[256];
169 struct spdk_nvmf_subsystem *subsystem;
170 TAILQ_INIT(&g_nvmf_tgt.subsystems);
171
172 strncpy(nqn, "nqn.2016-06.io.spdk:subsystem1", sizeof(nqn));
173 subsystem = spdk_nvmf_create_subsystem(nqn, SPDK_NVMF_SUBTYPE_NVME,
174 NVMF_SUBSYSTEM_MODE_DIRECT, NULL, NULL, NULL);
175 SPDK_CU_ASSERT_FATAL(subsystem != NULL);
176 CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
177 spdk_nvmf_delete_subsystem(subsystem);
178
179 /* Longest valid name */
180 strncpy(nqn, "nqn.2016-06.io.spdk:", sizeof(nqn));
181 memset(nqn + strlen(nqn), 'a', 222 - strlen(nqn));
182 nqn[222] = '\0';
183 CU_ASSERT(strlen(nqn) == 222);
184 subsystem = spdk_nvmf_create_subsystem(nqn, SPDK_NVMF_SUBTYPE_NVME,
185 NVMF_SUBSYSTEM_MODE_DIRECT, NULL, NULL, NULL);
186 SPDK_CU_ASSERT_FATAL(subsystem != NULL);
187 CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
188 spdk_nvmf_delete_subsystem(subsystem);
189
190 /* Name that is one byte longer than allowed */
191 strncpy(nqn, "nqn.2016-06.io.spdk:", sizeof(nqn));
192 memset(nqn + strlen(nqn), 'a', 223 - strlen(nqn));
193 nqn[223] = '\0';
194 CU_ASSERT(strlen(nqn) == 223);
195 subsystem = spdk_nvmf_create_subsystem(nqn, SPDK_NVMF_SUBTYPE_NVME,
196 NVMF_SUBSYSTEM_MODE_DIRECT, NULL, NULL, NULL);
197 CU_ASSERT(subsystem == NULL);
198 }
199
200 static void
201 nvmf_test_find_subsystem(void)
202 {
203 CU_ASSERT_PTR_NULL(nvmf_find_subsystem(NULL));
204 CU_ASSERT_PTR_NULL(nvmf_find_subsystem("fake"));
205 }
206
207 int main(int argc, char **argv)
208 {
209 CU_pSuite suite = NULL;
210 unsigned int num_failures;
211
212 if (CU_initialize_registry() != CUE_SUCCESS) {
213 return CU_get_error();
214 }
215
216 suite = CU_add_suite("nvmf", NULL, NULL);
217 if (suite == NULL) {
218 CU_cleanup_registry();
219 return CU_get_error();
220 }
221
222 if (
223 CU_add_test(suite, "create_subsystem", nvmf_test_create_subsystem) == NULL ||
224 CU_add_test(suite, "find_subsystem", nvmf_test_find_subsystem) == NULL) {
225 CU_cleanup_registry();
226 return CU_get_error();
227 }
228
229 CU_basic_set_mode(CU_BRM_VERBOSE);
230 CU_basic_run_tests();
231 num_failures = CU_get_number_of_failures();
232 CU_cleanup_registry();
233 return num_failures;
234 }