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 #include "spdk_cunit.h" 36 #include "nvme/nvme_opal.c" 37 #include "common/lib/test_env.c" 38 39 SPDK_LOG_REGISTER_COMPONENT(nvme) 40 41 DEFINE_STUB(spdk_nvme_ctrlr_cmd_security_receive, int, 42 (struct spdk_nvme_ctrlr *ctrlr, uint8_t secp, uint16_t spsp, 43 uint8_t nssf, void *payload, uint32_t payload_size, 44 spdk_nvme_cmd_cb cb_fn, void *cb_arg), 1); 45 46 DEFINE_STUB(spdk_nvme_ctrlr_security_receive, int, 47 (struct spdk_nvme_ctrlr *ctrlr, uint8_t secp, 48 uint16_t spsp, uint8_t nssf, void *payload, size_t size), 0); 49 50 DEFINE_STUB(spdk_nvme_ctrlr_process_admin_completions, int, 51 (struct spdk_nvme_ctrlr *ctrlr), 0); 52 53 DEFINE_STUB(spdk_nvme_ctrlr_cmd_security_send, int, 54 (struct spdk_nvme_ctrlr *ctrlr, uint8_t secp, 55 uint16_t spsp, uint8_t nssf, void *payload, 56 uint32_t payload_size, spdk_nvme_cmd_cb cb_fn, void *cb_arg), 0); 57 58 static int g_ut_recv_status = 0; 59 static void *g_ut_sess_ctx; 60 61 static void 62 ut_opal_sess_cb(struct opal_session *sess, int status, void *ctx) 63 { 64 g_ut_recv_status = status; 65 g_ut_sess_ctx = ctx; 66 } 67 68 static void 69 reset_ut_global_variables(void) 70 { 71 g_ut_recv_status = 0; 72 g_ut_sess_ctx = NULL; 73 } 74 75 static void 76 test_opal_nvme_security_recv_send_done(void) 77 { 78 struct spdk_nvme_cpl cpl = {}; 79 struct spdk_opal_compacket header = {}; 80 struct spdk_opal_dev dev = {}; 81 struct opal_session sess = {}; 82 83 sess.sess_cb = ut_opal_sess_cb; 84 sess.cb_arg = (void *)0xDEADBEEF; 85 sess.dev = &dev; 86 memcpy(sess.resp, &header, sizeof(header)); 87 88 /* Case 1: receive/send IO error */ 89 reset_ut_global_variables(); 90 cpl.status.sct = SPDK_NVME_SCT_MEDIA_ERROR; 91 92 opal_nvme_security_recv_done(&sess, &cpl); 93 CU_ASSERT(g_ut_recv_status == -EIO); 94 CU_ASSERT(g_ut_sess_ctx == (void *)0xDEADBEEF); 95 96 reset_ut_global_variables(); 97 opal_nvme_security_send_done(&sess, &cpl); 98 CU_ASSERT(g_ut_recv_status == -EIO); 99 CU_ASSERT(g_ut_sess_ctx == (void *)0xDEADBEEF); 100 101 /* Case 2: receive with opal header no outstanding data */ 102 reset_ut_global_variables(); 103 memset(&header, 0, sizeof(header)); 104 cpl.status.sct = SPDK_NVME_SCT_GENERIC; 105 106 opal_nvme_security_recv_done(&sess, &cpl); 107 CU_ASSERT(g_ut_recv_status == 0); 108 CU_ASSERT(g_ut_sess_ctx == (void *)0xDEADBEEF); 109 110 /* Case 3: receive with opal header outstanding data and send done success */ 111 reset_ut_global_variables(); 112 header.outstanding_data = 0xff; 113 memcpy(sess.resp, &header, sizeof(header)); 114 cpl.status.sct = SPDK_NVME_SCT_GENERIC; 115 116 opal_nvme_security_recv_done(&sess, &cpl); 117 CU_ASSERT(g_ut_recv_status == 1); 118 CU_ASSERT(g_ut_sess_ctx == (void *)0xDEADBEEF); 119 120 reset_ut_global_variables(); 121 opal_nvme_security_send_done(&sess, &cpl); 122 CU_ASSERT(g_ut_recv_status == 1); 123 CU_ASSERT(g_ut_sess_ctx == (void *)0xDEADBEEF); 124 } 125 126 static void 127 test_opal_add_short_atom_header(void) 128 { 129 struct opal_session sess = {}; 130 int err = 0; 131 132 /* short atom header */ 133 memset(&sess, 0, sizeof(sess)); 134 sess.cmd_pos = 0; 135 136 opal_add_token_bytestring(&err, &sess, spdk_opal_uid[UID_SMUID], 137 OPAL_UID_LENGTH); 138 CU_ASSERT(sess.cmd[0] & SPDK_SHORT_ATOM_ID); 139 CU_ASSERT(sess.cmd[0] & SPDK_SHORT_ATOM_BYTESTRING_FLAG); 140 CU_ASSERT((sess.cmd[0] & SPDK_SHORT_ATOM_SIGN_FLAG) == 0); 141 CU_ASSERT(sess.cmd_pos == OPAL_UID_LENGTH + 1); 142 CU_ASSERT(!memcmp(&sess.cmd[1], spdk_opal_uid, OPAL_UID_LENGTH + 1)); 143 144 /* medium atom header */ 145 memset(&sess, 0, sizeof(sess)); 146 sess.cmd_pos = 0; 147 148 opal_add_token_bytestring(&err, &sess, spdk_opal_uid[UID_SMUID], 149 0x10); 150 CU_ASSERT(sess.cmd[0] & SPDK_SHORT_ATOM_ID); 151 CU_ASSERT(sess.cmd[0] & SPDK_MEDIUM_ATOM_BYTESTRING_FLAG); 152 CU_ASSERT((sess.cmd[0] & SPDK_MEDIUM_ATOM_SIGN_FLAG) == 0); 153 CU_ASSERT(sess.cmd_pos == 0x12); 154 CU_ASSERT(!memcmp(&sess.cmd[2], spdk_opal_uid, 0x10)); 155 156 /* Invalid length */ 157 memset(&sess, 0, sizeof(sess)); 158 err = 0; 159 160 opal_add_token_bytestring(&err, &sess, spdk_opal_uid[UID_SMUID], 161 0x1000); 162 CU_ASSERT(err == -ERANGE); 163 CU_ASSERT(sess.cmd_pos == 0); 164 } 165 166 int main(int argc, char **argv) 167 { 168 CU_pSuite suite = NULL; 169 unsigned int num_failures; 170 171 CU_set_error_action(CUEA_ABORT); 172 CU_initialize_registry(); 173 174 suite = CU_add_suite("nvme_opal", NULL, NULL); 175 CU_ADD_TEST(suite, test_opal_nvme_security_recv_send_done); 176 CU_ADD_TEST(suite, test_opal_add_short_atom_header); 177 178 CU_basic_set_mode(CU_BRM_VERBOSE); 179 CU_basic_run_tests(); 180 num_failures = CU_get_number_of_failures(); 181 CU_cleanup_registry(); 182 return num_failures; 183 } 184