decodenetnum.c revision 284990
1284990Scy#include "config.h"
2284990Scy#include "ntp_stdlib.h"
3284990Scy#include "ntp_calendar.h"
4284990Scy#include "unity.h"
5284990Scy
6284990Scy#include "sockaddrtest.h"
7284990Scy
8284990Scy
9284990Scyvoid test_IPv4AddressOnly(void) {
10284990Scy	const char *str = "192.0.2.1";
11284990Scy	sockaddr_u actual;
12284990Scy
13284990Scy	sockaddr_u expected;
14284990Scy	expected.sa4.sin_family = AF_INET;
15284990Scy	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
16284990Scy	SET_PORT(&expected, NTP_PORT);
17284990Scy
18284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
19284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
20284990Scy}
21284990Scy
22284990Scyvoid test_IPv4AddressWithPort(void) {
23284990Scy	const char *str = "192.0.2.2:2000";
24284990Scy	sockaddr_u actual;
25284990Scy
26284990Scy	sockaddr_u expected;
27284990Scy	expected.sa4.sin_family = AF_INET;
28284990Scy	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
29284990Scy	SET_PORT(&expected, 2000);
30284990Scy
31284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
32284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
33284990Scy}
34284990Scy
35284990Scyvoid test_IPv6AddressOnly(void) {
36284990Scy	const struct in6_addr address = {
37284990Scy		0x20, 0x01, 0x0d, 0xb8,
38284990Scy        0x85, 0xa3, 0x08, 0xd3,
39284990Scy        0x13, 0x19, 0x8a, 0x2e,
40284990Scy        0x03, 0x70, 0x73, 0x34
41284990Scy	};
42284990Scy
43284990Scy	const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
44284990Scy	sockaddr_u actual;
45284990Scy
46284990Scy	sockaddr_u expected;
47284990Scy	expected.sa6.sin6_family = AF_INET6;
48284990Scy	expected.sa6.sin6_addr = address;
49284990Scy	SET_PORT(&expected, NTP_PORT);
50284990Scy
51284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
52284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
53284990Scy}
54284990Scy
55284990Scyvoid test_IPv6AddressWithPort(void) {
56284990Scy	const struct in6_addr address = {
57284990Scy		0x20, 0x01, 0x0d, 0xb8,
58284990Scy        0x85, 0xa3, 0x08, 0xd3,
59284990Scy        0x13, 0x19, 0x8a, 0x2e,
60284990Scy        0x03, 0x70, 0x73, 0x34
61284990Scy	};
62284990Scy
63284990Scy	const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
64284990Scy	sockaddr_u actual;
65284990Scy
66284990Scy	sockaddr_u expected;
67284990Scy	expected.sa6.sin6_family = AF_INET6;
68284990Scy	expected.sa6.sin6_addr = address;
69284990Scy	SET_PORT(&expected, 3000);
70284990Scy
71284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
72284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
73284990Scy}
74284990Scy
75284990Scyvoid test_IllegalAddress(void) {
76284990Scy	const char *str = "192.0.2.270:2000";
77284990Scy	sockaddr_u actual;
78284990Scy
79284990Scy	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
80284990Scy}
81284990Scy
82284990Scyvoid test_IllegalCharInPort(void) {
83284990Scy	/* An illegal port does not make the decodenetnum fail, but instead
84284990Scy	 * makes it use the standard port.
85284990Scy	 */
86284990Scy	const char *str = "192.0.2.1:a700";
87284990Scy	sockaddr_u actual;
88284990Scy
89284990Scy	sockaddr_u expected;
90284990Scy	expected.sa4.sin_family = AF_INET;
91284990Scy	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
92284990Scy	SET_PORT(&expected, NTP_PORT);
93284990Scy
94284990Scy	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
95284990Scy	TEST_ASSERT_TRUE(IsEqual(expected, actual));
96284990Scy}
97