OSDN Git Service

a2f3f0ec706335b956f689dc2132c449f4901387
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7directord / t / 06_convert.t
1 use strict;
2 use warnings;
3 no warnings qw(redefine once);
4 use lib qw(t/lib lib);
5 use subs qw(print);
6 use Cwd;
7 use L7lib;
8 use Test::More tests => 83;
9 use Config;
10
11 L7lib::chdir();
12 L7lib::comment_out();
13 require './l7directord';
14
15 override();
16
17 #...............................................
18 # test start
19 #   - _ld_service_resolve
20 {
21     my %v = ();
22     my $port = 80;
23     _ld_service_resolve(\%v, $port);
24     is $v{service}, 'http', '_ld_service_resolve - http ok';
25 }
26 {
27     my %v = ();
28     my $port = 443;
29     _ld_service_resolve(\%v, $port);
30     is $v{service}, 'https', '_ld_service_resolve - https ok';
31 }
32 {
33     my %v = ();
34     my $port = 21;
35     _ld_service_resolve(\%v, $port);
36     is $v{service}, 'ftp', '_ld_service_resolve - ftp ok';
37 }
38 {
39     my %v = ();
40     my $port = 25;
41     _ld_service_resolve(\%v, $port);
42     is $v{service}, 'smtp', '_ld_service_resolve - smtp ok';
43 }
44 {
45     my %v = ();
46     my $port = 110;
47     _ld_service_resolve(\%v, $port);
48     is $v{service}, 'pop', '_ld_service_resolve - pop ok';
49 }
50 {
51     my %v = ();
52     my $port = 119;
53     _ld_service_resolve(\%v, $port);
54     is $v{service}, 'nntp', '_ld_service_resolve - nntp ok';
55 }
56 {
57     my %v = ();
58     my $port = 143;
59     _ld_service_resolve(\%v, $port);
60     is $v{service}, 'imap', '_ld_service_resolve - imap ok';
61 }
62 {
63     my %v = ();
64     my $port = 389;
65     _ld_service_resolve(\%v, $port);
66     is $v{service}, 'ldap', '_ld_service_resolve - ldap ok';
67 }
68 {
69     my %v = ();
70     my $port = 53;
71     _ld_service_resolve(\%v, $port);
72     is $v{service}, 'dns', '_ld_service_resolve - dns ok';
73 }
74 {
75     my %v = ();
76     my $port = 3306;
77     _ld_service_resolve(\%v, $port);
78     is $v{service}, 'mysql', '_ld_service_resolve - mysql ok';
79 }
80 {
81     my %v = ();
82     my $port = 5432;
83     _ld_service_resolve(\%v, $port);
84     is $v{service}, 'pgsql', '_ld_service_resolve - pgsql ok';
85 }
86 {
87     my %v = ();
88     my $port = 5060;
89     _ld_service_resolve(\%v, $port);
90     is $v{service}, 'sip', '_ld_service_resolve - sip ok';
91 }
92 {
93     my %v = ();
94     my $port = 100000;
95     _ld_service_resolve(\%v, $port);
96     is $v{service}, 'none', '_ld_service_resolve - none ok';
97 }
98 {
99     my $port = 100000;
100     _ld_service_resolve(undef, $port);
101 }
102 {
103     my %v = ();
104     _ld_service_resolve(\%v, undef);
105     is $v{service}, undef, '_ld_service_resolve - port is undef';
106 }
107 {
108     my %v = ( service => 'pop' );
109     my $port = 80;
110     _ld_service_resolve(\%v, $port);
111     is $v{service}, 'pop', '_ld_service_resolve - service is already exist';
112 }
113 #   - is_octet
114 {
115     my $got = is_octet(0);
116     is $got, 1, 'is_octet - 0 is ok';
117 }
118 {
119     my $got = is_octet(-1);
120     is $got, 0, 'is_octet - -1 is error';
121 }
122 {
123     my $got = is_octet(255);
124     is $got, 1, 'is_octet - 255 is ok';
125 }
126 {
127     my $got = is_octet(256);
128     is $got, 0, 'is_octet - 256 is error';
129 }
130 {
131     my $got = is_octet();
132     is $got, 0, 'is_octet - undef is error';
133 }
134 {
135     my $got = is_octet('one');
136     is $got, 0, 'is_octet - one(string) is error';
137 }
138 #   - is_ip
139 {
140     my $got = is_ip();
141     is $got, 0, 'is_ip - undef is error';
142 }
143 {
144     my $got = is_ip('0.0.0.0');
145     is $got, 1, 'is_ip - 0.0.0.0 is ok';
146 }
147 {
148     my $got = is_ip('-1.-1.-1.-1');
149     is $got, 0, 'is_ip - -1.-1.-1.-1 is error';
150 }
151 {
152     my $got = is_ip('255.255.255.255');
153     is $got, 1, 'is_ip - 255.255.255.255 is ok';
154 }
155 {
156     my $got = is_ip('256.256.256.256');
157     is $got, 0, 'is_ip - 256.256.256.256 is error';
158 }
159 {
160     my $got = is_ip('localhost');
161     is $got, 0, 'is_ip - localhost(invalid format) is error';
162 }
163 ###############################################################################################
164 #   - is_ip6  IPv6 function
165 {
166 ### Pattern ip:none
167     my @got = is_ip6();
168     my ($ret, @address ) = @got;
169     is $ret, 0, 'is_ip6 - undef is error';
170 }
171 {
172 ### Pattern ip:
173     my @got = is_ip6('[::0]');
174     my ($ret, @address ) = @got;
175     is $ret, 1, 'is_ip6 - [::0] is ok';
176 }
177 {
178 ### Pattern ip:[::1]
179     my @got = is_ip6('[::1]');
180     my ($ret, @address ) = @got;
181     is $ret, 1, 'is_ip6 - [::1] is ok';
182 }
183 {
184 ### Pattern ip:[2001::1]
185     my @got = is_ip6('[2001::1]');
186     my ($ret, @address ) = @got;
187     is $ret, 1, 'is_ip6 - [2001::1] is ok';
188 }
189 {
190 ### Pattern ip:[fe80::1%eth1]
191     my @got = is_ip6('[fe80::1%eth1]');
192     my ($ret, @address ) = @got;
193     is $ret, 1, 'is_ip6 - [fe80::1%eth1] is ok';
194     my $alleged_ip  = join ":", @address;
195     is $alleged_ip, 'fe80:0:0:0:0:0:0:1%eth1', 'is_ip6 - [fe80::1%eth1] is ok';
196 }
197 {
198 ### Pattern ip:[1234:5678:90ab:cdef:1234:5678:90ab:cdef]
199     my @got = is_ip6('[1234:5678:90ab:cdef:1234:5678:90ab:cdef]');
200     my ($ret, @address ) = @got;
201     is $ret, 1, 'is_ip6 - [1234:5678:90ab:cdef:1234:5678:90ab:cdef] is ok';
202 }
203 {
204 ### Pattern ip:[::90ab:cdef:1234:5678:90ab:cdef]
205     my @got = is_ip6('[::90ab:cdef:1234:5678:90ab:cdef]');
206     my ($ret, @address ) = @got;
207     is $ret, 1, 'is_ip6 - [::90ab:cdef:1234:5678:90ab:cdef] is ok';
208 }
209 {
210 ### Pattern ip:[1234:5678:90ab:cdef:1234:5678::]
211     my @got = is_ip6('[1234:5678:90ab:cdef:1234:5678::]');
212     my ($ret, @address ) = @got;
213     is $ret, 1, 'is_ip6 - [1234:5678:90ab:cdef:1234:5678::] is ok';
214 }
215 {
216 ### Pattern ip:[1234:5678:90ab:cdef:1234:5678::2]
217     my @got = is_ip6('[1234:5678:90ab:cdef:1234:5678::2]');
218     my ($ret, @address ) = @got;
219     is $ret, 1, 'is_ip6 - [1234:5678:90ab:cdef:1234:5678::2] is ok';
220 }
221 {
222 ### Pattern ip:[1234:5678:9012:3456:7890:1234:5678:9012:3456]
223     my @got = is_ip6('[1234:5678:9012:3456:7890:1234:5678:9012:3456]');
224     my ($ret, @address ) = @got;
225     is $ret, 0, 'is_ip6 - [1234:5678:9012:3456:7890:1234:5678:9012:3456] is error';
226 }
227 {
228 ### Pattern ip:[fffz::ffff]
229     my @got = is_ip6('[fffz::ffff]');
230     my ($ret, @address ) = @got;
231     is $ret, 0, 'is_ip6 - [fffz::ffff] is error';
232 }
233 {
234 ### Pattern ip:localhost
235     my @got = is_ip6('localhost');
236     my ($ret, @address ) = @got;
237     is $ret, 0, 'is_ip6 - localhost(invalid format) is error';
238 }
239 ###############################################################################################
240 #   - ip_to_int
241 {
242     my @got = ip_to_int();
243     my ($ipver, $addr ) = @got;
244     is $addr, -1, 'ip_to_int - undef is error';
245 }
246 {
247     my @got = ip_to_int('0.0.0.0');
248     my ($ipver, $addr ) = @got;
249     is $addr, 0, 'ip_to_int - 0.0.0.0 is ok';
250 }
251 {
252     my @got = ip_to_int('-1.-1.-1.-1');
253     my ($ipver, $addr ) = @got;
254     is $addr, -1, 'ip_to_int - -1.-1.-1.-1 is error';
255 }
256 {
257     my @got = ip_to_int('255.255.255.255');
258     my ($ipver, $addr ) = @got;
259     is $addr, 4294967295, 'ip_to_int - 255.255.255.255 is ok';
260 }
261 {
262     my @got = ip_to_int('127.0.0.1');
263     my ($ipver, $addr ) = @got;
264     is $addr, 2130706433, 'ip_to_int - 127.0.0.1 is ok';
265 }
266 {
267     my @got = ip_to_int('256.256.256.256');
268     my ($ipver, $addr ) = @got;
269     is $addr, -1, 'ip_to_int - 256.256.256.256 is error';
270 }
271 {
272     my @got = ip_to_int('localhost');
273     my ($ipver, $addr ) = @got;
274     is $addr, -1, 'ip_to_int - localhost(invalid format) is error';
275 }
276 ## IPv6 Function
277 {
278     my @got = ip_to_int('::');
279     my ($ipver, $addr ) = @got;
280     is $addr, 0, 'ip_to_int - [::] is ok';
281 }
282 {
283     my @got = ip_to_int('[::1]');
284     my ($ipver, $addr ) = @got;
285     is $addr, 1, 'ip_to_int - [::1] is ok';
286 }
287 {
288     my @got = ip_to_int('[2001::1]');
289     my ($ipver, $addr, $addr2 ) = @got;
290     is $ipver, 'ipv6', 'ip_to_int - ipv6 is ok';
291     is $addr, 1, 'ip_to_int - [2001::1] -> interface is ok';
292     is $addr2, 2306124484190404608, 'ip_to_int - [2001::1] prefix is ok';
293 }
294 {
295     my @got = ip_to_int('[1:2:3:4:5:6:7:8]');
296     my ($ipver, $addr, $addr2 ) = @got;
297     is $ipver, 'ipv6', 'ip_to_int - ipv6 is ok';
298     is $addr, 1407400653815816, 'ip_to_int - [1:2:3:4:5:6:7:8] -> interface is ok';
299     is $addr2, 281483566841860, 'ip_to_int - [1:2:3:4:5:6:7:8] is ok';
300 }
301 {
302     my @got = ip_to_int('[1234:5678:90ab:cdef:1234:5678:90ab:cdef]');
303     my ($ipver, $addr, $addr2 ) = @got;
304     is $ipver, 'ipv6', 'ip_to_int - ipv6 is ok';
305     is $addr, 1311768467294899695, 'ip_to_int - [1234:5678:90ab:cdef:1234:5678:90ab:cdef] -> interface is ok';
306     is $addr2, 1311768467294899695, 'ip_to_int - [1234:5678:90ab:cdef:1234:5678:90ab:cdef] is ok';
307 }
308 {
309     my @got = ip_to_int('[::90ab:cdef:1234:5678:90ab:cdef]');
310     my ($ipver, $addr, $addr2 ) = @got;
311     is $ipver, 'ipv6', 'ip_to_int - ipv6 is ok';
312     is $addr, 1311768467294899695, 'ip_to_int - [::90ab:cdef:1234:5678:90ab:cdef] -> interface is ok';
313     is $addr2, 2427178479, 'ip_to_int - [::90ab:cdef:1234:5678:90ab:cdef] is ok';
314 }
315 {
316     my @got = ip_to_int('[1234:5678:90ab:cdef:1234:5678::]');
317     my ($ipver, $addr, $addr2 ) = @got;
318     is $ipver, 'ipv6', 'ip_to_int - ipv6 is ok';
319     is $addr, 1311768464867721216, 'ip_to_int - [1234:5678:90ab:cdef:1234:5678::] -> interface is ok';
320     is $addr2, 1311768467294899695, 'ip_to_int - [1234:5678:90ab:cdef:1234:5678::] is ok';
321 }
322 {
323     my @got = ip_to_int('[1234:5678:90ab:cdef:1234:5678::2]');
324     my ($ipver, $addr, $addr2 ) = @got;
325     is $ipver, 'ipv6', 'ip_to_int - ipv6 is ok';
326     is $addr, 1311768464867721218, 'ip_to_int - [1234:5678:90ab:cdef:1234:5678::2] -> interface is ok';
327     is $addr2, 1311768467294899695, 'ip_to_int - [1234:5678:90ab:cdef:1234:5678::2] is ok';
328 }
329 {
330     my @got = ip_to_int('[1234:5678:9012:3456:7890:1234:5678:9012:3456]');
331     my ($ipver, $addr ) = @got;
332     is $addr, -1, 'ip_to_int - [1234:5678:9012:3456:7890:1234:5678:9012:3456] is error';
333 }
334 {
335     my @got = ip_to_int('fffz::ffff');
336     my ($ipver, $addr ) = @got;
337     is $addr, -1, 'ip_to_int - fffz::ffff is error';
338 }
339 ###############################################################################################
340 #   - int_to_ip
341 #   - int_to_ip
342 {
343     my $got = int_to_ip('ipv4', 0 );
344     is $got, '0.0.0.0', 'int_to_ip - 0 is ok';
345 }
346 {
347     my $got = int_to_ip('ipv4', -1);
348     is $got, undef, 'int_to_ip - -1 is error';
349 }
350 {
351     my $got = int_to_ip('ipv4', 2130706433);
352     is $got, '127.0.0.1', 'int_to_ip - 2130706433 is ok';
353 }
354 {
355     my $got = int_to_ip('ipv4', 4294967295);
356     is $got, '255.255.255.255', 'int_to_ip - 4294967295 is ok';
357 }
358 SKIP: {
359     my $got = int_to_ip('ipv4', 4294967296);
360     if ( $Config{'use64bitint'} ) {
361         is $got, '0.0.0.0', 'int_to_ip - 4294967296 is ok (overflow)';
362     }
363     else {
364         is $got, '255.255.255.255', 'int_to_ip - 4294967296 is ok (overflow)';
365     }
366 }
367 {
368     my $got = int_to_ip();
369     is $got, undef, 'int_to_ip - undef is error';
370 }
371 {
372     my $got = int_to_ip('ipv4','one');
373     is $got, undef, 'int_to_ip - one(string) is error';
374 }
375 ###
376 ### IPv6 Tests
377 ###
378 {
379     my $got = int_to_ip('ipv6', 0, 0);
380     is $got, '0:0:0:0:0:0:0:0', 'int_to_ip - 0:0:0:0:0:0:0:0 is ok';
381 }
382 {
383     my $got = int_to_ip('ipv6', 1, 0);
384     is $got, '0:0:0:0:0:0:0:1', 'int_to_ip - 0:0:0:0:0:0:0:1 is ok';
385 }
386 {
387     my $got = int_to_ip('ipv6', 1407400653815816,281483566841860);
388     is $got, '1:2:3:4:5:6:7:8', 'int_to_ip - [1:2:3:4:5:6:7:8] is ok';
389 }
390 {
391     my $got = int_to_ip('ipv6', 1311768467294899695,1311768467294899695);
392     is $got, '1234:5678:90ab:cdef:1234:5678:90ab:cdef', 'int_to_ip - [1234:5678:90ab:cdef:1234:5678:90ab:cdef] is ok';
393 }
394 ## ::90ab:cdef:1234:5678:90ab:cdef
395 {
396     my $got = int_to_ip('ipv6', 1311768467294899695, 2427178479);
397     is $got, '0:0:90ab:cdef:1234:5678:90ab:cdef', 'int_to_ip - [0:0:90ab:cdef:1234:5678:90ab:cdef] is ok';
398 }
399 ## 1234:5678:90ab:cdef:1234:5678::
400 {
401     my $got = int_to_ip('ipv6', 1311768464867721216,1311768467294899695);
402     is $got, '1234:5678:90ab:cdef:1234:5678:0:0', 'int_to_ip - [1234:5678:90ab:cdef:1234:5678:0:0] is ok';
403 }
404 ## 1234:5678:90ab:cdef:1234:5678::2
405 {
406     my $got = int_to_ip('ipv6', 1311768464867721218, 1311768467294899695);
407     is $got, '1234:5678:90ab:cdef:1234:5678:0:2', 'int_to_ip - [1234:5678:90ab:cdef:1234:5678:0:2] is ok';
408 }
409
410
411
412 #####################################################################
413 # test end
414 #...............................................
415
416 L7lib::comment_in();
417
418 sub override {
419     *ld_log = \&__ld_log;
420 }
421 sub __ld_log {
422 }