| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <errno.h>
|
|---|
| 30 | #include <inet/endpoint.h>
|
|---|
| 31 | #include <io/log.h>
|
|---|
| 32 | #include <pcut/pcut.h>
|
|---|
| 33 |
|
|---|
| 34 | #include "../conn.h"
|
|---|
| 35 | #include "../rqueue.h"
|
|---|
| 36 | #include "../ucall.h"
|
|---|
| 37 |
|
|---|
| 38 | PCUT_INIT
|
|---|
| 39 |
|
|---|
| 40 | PCUT_TEST_SUITE(conn);
|
|---|
| 41 |
|
|---|
| 42 | static tcp_rqueue_cb_t test_rqueue_cb = {
|
|---|
| 43 | .seg_received = tcp_as_segment_arrived
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | PCUT_TEST_BEFORE
|
|---|
| 47 | {
|
|---|
| 48 | int rc;
|
|---|
| 49 |
|
|---|
| 50 | /* We will be calling functions that perform logging */
|
|---|
| 51 | rc = log_init("test-tcp");
|
|---|
| 52 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 53 |
|
|---|
| 54 | rc = tcp_conns_init();
|
|---|
| 55 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 56 |
|
|---|
| 57 | tcp_rqueue_init(&test_rqueue_cb);
|
|---|
| 58 | tcp_rqueue_fibril_start();
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | PCUT_TEST_AFTER
|
|---|
| 62 | {
|
|---|
| 63 | tcp_rqueue_fini();
|
|---|
| 64 | tcp_conns_fini();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /** Test creating and deleting connection */
|
|---|
| 68 | PCUT_TEST(new_delete)
|
|---|
| 69 | {
|
|---|
| 70 | tcp_conn_t *conn;
|
|---|
| 71 | inet_ep2_t epp;
|
|---|
| 72 |
|
|---|
| 73 | inet_ep2_init(&epp);
|
|---|
| 74 | conn = tcp_conn_new(&epp);
|
|---|
| 75 | PCUT_ASSERT_NOT_NULL(conn);
|
|---|
| 76 |
|
|---|
| 77 | tcp_conn_lock(conn);
|
|---|
| 78 | tcp_conn_reset(conn);
|
|---|
| 79 | tcp_conn_unlock(conn);
|
|---|
| 80 | tcp_conn_delete(conn);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /** Test adding, finding and deleting a connection */
|
|---|
| 84 | PCUT_TEST(add_find_delete)
|
|---|
| 85 | {
|
|---|
| 86 | tcp_conn_t *conn, *cfound;
|
|---|
| 87 | inet_ep2_t epp;
|
|---|
| 88 | int rc;
|
|---|
| 89 |
|
|---|
| 90 | inet_ep2_init(&epp);
|
|---|
| 91 |
|
|---|
| 92 | conn = tcp_conn_new(&epp);
|
|---|
| 93 | PCUT_ASSERT_NOT_NULL(conn);
|
|---|
| 94 |
|
|---|
| 95 | rc = tcp_conn_add(conn);
|
|---|
| 96 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 97 |
|
|---|
| 98 | /* Find the connection */
|
|---|
| 99 | cfound = tcp_conn_find_ref(&conn->ident);
|
|---|
| 100 | PCUT_ASSERT_EQUALS(conn, cfound);
|
|---|
| 101 | tcp_conn_delref(cfound);
|
|---|
| 102 |
|
|---|
| 103 | /* We should have been assigned a port address, should not match */
|
|---|
| 104 | cfound = tcp_conn_find_ref(&epp);
|
|---|
| 105 | PCUT_ASSERT_EQUALS(NULL, cfound);
|
|---|
| 106 |
|
|---|
| 107 | tcp_conn_lock(conn);
|
|---|
| 108 | tcp_conn_reset(conn);
|
|---|
| 109 | tcp_conn_unlock(conn);
|
|---|
| 110 | tcp_conn_delete(conn);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** Test trying to connect to endpoint that sends RST back */
|
|---|
| 114 | PCUT_TEST(connect_rst)
|
|---|
| 115 | {
|
|---|
| 116 | tcp_conn_t *conn;
|
|---|
| 117 | inet_ep2_t epp;
|
|---|
| 118 | int rc;
|
|---|
| 119 |
|
|---|
| 120 | tcp_conn_lb = tcp_lb_segment;
|
|---|
| 121 |
|
|---|
| 122 | inet_ep2_init(&epp);
|
|---|
| 123 | inet_addr(&epp.local.addr, 127, 0, 0, 1);
|
|---|
| 124 | inet_addr(&epp.remote.addr, 127, 0, 0, 1);
|
|---|
| 125 | epp.remote.port = inet_port_user_lo;
|
|---|
| 126 |
|
|---|
| 127 | conn = tcp_conn_new(&epp);
|
|---|
| 128 | PCUT_ASSERT_NOT_NULL(conn);
|
|---|
| 129 |
|
|---|
| 130 | rc = tcp_conn_add(conn);
|
|---|
| 131 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 132 |
|
|---|
| 133 | PCUT_ASSERT_INT_EQUALS(st_listen, conn->cstate);
|
|---|
| 134 |
|
|---|
| 135 | tcp_conn_lock(conn);
|
|---|
| 136 | tcp_conn_sync(conn);
|
|---|
| 137 | PCUT_ASSERT_INT_EQUALS(st_syn_sent, conn->cstate);
|
|---|
| 138 |
|
|---|
| 139 | while (conn->cstate == st_syn_sent)
|
|---|
| 140 | fibril_condvar_wait(&conn->cstate_cv, &conn->lock);
|
|---|
| 141 |
|
|---|
| 142 | PCUT_ASSERT_INT_EQUALS(st_closed, conn->cstate);
|
|---|
| 143 |
|
|---|
| 144 | tcp_conn_unlock(conn);
|
|---|
| 145 | tcp_conn_delete(conn);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | PCUT_EXPORT(conn);
|
|---|