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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// SPDX-License-Identifier: BSD-3-Clause
/**
* @file test-sockaddr.c
*
* @brief mptcpd sockaddr utilities test.
*
* Copyright (c) 2021, 2022, Intel Corporation
*/
#include <sys/un.h>
#include <ell/ell.h>
#include <mptcpd/private/sockaddr.h>
#include "test-plugin.h" // For test sockaddrs
#undef NDEBUG
#include <assert.h>
static void test_network_byte_swap(void const *test_data)
{
(void) test_data;
uint16_t const hs = 0x1234;
uint32_t const hl = 0x040200C0;
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint16_t const ns = 0x3412;
uint32_t const nl = 0xC0000204;
assert(MPTCPD_CONSTANT_HTONS(hs) == ns);
assert(MPTCPD_CONSTANT_HTONL(hl) == nl);
#else
assert(MPTCPD_CONSTANT_HTONS(hs) == hs);
assert(MPTCPD_CONSTANT_HTONL(hl) == hl);
#endif // __BYTE_ORDER == __LITTLE_ENDIAN
}
static void test_bad_sockaddr_init(void const *test_data)
{
(void) test_data;
static in_addr_t const *const bad_addr4 = NULL;
static struct in6_addr const *const bad_addr6 = NULL;
static struct sockaddr_storage *const bad_addr = NULL;
static in_addr_t const *const good_addr4 =
&test_laddr_1.sin_addr.s_addr;
static unsigned short const port = 0;
struct sockaddr_storage good_addr = { .ss_family = AF_UNSPEC };
// No source in{6}_addrs.
assert(!mptcpd_sockaddr_storage_init(bad_addr4,
bad_addr6,
port,
&good_addr));
// No destination addr.
assert(!mptcpd_sockaddr_storage_init(good_addr4,
bad_addr6,
port,
bad_addr));
}
static void test_sockaddr_in_init(void const *test_data)
{
(void) test_data;
in_addr_t const *const addr4 = &test_laddr_1.sin_addr.s_addr;
struct in6_addr const *const addr6 = NULL;
unsigned short const port = test_laddr_1.sin_port;
struct sockaddr_storage addr = { .ss_family = AF_UNSPEC };
bool const initialized =
mptcpd_sockaddr_storage_init(addr4, addr6, port, &addr);
assert(initialized
&& sockaddr_is_equal((struct sockaddr const *) &test_laddr_1,
(struct sockaddr const *) &addr));
}
static void test_sockaddr_in6_init(void const *test_data)
{
(void) test_data;
in_addr_t const *const addr4 = NULL;
struct in6_addr const *const addr6 = &test_laddr_2.sin6_addr;
unsigned short const port = test_laddr_2.sin6_port;
struct sockaddr_storage addr = { .ss_family = AF_UNSPEC };
bool const initialized =
mptcpd_sockaddr_storage_init(addr4, addr6, port, &addr);
assert(initialized
&& sockaddr_is_equal((struct sockaddr const *) &test_laddr_2,
(struct sockaddr const *) &addr));
}
static void test_copy_null(void const *test_data)
{
(void) test_data;
assert(mptcpd_sockaddr_copy(NULL) == NULL);
}
static bool test_copy_inet(struct sockaddr const *src)
{
struct sockaddr *const dst = mptcpd_sockaddr_copy(src);
bool const is_equal = sockaddr_is_equal(src, dst);
l_free(dst);
return is_equal;
}
static void test_copy_af_inet(void const *test_data)
{
(void) test_data;
struct sockaddr_in const addr = {
.sin_family = AF_INET,
.sin_port = htons(0x1234),
.sin_addr = {
.s_addr = htonl(0xC0000201) // 192.0.2.1
}
};
struct sockaddr const *const src = (struct sockaddr const *) &addr;
assert(test_copy_inet(src));
}
static void test_copy_af_inet6(void const *test_data)
{
(void) test_data;
struct sockaddr_in6 const addr = {
.sin6_family = AF_INET6,
.sin6_port = htons(0x5678),
.sin6_addr = {
.s6_addr = { [0] = 0x20,
[1] = 0x01,
[2] = 0x0D,
[3] = 0xB8,
[14] = 0x01,
[15] = 0x02 } // 2001:DB8::102
}
};
struct sockaddr const *const src =
(struct sockaddr const *) &addr;
assert(test_copy_inet(src));
}
static void test_copy_non_inet(void const *test_data)
{
(void) test_data;
struct sockaddr const src = { .sa_family = AF_UNIX };
assert(mptcpd_sockaddr_copy(&src) == NULL);
}
int main(int argc, char *argv[])
{
l_log_set_stderr();
l_test_init(&argc, &argv);
l_test_add("network byte swap", test_network_byte_swap, NULL);
l_test_add("bad sockaddr init", test_bad_sockaddr_init, NULL);
l_test_add("sockaddr_in init", test_sockaddr_in_init, NULL);
l_test_add("sockaddr_in6 init", test_sockaddr_in6_init, NULL);
l_test_add("copy - NULL", test_copy_null, NULL);
l_test_add("copy - AF_INET", test_copy_af_inet, NULL);
l_test_add("copy - AF_INET6", test_copy_af_inet6, NULL);
l_test_add("copy - non-INET", test_copy_non_inet, NULL);
return l_test_run();
}
/*
Local Variables:
c-file-style: "linux"
End:
*/
|