]>
Commit | Line | Data |
---|---|---|
9b0a0ba9 OZ |
1 | /* |
2 | * BIRD Library -- IP address functions Tests | |
3 | * | |
4 | * (c) 2015 CZ.NIC z.s.p.o. | |
5 | * | |
6 | * Can be freely distributed and used under the terms of the GNU GPL. | |
7 | */ | |
8 | ||
9 | #include "test/birdtest.h" | |
10 | ||
11 | #include "lib/ip.h" | |
12 | ||
13 | #define IP4_MAX_LEN 16 | |
14 | ||
15 | static int | |
16 | test_ipa_pton(void *out_, const void *in_, const void *expected_out_) | |
17 | { | |
18 | ip_addr *out = out_; | |
19 | const char *in = in_; | |
20 | const ip_addr *expected_out = expected_out_; | |
21 | ||
22 | if (ipa_is_ip4(*expected_out)) | |
23 | { | |
24 | ip4_addr ip4; | |
25 | bt_assert(ip4_pton(in, &ip4)); | |
26 | *out = ipa_from_ip4(ip4); | |
27 | } | |
28 | else | |
29 | { | |
30 | bt_assert(ip6_pton(in, out)); | |
31 | /* ip_addr == ip6_addr */ | |
32 | } | |
33 | ||
5e3cd0e5 | 34 | return ipa_equal(*out, *expected_out); |
9b0a0ba9 OZ |
35 | } |
36 | ||
37 | static int | |
38 | t_ip4_pton(void) | |
39 | { | |
40 | struct bt_pair test_vectors[] = { | |
41 | { | |
42 | .in = "192.168.1.128", | |
43 | .out = & ipa_build4(192, 168, 1, 128), | |
44 | }, | |
45 | { | |
46 | .in = "255.255.255.255", | |
47 | .out = & ipa_build4(255, 255, 255, 255), | |
48 | }, | |
49 | { | |
50 | .in = "0.0.0.0", | |
51 | .out = & ipa_build4(0, 0, 0, 0), | |
52 | }, | |
53 | }; | |
54 | ||
55 | return bt_assert_batch(test_vectors, test_ipa_pton, bt_fmt_str, bt_fmt_ipa); | |
56 | } | |
57 | ||
58 | static int | |
59 | t_ip6_pton(void) | |
60 | { | |
61 | struct bt_pair test_vectors[] = { | |
62 | { | |
63 | .in = "2001:0db8:0000:0000:0000:0000:1428:57ab", | |
64 | .out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB), | |
65 | }, | |
66 | { | |
67 | .in = "2001:0db8:0000:0000:0000::1428:57ab", | |
68 | .out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB), | |
69 | }, | |
70 | { | |
71 | .in = "2001:0db8::1428:57ab", | |
72 | .out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB), | |
73 | }, | |
74 | { | |
75 | .in = "2001:db8::1428:57ab", | |
76 | .out = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB), | |
77 | }, | |
78 | { | |
79 | .in = "::1", | |
80 | .out = & ipa_build6(0x00000000, 0x00000000, 0x00000000, 0x00000001), | |
81 | }, | |
82 | { | |
83 | .in = "::", | |
84 | .out = & ipa_build6(0x00000000, 0x00000000, 0x00000000, 0x00000000), | |
85 | }, | |
86 | { | |
87 | .in = "2605:2700:0:3::4713:93e3", | |
88 | .out = & ipa_build6(0x26052700, 0x00000003, 0x00000000, 0x471393E3), | |
89 | }, | |
90 | }; | |
91 | ||
92 | return bt_assert_batch(test_vectors, test_ipa_pton, bt_fmt_str, bt_fmt_ipa); | |
93 | } | |
94 | ||
95 | static int | |
96 | test_ipa_ntop(void *out_, const void *in_, const void *expected_out_) | |
97 | { | |
98 | char *out = out_; | |
99 | const ip_addr *in = in_; | |
100 | const char *expected_out = expected_out_; | |
101 | ||
102 | if (ipa_is_ip4(*in)) | |
103 | ip4_ntop(ipa_to_ip4(*in), out); | |
104 | else | |
105 | ip6_ntop(ipa_to_ip6(*in), out); | |
106 | ||
107 | int result = strncmp(out, expected_out, ipa_is_ip4(*in) ? IP4_MAX_TEXT_LENGTH : IP6_MAX_TEXT_LENGTH) == 0; | |
5e3cd0e5 | 108 | return result; |
9b0a0ba9 OZ |
109 | } |
110 | ||
111 | static int | |
112 | t_ip4_ntop(void) | |
113 | { | |
114 | struct bt_pair test_vectors[] = { | |
115 | { | |
116 | .in = & ipa_build4(192, 168, 1, 128), | |
117 | .out = "192.168.1.128", | |
118 | }, | |
119 | { | |
120 | .in = & ipa_build4(255, 255, 255, 255), | |
121 | .out = "255.255.255.255", | |
122 | }, | |
123 | { | |
124 | .in = & ipa_build4(0, 0, 0, 1), | |
125 | .out = "0.0.0.1", | |
126 | }, | |
127 | }; | |
128 | ||
129 | return bt_assert_batch(test_vectors, test_ipa_ntop, bt_fmt_ipa, bt_fmt_str); | |
130 | } | |
131 | ||
132 | static int | |
133 | t_ip6_ntop(void) | |
134 | { | |
135 | struct bt_pair test_vectors[] = { | |
136 | { | |
137 | .in = & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB), | |
138 | .out = "2001:db8::1428:57ab", | |
139 | }, | |
140 | { | |
141 | .in = & ipa_build6(0x26052700, 0x00000003, 0x00000000, 0x471393E3), | |
142 | .out = "2605:2700:0:3::4713:93e3", | |
143 | }, | |
144 | }; | |
145 | ||
146 | return bt_assert_batch(test_vectors, test_ipa_ntop, bt_fmt_ipa, bt_fmt_str); | |
147 | } | |
148 | ||
149 | int | |
150 | main(int argc, char *argv[]) | |
151 | { | |
152 | bt_init(argc, argv); | |
153 | ||
154 | bt_test_suite(t_ip4_pton, "Converting IPv4 string to ip4_addr struct"); | |
155 | bt_test_suite(t_ip6_pton, "Converting IPv6 string to ip6_addr struct"); | |
156 | bt_test_suite(t_ip4_ntop, "Converting ip4_addr struct to IPv4 string"); | |
157 | bt_test_suite(t_ip6_ntop, "Converting ip6_addr struct to IPv6 string"); | |
158 | ||
159 | return bt_exit_value(); | |
160 | } | |
161 |