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, |
| 52 | NULL)) != MEMIF_ERR_SUCCESS) |
| 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, |
| 100 | NULL)) != MEMIF_ERR_SUCCESS) |
| 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, |
| 120 | NULL)) != MEMIF_ERR_SUCCESS) |
| 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); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 143 | ck_assert_str_eq (i->secret, conn.args.secret); |
| 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, |
| 153 | NULL)) != MEMIF_ERR_SUCCESS) |
| 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, |
| 189 | NULL)) != MEMIF_ERR_SUCCESS) |
| 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, |
| 230 | NULL)) != MEMIF_ERR_SUCCESS) |
| 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); |
| 245 | ck_assert_str_eq (e->msg.connect.if_name, TEST_IF_NAME); |
| 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, |
| 255 | NULL)) != MEMIF_ERR_SUCCESS) |
| 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); |
| 270 | ck_assert_str_eq (e->msg.connect.if_name, TEST_IF_NAME); |
| 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, |
| 296 | 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 | |
| 299 | if ((err = memif_msg_send_hello (conn.fd)) != MEMIF_ERR_SUCCESS) |
| 300 | ck_assert_msg (err == MEMIF_ERR_BAD_FD, |
| 301 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 302 | } |
| 303 | |
| 304 | END_TEST |
| 305 | START_TEST (test_send_disconnect) |
| 306 | { |
| 307 | int err; |
| 308 | memif_connection_t conn; |
| 309 | conn.fd = -1; |
| 310 | |
| 311 | /* only possible fail if memif_msg_send fails... */ |
| 312 | /* obsolete without socket */ |
| 313 | if ((err = |
| 314 | memif_msg_send_disconnect (conn.fd, "unit_test_dc", |
| 315 | 0)) != MEMIF_ERR_SUCCESS) |
| 316 | ck_assert_msg (err == MEMIF_ERR_BAD_FD, "err code: %u, err msg: %s", err, |
| 317 | memif_strerror (err)); |
| 318 | } |
| 319 | |
| 320 | END_TEST |
| 321 | START_TEST (test_recv_hello) |
| 322 | { |
| 323 | int err; |
| 324 | memif_connection_t conn; |
| 325 | memif_msg_t msg; |
| 326 | |
| 327 | memif_msg_hello_t *h = &msg.hello; |
| 328 | |
| 329 | msg.type = MEMIF_MSG_TYPE_HELLO; |
| 330 | |
| 331 | h->min_version = MEMIF_VERSION; |
| 332 | h->max_version = MEMIF_VERSION; |
| 333 | h->max_s2m_ring = 1; |
| 334 | h->max_m2s_ring = 1; |
| 335 | h->max_log2_ring_size = 14; |
| 336 | strncpy ((char *) h->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 337 | memset (conn.remote_name, 0, sizeof (conn.remote_name)); |
| 338 | |
| 339 | conn.args.num_s2m_rings = 4; |
| 340 | conn.args.num_m2s_rings = 6; |
| 341 | conn.args.log2_ring_size = 10; |
| 342 | |
| 343 | if ((err = memif_msg_receive_hello (&conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 344 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 345 | |
| 346 | ck_assert_uint_eq (conn.run_args.num_s2m_rings, 2); |
| 347 | ck_assert_uint_eq (conn.run_args.num_m2s_rings, 2); |
| 348 | ck_assert_uint_eq (conn.run_args.log2_ring_size, 10); |
| 349 | ck_assert_str_eq (conn.remote_name, TEST_IF_NAME); |
| 350 | |
| 351 | h->max_version = 9; |
| 352 | if ((err = memif_msg_receive_hello (&conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 353 | ck_assert_msg (err == MEMIF_ERR_PROTO, |
| 354 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 355 | } |
| 356 | |
| 357 | END_TEST |
| 358 | START_TEST (test_recv_init) |
| 359 | { |
| 360 | int err; |
| 361 | memif_connection_t conn; |
| 362 | |
| 363 | conn.args.interface_id = 69; |
| 364 | conn.args.is_master = 1; |
| 365 | conn.fd = -1; |
| 366 | conn.args.mode = 0; |
| 367 | memset (conn.args.secret, '\0', 24); |
| 368 | strncpy ((char *) conn.args.secret, TEST_SECRET, strlen (TEST_SECRET)); |
| 369 | |
| 370 | memif_msg_t msg; |
| 371 | |
| 372 | memif_msg_init_t *i = &msg.init; |
| 373 | |
| 374 | msg.type = MEMIF_MSG_TYPE_INIT; |
| 375 | |
| 376 | i->version = MEMIF_VERSION; |
| 377 | i->id = 69; |
| 378 | i->mode = 0; |
| 379 | memset (i->name, '\0', 32); |
| 380 | memset (i->secret, '\0', 24); |
| 381 | strncpy ((char *) i->name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 382 | strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); |
| 383 | |
| 384 | memif_socket_t ms; |
| 385 | ms.interface_list_len = 1; |
| 386 | ms.interface_list = malloc (sizeof (memif_list_elt_t)); |
| 387 | memif_list_elt_t elt; |
| 388 | elt.key = 69; |
| 389 | elt.data_struct = &conn; |
| 390 | add_list_elt (&elt, &ms.interface_list, &ms.interface_list_len); |
| 391 | |
| 392 | if ((err = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 393 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
| 394 | NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 395 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 396 | |
| 397 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 398 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 399 | |
| 400 | i->version = 9; |
| 401 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 402 | ck_assert_msg (err == MEMIF_ERR_PROTO, |
| 403 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 404 | i->version = MEMIF_VERSION; |
| 405 | |
| 406 | i->id = 78; |
| 407 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 408 | ck_assert_msg (err == MEMIF_ERR_ID, |
| 409 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 410 | i->id = 69; |
| 411 | |
| 412 | i->mode = 1; |
| 413 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 414 | ck_assert_msg (err == MEMIF_ERR_MODE, |
| 415 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 416 | i->mode = 0; |
| 417 | |
| 418 | i->secret[0] = '\0'; |
| 419 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 420 | ck_assert_msg (err == MEMIF_ERR_SECRET, |
| 421 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 422 | strncpy ((char *) i->secret, TEST_SECRET, strlen (TEST_SECRET)); |
| 423 | |
| 424 | conn.args.is_master = 0; |
| 425 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 426 | ck_assert_msg (err == MEMIF_ERR_ACCSLAVE, |
| 427 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 428 | conn.args.is_master = 1; |
| 429 | |
| 430 | conn.fd = 5; |
| 431 | if ((err = memif_msg_receive_init (&ms, -1, &msg)) != MEMIF_ERR_SUCCESS) |
| 432 | ck_assert_msg ((err == MEMIF_ERR_ALRCONN) || (err == MEMIF_ERR_BAD_FD), |
| 433 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 434 | } |
| 435 | |
| 436 | END_TEST |
| 437 | START_TEST (test_recv_add_region) |
| 438 | { |
| 439 | int err; |
| 440 | memif_connection_t conn; |
| 441 | conn.regions = NULL; |
| 442 | memif_msg_t msg; |
| 443 | msg.type = MEMIF_MSG_TYPE_ADD_REGION; |
| 444 | msg.add_region.size = 2048; |
| 445 | msg.add_region.index = 0; |
| 446 | |
| 447 | int fd = 5; |
| 448 | |
| 449 | if ((err = |
| 450 | memif_msg_receive_add_region (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) |
| 451 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 452 | |
| 453 | memif_region_t *mr = conn.regions; |
| 454 | |
| 455 | ck_assert_uint_eq (mr->fd, fd); |
| 456 | ck_assert_uint_eq (mr->region_size, 2048); |
| 457 | ck_assert_ptr_eq (mr->shm, NULL); |
| 458 | } |
| 459 | |
| 460 | END_TEST |
| 461 | START_TEST (test_recv_add_ring) |
| 462 | { |
| 463 | int err; |
| 464 | memif_connection_t conn; |
| 465 | int fd = 5; |
| 466 | memif_msg_t msg; |
| 467 | conn.args.num_s2m_rings = 2; |
| 468 | conn.args.num_m2s_rings = 2; |
| 469 | conn.rx_queues = NULL; |
| 470 | conn.tx_queues = NULL; |
| 471 | |
| 472 | msg.type = MEMIF_MSG_TYPE_ADD_RING; |
| 473 | memif_msg_add_ring_t *ar = &msg.add_ring; |
| 474 | |
| 475 | ar->log2_ring_size = 10; |
| 476 | ar->region = 0; |
| 477 | ar->offset = 0; |
| 478 | ar->flags = 0; |
| 479 | ar->flags |= MEMIF_MSG_ADD_RING_FLAG_S2M; |
| 480 | ar->index = 1; |
| 481 | |
| 482 | if ((err = |
| 483 | memif_msg_receive_add_ring (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) |
| 484 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 485 | |
| 486 | |
| 487 | ar->offset = 2048; |
| 488 | ar->flags &= ~MEMIF_MSG_ADD_RING_FLAG_S2M; |
| 489 | |
| 490 | if ((err = |
| 491 | memif_msg_receive_add_ring (&conn, &msg, fd)) != MEMIF_ERR_SUCCESS) |
| 492 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 493 | |
| 494 | } |
| 495 | |
| 496 | END_TEST |
| 497 | START_TEST (test_recv_connect) |
| 498 | { |
| 499 | int err; |
| 500 | memif_conn_handle_t c = NULL; |
| 501 | memif_conn_args_t args; |
| 502 | memset (&args, 0, sizeof (args)); |
| 503 | |
| 504 | args.interface_id = 0; |
| 505 | args.is_master = 0; |
| 506 | args.mode = 0; |
| 507 | |
| 508 | if ((err = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 509 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
| 510 | NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 511 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 512 | |
| 513 | if ((err = memif_create (&c, &args, on_connect, |
| 514 | on_disconnect, on_interrupt, |
| 515 | NULL)) != MEMIF_ERR_SUCCESS) |
| 516 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 517 | |
| 518 | memif_connection_t *conn = (memif_connection_t *) c; |
| 519 | |
| 520 | conn->run_args.num_s2m_rings = 1; |
| 521 | conn->run_args.num_m2s_rings = 1; |
| 522 | conn->run_args.log2_ring_size = 10; |
| 523 | conn->run_args.buffer_size = 2048; |
| 524 | |
| 525 | if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) |
| 526 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 527 | |
| 528 | memif_msg_t msg; |
| 529 | memset (&msg, 0, sizeof (msg)); |
| 530 | msg.type = MEMIF_MSG_TYPE_CONNECT; |
| 531 | |
| 532 | memset (msg.connect.if_name, 0, sizeof (msg.connect.if_name)); |
| 533 | strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 534 | |
| 535 | if ((err = memif_msg_receive_connect (conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 536 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 537 | |
| 538 | ck_assert_str_eq (conn->remote_if_name, TEST_IF_NAME); |
| 539 | } |
| 540 | |
| 541 | END_TEST |
| 542 | START_TEST (test_recv_connected) |
| 543 | { |
| 544 | int err; |
| 545 | memif_conn_handle_t c = NULL; |
| 546 | memif_conn_args_t args; |
| 547 | memset (&args, 0, sizeof (args)); |
| 548 | |
| 549 | args.interface_id = 0; |
| 550 | args.is_master = 0; |
| 551 | args.mode = 0; |
| 552 | |
| 553 | if ((err = |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 554 | memif_init (control_fd_update, TEST_APP_NAME, NULL, |
| 555 | NULL)) != MEMIF_ERR_SUCCESS) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 556 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 557 | |
| 558 | if ((err = memif_create (&c, &args, on_connect, |
| 559 | on_disconnect, on_interrupt, |
| 560 | NULL)) != MEMIF_ERR_SUCCESS) |
| 561 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 562 | |
| 563 | memif_connection_t *conn = (memif_connection_t *) c; |
| 564 | |
| 565 | conn->run_args.num_s2m_rings = 1; |
| 566 | conn->run_args.num_m2s_rings = 1; |
| 567 | conn->run_args.log2_ring_size = 10; |
| 568 | conn->run_args.buffer_size = 2048; |
| 569 | |
| 570 | if ((err = memif_init_regions_and_queues (conn)) != MEMIF_ERR_SUCCESS) |
| 571 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 572 | |
| 573 | memif_msg_t msg; |
| 574 | memset (&msg, 0, sizeof (msg)); |
| 575 | msg.type = MEMIF_MSG_TYPE_CONNECT; |
| 576 | |
| 577 | memset (msg.connect.if_name, 0, sizeof (msg.connect.if_name)); |
| 578 | strncpy ((char *) msg.connect.if_name, TEST_IF_NAME, strlen (TEST_IF_NAME)); |
| 579 | |
| 580 | if ((err = memif_msg_receive_connected (conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 581 | ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err)); |
| 582 | |
| 583 | ck_assert_str_eq (conn->remote_if_name, TEST_IF_NAME); |
| 584 | } |
| 585 | |
| 586 | END_TEST |
| 587 | START_TEST (test_recv_disconnect) |
| 588 | { |
| 589 | int err; |
| 590 | memif_connection_t conn; |
| 591 | memif_msg_t msg; |
| 592 | msg.type = MEMIF_MSG_TYPE_DISCONNECT; |
| 593 | memset (msg.disconnect.string, 0, sizeof (msg.disconnect.string)); |
| 594 | strncpy ((char *) msg.disconnect.string, "unit_test_dc", 12); |
| 595 | |
| 596 | if ((err = memif_msg_receive_disconnect (&conn, &msg)) != MEMIF_ERR_SUCCESS) |
| 597 | ck_assert_msg (err == MEMIF_ERR_DISCONNECT, |
| 598 | "err code: %u, err msg: %s", err, memif_strerror (err)); |
| 599 | |
| 600 | ck_assert_str_eq (conn.remote_disconnect_string, "unit_test_dc"); |
| 601 | } |
| 602 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 603 | END_TEST Suite * |
| 604 | socket_suite () |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 605 | { |
| 606 | Suite *s; |
| 607 | TCase *tc_msg_queue; |
| 608 | TCase *tc_msg_enq; |
| 609 | TCase *tc_msg_send; |
| 610 | TCase *tc_msg_recv; |
| 611 | |
| 612 | /* create socket test suite */ |
| 613 | s = suite_create ("Socket messaging"); |
| 614 | |
| 615 | /* create msg queue test case */ |
| 616 | tc_msg_queue = tcase_create ("Message queue"); |
| 617 | /* add tests to test case */ |
| 618 | tcase_add_test (tc_msg_queue, test_msg_queue); |
| 619 | |
| 620 | /* create msg enq test case */ |
| 621 | tc_msg_enq = tcase_create ("Message enqueue"); |
| 622 | /* add tests to test case */ |
| 623 | tcase_add_test (tc_msg_enq, test_enq_ack); |
| 624 | tcase_add_test (tc_msg_enq, test_enq_init); |
| 625 | tcase_add_test (tc_msg_enq, test_enq_add_region); |
| 626 | tcase_add_test (tc_msg_enq, test_enq_add_ring); |
| 627 | tcase_add_test (tc_msg_enq, test_enq_connect); |
| 628 | tcase_add_test (tc_msg_enq, test_enq_connected); |
| 629 | |
| 630 | /* create msg send test case */ |
| 631 | tc_msg_send = tcase_create ("Message send"); |
| 632 | /* add tests to test case */ |
| 633 | tcase_add_test (tc_msg_send, test_send); |
| 634 | tcase_add_test (tc_msg_send, test_send_hello); |
| 635 | tcase_add_test (tc_msg_send, test_send_disconnect); |
| 636 | |
| 637 | /* create msg recv test case */ |
| 638 | tc_msg_recv = tcase_create ("Message receive"); |
| 639 | /* add tests to test case */ |
| 640 | tcase_add_test (tc_msg_recv, test_recv_hello); |
| 641 | tcase_add_test (tc_msg_recv, test_recv_init); |
| 642 | tcase_add_test (tc_msg_recv, test_recv_add_region); |
| 643 | tcase_add_test (tc_msg_recv, test_recv_add_ring); |
| 644 | tcase_add_test (tc_msg_recv, test_recv_connect); |
| 645 | tcase_add_test (tc_msg_recv, test_recv_connected); |
| 646 | tcase_add_test (tc_msg_recv, test_recv_disconnect); |
| 647 | |
| 648 | /* add test cases to test suite */ |
| 649 | suite_add_tcase (s, tc_msg_queue); |
| 650 | suite_add_tcase (s, tc_msg_enq); |
| 651 | suite_add_tcase (s, tc_msg_send); |
| 652 | suite_add_tcase (s, tc_msg_recv); |
| 653 | |
| 654 | /* return socket test suite to test runner */ |
| 655 | return s; |
| 656 | } |