1850e2753Smillert#!/usr/bin/perl -w 2850e2753SmillertBEGIN { 3850e2753Smillert chdir 't' if -d 't'; 4850e2753Smillert @INC = '../lib'; 5850e2753Smillert} 6850e2753Smillert 7850e2753Smillertuse Test::More tests => 80; 8850e2753Smillertuse strict; 9850e2753Smillertuse IO::Handle; 10850e2753Smillertuse Fcntl; 11850e2753Smillert 12850e2753Smillertmy $pname = "/pipe/perl_pipe_test$$"; 13850e2753Smillert 14850e2753Smillertok !eval {OS2::pipe $pname, 'wait'}, 'wait for non-existing pipe fails'; 15850e2753Smillertis 0 + $^E, 3, 'correct error code'; 16850e2753Smillertok my $server_pipe = OS2::pipe($pname, 'rw'), 'create pipe, no connect'; 17850e2753Smillertok((my $fd = fileno $server_pipe) >= 0, 'has a fileno'); 18850e2753Smillertis +(OS2::pipeCntl($server_pipe, 'readstate'))[0], 2, 'is listening'; 19850e2753Smillertis OS2::pipeCntl($server_pipe, 'state') & 0xFF, 1, 'max count=1'; 20850e2753Smillert 21850e2753Smillertok 0 > OS2::pipeCntl($server_pipe, 'connect', !'wait'), 'connect nowait'; 22850e2753Smillert 23850e2753Smillertok open(my $fh, '+<', $pname), 'open client end'; 24850e2753Smillert#ok sysopen($fh, $pname, O_RDWR), 'sysopen client end' . $^E; 25850e2753Smillert#my ($fd1, $action) = OS2::open $pname, 0x2042 or warn $^E; # ERROR,SHARE,RDWR 26850e2753Smillertis +(OS2::pipeCntl($server_pipe, 'readstate'))[0], 3, 'is connected'; 27850e2753Smillertok 0 < OS2::pipeCntl($server_pipe, 'connect', !'wait'), 'connect nowait'; 28850e2753Smillertok OS2::pipeCntl($server_pipe, 'connect', 'wait'), 'connect wait'; 29850e2753Smillertis $server_pipe->autoflush, 0, 'autoflush server'; # Returns the old value 30850e2753Smillertis $fh->autoflush, 0, 'autoflush'; # Returns the old value 31850e2753Smillertok syswrite($server_pipe, "some string\n"), 'server write'; 32850e2753Smillertis scalar <$fh>, "some string\n", 'client read'; 33850e2753Smillertok syswrite($fh, "another string\n"), 'client write'; 34850e2753Smillert 35850e2753Smillertis OS2::pipeCntl($server_pipe, 'peek'), "another string\n", 'peeking is fine'; 36850e2753Smillertmy ($st, $bytesAvail, $bytesInMess) = OS2::pipeCntl($server_pipe, 'readstate'); 37850e2753Smillertmy ($name, $remoteID, $outBuffer, $inBuffer, $maxInstance, $countInstance) 38850e2753Smillert = OS2::pipeCntl($server_pipe, 'info'); 39850e2753Smillertis $bytesAvail, length("another string\n"), 'count bytes'; 40850e2753Smillertis $remoteID, 0, 'not remote'; 41850e2753Smillertis $maxInstance, 1, 'max count is 1'; 42850e2753Smillertis $countInstance, 1, 'count is 1'; 43850e2753Smillert#is $len, length($pname) + 1, 'length of name is 1 more than the actual'; 44850e2753Smillert(my $tmp = $pname) =~ s,/,\\,g; 45850e2753Smillertis lc $name, lc $tmp, 'name is correct (up to case)'; 46850e2753Smillert 47850e2753Smillert# If do print() instead of syswrite(), this gets "some string\n" instead!!! 48850e2753Smillertis scalar <$server_pipe>, "another string\n", 'server read'; 49850e2753Smillert 50850e2753Smillertok !open(my $fh1, '+<', $pname), 'open client end fails'; 51850e2753Smillert 52850e2753Smillert# No new child present, return -1 53850e2753Smillertok 0 > OS2::pipeCntl($server_pipe, 'reset', !'wait'), 'server reset, no wait'; 54850e2753Smillertok eof($fh), 'client EOF'; 55850e2753Smillertok(($fh->clearerr, 1), 'client clear EOF'); # XXXX Returns void 56850e2753Smillert 57850e2753Smillert$!=0; $^E = 0; 58850e2753Smillertok close $fh, 'close client'; 59850e2753Smillert#diag $!; 60850e2753Smillert#diag $^E; 61850e2753Smillertis fileno $fh, undef, 'was actually closed...'; 62850e2753Smillert 63850e2753Smillertok open($fh, '+<', $pname), 'open client end'; 64850e2753Smillert 65850e2753Smillertis $fh->autoflush, 1, 'autoflush'; # Returns the old value 66850e2753Smillertok syswrite($server_pipe, "some string\n"), 'server write'; 67850e2753Smillertis scalar <$fh>, "some string\n", 'client read'; 68850e2753Smillertok syswrite($fh, "another string\n"), 'client write'; 69850e2753Smillert 70850e2753Smillert# If do print() instead of syswrite(), this gets "some string\n" instead!!! 71850e2753Smillertis scalar <$server_pipe>, "another string\n", 'server read'; 72850e2753Smillert 73850e2753Smillertok syswrite($server_pipe, "some string\n"), 'server write'; 74850e2753Smillertok syswrite($fh, "another string\n"), 'client write'; 75850e2753Smillertis scalar <$fh>, "some string\n", 'client read'; 76850e2753Smillert 77850e2753Smillert# If do print() instead of syswrite(), this gets "some string\n" instead!!! 78850e2753Smillertis scalar <$server_pipe>, "another string\n", 'server read'; 79850e2753Smillert 80850e2753Smillertok syswrite($server_pipe, "some string\n"), 'server write'; 81850e2753Smillertok syswrite($fh, "another string\n"), 'client write'; 82850e2753Smillert 83850e2753Smillertok((sysread $fh, my $in, 2000), 'client sysread'); 84850e2753Smillertis $in, "some string\n", 'client sysread correct'; 85850e2753Smillert 86850e2753Smillert# If do print() instead of syswrite(), this gets "some string\n" instead!!! 87850e2753Smillertok((sysread $server_pipe, $in, 2000), 'server sysread'); 88850e2753Smillertis $in, "another string\n", 'server sysread correct'; 89850e2753Smillert 90850e2753Smillertok !open($fh1, '+<', $pname), 'open client end fails'; 91850e2753Smillert 92850e2753Smillert# XXXX Not needed??? 93850e2753Smillert#ok(($fh->clearerr, 1), 'client clear EOF'); # XXXX Returns void 94850e2753Smillert 95850e2753Smillertok close $fh, 'close client'; 96850e2753Smillertok eof $server_pipe, 'server EOF'; # Creates an error condition 97850e2753Smillert 98850e2753Smillertmy $pid = system 4|0x40000, $^X, '-wle', <<'EOS', $pname; # SESSION|INDEPENDENT 99850e2753Smillert my $success; 100850e2753Smillert END {sleep($success ? 1 : 10);} 101850e2753Smillert my $mess = ''; 102850e2753Smillert $SIG{TERM} = sub {die "kid1 error: Got SIGTERM\nmess=`$mess'"}; 103850e2753Smillert my $pn = shift; 104850e2753Smillert my $fh; 105850e2753Smillert eval { 106850e2753Smillert $mess .= "Pipe open fails\n" unless open $fh, '+<', $pn; 107850e2753Smillert my $t = time; ### TIMESTAMP0 108850e2753Smillert warn "kid1: Wait for pipe...\n"; 109850e2753Smillert $mess .= "Pipe became available\n" if OS2::pipe $pn, 'wait'; 110850e2753Smillert my $t1 = time() - $t; ### TIMESTAMP1 111850e2753Smillert $mess .= "Unexpected delay $t1\n" unless $t1 >= 1 and $t1 <= 3; 112850e2753Smillert warn "kid1: sleep 4...\n"; 113850e2753Smillert sleep 4; 114850e2753Smillert $mess .= "Pipe open\n" if open $fh, '+<', $pn; 115850e2753Smillert binmode $fh; 116850e2753Smillert 1; ### TIMESTAMP2 117850e2753Smillert } or warn $@; 118850e2753Smillert warn "kid1: pipe opened...\n"; 119850e2753Smillert select $fh; $| = 1; 120850e2753Smillert my $c = syswrite $fh, $mess or warn "print: $!"; 121850e2753Smillert warn "kid1: Wrote $c bytes\n"; 122850e2753Smillert warn $mess; 123850e2753Smillert close $fh or die "kid1 error: close: $!"; 124850e2753Smillert $success = 1; 125850e2753SmillertEOS 126850e2753Smillert 127850e2753Smillertok $pid > 0, 'kid pid'; 128850e2753Smillert 129850e2753Smillert### TIMESTAMP0 130850e2753Smillertsleep 2; 131850e2753Smillertmy $t = time; 132850e2753Smillert### TIMESTAMP1 133850e2753Smillert# New child present; will clear error condition... 134850e2753Smillertok 0 < OS2::pipeCntl($server_pipe, 'reset', 'wait'), 'server reset, wait'; 135850e2753Smillert### TIMESTAMP2 136850e2753Smillertmy $t1 = time() - $t; 137850e2753Smillertok $t1 <= 6 && $t1 >= 2, 'correct delay'; 138850e2753Smillert 139850e2753Smillertsleep 2; 140850e2753Smillert 141850e2753Smillertok binmode($server_pipe), 'binmode'; 142850e2753Smillertok !eof $server_pipe, 'server: no EOF'; 143850e2753Smillertmy @in = <$server_pipe>; 144850e2753Smillertmy @exp = ( "Pipe open fails\n", "Pipe became available\n", "Pipe open\n"); 145850e2753Smillert 146850e2753Smillertis "@in", "@exp", 'expected data'; 147850e2753Smillert 148850e2753Smillert# Can't switch to message mode if created in byte mode... 149850e2753Smillertok close $server_pipe, 'server close'; 150850e2753Smillertok $server_pipe = OS2::pipe($pname, 'RW'), 'create pipe in message mode'; 151850e2753Smillertok OS2::pipeCntl($server_pipe, 'byte'), 'can switch to byte mode'; 152850e2753Smillertok OS2::pipeCntl($server_pipe, 'message'), 'can switch to message mode'; 153850e2753Smillert 154850e2753Smillert$pid = system 4|0x40000, $^X, '-wle', <<'EOS', $pname, $$; # SESSION|INDEPENDENT 155850e2753Smillert END {sleep 2} 156850e2753Smillert my ($name, $ppid) = (shift, shift); 157850e2753Smillert $name =~ s,/,\\,g; 158850e2753Smillert $name = uc $name; 159850e2753Smillert warn "kid2: OS2::pipe $name, 'call', ...\n"; 160850e2753Smillert my $got = OS2::pipe $name, 'call', "Is your pid $ppid?\n"; 161850e2753Smillert my $ok = $got eq 'Yes'; 162850e2753Smillert warn "kid2: got `$got'\n"; 163850e2753Smillert OS2::pipe $name, 'call', $ok ? "fine\n" : "bad\n"; 164850e2753SmillertEOS 165850e2753Smillert 166850e2753Smillertok $pid, 'kid started'; 167*898184e3Ssthensleep 2; # XXX How to synchronize with kid??? 168850e2753Smillert$in = scalar <$server_pipe>; 169850e2753Smillertmy $ok1 = ($in || '') eq "Is your pid $$?\n"; 170850e2753Smillertis $in, "Is your pid $$?\n", 'call in'; 171850e2753Smillertok syswrite($server_pipe, $ok1 ? 'Yes' : 'No' ), 'server write'; 172850e2753Smillert 173850e2753Smillertok 0 < OS2::pipeCntl($server_pipe, 'reset', 'wait'), 'server reset, wait'; 174850e2753Smillert$in = scalar <$server_pipe>; 175850e2753Smillertis $in, "fine\n", 'call in'; 176850e2753Smillertok syswrite($server_pipe, 'ending' ), 'server write'; 177850e2753Smillert 178850e2753Smillertok close $server_pipe, 'server close'; 179850e2753Smillert 180850e2753Smillertok $server_pipe = OS2::pipe($pname, 'W'), 'create pipe in message write mode'; 181850e2753Smillertok !eval {OS2::pipeCntl($server_pipe, 'readstate'); 1}, 'readstate fails, as expected'; 182850e2753Smillertok close $server_pipe, 'server close'; 183850e2753Smillert 184850e2753Smillertok $server_pipe = OS2::pipe($pname, 'w'), 'create pipe in byte write mode'; 185850e2753Smillertok !eval {OS2::pipeCntl($server_pipe, 'readstate'); 1}, 'readstate fails, as expected'; 186850e2753Smillertok close $server_pipe, 'server close'; 187850e2753Smillert 188850e2753Smillertok $server_pipe = OS2::pipe($pname, 'r'), 'create pipe in byte read mode'; 189850e2753Smillertis +(OS2::pipeCntl($server_pipe, 'readstate'))[0], 2, 'is listening'; 190850e2753Smillertok close $server_pipe, 'server close'; 191850e2753Smillert 192850e2753Smillertok $server_pipe = OS2::pipe($pname, 'r', 0), 'create-no-connect pipe in byte read mode'; 193850e2753Smillertis +(OS2::pipeCntl($server_pipe, 'readstate'))[0], 1, 'is disconnected'; 194850e2753Smillertok close $server_pipe, 'server close'; 195850e2753Smillert 196850e2753Smillertok $server_pipe = OS2::pipe($pname, 'R'), 'create pipe in message read mode'; 197850e2753Smillertis +(OS2::pipeCntl($server_pipe, 'readstate'))[0], 2, 'is listening'; 198850e2753Smillertok close $server_pipe, 'server close'; 199850e2753Smillert 200850e2753Smillert#is waitpid($pid, 0), $pid, 'kid ended'; 201850e2753Smillert#is $?, 0, 'kid exitcode'; 202