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 "spdk/stdinc.h" 35 36 #include "spdk_cunit.h" 37 38 #include "nvmf/ctrlr_bdev.c" 39 40 41 SPDK_LOG_REGISTER_COMPONENT("nvmf", SPDK_LOG_NVMF) 42 43 int 44 spdk_nvmf_request_complete(struct spdk_nvmf_request *req) 45 { 46 return -1; 47 } 48 49 const char * 50 spdk_bdev_get_name(const struct spdk_bdev *bdev) 51 { 52 return "test"; 53 } 54 55 uint32_t 56 spdk_bdev_get_block_size(const struct spdk_bdev *bdev) 57 { 58 abort(); 59 return 0; 60 } 61 62 uint64_t 63 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev) 64 { 65 abort(); 66 return 0; 67 } 68 69 uint32_t 70 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev) 71 { 72 abort(); 73 return 0; 74 } 75 76 struct spdk_io_channel * 77 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc) 78 { 79 return NULL; 80 } 81 82 int 83 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 84 uint64_t offset_blocks, uint64_t num_blocks, 85 spdk_bdev_io_completion_cb cb, void *cb_arg) 86 { 87 return 0; 88 } 89 90 int 91 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 92 uint64_t offset_blocks, uint64_t num_blocks, 93 spdk_bdev_io_completion_cb cb, void *cb_arg) 94 { 95 return 0; 96 } 97 98 bool 99 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type) 100 { 101 return false; 102 } 103 104 int 105 spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch, 106 struct spdk_bdev_io_wait_entry *entry) 107 { 108 return 0; 109 } 110 111 int 112 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 113 uint64_t offset_blocks, uint64_t num_blocks, 114 spdk_bdev_io_completion_cb cb, void *cb_arg) 115 { 116 return 0; 117 } 118 119 int 120 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 121 struct iovec *iov, int iovcnt, 122 uint64_t offset_blocks, uint64_t num_blocks, 123 spdk_bdev_io_completion_cb cb, void *cb_arg) 124 { 125 return 0; 126 } 127 128 int 129 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 130 uint64_t offset_blocks, uint64_t num_blocks, 131 spdk_bdev_io_completion_cb cb, void *cb_arg) 132 { 133 return 0; 134 } 135 136 int spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 137 struct iovec *iov, int iovcnt, 138 uint64_t offset_blocks, uint64_t num_blocks, 139 spdk_bdev_io_completion_cb cb, void *cb_arg) 140 { 141 return 0; 142 } 143 144 int 145 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 146 uint64_t offset_blocks, uint64_t num_blocks, 147 spdk_bdev_io_completion_cb cb, void *cb_arg) 148 { 149 return 0; 150 } 151 152 int 153 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, 154 struct spdk_io_channel *ch, 155 const struct spdk_nvme_cmd *cmd, 156 void *buf, size_t nbytes, 157 spdk_bdev_io_completion_cb cb, void *cb_arg) 158 { 159 return 0; 160 } 161 162 void spdk_bdev_free_io(struct spdk_bdev_io *bdev_io) 163 { 164 } 165 166 const char *spdk_nvmf_subsystem_get_nqn(struct spdk_nvmf_subsystem *subsystem) 167 { 168 return NULL; 169 } 170 171 struct spdk_nvmf_ns * 172 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 173 { 174 abort(); 175 return NULL; 176 } 177 178 struct spdk_nvmf_ns * 179 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem) 180 { 181 abort(); 182 return NULL; 183 } 184 185 struct spdk_nvmf_ns * 186 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *prev_ns) 187 { 188 abort(); 189 return NULL; 190 } 191 192 void spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, int *sct, int *sc) 193 { 194 } 195 196 static void 197 test_get_rw_params(void) 198 { 199 struct spdk_nvme_cmd cmd = {0}; 200 uint64_t lba; 201 uint64_t count; 202 203 lba = 0; 204 count = 0; 205 to_le64(&cmd.cdw10, 0x1234567890ABCDEF); 206 to_le32(&cmd.cdw12, 0x9875 | SPDK_NVME_IO_FLAGS_FORCE_UNIT_ACCESS); 207 nvmf_bdev_ctrlr_get_rw_params(&cmd, &lba, &count); 208 CU_ASSERT(lba == 0x1234567890ABCDEF); 209 CU_ASSERT(count == 0x9875 + 1); /* NOTE: this field is 0's based, hence the +1 */ 210 } 211 212 static void 213 test_lba_in_range(void) 214 { 215 /* Trivial cases (no overflow) */ 216 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1) == true); 217 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1000) == true); 218 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1001) == false); 219 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 999) == true); 220 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 1000) == false); 221 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 999, 1) == true); 222 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1000, 1) == false); 223 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1001, 1) == false); 224 225 /* Overflow edge cases */ 226 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 0, UINT64_MAX) == true); 227 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 1, UINT64_MAX) == false) 228 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX - 1, 1) == true); 229 CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX, 1) == false); 230 } 231 232 int main(int argc, char **argv) 233 { 234 CU_pSuite suite = NULL; 235 unsigned int num_failures; 236 237 if (CU_initialize_registry() != CUE_SUCCESS) { 238 return CU_get_error(); 239 } 240 241 suite = CU_add_suite("nvmf", NULL, NULL); 242 if (suite == NULL) { 243 CU_cleanup_registry(); 244 return CU_get_error(); 245 } 246 247 if ( 248 CU_add_test(suite, "get_rw_params", test_get_rw_params) == NULL || 249 CU_add_test(suite, "lba_in_range", test_lba_in_range) == NULL 250 ) { 251 CU_cleanup_registry(); 252 return CU_get_error(); 253 } 254 255 CU_basic_set_mode(CU_BRM_VERBOSE); 256 CU_basic_run_tests(); 257 num_failures = CU_get_number_of_failures(); 258 CU_cleanup_registry(); 259 return num_failures; 260 } 261