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