From 11f119e25db4f3d0f80a8b155ce25e9e1840b119 Mon Sep 17 00:00:00 2001 From: Mao Jiang Date: Tue, 2 Mar 2021 09:39:45 +0800 Subject: [PATCH] test/nvme_cuse: build nvme_cuse unittest file Include cuse_nvme_submit_io_read/write cases. Change-Id: Id55ec798167a36a7fb6e1d9ddb032299d476962e Signed-off-by: Mao Jiang Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/6651 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Shuhei Matsumoto --- test/unit/lib/nvme/Makefile | 1 + test/unit/lib/nvme/nvme_cuse.c/.gitignore | 1 + test/unit/lib/nvme/nvme_cuse.c/Makefile | 38 ++++ test/unit/lib/nvme/nvme_cuse.c/nvme_cuse_ut.c | 166 ++++++++++++++++++ test/unit/unittest.sh | 4 + 5 files changed, 210 insertions(+) create mode 100644 test/unit/lib/nvme/nvme_cuse.c/.gitignore create mode 100644 test/unit/lib/nvme/nvme_cuse.c/Makefile create mode 100644 test/unit/lib/nvme/nvme_cuse.c/nvme_cuse_ut.c diff --git a/test/unit/lib/nvme/Makefile b/test/unit/lib/nvme/Makefile index 408bfcf68..d68d56bfb 100644 --- a/test/unit/lib/nvme/Makefile +++ b/test/unit/lib/nvme/Makefile @@ -38,6 +38,7 @@ DIRS-y = nvme.c nvme_ctrlr.c nvme_ctrlr_cmd.c nvme_ctrlr_ocssd_cmd.c nvme_ns.c n nvme_quirks.c nvme_tcp.c nvme_uevent.c nvme_transport.c nvme_io_msg.c \ DIRS-$(CONFIG_RDMA) += nvme_rdma.c +DIRS-$(CONFIG_NVME_CUSE) += nvme_cuse.c .PHONY: all clean $(DIRS-y) diff --git a/test/unit/lib/nvme/nvme_cuse.c/.gitignore b/test/unit/lib/nvme/nvme_cuse.c/.gitignore new file mode 100644 index 000000000..5d5f72379 --- /dev/null +++ b/test/unit/lib/nvme/nvme_cuse.c/.gitignore @@ -0,0 +1 @@ +nvme_cuse_ut diff --git a/test/unit/lib/nvme/nvme_cuse.c/Makefile b/test/unit/lib/nvme/nvme_cuse.c/Makefile new file mode 100644 index 000000000..00d9dd733 --- /dev/null +++ b/test/unit/lib/nvme/nvme_cuse.c/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = nvme_cuse_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/test/unit/lib/nvme/nvme_cuse.c/nvme_cuse_ut.c b/test/unit/lib/nvme/nvme_cuse.c/nvme_cuse_ut.c new file mode 100644 index 000000000..1ec7a5190 --- /dev/null +++ b/test/unit/lib/nvme/nvme_cuse.c/nvme_cuse_ut.c @@ -0,0 +1,166 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" +#include "spdk_cunit.h" +#include "nvme/nvme_cuse.c" +#include "common/lib/nvme/common_stubs.h" + +SPDK_LOG_REGISTER_COMPONENT(nvme) + +DEFINE_STUB(spdk_nvme_ctrlr_alloc_cmb_io_buffer, void *, + (struct spdk_nvme_ctrlr *ctrlr, size_t size), NULL); + +DEFINE_STUB(spdk_nvme_ctrlr_cmd_admin_raw, int, (struct spdk_nvme_ctrlr *ctrlr, + struct spdk_nvme_cmd *cmd, void *buf, uint32_t len, + spdk_nvme_cmd_cb cb_fn, void *cb_arg), 0); + +DEFINE_STUB(spdk_nvme_ctrlr_get_num_ns, uint32_t, + (struct spdk_nvme_ctrlr *ctrlr), 128); + +DEFINE_STUB(spdk_nvme_ctrlr_reset, int, (struct spdk_nvme_ctrlr *ctrlr), 0); + +DEFINE_STUB(spdk_nvme_ns_cmd_read, int, + (struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, + void *payload, uint64_t lba, uint32_t lba_count, + spdk_nvme_cmd_cb cb_fn, void *cb_arg, uint32_t io_flags), 0); + +DEFINE_STUB(spdk_nvme_ns_cmd_write, int, + (struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, + void *payload, uint64_t lba, uint32_t lba_count, + spdk_nvme_cmd_cb cb_fn, void *cb_arg, uint32_t io_flags), 0); + +DEFINE_STUB(spdk_nvme_ns_get_num_sectors, uint64_t, + (struct spdk_nvme_ns *ns), 0); + +DEFINE_STUB(spdk_nvme_ns_get_sector_size, uint32_t, + (struct spdk_nvme_ns *ns), 0); + +DEFINE_STUB_V(spdk_unaffinitize_thread, (void)); + +DEFINE_STUB(spdk_nvme_ctrlr_get_ns, struct spdk_nvme_ns *, + (struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid), NULL); + +DEFINE_STUB(nvme_io_msg_ctrlr_register, int, + (struct spdk_nvme_ctrlr *ctrlr, + struct nvme_io_msg_producer *io_msg_producer), 0); + +DEFINE_STUB_V(nvme_io_msg_ctrlr_unregister, + (struct spdk_nvme_ctrlr *ctrlr, + struct nvme_io_msg_producer *io_msg_producer)); + +DEFINE_STUB(spdk_nvme_ctrlr_is_active_ns, bool, + (struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid), true); + +DEFINE_STUB(fuse_reply_err, int, (fuse_req_t req, int err), 0); + +struct cuse_io_ctx *g_ut_ctx; + +DEFINE_RETURN_MOCK(nvme_io_msg_send, int); +int +nvme_io_msg_send(struct spdk_nvme_ctrlr *ctrlr, + uint32_t nsid, spdk_nvme_io_msg_fn fn, void *arg) +{ + g_ut_ctx = arg; + + HANDLE_RETURN_MOCK(nvme_io_msg_send); + return 0; +} + +static void +test_cuse_nvme_submit_io_read_write(void) +{ + struct cuse_device cuse_device = {}; + struct fuse_file_info fi = {}; + struct nvme_user_io *user_io = NULL; + char arg[1024] = {}; + fuse_req_t req = (void *)0xDEEACDFF; + unsigned flags = FUSE_IOCTL_DIR; + uint32_t block_size = 4096; + size_t in_bufsz = 4096; + size_t out_bufsz = 4096; + + /* Allocate memory to avoid stack buffer overflow */ + user_io = calloc(3, 4096); + SPDK_CU_ASSERT_FATAL(user_io != NULL); + cuse_device.ctrlr = (void *)0xDEADBEEF; + cuse_device.nsid = 1; + user_io->slba = 1024; + user_io->nblocks = 1; + g_ut_ctx = NULL; + + /* Submit IO read */ + cuse_nvme_submit_io_read(&cuse_device, req, 0, arg, &fi, flags, + block_size, user_io, in_bufsz, out_bufsz); + CU_ASSERT(g_ut_ctx != NULL); + CU_ASSERT(g_ut_ctx->req == req); + CU_ASSERT(g_ut_ctx->lba = user_io->slba); + CU_ASSERT(g_ut_ctx->lba_count == (uint32_t)(user_io->nblocks + 1)); + CU_ASSERT(g_ut_ctx->data_len == + (int)((user_io->nblocks + 1) * block_size)); + CU_ASSERT(g_ut_ctx->data != NULL); + cuse_io_ctx_free(g_ut_ctx); + + /* Submit IO write */ + g_ut_ctx = NULL; + + cuse_nvme_submit_io_write(&cuse_device, req, 0, arg, &fi, flags, + block_size, user_io, in_bufsz, out_bufsz); + CU_ASSERT(g_ut_ctx != NULL); + CU_ASSERT(g_ut_ctx->req == req); + CU_ASSERT(g_ut_ctx->lba = user_io->slba); + CU_ASSERT(g_ut_ctx->lba_count == (uint32_t)(user_io->nblocks + 1)); + CU_ASSERT(g_ut_ctx->data_len == + (int)((user_io->nblocks + 1) * block_size)); + CU_ASSERT(g_ut_ctx->data != NULL); + cuse_io_ctx_free(g_ut_ctx); + free(user_io); +} + +int main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("nvme_cuse", NULL, NULL); + CU_ADD_TEST(suite, test_cuse_nvme_submit_io_read_write); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} diff --git a/test/unit/unittest.sh b/test/unit/unittest.sh index d70ea1a66..16f29cfc1 100755 --- a/test/unit/unittest.sh +++ b/test/unit/unittest.sh @@ -210,6 +210,10 @@ if grep -q '#define SPDK_CONFIG_RDMA 1' $rootdir/include/spdk/config.h; then run_test "unittest_nvme_rdma" $valgrind $testdir/lib/nvme/nvme_rdma.c/nvme_rdma_ut fi +if grep -q '#define SPDK_CONFIG_NVME_CUSE 1' $rootdir/include/spdk/config.h; then + run_test "unittest_nvme_cuse" $valgrind $testdir/lib/nvme/nvme_cuse.c/nvme_cuse_ut +fi + run_test "unittest_nvmf" unittest_nvmf if grep -q '#define SPDK_CONFIG_FC 1' $rootdir/include/spdk/config.h; then run_test "unittest_nvmf_fc" $valgrind $testdir/lib/nvmf/fc.c/fc_ut