aboutsummaryrefslogtreecommitdiff
path: root/tests/libntp/netof.cpp
blob: 67b9df4e7cf77e0191701b2846581f480993b9c4 (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
#include "sockaddrtest.h"

class netofTest : public sockaddrtest  {
};

TEST_F(netofTest, ClassBAddress) {
	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);

	ASSERT_TRUE(actual != NULL);
	EXPECT_TRUE(IsEqual(expected, *actual));
}

TEST_F(netofTest, ClassCAddress) {
	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);

	ASSERT_TRUE(actual != NULL);
	EXPECT_TRUE(IsEqual(expected, *actual));
}

TEST_F(netofTest, ClassAAddress) {
	/* 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);

	ASSERT_TRUE(actual != NULL);
	EXPECT_TRUE(IsEqual(expected, *actual));
}

TEST_F(netofTest, IPv6Address) {
	/* 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);

	ASSERT_TRUE(actual != NULL);
	EXPECT_TRUE(IsEqual(expected, *actual));
}