]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_decodenetnum.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_decodenetnum.cpp
1 #include "g_sockaddrtest.h"
2
3 class decodenetnumTest : public sockaddrtest {
4 };
5
6 TEST_F(decodenetnumTest, IPv4AddressOnly) {
7         const char *str = "192.0.2.1";
8         sockaddr_u actual;
9
10         sockaddr_u expected;
11         expected.sa4.sin_family = AF_INET;
12         expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
13         SET_PORT(&expected, NTP_PORT);
14
15         ASSERT_TRUE(decodenetnum(str, &actual));
16         EXPECT_TRUE(IsEqual(expected, actual));
17 }
18
19 TEST_F(decodenetnumTest, IPv4AddressWithPort) {
20         const char *str = "192.0.2.2:2000";
21         sockaddr_u actual;
22
23         sockaddr_u expected;
24         expected.sa4.sin_family = AF_INET;
25         expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
26         SET_PORT(&expected, 2000);
27
28         ASSERT_TRUE(decodenetnum(str, &actual));
29         EXPECT_TRUE(IsEqual(expected, actual));
30 }
31
32 TEST_F(decodenetnumTest, IPv6AddressOnly) {
33         const struct in6_addr address = {
34                 0x20, 0x01, 0x0d, 0xb8,
35         0x85, 0xa3, 0x08, 0xd3, 
36         0x13, 0x19, 0x8a, 0x2e,
37         0x03, 0x70, 0x73, 0x34
38         };
39
40         const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
41         sockaddr_u actual;
42
43         sockaddr_u expected;
44         expected.sa6.sin6_family = AF_INET6;
45         expected.sa6.sin6_addr = address;
46         SET_PORT(&expected, NTP_PORT);
47
48         ASSERT_TRUE(decodenetnum(str, &actual));
49         EXPECT_TRUE(IsEqual(expected, actual));
50 }
51
52 TEST_F(decodenetnumTest, IPv6AddressWithPort) {
53         const struct in6_addr address = {
54                 0x20, 0x01, 0x0d, 0xb8,
55         0x85, 0xa3, 0x08, 0xd3, 
56         0x13, 0x19, 0x8a, 0x2e,
57         0x03, 0x70, 0x73, 0x34
58         };
59
60         const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
61         sockaddr_u actual;
62
63         sockaddr_u expected;
64         expected.sa6.sin6_family = AF_INET6;
65         expected.sa6.sin6_addr = address;
66         SET_PORT(&expected, 3000);
67
68         ASSERT_TRUE(decodenetnum(str, &actual));
69         EXPECT_TRUE(IsEqual(expected, actual));
70 }
71
72 TEST_F(decodenetnumTest, IllegalAddress) {
73         const char *str = "192.0.2.270:2000";
74         sockaddr_u actual;
75
76         ASSERT_FALSE(decodenetnum(str, &actual));
77 }
78
79 TEST_F(decodenetnumTest, IllegalCharInPort) {
80         /* An illegal port does not make the decodenetnum fail, but instead
81          * makes it use the standard port.
82          */
83         const char *str = "192.0.2.1:a700";
84         sockaddr_u actual;
85
86         sockaddr_u expected;
87         expected.sa4.sin_family = AF_INET;
88         expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
89         SET_PORT(&expected, NTP_PORT);
90
91         ASSERT_TRUE(decodenetnum(str, &actual));
92         EXPECT_TRUE(IsEqual(expected, actual));
93 }