Debian Codegraph @

t/fileevent.t

raw
1 BEGIN { $^W = 1; $| = 1;}
2 use Test;
3
4 #!/usr/local/bin/perl -w
5 #
6 # Severely hacked test based on IPC example from
7 # Advanced Perl/Tk Programming.
8
9 use 5.005;
10 use Carp;
11 #use IPADM;
12 use IO::Socket;
13 use Tk;
14 use Tk::LabEntry;
15 use Tk::ROText;
16 use strict;
17 use vars qw/$EXIT $MW $PID/;
18 use subs qw/do_command fini init ipsnd list_subnet open_subnet
19 pipe_in pipe_out read_sdb start_ipc_helper/;
20
21 use vars qw/$AUTHORIZED_CLIENTS $DAEMON_HOST $DAEMON_PORT $DEBUG
22 $EOF $MOD_DB_PATH $ORGANIZATION $SDB_PATH $VERSION/;
23
24 BEGIN { plan test => 1}
25
26 if ($^O eq 'MSWin32' || $^O eq 'cygwin') {
27 skip(1, 'Cannot test pipe/fork/exec/fileevent on Win32 systems.');
28 CORE::exit();
29 }
30
31 $AUTHORIZED_CLIENTS = 'localhost|loopback'; # a regex
32 $DAEMON_HOST = 'localhost';
33 $DAEMON_PORT = 1234;
34 $DEBUG = 1;
35 $EOF = '_END_OF_INFORMATION_';
36 $ORGANIZATION = 'Subnet List for ACME Rocket Supply, Inc.';
37
38 init;
39 MainLoop;
40 fini;
41
42 sub do_command {
43
44 # Issue a single IPADM command and wait for a reply. Using
45 # pipes and fileevent() allows X11 events to continue flowing.
46
47 pipe_out @_;
48 return pipe_in;
49
50 } # end do_command
51
52 sub pipe_in {
53
54 # Now that the IPADM command has been issued, keep sysread-ing
55 # until the $EOF string is read, and return all the accumulated
56 # data, excluding $EOF.
57
58 my(@data, $sysbuf, $sysdata, $sysstat, $wait);
59
60 $MW->fileevent(\*PR, 'readable' => sub {
61
62 # print STDERR "IN: about to sysread ...\n" if $DEBUG;
63 if ( ($Tk::VERSION cmp '800.015') <= 0 ) {
64 # if ( defined($ARGV[0]) ) {
65 $sysbuf = <PR>;
66 # print STDERR "IN: sysbuf=$sysbuf\n" if $DEBUG;
67 } else {
68 $sysstat = sysread PR, $sysbuf, 7;
69 die "ipadm: sysread error $!" unless defined $sysstat;
70 # print STDERR "IN: bytes=$sysstat\n" if $DEBUG;
71 }
72 $sysdata .= $sysbuf;
73 if ($sysdata =~ /$EOF$/s) {
74 @data = split /\n/, $sysdata;
75 $#data--; # throw $EOF away
76 $wait++; # unblock waitVariable()
77 }
78
79 });
80
81 $MW->waitVariable(\$wait);
82 $MW->fileevent(\*PR, 'readable' => '');
83 # print STDERR "IN: @data\n" if $DEBUG;
84
85 @data;
86
87 } # end pipe_in
88
89 sub pipe_out {
90
91 # Issue an IPADM command by syswrite-ing all the data plus
92 # the terminating $EOF.
93
94 return unless defined $_[0];
95
96 my($bytes, $offset, $sysdata, $sysstat, $wait);
97
98 $sysdata = join '', @_, "$EOF\n";
99 $bytes = length $sysdata;
100 $offset = 0;
101
102 $MW->fileevent(\*PW, 'writable' => sub {
103
104 while ($bytes > 0) {
105 $sysstat = syswrite PW, $sysdata, $bytes, $offset;
106 die "ipadm: syswrite error $!" unless defined $sysstat;
107 $bytes -= $sysstat;
108 $offset += $sysstat;
109 }
110 $wait++;
111
112 });
113
114 $MW->waitVariable(\$wait);
115 $MW->fileevent(\*PW, 'writable' => '');
116 # print STDERR "OT: $sysdata\n" if $DEBUG;
117
118 } # end pipe_out
119
120 sub fini {
121 kill 'SIGTERM', $PID;
122 unlink '/tmp/ipadmh';
123 exit $EXIT;
124 }
125
126 sub init {
127
128 my $frog = defined $ARGV[0] ? '<HANDLE>' : 'sysread()';
129 my $well = 'all is well';
130 if ( (($Tk::VERSION cmp '800.015') <= 0) and ($frog eq 'sysread()') ) {
131 $well = 'all is NOT well';
132 }
133 if ( (($Tk::VERSION cmp '800.015') > 0) and ($frog eq '<HANDLE>') ) {
134 $well = 'all is NOT well';
135 }
136
137 open(HELPER, ">/tmp/ipadmh") or die "cannot write helper program: $!";
138 while ($_ = <DATA>) {
139 print HELPER;
140 }
141 close HELPER or die $!;
142 chmod 0755, '/tmp/ipadmh';
143
144 $MW = MainWindow->new;
145 $MW->title('ipadm - Administer IP Nodes');
146 $MW->iconname('ipadm');
147 $MW->minsize(50, 50);
148 $MW->geometry("+100+50");
149 $MW->protocol('WM_DELETE_WINDOW' => \&fini);
150
151 # Create the menubar and friends.
152
153 my $menubar = $MW->Menu;
154 $MW->configure(-menu => $menubar);
155
156 my $file = $menubar->cascade(-label => '~File');
157 my $edit = $menubar->cascade(-label => '~Edit');
158 my $help = $menubar->cascade(-label => '~Help');
159
160 $file->command(-label => 'Quit', -command => \&fini);
161 $edit->command(-label => 'Fast Find', -command => [$MW => 'bell']);
162 $help->command(-label => 'About', -command => sub {
163 $MW->messageBox(-message => "ipadm $VERSION\n\n99/07/15")});
164
165 # Create the subnets table, a list of hypertext links, and some tags
166 # to highlight the active entry and a binding to load a subnet.
167
168 my $t = $MW->Scrolled('ROText',
169 qw/-width 80 -height 10 -relief ridge -scrollbars w/);
170 $t->pack(qw/-padx 5 -pady 3 -fill both -expand 1/);
171
172 $t->tagConfigure(qw/title -font/ => 'Helvetica 18 bold');
173 $t->tagConfigure(qw/subnet -lmargin1 .5c -lmargin2 1c -foreground blue/);
174 $t->tagConfigure(qw/hot -relief raised -borderwidth 1 -background green/);
175
176 start_ipc_helper;
177
178 $t->insert('end', "\n$ORGANIZATION\n\n", ['title']);
179
180 # Get a list of subnets from ipadmd, sort the subnets by
181 # IP number, and add the title string to the text widget,
182 # tagged with the SDB file name.
183
184 my($status, @subnet_list) = do_command "get_subnet_list\n";
185 die "Cannot get SDB list" unless $status =~ /OK/;
186
187 foreach (sort numerically @subnet_list) {
188 my($sdb, $title) = /^(\S+)\s+(.*)$/;
189 $t->insert('end', "$title\n", ['subnet', $sdb]);
190 }
191
192 #$t->tagBind(qw/subnet <ButtonRelease-1>/ => \&open_subnet);
193
194 my $last_hot = '';
195 $t->tagBind(qw/subnet <Enter>/ => sub {
196 my $text = shift; # SUBTLE STATEMENT HERE (-:
197 my($x, $y) = ($Tk::event->x, $Tk::event->y);
198 $last_hot = $text->index("\@$x,$y linestart");
199 $text->tagAdd('hot', $last_hot, "$last_hot lineend");
200 });
201 $t->tagBind(qw/subnet <Leave>/ => sub {
202 shift->tagRemove(qw/hot 1.0 end/);
203 });
204 $t->tagBind(qw/subnet <Motion>/ => sub {
205 my $text = shift;
206 my($x, $y) = ($Tk::event->x, $Tk::event->y);
207 my $new_hot = $text->index("\@$x,$y linestart");
208 if ($new_hot ne $last_hot) {
209 $text->tagRemove(qw/hot 1.0 end/);
210 $text->tagAdd('hot', $new_hot, "$new_hot lineend");
211 $last_hot = $new_hot;
212 }
213 });
214
215 # Sanity check, see if the 4th line was read.
216
217 my $fourth = 'Subnet 128B, ACME Rubber Band Development';
218 $MW->update;
219 $MW->after(1000);
220 my $data = $t->get('7.0', '7.0 lineend');
221 #print "four=$fourth!\n";
222 #print "data=$data!\n";
223 $EXIT = ($data eq $fourth) ? 0 : 1;
224 ok($data, $fourth);
225 $MW->destroy;
226
227 } # end init
228
229 sub lsearch { # $o = lsearch $regexp, @list;
230
231 # Search the list using the supplied regular expression and return its
232 # ordinal, or -1 if not found.
233
234 my($regexp, @list) = @_;
235 my($i);
236
237 for ($i=0; $i<=$#list; $i++) {
238 return $i if $list[$i] =~ /$regexp/;
239 }
240 return -1;
241
242 } # end lsearch
243
244 sub numerically {
245 my($n1, $n2);
246 ($n1) = $a =~ /Subnet_(\d+)/;
247 ($n2) = $b =~ /Subnet_(\d+)/;
248 if ($n1 != $n2) {
249 $n1 <=> $n2;
250 } else {
251 $a cmp $b;
252 }
253 }
254
255 sub start_ipc_helper {
256
257 # Start a child process and use pipes to talk with it. The child
258 # uses sockets to talk to the remote IPADM daemon.
259
260 $SIG{PIPE} = sub {print STDERR "ipadmh pipe failure.\n"; exit};
261
262 pipe CR, PW or die "cr/pw pipe $!";
263 pipe PR, CW or die "pr/cw pipe $!";
264
265 if ($PID = fork) { # parent, ipadm/Tk
266 close CR;
267 close CW;
268 PW->autoflush(1);
269 } elsif (defined $PID) { # child, exec ipadmh
270 close PR;
271 close PW;
272 open STDIN, "<&CR" or die "STDIN open $!";
273 open STDOUT, ">&CW" or die "STDOUT open $!";
274 open STDERR, ">&CW" or die "STDERR open $!";
275 STDOUT->autoflush(1);
276 STDERR->autoflush(1);
277 exec($^X,"/tmp/ipadmh", $DAEMON_HOST, $DAEMON_PORT) or die "exec $!";
278 die "exec warp $!";
279 } else {
280 die "fork $!";
281 } # ifend fork
282
283 my(@stat) = do_command undef; # did helper make a connection?
284 return if $stat[0] =~ /Connect OK/;
285
286 $MW->messageBox(-message => "Cannot connect to remote IPADM daemon " .
287 "$DAEMON_HOST:$DAEMON_PORT. Please try again later.",
288 -title => 'Daemon is Dead', -icon => 'warning',
289 -type => 'OK');
290 fini;
291
292 } # end start_ipc_helper
293 __DATA__
294 #!/usr/local/bin/perl
295
296 use 5.005;
297 #use IPADM;
298 use IO::Socket;
299 use strict;
300
301 my $EOF = '_END_OF_INFORMATION_';
302
303 do {print "Usage: ipadmh host port\n"; exit} unless @ARGV == 2;
304
305 STDOUT->autoflush(1); # unbuffer output
306 sub timeout {print "1 Socket Timeout\n$EOF\n"; $SIG{ALRM} = \&timeout}
307 $SIG{PIPE} = sub {print "2 Pipe Error.\n$EOF\n"};
308
309 my $sock = 'defined';
310 print +((defined $sock) ? "0 Connect OK" : "3 Connect Failed"), "\n$EOF\n";
311
312
313 while(<STDIN>) {
314 last if /^$EOF$/;
315 }
316
317 my(@data) = ();
318 $SIG{ALRM} = \&timeout; # reset handler
319 alarm 60;
320
321 @data = <<"END-DATA";
322 0 OK
323 Subnet_1.sdb Subnet 1, ACME Bean Counting Department
324 Subnet_128A.sdb Subnet 128A, ACME Coil Spring Development
325 Subnet_128B.sdb Subnet 128B, ACME Rubber Band Development
326 Subnet_2.sdb Subnet 2, ACME Purchasing Department
327 $EOF
328 END-DATA
329
330 alarm 0;
331 #print (/^$EOF$/ ? @data : "4 Daemon Failure\n$EOF\n");
332 print @data;
333 __END__
334 print <<"END-HELP";
335
336 Run this program with or without a command line argument. If
337 \$ARGV[0] is defined then <HANDLE> is used by the fileevent()
338 callback to read from the pipe. If \$ARGV[0] is not defined,
339 then sysread() is used.
340
341 This is Tk version $Tk::VERSION, and we'll use $frog to read
342 from the pipe.
343
344 When all is well, you should see a MainWindow with the heading
345 "Subnet List for ACME Rocket Supply, Inc", followed by a list
346 of (4) subnets.
347
348 ================================================================
349 = With this combination of Tk $Tk::VERSION and $frog I suspect
350 = $well!
351 ================================================================
352
353 The subnet list comes from a helper program via a pipe - the
354 real helper program gets this data via a socket from a server,
355 but for this demo the subnet data is hardcoded in the helper.
356
357 Regardless, the parent Tk program execs the helper and first
358 "asks" for the subnet list by issuing a command on its write
359 pipe, which the helper reads on its STDIN. For this demo, the
360 helper doesn't even look at what it reads but simply writes the
361 subnet list to its STDOUT.
362
363 Now for the bug: the Perl/Tk parent needs to read this subnet
364 list from its input pipe, but the fileevent() *mechanism*
365 changed after Tk version 800.015. The subroutine pipe_in() in
366 the Tk parent "ipadm" needs to read via a <HANDLE> for Tk800.015
367 and before, but sysread() for Tk800.018.
368
369 END-HELP
370