OSDN Git Service

2009/12/13 一時更新
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7directord / t / 33_wrapper.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 => 39;
9 use Config;
10 use Socket;
11 use Socket6;
12
13 L7lib::chdir();
14 L7lib::comment_out();
15 require './l7directord';
16 override();
17
18 close *STDOUT;
19 open  *STDOUT, '>', '/dev/null';
20 close *STDERR;
21 open  *STDERR, '>', '/dev/null';
22
23 #...............................................
24 # test start
25 #   - command_wrapper
26 {
27     my @got = command_wrapper();
28     is_deeply \@got, [$?, ''], 'command_wrapper - args is undef';
29 }
30 {
31     my @got = command_wrapper('ls');
32     is $got[0], 0, 'command_wrapper - ls is ok';
33     like $got[1], qr/Makefile.*l7directord.*t/s, 'command_wrapper - ls result';
34 }
35 {
36     my @got = command_wrapper('env LANG=C ls /proc/100000 2>&1');
37     if ( $Config{'use64bitint'} ) {
38         is_deeply \@got, [512, "ls: /proc/100000: No such file or directory\n"], 'command_wrapper - ls /proc/100000(string) is ok';
39     }
40     else {
41         is_deeply \@got, [256, "ls: /proc/100000: No such file or directory\n"], 'command_wrapper - ls /proc/100000(string) is ok';
42     }
43 }
44 {
45     my @got = command_wrapper('/etc');
46     is_deeply \@got, [-1, undef], 'command_wrapper - no such command';
47 }
48 #   - system_wrapper
49 {
50     my $got = system_wrapper();
51     is $got, 65280, 'system_wrapper - args is undef';
52 }
53 {
54     my $got = system_wrapper('ls');
55     is $got, 0, 'system_wrapper - ls is ok';
56 }
57 {
58     my $got = system_wrapper('ls /proc/100000');
59     if ( $Config{'use64bitint'} ) {
60         is $got, 512, 'system_wrapper - ls /proc/100000(string) is ok';
61     }
62     else {
63         is $got, 256, 'system_wrapper - ls /proc/100000(string) is ok';
64     }
65 }
66 {
67     my $got = system_wrapper('ls', '/proc/100000');
68     if ( $Config{'use64bitint'} ) {
69         is $got, 512, 'system_wrapper - ls /proc/100000(array) is ok';
70     }
71     else {
72         is $got, 256, 'system_wrapper - ls /proc/100000(array) is ok';
73     }
74 }
75 {
76     my $got = system_wrapper('/etc');
77     is $got, -1, 'system_wrapper - no such command';
78 }
79 #   - exec_wrapper
80 {
81     my $got = exec_wrapper();
82     is $got, 0, 'exec_wrapper - args is undef';
83 }
84 {
85     my $got = exec_wrapper('/etc');
86     is $got, 0, 'exec_wrapper - no such command';
87 }
88 #   - ld_gethostbyname
89 {
90     my $got = ld_gethostbyname();
91     is $got, undef, 'ld_gethostbyname - name is undef';
92 }
93 {
94     my $got = ld_gethostbyname('localhost');
95     is $got, '127.0.0.1', 'ld_gethostbyname - resolve localhost';
96 }
97 {
98     my $got = ld_gethostbyname('0.0.0.0');
99     is $got, '0.0.0.0', 'ld_gethostbyname - already resolved';
100 }
101 {
102     my $got = ld_gethostbyname('any host name');
103     is $got, undef, 'ld_gethostbyname - cannot resolve';
104 }
105 #   - ld_getservbyname
106 {
107     my $name = undef;
108     my $protocol = 'tcp';
109     my $got = ld_getservbyname($name, $protocol);
110     is $got, undef, 'ld_getservbyname - name is undef';
111 }
112 {
113     my $name = 'www';
114     my $protocol = undef;
115     my $got = ld_getservbyname($name, $protocol);
116     is $got, 80, 'ld_getservbyname - protocol is undef';
117 }
118 {
119     my $name = 0;
120     my $protocol = 'tcp';
121     my $got = ld_getservbyname($name, $protocol);
122     is $got, 0, 'ld_getservbyname - name is 0(min)';
123 }
124 {
125     my $name = 65535;
126     my $protocol = 'tcp';
127     my $got = ld_getservbyname($name, $protocol);
128     is $got, 65535, 'ld_getservbyname - name is 65535(max)';
129 }
130 {
131     my $name = 65536;
132     my $protocol = 'tcp';
133     my $got = ld_getservbyname($name, $protocol);
134     is $got, undef, 'ld_getservbyname - name is 65536(error)';
135 }
136 {
137     my $name = 'foobar';
138     my $protocol = 'tcp';
139     my $got = ld_getservbyname($name, $protocol);
140     is $got, undef, 'ld_getservbyname - no such service name';
141 }
142 {
143     my $name = 'www';
144     my $protocol = 'foo';
145     my $got = ld_getservbyname($name, $protocol);
146     is $got, undef, 'ld_getservbyname - no such protocol name';
147 }
148 {
149     my $name = 'www';
150     my $protocol = 'tcp';
151     my $got = ld_getservbyname($name, $protocol);
152     is $got, 80, 'ld_getservbyname - resolve www tcp';
153 }
154 {
155     my $name = 'www';
156     my $protocol = 'udp';
157     my $got = ld_getservbyname($name, $protocol);
158     is $got, 80, 'ld_getservbyname - resolve www udp';
159 }
160 #   - ld_getservhostbyname
161 {
162     my $hostserv = '127.0.0.1:80';
163     my $protocol = 'tcp';
164     my $got = ld_gethostservbyname($hostserv, $protocol);
165     is_deeply $got, { ip => '127.0.0.1', port => 80 }, 'ld_getservhostbyname - resolve localhost:www tcp';
166 }
167 {
168     my $hostserv = 'localhost:www';
169     my $protocol = 'tcp';
170     my $got = ld_gethostservbyname($hostserv, $protocol);
171     is_deeply $got, { ip => '127.0.0.1', port => 80 }, 'ld_getservhostbyname - resolve localhost:www tcp';
172 }
173 {
174     my $hostserv = 'localhost:www';
175     my $protocol = 'udp';
176     my $got = ld_gethostservbyname($hostserv, $protocol);
177     is_deeply $got, { ip => '127.0.0.1', port => 80 }, 'ld_getservhostbyname - resolve localhost:www udp';
178 }
179 {
180     my $hostserv = 'localhost:www';
181     my $protocol = undef;
182     my $got = ld_gethostservbyname($hostserv, $protocol);
183     is_deeply $got, { ip => '127.0.0.1', port => 80 }, 'ld_getservhostbyname - protocol is undef';
184 }
185 {
186     my $hostserv = 'localhost:www';
187     my $protocol = 'foo';
188     my $got = ld_gethostservbyname($hostserv, $protocol);
189     is $got, undef, 'ld_getservhostbyname - no such protocol';
190 }
191 {
192     my $hostserv = 'foo-bar-host:www';
193     my $protocol = 'tcp';
194     my $got = ld_gethostservbyname($hostserv, $protocol);
195     is $got, undef, 'ld_getservhostbyname - invalid host address';
196 }
197 {
198     my $hostserv = 'localhost:foobar';
199     my $protocol = 'tcp';
200     my $got = ld_gethostservbyname($hostserv, $protocol);
201     is $got, undef, 'ld_getservhostbyname - invalid service name';
202 }
203 {
204     my $hostserv = undef;
205     my $protocol = 'tcp';
206     my $got = ld_gethostservbyname($hostserv, $protocol);
207     is $got, undef, 'ld_getservhostbyname - hostserv is undef';
208 }
209 {
210     my $hostserv = 'localhost';
211     my $protocol = 'tcp';
212     my $got = ld_gethostservbyname($hostserv, $protocol);
213     is $got, undef, 'ld_getservhostbyname - hostserv is invalid format';
214 }
215 ##########################################################
216 ### IPv6 
217 #   - ld_getservhostbyname
218 {
219     my $hostserv = '[2001::11]:80';
220     my $protocol = 'tcp';
221     my $got = ld_gethostservbyname($hostserv, $protocol);
222     is_deeply $got, { ip => '2001::11', port => 80 }, 'ld_getservhostbyname - [2001::11]:80 tcp';
223 }
224 {
225     my $hostserv = '[2001::11]:www';
226     my $protocol = 'udp';
227     my $got = ld_gethostservbyname($hostserv, $protocol);
228     is_deeply $got, { ip => '2001::11', port => 80 }, 'ld_getservhostbyname - resolve localhost:www udp';
229 }
230 {
231     my $hostserv = 'um08eth1ipv6:www';
232     my $protocol = undef;
233     my $got = ld_gethostservbyname($hostserv, $protocol);
234     is_deeply $got, { ip => '2001::11', port => 80 }, 'ld_getservhostbyname - protocol is undef';
235 }
236 {
237     my $hostserv = '10.144.169.222:www';
238     my $protocol = 'foo';
239     my $got = ld_gethostservbyname($hostserv, $protocol);
240     is $got, undef, 'ld_getservhostbyname - no such protocol';
241 }
242 {
243     my $hostserv = 'localhost6:80';
244     my $protocol = 'tcp';
245     my $got = ld_gethostservbyname($hostserv, $protocol);
246     is_deeply $got, { ip => '::1'     , port => 80 }, 'ld_getservhostbyname - invalid host address';
247 }
248 # test end
249 #...............................................
250
251 L7lib::comment_in();
252
253 sub set_default {
254 }
255 sub override {
256     *ld_log = \&__ld_log;
257 }
258 sub __ld_log {
259 }