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_cunit.h" 35 #include "spdk/string.h" 36 #include "spdk/stdinc.h" 37 38 #include "blobfs/bdev/blobfs_bdev.c" 39 40 int g_fserrno; 41 42 bool g_bdev_open_ext_fail = false; 43 bool g_bdev_create_bs_dev_from_desc_fail = false; 44 bool g_fs_load_fail = false; 45 bool g_fs_unload_fail = false; 46 bool g_bs_bdev_claim_fail = false; 47 bool g_blobfs_fuse_start_fail = false; 48 49 const char *g_bdev_name = "ut_bdev"; 50 51 int 52 spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb, 53 void *event_ctx, struct spdk_bdev_desc **_desc) 54 { 55 if (g_bdev_open_ext_fail) { 56 return -1; 57 } 58 59 return 0; 60 } 61 62 static void 63 bs_dev_destroy(struct spdk_bs_dev *dev) 64 { 65 } 66 67 struct spdk_bs_dev * 68 spdk_bdev_create_bs_dev_from_desc(struct spdk_bdev_desc *desc) 69 { 70 static struct spdk_bs_dev bs_dev; 71 72 if (g_bdev_create_bs_dev_from_desc_fail) { 73 return NULL; 74 } 75 76 bs_dev.destroy = bs_dev_destroy; 77 return &bs_dev; 78 } 79 80 void 81 spdk_fs_load(struct spdk_bs_dev *dev, fs_send_request_fn send_request_fn, 82 spdk_fs_op_with_handle_complete cb_fn, void *cb_arg) 83 { 84 int rc = 0; 85 86 if (g_fs_load_fail) { 87 rc = -1; 88 } 89 90 cb_fn(cb_arg, NULL, rc); 91 return; 92 } 93 94 void 95 spdk_fs_unload(struct spdk_filesystem *fs, spdk_fs_op_complete cb_fn, void *cb_arg) 96 { 97 int rc = 0; 98 99 if (g_fs_unload_fail) { 100 rc = -1; 101 } 102 103 cb_fn(cb_arg, rc); 104 return; 105 } 106 107 void 108 spdk_fs_init(struct spdk_bs_dev *dev, struct spdk_blobfs_opts *opt, 109 fs_send_request_fn send_request_fn, 110 spdk_fs_op_with_handle_complete cb_fn, void *cb_arg) 111 { 112 int rc = 0; 113 114 if (g_fs_load_fail) { 115 rc = -1; 116 } 117 118 cb_fn(cb_arg, NULL, rc); 119 return; 120 } 121 122 int 123 spdk_bs_bdev_claim(struct spdk_bs_dev *bs_dev, struct spdk_bdev_module *module) 124 { 125 if (g_bs_bdev_claim_fail == true) { 126 return -1; 127 } 128 129 return 0; 130 } 131 132 int 133 spdk_blobfs_fuse_start(const char *bdev_name, const char *mountpoint, struct spdk_filesystem *fs, 134 blobfs_fuse_unmount_cb cb_fn, void *cb_arg, struct spdk_blobfs_fuse **_bfuse) 135 { 136 if (g_blobfs_fuse_start_fail == true) { 137 return -1; 138 } 139 140 return 0; 141 } 142 143 void 144 spdk_bdev_close(struct spdk_bdev_desc *desc) 145 { 146 } 147 148 int 149 spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx) 150 { 151 fn(ctx); 152 return 0; 153 } 154 155 struct spdk_thread * 156 spdk_get_thread(void) 157 { 158 struct spdk_thread *thd = (struct spdk_thread *)0x1; 159 160 return thd; 161 } 162 163 const char * 164 spdk_bdev_get_name(const struct spdk_bdev *bdev) 165 { 166 return g_bdev_name; 167 } 168 169 void 170 spdk_fs_opts_init(struct spdk_blobfs_opts *opts) 171 { 172 } 173 174 void 175 spdk_blobfs_fuse_send_request(fs_request_fn fn, void *arg) 176 { 177 } 178 179 void 180 spdk_blobfs_fuse_stop(struct spdk_blobfs_fuse *bfuse) 181 { 182 } 183 184 static void 185 blobfs_bdev_op_complete(void *cb_arg, int fserrno) 186 { 187 g_fserrno = fserrno; 188 } 189 190 static void 191 spdk_blobfs_bdev_detect_test(void) 192 { 193 /* spdk_bdev_open_ext() fails */ 194 g_bdev_open_ext_fail = true; 195 spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL); 196 CU_ASSERT(g_fserrno != 0); 197 198 g_bdev_open_ext_fail = false; 199 200 /* spdk_bdev_create_bs_dev_from_desc() fails */ 201 g_bdev_create_bs_dev_from_desc_fail = true; 202 spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL); 203 CU_ASSERT(g_fserrno != 0); 204 205 g_bdev_create_bs_dev_from_desc_fail = false; 206 207 /* spdk_fs_load() fails */ 208 g_fs_load_fail = true; 209 spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL); 210 CU_ASSERT(g_fserrno != 0); 211 212 g_fs_load_fail = false; 213 214 /* spdk_fs_unload() fails */ 215 g_fs_unload_fail = true; 216 spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL); 217 CU_ASSERT(g_fserrno != 0); 218 219 g_fs_unload_fail = false; 220 221 /* no fail */ 222 spdk_blobfs_bdev_detect(g_bdev_name, blobfs_bdev_op_complete, NULL); 223 CU_ASSERT(g_fserrno == 0); 224 } 225 226 static void 227 spdk_blobfs_bdev_create_test(void) 228 { 229 uint32_t cluster_sz = 1024 * 1024; 230 231 /* spdk_bdev_open_ext() fails */ 232 g_bdev_open_ext_fail = true; 233 spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL); 234 CU_ASSERT(g_fserrno != 0); 235 236 g_bdev_open_ext_fail = false; 237 238 /* spdk_bdev_create_bs_dev_from_desc() fails */ 239 g_bdev_create_bs_dev_from_desc_fail = true; 240 spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL); 241 CU_ASSERT(g_fserrno != 0); 242 243 g_bdev_create_bs_dev_from_desc_fail = false; 244 245 /* spdk_bs_bdev_claim() fails */ 246 g_bs_bdev_claim_fail = true; 247 spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL); 248 CU_ASSERT(g_fserrno != 0); 249 250 g_bs_bdev_claim_fail = false; 251 252 /* spdk_fs_init() fails */ 253 g_fs_load_fail = true; 254 spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL); 255 CU_ASSERT(g_fserrno != 0); 256 257 g_fs_load_fail = false; 258 259 /* spdk_fs_unload() fails */ 260 g_fs_unload_fail = true; 261 spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL); 262 CU_ASSERT(g_fserrno != 0); 263 264 g_fs_unload_fail = false; 265 266 /* no fail */ 267 spdk_blobfs_bdev_create(g_bdev_name, cluster_sz, blobfs_bdev_op_complete, NULL); 268 CU_ASSERT(g_fserrno == 0); 269 } 270 271 static void 272 spdk_blobfs_bdev_mount_test(void) 273 { 274 #ifdef SPDK_CONFIG_FUSE 275 const char *mountpoint = "/mnt"; 276 277 /* spdk_bdev_open_ext() fails */ 278 g_bdev_open_ext_fail = true; 279 spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL); 280 CU_ASSERT(g_fserrno != 0); 281 282 g_bdev_open_ext_fail = false; 283 284 /* spdk_bdev_create_bs_dev_from_desc() fails */ 285 g_bdev_create_bs_dev_from_desc_fail = true; 286 spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL); 287 CU_ASSERT(g_fserrno != 0); 288 289 g_bdev_create_bs_dev_from_desc_fail = false; 290 291 /* spdk_bs_bdev_claim() fails */ 292 g_bs_bdev_claim_fail = true; 293 spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL); 294 CU_ASSERT(g_fserrno != 0); 295 296 g_bs_bdev_claim_fail = false; 297 298 /* spdk_fs_load() fails */ 299 g_fs_load_fail = true; 300 spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL); 301 CU_ASSERT(g_fserrno != 0); 302 303 g_fs_load_fail = false; 304 305 /* spdk_blobfs_fuse_start() fails */ 306 g_blobfs_fuse_start_fail = true; 307 spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL); 308 CU_ASSERT(g_fserrno != 0); 309 310 g_blobfs_fuse_start_fail = false; 311 312 /* no fail */ 313 spdk_blobfs_bdev_mount(g_bdev_name, mountpoint, blobfs_bdev_op_complete, NULL); 314 CU_ASSERT(g_fserrno == 0); 315 #endif 316 } 317 318 int main(int argc, char **argv) 319 { 320 CU_pSuite suite = NULL; 321 unsigned int num_failures; 322 323 if (CU_initialize_registry() != CUE_SUCCESS) { 324 return CU_get_error(); 325 } 326 327 suite = CU_add_suite("blobfs_bdev_ut", NULL, NULL); 328 if (suite == NULL) { 329 CU_cleanup_registry(); 330 return CU_get_error(); 331 } 332 333 if ( 334 CU_add_test(suite, "spdk_blobfs_bdev_detect_test", spdk_blobfs_bdev_detect_test) == NULL || 335 CU_add_test(suite, "spdk_blobfs_bdev_create_test", spdk_blobfs_bdev_create_test) == NULL || 336 CU_add_test(suite, "spdk_blobfs_bdev_mount_test", spdk_blobfs_bdev_mount_test) == NULL 337 ) { 338 CU_cleanup_registry(); 339 return CU_get_error(); 340 } 341 342 CU_basic_set_mode(CU_BRM_VERBOSE); 343 CU_basic_run_tests(); 344 num_failures = CU_get_number_of_failures(); 345 CU_cleanup_registry(); 346 347 return num_failures; 348 } 349 350 SPDK_LOG_REGISTER_COMPONENT("blobfs", SPDK_LOG_BLOBFS) 351