]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/ip_test.c
2 * BIRD Library -- IP address functions Tests
4 * (c) 2015 CZ.NIC z.s.p.o.
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #include "test/birdtest.h"
13 #define IP4_MAX_LEN 16
16 test_ipa_pton(void *out_
, const void *in_
, const void *expected_out_
)
20 const ip_addr
*expected_out
= expected_out_
;
22 if (ipa_is_ip4(*expected_out
))
25 bt_assert(ip4_pton(in
, &ip4
));
26 *out
= ipa_from_ip4(ip4
);
30 bt_assert(ip6_pton(in
, out
));
31 /* ip_addr == ip6_addr */
34 return ipa_equal(*out
, *expected_out
);
40 struct bt_pair test_vectors
[] = {
42 .in
= "192.168.1.128",
43 .out
= & ipa_build4(192, 168, 1, 128),
46 .in
= "255.255.255.255",
47 .out
= & ipa_build4(255, 255, 255, 255),
51 .out
= & ipa_build4(0, 0, 0, 0),
55 return bt_assert_batch(test_vectors
, test_ipa_pton
, bt_fmt_str
, bt_fmt_ipa
);
61 struct bt_pair test_vectors
[] = {
63 .in
= "2001:0db8:0000:0000:0000:0000:1428:57ab",
64 .out
= & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
67 .in
= "2001:0db8:0000:0000:0000::1428:57ab",
68 .out
= & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
71 .in
= "2001:0db8::1428:57ab",
72 .out
= & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
75 .in
= "2001:db8::1428:57ab",
76 .out
= & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
80 .out
= & ipa_build6(0x00000000, 0x00000000, 0x00000000, 0x00000001),
84 .out
= & ipa_build6(0x00000000, 0x00000000, 0x00000000, 0x00000000),
87 .in
= "2605:2700:0:3::4713:93e3",
88 .out
= & ipa_build6(0x26052700, 0x00000003, 0x00000000, 0x471393E3),
92 return bt_assert_batch(test_vectors
, test_ipa_pton
, bt_fmt_str
, bt_fmt_ipa
);
96 test_ipa_ntop(void *out_
, const void *in_
, const void *expected_out_
)
99 const ip_addr
*in
= in_
;
100 const char *expected_out
= expected_out_
;
103 ip4_ntop(ipa_to_ip4(*in
), out
);
105 ip6_ntop(ipa_to_ip6(*in
), out
);
107 int result
= strncmp(out
, expected_out
, ipa_is_ip4(*in
) ? IP4_MAX_TEXT_LENGTH
: IP6_MAX_TEXT_LENGTH
) == 0;
114 struct bt_pair test_vectors
[] = {
116 .in
= & ipa_build4(192, 168, 1, 128),
117 .out
= "192.168.1.128",
120 .in
= & ipa_build4(255, 255, 255, 255),
121 .out
= "255.255.255.255",
124 .in
= & ipa_build4(0, 0, 0, 1),
129 return bt_assert_batch(test_vectors
, test_ipa_ntop
, bt_fmt_ipa
, bt_fmt_str
);
135 struct bt_pair test_vectors
[] = {
137 .in
= & ipa_build6(0x20010DB8, 0x00000000, 0x00000000, 0x142857AB),
138 .out
= "2001:db8::1428:57ab",
141 .in
= & ipa_build6(0x26052700, 0x00000003, 0x00000000, 0x471393E3),
142 .out
= "2605:2700:0:3::4713:93e3",
146 return bt_assert_batch(test_vectors
, test_ipa_ntop
, bt_fmt_ipa
, bt_fmt_str
);
150 main(int argc
, char *argv
[])
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");
159 return bt_exit_value();