恥ずかしい勘違いから生まれた、DHCP6の不要かつ部分的な実装
Revision | d2baf16d7ed8214f6d1a8aee87069cf5d7fa3999 (tree) |
---|---|
Time | 2021-08-17 06:14:31 |
Author | dyknon <dyknon@user...> |
Commiter | dyknon |
Add ipv6 address test
@@ -0,0 +1,8 @@ | ||
1 | +export PERL5LIB=. | |
2 | +TESTS=$(shell find t -type f -name "*.t") | |
3 | +TEST_TARGETS=$(foreach test,$(TESTS),maketest.$(test)) | |
4 | + | |
5 | +test: $(TEST_TARGETS) | |
6 | + | |
7 | +$(TEST_TARGETS): | |
8 | + perl $(subst maketest.,,$@) |
@@ -0,0 +1,172 @@ | ||
1 | +#!/usr/bin/perl | |
2 | + | |
3 | +use strict; | |
4 | +use warnings; | |
5 | +use Test::More; | |
6 | +use Scalar::Util qw/refaddr/; | |
7 | +use Math::BigInt; | |
8 | + | |
9 | +use Net::DHCP6::Value::Ipv6Addr; | |
10 | + | |
11 | +sub isaip { | |
12 | + my $ip = shift; | |
13 | + eval{$ip->isa("Net::DHCP6::Value::Ipv6Addr")}; | |
14 | +} | |
15 | +sub ip { | |
16 | + my $from = shift; | |
17 | + my $ip = Net::DHCP6::Value::Ipv6Addr->new($from); | |
18 | + ok(isaip($ip), "construct from \"$from\""); | |
19 | + $ip; | |
20 | +} | |
21 | +sub ipbytes { | |
22 | + my $from = shift; | |
23 | + my $ip = Net::DHCP6::Value::Ipv6Addr->from_bytes($from); | |
24 | + ok(isaip($ip), "construct from bytes"); | |
25 | + $ip; | |
26 | +} | |
27 | + | |
28 | +plan tests => 7; | |
29 | + | |
30 | +my @ok = ( | |
31 | + "unspecified address" | |
32 | + => ["::", "::0", "0::", "0::0", "00::000:0000", "0:"x7 . "0", "\0"x16], | |
33 | + "loopback address" | |
34 | + => ["::1", "0::01", "::0:0001", "0000:"x7 . "0001", "\0"x15 . "\x1"], | |
35 | + "maxvalue address" | |
36 | + => ["ffff:"x7 . "ffff", "\xff"x16], | |
37 | + "5555 address" | |
38 | + => ["5555:"x7 . "5555", "\x55"x16], | |
39 | + "aaaa address" | |
40 | + => ["aaaa:"x7 . "aaaa", "\xaa"x16], | |
41 | + "0123 address" | |
42 | + => ["123:4567:89ab:cdef:123:4567:89ab:cdef", | |
43 | + "0123:4567:89ab:cdef:0123:4567:89AB:CDEF", | |
44 | + "\x01\x23\x45\x67\x89\xab\xcd\xef"x2], | |
45 | + "1::1:0:0:0" | |
46 | + => ["1::1:0:0:0", "1:0:0:0:1:0:0:0", "\0\1\0\0\0\0\0\0"x2], | |
47 | + "1:1:0:0:1::" | |
48 | + => ["1:1:0:0:1::", "\0\1\0\1\0\0\0\0\0\1\0\0\0\0\0\0"], | |
49 | + "v4embedded address: plain" | |
50 | + => ["::a00:1", "0::10.0.0.1", "0:0:0:0:0:0:10.0.0.1", "::10.0.0.1", | |
51 | + "\0"x12 . "\x0a\0\0\1"], | |
52 | + "v4embedded address: ::ffff:0:0/96" | |
53 | + => ["::ffff:ac10:ff", "::ffff:172.16.0.255", | |
54 | + "\0"x10 . "\xff\xff\xac\x10\0\xff"], | |
55 | + "v4embedded address: 64:ff9b::/96" | |
56 | + => ["64:ff9b::7f2a:0", "0064:ff9b::127.42.0.0", | |
57 | + "\0\x64\xff\x9b\0\0\0\0\0\0\0\0\x7f\x2a\0\0"], | |
58 | +); | |
59 | + | |
60 | +my @inval_str = ( | |
61 | + " ::", ":: ", "0", ":", "0:0:0:0:0:0:0", "0:0:0:0:0:0:0:", ":0:0:0:0:0:0:0", | |
62 | + "::00000", "::00001", "::10000", "::fffff", "0::0::0", "::0::", "0:::0", | |
63 | + ":::", "0: 0:0", "" | |
64 | +); | |
65 | + | |
66 | +subtest "translation between string and binary" => sub { | |
67 | + plan tests => @ok / 2; | |
68 | + for(0 .. int($#ok/2)){ | |
69 | + my $name = $ok[$_*2]; | |
70 | + my @val = @{$ok[$_*2+1]}; | |
71 | + subtest $name => sub { | |
72 | + plan tests => @val * 3; | |
73 | + my @ip = ((map{ip $_}@val[0 .. $#val-1]), | |
74 | + ipbytes $val[$#val]); | |
75 | + for(0 .. $#ip){ | |
76 | + my $from = $_ == $#ip ? "binary" : "\"$val[$_]\""; | |
77 | + is $ip[$_]->raw, $val[$#ip], "from $from to bytes"; | |
78 | + } | |
79 | + for(0 .. $#ip){ | |
80 | + my $from = $_ == $#ip ? "binary" : "\"$val[$_]\""; | |
81 | + is $ip[$_]->str, $val[0], "from $from to string"; | |
82 | + } | |
83 | + }; | |
84 | + } | |
85 | +}; | |
86 | + | |
87 | +subtest "construct from array" => sub { | |
88 | + plan tests => @ok / 2; | |
89 | + for(0 .. int($#ok/2)){ | |
90 | + my $name = $ok[$_*2]; | |
91 | + my @val = @{$ok[$_*2+1]}; | |
92 | + my $str = $val[0]; | |
93 | + my $byte = $val[$#val]; | |
94 | + subtest $name => sub { | |
95 | + plan tests => 6 * 2; | |
96 | + my $ip; | |
97 | + $ip = Net::DHCP6::Value::Ipv6Addr->new(unpack "C*", $byte); | |
98 | + ok isaip($ip), "given 16 element array to new"; | |
99 | + is "$ip", $str, " correct value"; | |
100 | + $ip = Net::DHCP6::Value::Ipv6Addr->new(unpack "S>*", $byte); | |
101 | + ok isaip($ip), "given 8 element array to new"; | |
102 | + is "$ip", $str, " correct value"; | |
103 | + $ip = Net::DHCP6::Value::Ipv6Addr->from_array(unpack "C*", $byte); | |
104 | + ok isaip($ip), "given 16 element array to from_array"; | |
105 | + is "$ip", $str, " correct value"; | |
106 | + $ip = Net::DHCP6::Value::Ipv6Addr->from_array(unpack "S>*", $byte); | |
107 | + ok isaip($ip), "given 8 element array to from_array"; | |
108 | + is "$ip", $str, " correct value"; | |
109 | + $ip = Net::DHCP6::Value::Ipv6Addr->new([unpack "C*", $byte]); | |
110 | + ok isaip($ip), "given ref to 16 element array to new"; | |
111 | + is "$ip", $str, " correct value"; | |
112 | + $ip = Net::DHCP6::Value::Ipv6Addr->new([unpack "S>*", $byte]); | |
113 | + ok isaip($ip), "given ref to 8 element array to new"; | |
114 | + is "$ip", $str, " correct value"; | |
115 | + }; | |
116 | + } | |
117 | +}; | |
118 | + | |
119 | +subtest "copy construct" => sub { | |
120 | + plan tests => @ok / 2; | |
121 | + for(0 .. int($#ok/2)){ | |
122 | + my $name = $ok[$_*2]; | |
123 | + my @val = @{$ok[$_*2+1]}; | |
124 | + my $str = $val[0]; | |
125 | + my $byte = $val[$#val]; | |
126 | + subtest $name => sub { | |
127 | + plan tests => 3; | |
128 | + my $ip = Net::DHCP6::Value::Ipv6Addr->new(unpack "C*", $byte); | |
129 | + my $ip2 = $ip; | |
130 | + my $copy = Net::DHCP6::Value::Ipv6Addr->new($ip); | |
131 | + ok isaip($copy), "copy constructed"; | |
132 | + ok refaddr($ip) != refaddr($copy), " not same reference"; | |
133 | + is "$ip", $str, " correct value"; | |
134 | + }; | |
135 | + } | |
136 | +}; | |
137 | + | |
138 | +subtest "construct from BigInt" => sub { | |
139 | + plan tests => @ok / 2; | |
140 | + for(0 .. int($#ok/2)){ | |
141 | + my $name = $ok[$_*2]; | |
142 | + my @val = @{$ok[$_*2+1]}; | |
143 | + my $str = $val[0]; | |
144 | + my $byte = $val[$#val]; | |
145 | + subtest $name => sub { | |
146 | + plan tests => 4; | |
147 | + my $hexrep = "0x".($byte =~ s/(.)/sprintf("%02x", ord($1))/ergs); | |
148 | + my $bi = Math::BigInt->new($hexrep); | |
149 | + my $ip; | |
150 | + $ip = Net::DHCP6::Value::Ipv6Addr->new($bi); | |
151 | + ok isaip($ip), "constructed by new"; | |
152 | + is "$ip", $str, " correct value"; | |
153 | + $ip = Net::DHCP6::Value::Ipv6Addr->from_bigint($bi); | |
154 | + ok isaip($ip), "constructed by from_bigint"; | |
155 | + is "$ip", $str, " correct value"; | |
156 | + }; | |
157 | + } | |
158 | +}; | |
159 | + | |
160 | +subtest "Invalid ipv6 address" => sub { | |
161 | + plan tests => scalar @inval_str; | |
162 | + for(@inval_str){ | |
163 | + ok !eval{Net::DHCP6::Value::Ipv6Addr->new($_); 1;}, $_; | |
164 | + } | |
165 | +}; | |
166 | + | |
167 | + | |
168 | +{ | |
169 | + my $ip = Net::DHCP6::Value::Ipv6Addr->new; | |
170 | + ok isaip($ip), "constructed by empty new"; | |
171 | + is "$ip", "::", " is a unspecified address"; | |
172 | +} |