Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #include <socket_test.h> |
| 19 | |
| 20 | #include <memif_private.h> |
| 21 | #include <socket.h> |
| 22 | |
| 23 | static int |
| 24 | get_queue_len (memif_msg_queue_elt_t * q) |
| 25 | { |
| 26 | int r = 0; |
| 27 | memif_msg_queue_elt_t *c = q; |
| 28 | while (c != NULL) |
| 29 | { |
| 30 | r++; |
| 31 | c = c->next; |
| 32 | } |
| 33 | return r; |
| 34 | } |
| 35 | |
| 36 | static void |
| 37 | queue_free (memif_msg_queue_elt_t ** e) |
| 38 | { |
| 39 | if (*e == NULL) |
| 40 | return; |
| 41 | queue_free (&(*e)->next); |
| 42 | free (*e); |
| 43 | *e = NULL; |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | START_TEST (test_msg_queue) |
| 48 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 49 | int err; |
| 50 | if ((err = |
| 51 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 52 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 53 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 54 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 55 | memif_connection_t conn; |
| 56 | conn.msg_queue = NULL; |
| 57 | conn.fd = -1; |
| 58 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 59 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 60 | int i, len = 10; |
| 61 | |
| 62 | for (i = 0; i < len; i++) |
| 63 | { |
| 64 | if (i % 2) |
| 65 | memif_msg_enq_ack (&conn); |
| 66 | else |
| 67 | memif_msg_enq_init (&conn); |
| 68 | } |
| 69 | |
| 70 | ck_assert_int_eq (len, get_queue_len (conn.msg_queue)); |
| 71 | |
| 72 | int pop = 6; |
| 73 | |
| 74 | for (i = 0; i < pop; i++) |
| 75 | { |
| 76 | if (i % 2) |
| 77 | { |
| 78 | ck_assert_uint_eq (conn.msg_queue->msg.type, MEMIF_MSG_TYPE_ACK); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | ck_assert_uint_eq (conn.msg_queue->msg.type, MEMIF_MSG_TYPE_INIT); |
| 83 | } |
| 84 | conn.flags |= MEMIF_CONNECTION_FLAG_WRITE; |
| 85 | /* function will return -1 because no socket is created */ |
| 86 | memif_conn_fd_write_ready (&conn); |
| 87 | } |
| 88 | |
| 89 | ck_assert_int_eq ((len - pop), get_queue_len (conn.msg_queue)); |
| 90 | |
| 91 | queue_free (&conn.msg_queue); |
| 92 | } |
| 93 | |
| 94 | END_TEST |
| 95 | START_TEST (test_enq_ack) |
| 96 | { |
| 97 | int err; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 98 | if ((err = |
| 99 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 100 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 101 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 102 | memif_connection_t conn; |
| 103 | conn.msg_queue = NULL; |
| 104 | |
| 105 | if ((err = memif_msg_enq_ack (&conn)) != MEMIF_ERR_SUCCESS) |
| 106 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 107 | memif_msg_queue_elt_t *e = conn.msg_queue; |
| 108 | |
| 109 | ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_ACK); |
| 110 | ck_assert_int_eq (e->fd, -1); |
| 111 | queue_free (&conn.msg_queue); |
| 112 | } |
| 113 | |
| 114 | END_TEST |
| 115 | START_TEST (test_enq_init) |
| 116 | { |
| 117 | int err; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 118 | if ((err = |
| 119 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 120 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 121 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 122 | memif_connection_t conn; |
| 123 | conn.msg_queue = NULL; |
| 124 | |
| 125 | conn.args.interface_id = 69; |
| 126 | conn.args.mode = 0; |
| 127 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 128 | strncpy ((char *) conn.args.secret, TEST_SECRET, strlen (TEST_SECRET)); |
| 129 | |
| 130 | if ((err = memif_msg_enq_init (&conn)) != MEMIF_ERR_SUCCESS) |
| 131 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 132 | |
| 133 | memif_msg_queue_elt_t *e = conn.msg_queue; |
| 134 | |
| 135 | ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_INIT); |
| 136 | ck_assert_int_eq (e->fd, -1); |
| 137 | |
| 138 | memif_msg_init_t *i = &e->msg.init; |
| 139 | |
| 140 | ck_assert_uint_eq (i->version, MEMIF_VERSION); |
| 141 | ck_assert_uint_eq (i->id, conn.args.interface_id); |
| 142 | ck_assert_uint_eq (i->mode, conn.args.mode); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 143 | ck_assert_str_eq ((char *)i->secret, (char *)conn.args.secret); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 144 | queue_free (&conn.msg_queue); |
| 145 | } |
| 146 | |
| 147 | END_TEST |
| 148 | START_TEST (test_enq_add_region) |
| 149 | { |
| 150 | int err; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 151 | if ((err = |
| 152 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 153 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 154 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 155 | memif_connection_t conn; |
| 156 | conn.msg_queue = NULL; |
| 157 | conn.regions = (memif_region_t *) malloc (sizeof (memif_region_t)); |
| 158 | memif_region_t *mr = conn.regions; |
| 159 | mr->fd = 5; |
| 160 | mr->region_size = 2048; |
| 161 | uint8_t region_index = 0; |
| 162 | |
| 163 | if ((err = |
| 164 | memif_msg_enq_add_region (&conn, region_index)) != MEMIF_ERR_SUCCESS) |
| 165 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 166 | |
| 167 | memif_msg_queue_elt_t *e = conn.msg_queue; |
| 168 | |
| 169 | ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_ADD_REGION); |
| 170 | ck_assert_int_eq (e->fd, mr->fd); |
| 171 | |
| 172 | memif_msg_add_region_t *ar = &e->msg.add_region; |
| 173 | |
| 174 | ck_assert_uint_eq (ar->index, region_index); |
| 175 | ck_assert_uint_eq (ar->size, mr->region_size); |
| 176 | |
| 177 | free (conn.regions); |
| 178 | conn.regions = NULL; |
| 179 | mr = NULL; |
| 180 | queue_free (&conn.msg_queue); |
| 181 | } |
| 182 | |
| 183 | END_TEST |
| 184 | START_TEST (test_enq_add_ring) |
| 185 | { |
| 186 | int err; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 187 | if ((err = |
| 188 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 189 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 190 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 191 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 192 | memif_connection_t conn; |
| 193 | conn.msg_queue = NULL; |
| 194 | conn.rx_queues = (memif_queue_t *) malloc (sizeof (memif_queue_t)); |
| 195 | conn.tx_queues = (memif_queue_t *) malloc (sizeof (memif_queue_t)); |
| 196 | |
| 197 | memif_queue_t *mq = conn.tx_queues; |
| 198 | uint8_t dir = MEMIF_RING_S2M; |
| 199 | mq->int_fd = 5; |
| 200 | mq->offset = 0; |
| 201 | mq->log2_ring_size = 10; |
| 202 | |
| 203 | if ((err = memif_msg_enq_add_ring (&conn, 0, dir)) != MEMIF_ERR_SUCCESS) |
| 204 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 205 | |
| 206 | memif_msg_queue_elt_t *e = conn.msg_queue; |
| 207 | |
| 208 | ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_ADD_RING); |
| 209 | ck_assert_int_eq (e->fd, mq->int_fd); |
| 210 | |
| 211 | memif_msg_add_ring_t *ar = &e->msg.add_ring; |
| 212 | |
| 213 | ck_assert_uint_eq (ar->index, 0); |
| 214 | ck_assert_uint_eq (ar->offset, mq->offset); |
| 215 | ck_assert_uint_eq (ar->log2_ring_size, mq->log2_ring_size); |
| 216 | ck_assert (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M); |
| 217 | |
| 218 | dir = MEMIF_RING_M2S; |
| 219 | if ((err = memif_msg_enq_add_ring (&conn, 0, dir)) != MEMIF_ERR_SUCCESS) |
| 220 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 221 | queue_free (&conn.msg_queue); |
| 222 | } |
| 223 | |
| 224 | END_TEST |
| 225 | START_TEST (test_enq_connect) |
| 226 | { |
| 227 | int err; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 228 | if ((err = |
| 229 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 230 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 231 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 232 | memif_connection_t conn; |
| 233 | conn.msg_queue = NULL; |
| 234 | memset (conn.args.interface_name, 0, sizeof (conn.args.interface_name)); |
| 235 | strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, |
| 236 | strlen (TEST_IF_NAME)); |
| 237 | |
| 238 | if ((err = memif_msg_enq_connect (&conn)) != MEMIF_ERR_SUCCESS) |
| 239 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 240 | |
| 241 | memif_msg_queue_elt_t *e = conn.msg_queue; |
| 242 | |
| 243 | ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_CONNECT); |
| 244 | ck_assert_int_eq (e->fd, -1); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 245 | ck_assert_str_eq ((char *)e->msg.connect.if_name, TEST_IF_NAME); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 246 | queue_free (&conn.msg_queue); |
| 247 | } |
| 248 | |
| 249 | END_TEST |
| 250 | START_TEST (test_enq_connected) |
| 251 | { |
| 252 | int err; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 253 | if ((err = |
| 254 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 255 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 256 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 257 | memif_connection_t conn; |
| 258 | conn.msg_queue = NULL; |
| 259 | memset (conn.args.interface_name, 0, sizeof (conn.args.interface_name)); |
| 260 | strncpy ((char *) conn.args.interface_name, TEST_IF_NAME, |
| 261 | strlen (TEST_IF_NAME)); |
| 262 | |
| 263 | if ((err = memif_msg_enq_connected (&conn)) != MEMIF_ERR_SUCCESS) |
| 264 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 265 | |
| 266 | memif_msg_queue_elt_t *e = conn.msg_queue; |
| 267 | |
| 268 | ck_assert_uint_eq (e->msg.type, MEMIF_MSG_TYPE_CONNECTED); |
| 269 | ck_assert_int_eq (e->fd, -1); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 270 | ck_assert_str_eq ((char *)e->msg.connect.if_name, TEST_IF_NAME); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 271 | queue_free (&conn.msg_queue); |
| 272 | } |
| 273 | |
| 274 | END_TEST |
| 275 | START_TEST (test_send) |
| 276 | { |
| 277 | int err; |
| 278 | int fd = -1, afd = 5; |
| 279 | memif_msg_t msg; |
| 280 | memset (&msg, 0, sizeof (msg)); |
| 281 | |
| 282 | if ((err = memif_msg_send (fd, &msg, afd)) != MEMIF_ERR_SUCCESS) |
| 283 | ck_assert_msg (err == MEMIF_ERR_BAD_FD, |
| 284 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 285 | } |
| 286 | |
| 287 | END_TEST |
| 288 | START_TEST (test_send_hello) |
| 289 | { |
| 290 | int err; |
| 291 | memif_connection_t conn; |
| 292 | conn.fd = -1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 293 | |
| 294 | if ((err = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 295 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 296 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 297 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 298 | |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 299 | conn.args.socket = libmemif_main.default_socket; |
| 300 | |
| 301 | if ((err = memif_msg_send_hello (get_libmemif_main (conn.args.socket), conn.fd)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 302 | ck_assert_msg (err == MEMIF_ERR_BAD_FD, |
| 303 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 304 | } |
| 305 | |
| 306 | END_TEST |
| 307 | START_TEST (test_send_disconnect) |
| 308 | { |
| 309 | int err; |
| 310 | memif_connection_t conn; |
| 311 | conn.fd = -1; |
| 312 | |
| 313 | /* only possible fail if memif_msg_send fails... */ |
| 314 | /* obsolete without socket */ |
| 315 | if ((err = |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 316 | memif_msg_send_disconnect (conn.fd, (uint8_t *)"unit_test_dc", |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 317 | 0)) != MEMIF_ERR_SUCCESS) |
| 318 | ck_assert_msg (err == MEMIF_ERR_BAD_FD, "err code: %u, err msg: %s", err, |
| 319 | memif_strerror (err)); |
| 320 | } |
| 321 | |
| 322 | END_TEST |
| 323 | START_TEST (test_recv_hello) |
| 324 | { |
| 325 | int err; |
| 326 | memif_connection_t conn; |
| 327 | memif_msg_t msg; |
| 328 | |
| 329 | memif_msg_hello_t *h = &msg.hello; |
| 330 | |
| 331 | msg.type = MEMIF_MSG_TYPE_HELLO; |
| 332 | |
| 333 | h->min_version = MEMIF_VERSION; |
| 334 | h->max_version = MEMIF_VERSION; |
| 335 | h->max_s2m_ring = 1; |
| 336 | h->max_m2s_ring = 1; |
| 337 | h->max_log2_ring_size = 14; |
| 338 | strncpy ((char *) h->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 339 | memset (conn.remote_name, 0, sizeof (conn.remote_name)); |
| 340 | |
| 341 | conn.args.num_s2m_rings = 4; |
| 342 | conn.args.num_m2s_rings = 6; |
| 343 | conn.args.log2_ring_size = 10; |
| 344 | |
| 345 | if ((err = memif_msg_receive_hello (&conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 346 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 347 | |
| 348 | ck_assert_uint_eq (conn.run_args.num_s2m_rings, 2); |
| 349 | ck_assert_uint_eq (conn.run_args.num_m2s_rings, 2); |
| 350 | ck_assert_uint_eq (conn.run_args.log2_ring_size, 10); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 351 | ck_assert_str_eq ((char *)conn.remote_name, TEST_IF_NAME); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 352 | |
| 353 | h->max_version = 9; |
| 354 | if ((err = memif_msg_receive_hello (&conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 355 | ck_assert_msg (err == MEMIF_ERR_PROTO, |
| 356 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 357 | } |
| 358 | |
| 359 | END_TEST |
| 360 | START_TEST (test_recv_init) |
| 361 | { |
| 362 | int err; |
| 363 | memif_connection_t conn; |
| 364 | |
| 365 | conn.args.interface_id = 69; |
| 366 | conn.args.is_master = 1; |
| 367 | conn.fd = -1; |
| 368 | conn.args.mode = 0; |
| 369 | memset (conn.args.secret, '\0', 24); |
| 370 | strncpy ((char *) conn.args.secret, TEST_SECRET, strlen (TEST_SECRET)); |
| 371 | |
| 372 | memif_msg_t msg; |
| 373 | |
| 374 | memif_msg_init_t *i = &msg.init; |
| 375 | |
| 376 | msg.type = MEMIF_MSG_TYPE_INIT; |
| 377 | |
| 378 | i->version = MEMIF_VERSION; |
| 379 | i->id = 69; |
| 380 | i->mode = 0; |
| 381 | memset (i->name, '\0', 32); |
| 382 | memset (i->secret, '\0', 24); |
| 383 | strncpy ((char *) i->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 384 | strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); |
| 385 | |
| 386 | memif_socket_t ms; |
| 387 | ms.interface_list_len = 1; |
| 388 | ms.interface_list = malloc (sizeof (memif_list_elt_t)); |
| 389 | memif_list_elt_t elt; |
| 390 | elt.key = 69; |
| 391 | elt.data_struct = &conn; |
Jakub Grajciar | 17f2a7b | 2019-07-31 14:40:52 +0200 | [diff] [blame] | 392 | add_list_elt (get_libmemif_main (conn.args.socket), &elt, &ms.interface_list, &ms.interface_list_len); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 393 | |
| 394 | if ((err = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 395 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 396 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 397 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 398 | |
| 399 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 400 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 401 | |
| 402 | i->version = 9; |
| 403 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 404 | ck_assert_msg (err == MEMIF_ERR_PROTO, |
| 405 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 406 | i->version = MEMIF_VERSION; |
| 407 | |
| 408 | i->id = 78; |
| 409 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 410 | ck_assert_msg (err == MEMIF_ERR_ID, |
| 411 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 412 | i->id = 69; |
| 413 | |
| 414 | i->mode = 1; |
| 415 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 416 | ck_assert_msg (err == MEMIF_ERR_MODE, |
| 417 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 418 | i->mode = 0; |
| 419 | |
| 420 | i->secret[0] = '\0'; |
| 421 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 422 | ck_assert_msg (err == MEMIF_ERR_SECRET, |
| 423 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 424 | strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); |
| 425 | |
| 426 | conn.args.is_master = 0; |
| 427 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 428 | ck_assert_msg (err == MEMIF_ERR_ACCSLAVE, |
| 429 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 430 | conn.args.is_master = 1; |
| 431 | |
| 432 | conn.fd = 5; |
| 433 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 434 | ck_assert_msg ((err == MEMIF_ERR_ALRCONN) || (err == MEMIF_ERR_BAD_FD), |
| 435 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 436 | } |
| 437 | |
| 438 | END_TEST |
| 439 | START_TEST (test_recv_add_region) |
| 440 | { |
| 441 | int err; |
| 442 | memif_connection_t conn; |
| 443 | conn.regions = NULL; |
| 444 | memif_msg_t msg; |
| 445 | msg.type = MEMIF_MSG_TYPE_ADD_REGION; |
| 446 | msg.add_region.size = 2048; |
| 447 | msg.add_region.index = 0; |
| 448 | |
| 449 | int fd = 5; |
| 450 | |
| 451 | if ((err = |
| 452 | memif_msg_receive_add_region (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) |
| 453 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 454 | |
| 455 | memif_region_t *mr = conn.regions; |
| 456 | |
| 457 | ck_assert_uint_eq (mr->fd, fd); |
| 458 | ck_assert_uint_eq (mr->region_size, 2048); |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 459 | ck_assert_ptr_eq (mr->addr, NULL); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | END_TEST |
| 463 | START_TEST (test_recv_add_ring) |
| 464 | { |
| 465 | int err; |
| 466 | memif_connection_t conn; |
| 467 | int fd = 5; |
| 468 | memif_msg_t msg; |
| 469 | conn.args.num_s2m_rings = 2; |
| 470 | conn.args.num_m2s_rings = 2; |
| 471 | conn.rx_queues = NULL; |
| 472 | conn.tx_queues = NULL; |
| 473 | |
| 474 | msg.type = MEMIF_MSG_TYPE_ADD_RING; |
| 475 | memif_msg_add_ring_t *ar = &msg.add_ring; |
| 476 | |
| 477 | ar->log2_ring_size = 10; |
| 478 | ar->region = 0; |
| 479 | ar->offset = 0; |
| 480 | ar->flags = 0; |
| 481 | ar->flags |= MEMIF_MSG_ADD_RING_FLAG_S2M; |
| 482 | ar->index = 1; |
| 483 | |
| 484 | if ((err = |
| 485 | memif_msg_receive_add_ring (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) |
| 486 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 487 | |
| 488 | |
| 489 | ar->offset = 2048; |
| 490 | ar->flags &= ~MEMIF_MSG_ADD_RING_FLAG_S2M; |
| 491 | |
| 492 | if ((err = |
| 493 | memif_msg_receive_add_ring (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) |
| 494 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 495 | |
| 496 | } |
| 497 | |
| 498 | END_TEST |
| 499 | START_TEST (test_recv_connect) |
| 500 | { |
| 501 | int err; |
| 502 | memif_conn_handle_t c = NULL; |
| 503 | memif_conn_args_t args; |
| 504 | memset (&args, 0, sizeof (args)); |
| 505 | |
| 506 | args.interface_id = 0; |
| 507 | args.is_master = 0; |
| 508 | args.mode = 0; |
| 509 | |
| 510 | if ((err = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 511 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 512 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 513 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 514 | |
| 515 | if ((err = memif_create (&c, &args, on_connect, |
| 516 | on_disconnect, on_interrupt, |
| 517 | NULL)) != MEMIF_ERR_SUCCESS) |
| 518 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 519 | |
| 520 | memif_connection_t *conn = (memif_connection_t *) c; |
| 521 | |
| 522 | conn->run_args.num_s2m_rings = 1; |
| 523 | conn->run_args.num_m2s_rings = 1; |
| 524 | conn->run_args.log2_ring_size = 10; |
| 525 | conn->run_args.buffer_size = 2048; |
| 526 | |
| 527 | if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) |
| 528 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 529 | |
| 530 | memif_msg_t msg; |
| 531 | memset (&msg, 0, sizeof (msg)); |
| 532 | msg.type = MEMIF_MSG_TYPE_CONNECT; |
| 533 | |
| 534 | memset (msg.connect.if_name, 0, sizeof (msg.connect.if_name)); |
| 535 | strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 536 | |
| 537 | if ((err = memif_msg_receive_connect (conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 538 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 539 | |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 540 | ck_assert_str_eq ((char *)conn->remote_if_name, TEST_IF_NAME); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | END_TEST |
| 544 | START_TEST (test_recv_connected) |
| 545 | { |
| 546 | int err; |
| 547 | memif_conn_handle_t c = NULL; |
| 548 | memif_conn_args_t args; |
| 549 | memset (&args, 0, sizeof (args)); |
| 550 | |
| 551 | args.interface_id = 0; |
| 552 | args.is_master = 0; |
| 553 | args.mode = 0; |
| 554 | |
| 555 | if ((err = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 556 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 557 | NULL, NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 558 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 559 | |
| 560 | if ((err = memif_create (&c, &args, on_connect, |
| 561 | on_disconnect, on_interrupt, |
| 562 | NULL)) != MEMIF_ERR_SUCCESS) |
| 563 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 564 | |
| 565 | memif_connection_t *conn = (memif_connection_t *) c; |
| 566 | |
| 567 | conn->run_args.num_s2m_rings = 1; |
| 568 | conn->run_args.num_m2s_rings = 1; |
| 569 | conn->run_args.log2_ring_size = 10; |
| 570 | conn->run_args.buffer_size = 2048; |
| 571 | |
| 572 | if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) |
| 573 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 574 | |
| 575 | memif_msg_t msg; |
| 576 | memset (&msg, 0, sizeof (msg)); |
| 577 | msg.type = MEMIF_MSG_TYPE_CONNECT; |
| 578 | |
| 579 | memset (msg.connect.if_name, 0, sizeof (msg.connect.if_name)); |
| 580 | strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 581 | |
| 582 | if ((err = memif_msg_receive_connected (conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 583 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 584 | |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 585 | ck_assert_str_eq ((char *)conn->remote_if_name, TEST_IF_NAME); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | END_TEST |
| 589 | START_TEST (test_recv_disconnect) |
| 590 | { |
| 591 | int err; |
| 592 | memif_connection_t conn; |
| 593 | memif_msg_t msg; |
| 594 | msg.type = MEMIF_MSG_TYPE_DISCONNECT; |
| 595 | memset (msg.disconnect.string, 0, sizeof (msg.disconnect.string)); |
| 596 | strncpy ((char *) msg.disconnect.string, "unit_test_dc", 12); |
| 597 | |
| 598 | if ((err = memif_msg_receive_disconnect (&conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 599 | ck_assert_msg (err == MEMIF_ERR_DISCONNECT, |
| 600 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 601 | |
msardara | 8f554b7 | 2018-12-11 18:36:55 +0100 | [diff] [blame] | 602 | ck_assert_str_eq ((char *)conn.remote_disconnect_string, "unit_test_dc"); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 605 | END_TEST Suite * |
| 606 | socket_suite () |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 607 | { |
| 608 | Suite *s; |
| 609 | TCase *tc_msg_queue; |
| 610 | TCase *tc_msg_enq; |
| 611 | TCase *tc_msg_send; |
| 612 | TCase *tc_msg_recv; |
| 613 | |
| 614 | /* create socket test suite */ |
| 615 | s = suite_create ("Socket messaging"); |
| 616 | |
| 617 | /* create msg queue test case */ |
| 618 | tc_msg_queue = tcase_create ("Message queue"); |
| 619 | /* add tests to test case */ |
| 620 | tcase_add_test (tc_msg_queue, test_msg_queue); |
| 621 | |
| 622 | /* create msg enq test case */ |
| 623 | tc_msg_enq = tcase_create ("Message enqueue"); |
| 624 | /* add tests to test case */ |
| 625 | tcase_add_test (tc_msg_enq, test_enq_ack); |
| 626 | tcase_add_test (tc_msg_enq, test_enq_init); |
| 627 | tcase_add_test (tc_msg_enq, test_enq_add_region); |
| 628 | tcase_add_test (tc_msg_enq, test_enq_add_ring); |
| 629 | tcase_add_test (tc_msg_enq, test_enq_connect); |
| 630 | tcase_add_test (tc_msg_enq, test_enq_connected); |
| 631 | |
| 632 | /* create msg send test case */ |
| 633 | tc_msg_send = tcase_create ("Message send"); |
| 634 | /* add tests to test case */ |
| 635 | tcase_add_test (tc_msg_send, test_send); |
| 636 | tcase_add_test (tc_msg_send, test_send_hello); |
| 637 | tcase_add_test (tc_msg_send, test_send_disconnect); |
| 638 | |
| 639 | /* create msg recv test case */ |
| 640 | tc_msg_recv = tcase_create ("Message receive"); |
| 641 | /* add tests to test case */ |
| 642 | tcase_add_test (tc_msg_recv, test_recv_hello); |
| 643 | tcase_add_test (tc_msg_recv, test_recv_init); |
| 644 | tcase_add_test (tc_msg_recv, test_recv_add_region); |
| 645 | tcase_add_test (tc_msg_recv, test_recv_add_ring); |
| 646 | tcase_add_test (tc_msg_recv, test_recv_connect); |
| 647 | tcase_add_test (tc_msg_recv, test_recv_connected); |
| 648 | tcase_add_test (tc_msg_recv, test_recv_disconnect); |
| 649 | |
| 650 | /* add test cases to test suite */ |
| 651 | suite_add_tcase (s, tc_msg_queue); |
| 652 | suite_add_tcase (s, tc_msg_enq); |
| 653 | suite_add_tcase (s, tc_msg_send); |
| 654 | suite_add_tcase (s, tc_msg_recv); |
| 655 | |
| 656 | /* return socket test suite to test runner */ |
| 657 | return s; |
| 658 | } |