1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
* Copyright (C) 2013 - David Goulet <dgoulet@ev0ke.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 2 only, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <common/connection.h>
#include <common/defaults.h>
#include <tap/tap.h>
#define NUM_TESTS 13
static void test_connection_usage(void)
{
int ret;
struct connection *conn, *conn2, *l_conn;
struct connection_addr c_addr;
diag("Connection subsystem creation test");
ret = connection_addr_set(CONNECTION_DOMAIN_INET, "127.0.0.1", 9050,
&c_addr);
ok(ret == 0 &&
c_addr.domain == CONNECTION_DOMAIN_INET &&
c_addr.u.sin.sin_family == AF_INET &&
c_addr.u.sin.sin_port == htons(9050),
"Valid connection address creation");
conn = connection_create(42, (struct sockaddr *) &c_addr.u.sin);
ok(conn &&
conn->fd == 42 &&
conn->dest_addr.domain == CONNECTION_DOMAIN_INET &&
conn->refcount.count == 1,
"Valid connection creation");
if (!conn) {
return;
}
conn2 = connection_create(43, (struct sockaddr *) &c_addr.u.sin);
ok(conn2 &&
conn2->fd == 43 &&
conn2->dest_addr.domain == CONNECTION_DOMAIN_INET &&
conn2->refcount.count == 1,
"Valid second connection creation");
if (!conn2) {
return;
}
connection_registry_lock();
connection_insert(conn);
l_conn = connection_find(conn->fd);
ok(conn == l_conn, "Valid connection insert/find");
connection_insert(conn2);
l_conn = connection_find(conn2->fd);
ok(conn2 == l_conn, "Valid second connection insert/find");
connection_remove(conn);
l_conn = connection_find(conn->fd);
ok(conn != l_conn, "Valid connection remove/find");
connection_remove(conn2);
l_conn = connection_find(conn2->fd);
ok(conn2 != l_conn, "Valid second connection remove/find");
connection_registry_unlock();
connection_destroy(conn);
connection_destroy(conn2);
}
static void test_connection_creation(void)
{
int ret;
struct connection *conn;
struct connection_addr c_addr;
diag("Connection subsystem creation test");
ret = connection_addr_set(CONNECTION_DOMAIN_INET, "127.0.0.1", 9050,
&c_addr);
ok(ret == 0 &&
c_addr.domain == CONNECTION_DOMAIN_INET &&
c_addr.u.sin.sin_family == AF_INET &&
c_addr.u.sin.sin_port == htons(9050),
"Valid connection address creation");
conn = connection_create(42, (struct sockaddr *) &c_addr.u.sin);
ok(conn &&
conn->fd == 42 &&
conn->dest_addr.domain == CONNECTION_DOMAIN_INET &&
conn->refcount.count == 1,
"Valid connection creation");
connection_destroy(conn);
conn = connection_create(-1, (struct sockaddr *) &c_addr.u.sin);
ok(conn &&
conn->fd == -1 &&
conn->dest_addr.domain == CONNECTION_DOMAIN_INET &&
conn->refcount.count == 1,
"Valid connection creation with fd -1");
connection_destroy(conn);
conn = connection_create(42, NULL);
ok(conn &&
conn->fd == 42 &&
conn->dest_addr.domain == 0 &&
conn->refcount.count == 1,
"Valid connection creation with sockaddr NULL");
connection_destroy(conn);
ret = connection_addr_set(CONNECTION_DOMAIN_INET6, "::1", 9050,
&c_addr);
ok(ret == 0 &&
c_addr.domain == CONNECTION_DOMAIN_INET6 &&
c_addr.u.sin.sin_family == AF_INET6 &&
c_addr.u.sin.sin_port == htons(9050),
"Valid connection address creation for IPv6");
conn = connection_create(42, (struct sockaddr *) &c_addr.u.sin6);
ok(conn &&
conn->fd == 42 &&
conn->dest_addr.domain == CONNECTION_DOMAIN_INET6 &&
conn->refcount.count == 1,
"Valid connection creation for IPv6");
connection_destroy(conn);
}
int main(int argc, char **argv)
{
/* Libtap call for the number of tests planned. */
plan_tests(NUM_TESTS);
test_connection_creation();
test_connection_usage();
return exit_status();
}
|