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/util.h" 36 37 #include "spdk_internal/mock.h" 38 39 #include "spdk_cunit.h" 40 41 #include "sock/posix/posix.c" 42 43 DEFINE_STUB_V(spdk_net_impl_register, (struct spdk_net_impl *impl, int priority)); 44 DEFINE_STUB(spdk_sock_close, int, (struct spdk_sock **s), 0); 45 46 static void 47 _req_cb(void *cb_arg, int len) 48 { 49 *(bool *)cb_arg = true; 50 CU_ASSERT(len == 0); 51 } 52 53 static void 54 flush(void) 55 { 56 struct spdk_posix_sock_group_impl group = {}; 57 struct spdk_posix_sock psock = {}; 58 struct spdk_sock *sock = &psock.base; 59 struct spdk_sock_request *req1, *req2; 60 bool cb_arg1, cb_arg2; 61 int rc; 62 63 /* Set up data structures */ 64 TAILQ_INIT(&sock->queued_reqs); 65 TAILQ_INIT(&sock->pending_reqs); 66 sock->group_impl = &group.base; 67 68 req1 = calloc(1, sizeof(struct spdk_sock_request) + 2 * sizeof(struct iovec)); 69 SPDK_CU_ASSERT_FATAL(req1 != NULL); 70 SPDK_SOCK_REQUEST_IOV(req1, 0)->iov_base = (void *)100; 71 SPDK_SOCK_REQUEST_IOV(req1, 0)->iov_len = 32; 72 SPDK_SOCK_REQUEST_IOV(req1, 1)->iov_base = (void *)200; 73 SPDK_SOCK_REQUEST_IOV(req1, 1)->iov_len = 32; 74 req1->iovcnt = 2; 75 req1->cb_fn = _req_cb; 76 req1->cb_arg = &cb_arg1; 77 78 req2 = calloc(1, sizeof(struct spdk_sock_request) + 2 * sizeof(struct iovec)); 79 SPDK_CU_ASSERT_FATAL(req2 != NULL); 80 SPDK_SOCK_REQUEST_IOV(req2, 0)->iov_base = (void *)100; 81 SPDK_SOCK_REQUEST_IOV(req2, 0)->iov_len = 32; 82 SPDK_SOCK_REQUEST_IOV(req2, 1)->iov_base = (void *)200; 83 SPDK_SOCK_REQUEST_IOV(req2, 1)->iov_len = 32; 84 req2->iovcnt = 2; 85 req2->cb_fn = _req_cb; 86 req2->cb_arg = &cb_arg2; 87 88 /* Simple test - a request with a 2 element iovec 89 * that gets submitted in a single sendmsg. */ 90 spdk_sock_request_queue(sock, req1); 91 MOCK_SET(sendmsg, 64); 92 cb_arg1 = false; 93 rc = _sock_flush(sock); 94 CU_ASSERT(rc == 0); 95 CU_ASSERT(cb_arg1 == true); 96 CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs)); 97 98 /* Two requests, where both can fully send. */ 99 spdk_sock_request_queue(sock, req1); 100 spdk_sock_request_queue(sock, req2); 101 MOCK_SET(sendmsg, 128); 102 cb_arg1 = false; 103 cb_arg2 = false; 104 rc = _sock_flush(sock); 105 CU_ASSERT(rc == 0); 106 CU_ASSERT(cb_arg1 == true); 107 CU_ASSERT(cb_arg2 == true); 108 CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs)); 109 110 /* Two requests. Only first one can send */ 111 spdk_sock_request_queue(sock, req1); 112 spdk_sock_request_queue(sock, req2); 113 MOCK_SET(sendmsg, 64); 114 cb_arg1 = false; 115 cb_arg2 = false; 116 rc = _sock_flush(sock); 117 CU_ASSERT(rc == 0); 118 CU_ASSERT(cb_arg1 == true); 119 CU_ASSERT(cb_arg2 == false); 120 CU_ASSERT(TAILQ_FIRST(&sock->queued_reqs) == req2); 121 TAILQ_REMOVE(&sock->queued_reqs, req2, internal.link); 122 CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs)); 123 124 /* One request. Partial send. */ 125 spdk_sock_request_queue(sock, req1); 126 MOCK_SET(sendmsg, 10); 127 cb_arg1 = false; 128 rc = _sock_flush(sock); 129 CU_ASSERT(rc == 0); 130 CU_ASSERT(cb_arg1 == false); 131 CU_ASSERT(TAILQ_FIRST(&sock->queued_reqs) == req1); 132 133 /* Do a second flush that partial sends again. */ 134 MOCK_SET(sendmsg, 24); 135 cb_arg1 = false; 136 rc = _sock_flush(sock); 137 CU_ASSERT(rc == 0); 138 CU_ASSERT(cb_arg1 == false); 139 CU_ASSERT(TAILQ_FIRST(&sock->queued_reqs) == req1); 140 141 /* Flush the rest of the data */ 142 MOCK_SET(sendmsg, 30); 143 cb_arg1 = false; 144 rc = _sock_flush(sock); 145 CU_ASSERT(rc == 0); 146 CU_ASSERT(cb_arg1 == true); 147 CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs)); 148 149 free(req1); 150 free(req2); 151 } 152 153 int 154 main(int argc, char **argv) 155 { 156 CU_pSuite suite = NULL; 157 unsigned int num_failures; 158 159 CU_set_error_action(CUEA_ABORT); 160 CU_initialize_registry(); 161 162 suite = CU_add_suite("posix", NULL, NULL); 163 164 CU_ADD_TEST(suite, flush); 165 166 CU_basic_set_mode(CU_BRM_VERBOSE); 167 168 CU_basic_run_tests(); 169 170 num_failures = CU_get_number_of_failures(); 171 CU_cleanup_registry(); 172 173 return num_failures; 174 } 175