aboutsummaryrefslogtreecommitdiffstats
path: root/tests/libntp/netof.c
blob: 59dd7098d2a746c53fa665ecfa7ffbcd70e7c843 (plain) (blame)
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
#include "config.h"

#include "ntp_stdlib.h"
#include "ntp_calendar.h"

#include "unity.h"

#include "sockaddrtest.h"


void setUp(void);
void test_ClassBAddress(void);
void test_ClassCAddress(void);
void test_ClassAAddress(void);
void test_IPv6Address(void);


void
setUp(void)
{
	init_lib();

	return;
}


void
test_ClassBAddress(void)
{
	sockaddr_u input = CreateSockaddr4("172.16.2.1", NTP_PORT);
	sockaddr_u expected = CreateSockaddr4("172.16.0.0", NTP_PORT);

	sockaddr_u* actual = netof(&input);

	TEST_ASSERT_TRUE(actual != NULL);
	TEST_ASSERT_TRUE(IsEqual(expected, *actual));

	return;
}

void
test_ClassCAddress(void)
{
	sockaddr_u input = CreateSockaddr4("192.0.2.255", NTP_PORT);
	sockaddr_u expected = CreateSockaddr4("192.0.2.0", NTP_PORT);

	sockaddr_u* actual = netof(&input);

	TEST_ASSERT_TRUE(actual != NULL);
	TEST_ASSERT_TRUE(IsEqual(expected, *actual));

	return;
}


void
test_ClassAAddress(void)
{
	/* Class A addresses are assumed to be classless,
	 * thus the same address should be returned.
	 */
	sockaddr_u input = CreateSockaddr4("10.20.30.40", NTP_PORT);
	sockaddr_u expected = CreateSockaddr4("10.20.30.40", NTP_PORT);

	sockaddr_u* actual = netof(&input);

	TEST_ASSERT_TRUE(actual != NULL);
	TEST_ASSERT_TRUE(IsEqual(expected, *actual));

	return;
}

void
test_IPv6Address(void)
{
	/* IPv6 addresses are assumed to have 64-bit host- and 64-bit network parts. */
	const struct in6_addr input_address = { { {
		0x20, 0x01, 0x0d, 0xb8,
		0x85, 0xa3, 0x08, 0xd3, 
		0x13, 0x19, 0x8a, 0x2e,
		0x03, 0x70, 0x73, 0x34
	} } }; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334

	const struct in6_addr expected_address = { { {
		0x20, 0x01, 0x0d, 0xb8,
		0x85, 0xa3, 0x08, 0xd3, 
		0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00
	} } }; // 2001:0db8:85a3:08d3:0000:0000:0000:0000

	sockaddr_u input;
	input.sa6.sin6_family = AF_INET6;
	input.sa6.sin6_addr = input_address;
	SET_PORT(&input, 3000);

	sockaddr_u expected;
	expected.sa6.sin6_family = AF_INET6;
	expected.sa6.sin6_addr = expected_address;
	SET_PORT(&expected, 3000);

	sockaddr_u* actual = netof(&input);

	TEST_ASSERT_TRUE(actual != NULL);
	TEST_ASSERT_TRUE(IsEqual(expected, *actual));

	return;
}