mirror of
https://github.com/yarrick/iodine.git
synced 2024-11-10 02:39:18 +02:00
13d081f5ed
To work with check 0.15.0 Add == 0 to change fail_if to asserts as well.
221 lines
6.8 KiB
C
221 lines
6.8 KiB
C
/*
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>
|
|
*
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
|
|
#include <check.h>
|
|
#include <common.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <netdb.h>
|
|
|
|
START_TEST(test_topdomain_ok)
|
|
{
|
|
char *error;
|
|
|
|
ck_assert(check_topdomain("foo.0123456789.qwertyuiop.asdfghjkl.zxcvbnm.com", &error) == 0);
|
|
|
|
/* Not allowed to start with dot */
|
|
ck_assert(check_topdomain(".foo.0123456789.qwertyuiop.asdfghjkl.zxcvbnm.com", &error));
|
|
ck_assert(strcmp("Starts with a dot", error) == 0);
|
|
|
|
/* Test missing error msg ptr */
|
|
ck_assert(check_topdomain(".foo", NULL));
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(test_topdomain_length)
|
|
{
|
|
char *error;
|
|
|
|
/* Test empty and too short */
|
|
ck_assert(check_topdomain("", &error));
|
|
ck_assert(strcmp("Too short (< 3)", error) == 0);
|
|
ck_assert(check_topdomain("a", &error));
|
|
ck_assert(strcmp("Too short (< 3)", error) == 0);
|
|
ck_assert(check_topdomain(".a", &error));
|
|
ck_assert(strcmp("Too short (< 3)", error) == 0);
|
|
ck_assert(check_topdomain("a.", &error));
|
|
ck_assert(strcmp("Too short (< 3)", error) == 0);
|
|
ck_assert(check_topdomain("ab", &error));
|
|
ck_assert(strcmp("Too short (< 3)", error) == 0);
|
|
ck_assert(check_topdomain("a.b", &error) == 0);
|
|
ck_assert(strcmp("Too short (< 3)", error) == 0);
|
|
|
|
/* Test too long (over 128, need rest of space for data) */
|
|
ck_assert(check_topdomain(
|
|
"abcd12345.abcd12345.abcd12345.abcd12345.abcd12345."
|
|
"abcd12345.abcd12345.abcd12345.abcd12345.abcd12345."
|
|
"abcd12345.abcd12345.foo129xxx", &error));
|
|
ck_assert(strcmp("Too long (> 128)", error) == 0);
|
|
ck_assert(check_topdomain(
|
|
"abcd12345.abcd12345.abcd12345.abcd12345.abcd12345."
|
|
"abcd12345.abcd12345.abcd12345.abcd12345.abcd12345."
|
|
"abcd12345.abcd12345.foo128xx", &error) == 0);
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(test_topdomain_chunks)
|
|
{
|
|
char *error;
|
|
|
|
/* Must have at least one dot */
|
|
ck_assert(check_topdomain("abcde.gh", &error) == 0);
|
|
ck_assert(check_topdomain("abcdefgh", &error));
|
|
ck_assert(strcmp("No dots", error) == 0);
|
|
|
|
/* Not two consecutive dots */
|
|
ck_assert(check_topdomain("abc..defgh", &error));
|
|
ck_assert(strcmp("Consecutive dots", error) == 0);
|
|
|
|
/* Not end with a dots */
|
|
ck_assert(check_topdomain("abc.defgh.", &error));
|
|
ck_assert(strcmp("Ends with a dot", error) == 0);
|
|
|
|
/* No chunk longer than 63 chars */
|
|
ck_assert(check_topdomain("123456789012345678901234567890"
|
|
"123456789012345678901234567890333.com", &error) == 0);
|
|
ck_assert(check_topdomain("123456789012345678901234567890"
|
|
"1234567890123456789012345678904444.com", &error));
|
|
ck_assert(strcmp("Too long domain part (> 63)", error) == 0);
|
|
|
|
ck_assert(check_topdomain("abc.123456789012345678901234567890"
|
|
"123456789012345678901234567890333.com", &error) == 0);
|
|
ck_assert(check_topdomain("abc.123456789012345678901234567890"
|
|
"1234567890123456789012345678904444.com", &error));
|
|
ck_assert(strcmp("Too long domain part (> 63)", error) == 0);
|
|
|
|
ck_assert(check_topdomain("abc.123456789012345678901234567890"
|
|
"123456789012345678901234567890333", &error) == 0);
|
|
ck_assert(check_topdomain("abc.123456789012345678901234567890"
|
|
"1234567890123456789012345678904444", &error));
|
|
ck_assert(strcmp("Too long domain part (> 63)", error) == 0);
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(test_parse_format_ipv4)
|
|
{
|
|
char *host = "192.168.2.10";
|
|
char *formatted;
|
|
struct sockaddr_storage addr;
|
|
struct sockaddr_in *v4addr;
|
|
int addr_len;
|
|
|
|
addr_len = get_addr(host, 53, AF_INET, 0, &addr);
|
|
ck_assert(addr_len == sizeof(struct sockaddr_in));
|
|
|
|
v4addr = (struct sockaddr_in *) &addr;
|
|
ck_assert(v4addr->sin_addr.s_addr == htonl(0xc0a8020a));
|
|
ck_assert(v4addr->sin_port == htons(53));
|
|
|
|
formatted = format_addr(&addr, addr_len);
|
|
ck_assert(strcmp(host, formatted) == 0);
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(test_parse_format_ipv4_listen_all)
|
|
{
|
|
char *host = "0.0.0.0";
|
|
char *formatted;
|
|
struct sockaddr_storage addr;
|
|
struct sockaddr_in *v4addr;
|
|
int addr_len;
|
|
|
|
addr_len = get_addr(NULL, 53, AF_INET, AI_PASSIVE, &addr);
|
|
ck_assert(addr_len == sizeof(struct sockaddr_in));
|
|
|
|
v4addr = (struct sockaddr_in *) &addr;
|
|
ck_assert(v4addr->sin_addr.s_addr == htonl(0x00000000));
|
|
ck_assert(v4addr->sin_port == htons(53));
|
|
|
|
formatted = format_addr(&addr, addr_len);
|
|
ck_assert(strcmp(host, formatted) == 0);
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(test_parse_format_ipv6)
|
|
{
|
|
char *host = "2001:0db8:0505:0::123:0abc";
|
|
char *compact = "2001:db8:505::123:abc";
|
|
unsigned char v6_bits[] = {
|
|
0x20, 0x01, 0x0d, 0xb8, 0x05, 0x05, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x0a, 0xbc,
|
|
};
|
|
char *formatted;
|
|
struct sockaddr_storage addr;
|
|
struct sockaddr_in6 *v6addr;
|
|
int addr_len;
|
|
|
|
addr_len = get_addr(host, 53, AF_UNSPEC, 0, &addr);
|
|
ck_assert(addr_len == sizeof(struct sockaddr_in6));
|
|
|
|
v6addr = (struct sockaddr_in6 *) &addr;
|
|
ck_assert(memcmp(&v6addr->sin6_addr, v6_bits, sizeof(v6_bits)) == 0);
|
|
ck_assert(v6addr->sin6_port == htons(53));
|
|
|
|
formatted = format_addr(&addr, addr_len);
|
|
ck_assert(strcmp(compact, formatted) == 0);
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(test_parse_format_ipv4_mapped_ipv6)
|
|
{
|
|
char *v4mapped = "::FFFF:192.168.2.10";
|
|
char *host = "192.168.2.10";
|
|
unsigned char v6_bits[] = {
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x02, 0x0a,
|
|
};
|
|
char *formatted;
|
|
struct sockaddr_storage addr;
|
|
struct sockaddr_in6 *v6addr;
|
|
int addr_len;
|
|
|
|
addr_len = get_addr(v4mapped, 53, AF_INET6, 0, &addr);
|
|
ck_assert(addr_len == sizeof(struct sockaddr_in6));
|
|
|
|
v6addr = (struct sockaddr_in6 *) &addr;
|
|
ck_assert(memcmp(&v6addr->sin6_addr, v6_bits, sizeof(v6_bits)) == 0);
|
|
ck_assert(v6addr->sin6_port == htons(53));
|
|
|
|
/* Format as IPv4 address */
|
|
formatted = format_addr(&addr, addr_len);
|
|
ck_assert(strcmp(host, formatted) == 0);
|
|
}
|
|
END_TEST
|
|
|
|
TCase *
|
|
test_common_create_tests()
|
|
{
|
|
TCase *tc;
|
|
int sock;
|
|
|
|
tc = tcase_create("Common");
|
|
tcase_add_test(tc, test_topdomain_ok);
|
|
tcase_add_test(tc, test_topdomain_length);
|
|
tcase_add_test(tc, test_topdomain_chunks);
|
|
tcase_add_test(tc, test_parse_format_ipv4);
|
|
tcase_add_test(tc, test_parse_format_ipv4_listen_all);
|
|
|
|
/* Tests require IPv6 support */
|
|
sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
if (sock >= 0) {
|
|
close(sock);
|
|
tcase_add_test(tc, test_parse_format_ipv6);
|
|
tcase_add_test(tc, test_parse_format_ipv4_mapped_ipv6);
|
|
}
|
|
return tc;
|
|
}
|