| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strictscip-perl cpan POSIX-2008 0.28 strict/; |
| 4 | use warningsscip-perl cpan POSIX-2008 0.28 warnings/; |
| 5 | use sigtrapscip-perl cpan POSIX-2008 0.28 sigtrap/ qw(die normal-signals error-signals); |
| 6 | |
| 7 | use Errnoscip-perl cpan POSIX-2008 0.28 Errno/ ':POSIX'; |
| 8 | use Fcntlscip-perl cpan POSIX-2008 0.28 Fcntl/ qw(:DEFAULT :mode); |
| 9 | use File::Pathscip-perl cpan POSIX-2008 0.28 File/Path/ 'rmtree'; |
| 10 | use File::Tempscip-perl cpan POSIX-2008 0.28 File/Temp/ 'mktemp'; |
| 11 | use Test::Morescip-perl cpan POSIX-2008 0.28 Test/More/ tests => 60; |
| 12 | |
| 13 | use POSIX::2008scip-perl cpan POSIX-2008 0.28 POSIX/` 2008`/; |
| 14 | |
| 15 | my $tmpname = mktempscip-perl cpan POSIX-2008 0.28 main/mktemp().('tmpXXXXX'); |
| 16 | |
| 17 | sysopenscip-perl cpan POSIX-2008 0.28 main/sysopen(). my $fh, $tmpname, O_RDWR|O_CREAT|O_EXCL, 0600 or diescip-perl cpan POSIX-2008 0.28 main/die(). "$tmpname: $!"; |
| 18 | my $fd = filenoscip-perl cpan POSIX-2008 0.28 main/fileno(). $fh; |
| 19 | syswritescip-perl cpan POSIX-2008 0.28 main/syswrite(). $fh, '......'; |
| 20 | |
| 21 | # |
| 22 | # read/write |
| 23 | # |
| 24 | SKIP: { |
| 25 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 26 | if (! defined &POSIX::2008::write) { |
| 27 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(syswritescip-perl cpan POSIX-2008 0.28 main/syswrite().($fh, '111222'), '==', 6, '... syswrite(fh) bytes written (fallback)'); |
| 28 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'write() UNAVAILABLE', 2; |
| 29 | } |
| 30 | my $rv = eval { POSIX::2008::writescip-perl cpan POSIX-2008 0.28 main/write().($fh, '', -3) }; |
| 31 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /Negative/, 'write fails due to negative size'); |
| 32 | cmp_ok(POSIX::2008::write($fh, '111'), '==', 3, 'write(fh) bytes written'); |
| 33 | cmp_ok(POSIX::2008::write($fd, '222', 10), '==', 3, 'write(fd) bytes written'); |
| 34 | } |
| 35 | SKIP: { |
| 36 | my $buf; |
| 37 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 38 | if (! defined &POSIX::2008::read) { |
| 39 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(sysreadscip-perl cpan POSIX-2008 0.28 main/sysread().($fh, $buf, 6), '==', 6, '... sysread(fh) bytes read (fallback)'); |
| 40 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '111222', ' sysread(fh) string read (fallback)'); |
| 41 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'read() UNAVAILABLE', 3; |
| 42 | } |
| 43 | my $rv = eval { POSIX::2008::readscip-perl cpan POSIX-2008 0.28 main/read().($fh, my $buf, -3) }; |
| 44 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /Negative/, 'read fails due to negative size'); |
| 45 | cmp_ok(POSIX::2008::read($fh, $buf, 3), '==', 3, 'read(fh) bytes read'); |
| 46 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '111', 'read(fh) string read'); |
| 47 | cmp_ok(POSIX::2008::read($fd, $buf, 3), '==', 3, 'read(fd) bytes read'); |
| 48 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '222', 'read(fh) string read'); |
| 49 | } |
| 50 | |
| 51 | # |
| 52 | # pread/pwrite |
| 53 | # |
| 54 | SKIP: { |
| 55 | if (! defined &POSIX::2008::pwrite) { |
| 56 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 57 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(syswritescip-perl cpan POSIX-2008 0.28 main/syswrite().($fh, '333444'), '==', 6, '... syswrite(fh) bytes written (fallback)'); |
| 58 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'pwrite() UNAVAILABLE', 6; |
| 59 | } |
| 60 | my $rv = eval { POSIX::2008::pwritescip-perl cpan POSIX-2008 0.28 main/pwrite().($fh, 'foo', -3, 0) }; |
| 61 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /Negative/, 'pwrite fails due to negative size'); |
| 62 | foreach my $buf_offset (-1290880921, -4, 4, ~0) { |
| 63 | my $rv = eval { POSIX::2008::pwritescip-perl cpan POSIX-2008 0.28 main/pwrite().($fh, 'foo', undef, 0, $buf_offset) }; |
| 64 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /outside/, 'pwrite fails due to invalid buf_offset'); |
| 65 | } |
| 66 | cmp_ok(POSIX::2008::pwrite($fh, '444', undef, 3), '==', 3, 'pwrite(fh) bytes written'); |
| 67 | cmp_ok(POSIX::2008::pwrite($fd, '333', undef, 0), '==', 3, 'pwrite(fd) bytes written'); |
| 68 | } |
| 69 | SKIP: { |
| 70 | my $buf; |
| 71 | if (! defined &POSIX::2008::pread) { |
| 72 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 73 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(sysreadscip-perl cpan POSIX-2008 0.28 main/sysread().($fh, $buf, 6), '==', 6, '... sysread(fh) bytes read (fallback)'); |
| 74 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '333444', '... sysread(fh) string read (fallback)'); |
| 75 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'pread() UNAVAILABLE', 5; |
| 76 | } |
| 77 | my $rv = eval { POSIX::2008::preadscip-perl cpan POSIX-2008 0.28 main/pread().($fh, my $buf, -3, 0) }; |
| 78 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /Negative/, 'pread fails due to negative size'); |
| 79 | foreach my $buf_offset (-1290880921, -4) { |
| 80 | my $buf = 'foo'; |
| 81 | my $rv = eval { POSIX::2008::preadscip-perl cpan POSIX-2008 0.28 main/pread().($fh, $buf, 0, 0, $buf_offset) }; |
| 82 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /outside/, 'pwrite fails due to invalid buf_offset'); |
| 83 | } |
| 84 | cmp_ok(POSIX::2008::pread($fh, $buf, 3, 3), '==', 3, 'pread(fh) bytes read'); |
| 85 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '444', 'pread(fh) string read'); |
| 86 | cmp_ok(POSIX::2008::pread($fd, $buf, 3, 0), '==', 3, 'pread(fd) bytes read'); |
| 87 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '333', 'pread(fh) string read'); |
| 88 | } |
| 89 | |
| 90 | # |
| 91 | # readv/writev |
| 92 | # |
| 93 | SKIP: { |
| 94 | if (! defined &POSIX::2008::writev) { |
| 95 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'writev() UNAVAILABLE', 18; |
| 96 | } |
| 97 | |
| 98 | my ($rv, $buf); |
| 99 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 100 | truncatescip-perl cpan POSIX-2008 0.28 main/truncate(). $fh, 0; |
| 101 | |
| 102 | no warnings 'uninitialized'; |
| 103 | cmp_ok(POSIX::2008::writev($fh, [undef, '', '0', '11']), '==', 3, 'writev(fh) bytes written'); |
| 104 | cmp_ok(POSIX::2008::writev($fh, ['22', undef, '', '3']), '==', 3, 'writev(fh) bytes written'); |
| 105 | cmp_ok(POSIX::2008::writev($fh, ['4', '55', undef, '']), '==', 3, 'writev(fh) bytes written'); |
| 106 | cmp_ok(POSIX::2008::writev($fh, ['', '6', '77', undef]), '==', 3, 'writev(fh) bytes written'); |
| 107 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 108 | sysreadscip-perl cpan POSIX-2008 0.28 main/sysread(). $fh, $buf, 4096; |
| 109 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '011223455677'); |
| 110 | |
| 111 | my $teststring = 'abcdef:ABCDEF'; |
| 112 | my @offsets_ok = ( |
| 113 | 0, |
| 114 | intscip-perl cpan POSIX-2008 0.28 main/int().(lengthscip-perl cpan POSIX-2008 0.28 main/length().($teststring)/2), |
| 115 | lengthscip-perl cpan POSIX-2008 0.28 main/length().($teststring)-1, |
| 116 | lengthscip-perl cpan POSIX-2008 0.28 main/length().($teststring), |
| 117 | -1, |
| 118 | -intscip-perl cpan POSIX-2008 0.28 main/int().(lengthscip-perl cpan POSIX-2008 0.28 main/length().($teststring)/2), |
| 119 | -lengthscip-perl cpan POSIX-2008 0.28 main/length().($teststring), |
| 120 | ); |
| 121 | my @offsets_nok = ( |
| 122 | +lengthscip-perl cpan POSIX-2008 0.28 main/length().($teststring)+1, |
| 123 | -lengthscip-perl cpan POSIX-2008 0.28 main/length().($teststring)-1, |
| 124 | ~0, |
| 125 | +(~0 >> 1), |
| 126 | -(~0 >> 1), |
| 127 | ); |
| 128 | my $data_expected = joinscip-perl cpan POSIX-2008 0.28 main/join(). '', mapscip-perl cpan POSIX-2008 0.28 main/map(). substrscip-perl cpan POSIX-2008 0.28 main/substr().($teststring, $_scip-perl cpan POSIX-2008 0.28 main/$_.), @offsets_ok; |
| 129 | my $bufs = [ mapscip-perl cpan POSIX-2008 0.28 main/map(). [$teststring, $_scip-perl cpan POSIX-2008 0.28 main/$_.], @offsets_ok ]; |
| 130 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 131 | truncatescip-perl cpan POSIX-2008 0.28 main/truncate(). $fh, 0; |
| 132 | $rv = POSIX::2008::writevscip-perl cpan POSIX-2008 0.28 main/writev().($fh, $bufs); |
| 133 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($rv, '==', lengthscip-perl cpan POSIX-2008 0.28 main/length().($data_expected), 'writev(fh, arrayrefs) bytes written'); |
| 134 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 135 | sysreadscip-perl cpan POSIX-2008 0.28 main/sysread(). $fh, $buf, 4096; |
| 136 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', $data_expected, 'writev(fh, arrayrefs) data written'); |
| 137 | |
| 138 | foreach my $offset (@offsets_nok) { |
| 139 | $rv = eval { POSIX::2008::writevscip-perl cpan POSIX-2008 0.28 main/writev().($fh, [ [$teststring, $offset] ]) }; |
| 140 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!definedscip-perl cpan POSIX-2008 0.28 main/defined().($rv), "writev() croaks due to invalid offset $offset (rv:$rv)"); |
| 141 | likescip-perl cpan POSIX-2008 0.28 main/like().($@scip-perl cpan POSIX-2008 0.28 main/`$@`., qr/outside string/, "writev() offset $offset should be outside string"); |
| 142 | } |
| 143 | |
| 144 | $rv = eval { POSIX::2008::writevscip-perl cpan POSIX-2008 0.28 main/writev().($fh, "foobar") }; |
| 145 | # /i is needed because some Perls say "ARRAY", others say "array". |
| 146 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!definedscip-perl cpan POSIX-2008 0.28 main/defined().($rv) && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /not an ARRAY/i); |
| 147 | } |
| 148 | SKIP: { |
| 149 | if (! defined &POSIX::2008::readv) { |
| 150 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'readv() UNAVAILABLE', 10; |
| 151 | } |
| 152 | my $rv; |
| 153 | my @buf1; |
| 154 | my @buf2; |
| 155 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 156 | truncatescip-perl cpan POSIX-2008 0.28 main/truncate(). $fh, 0; |
| 157 | syswritescip-perl cpan POSIX-2008 0.28 main/syswrite(). $fh, 'abcdef'; |
| 158 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 159 | |
| 160 | $rv = eval { my $buf = 'foobar'; POSIX::2008::readvscip-perl cpan POSIX-2008 0.28 main/readv().($fh, $buf, []) }; |
| 161 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /'buffers' is not an ARRAY/i, 'readv rejects non-arrayref buffers'); |
| 162 | |
| 163 | $rv = eval { my $buf = {}; POSIX::2008::readvscip-perl cpan POSIX-2008 0.28 main/readv().($fh, $buf, []) }; |
| 164 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /'buffers' is not an ARRAY/i, 'readv rejects non-arrayref buffers'); |
| 165 | |
| 166 | $rv = eval { my $buf = []; POSIX::2008::readvscip-perl cpan POSIX-2008 0.28 main/readv().($fh, $buf, 'foobar') }; |
| 167 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /not an ARRAY/i, 'readv rejects non-arrayref sizes'); |
| 168 | |
| 169 | $rv = eval { POSIX::2008::readvscip-perl cpan POSIX-2008 0.28 main/readv().($fh, my $buf, [0, -1, 0]) }; |
| 170 | okscip-perl cpan POSIX-2008 0.28 main/ok().(!$rv && $@scip-perl cpan POSIX-2008 0.28 main/`$@`. =~ /Negative/, 'readv rejects negative size'); |
| 171 | |
| 172 | cmp_ok(POSIX::2008::readv($fh, @buf1, [0, 1, 0, 2]), '==', 3, 'readv(fh) bytes read'); |
| 173 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(scalarscip-perl cpan POSIX-2008 0.28 main/scalar().(@buf1), '==', 4, 'readv(fh) buffers read'); |
| 174 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(joinscip-perl cpan POSIX-2008 0.28 main/join().('', @buf1), 'eq', 'abc', 'readv(fh) strings read'); |
| 175 | cmp_ok(POSIX::2008::readv($fd, @buf2, [2, 0, 1, 0]), '==', 3, 'readv(fd) bytes read'); |
| 176 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(scalarscip-perl cpan POSIX-2008 0.28 main/scalar().(@buf2), '==', 4, 'readv(fd) buffers read'); |
| 177 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(joinscip-perl cpan POSIX-2008 0.28 main/join().('', @buf2), 'eq', 'def', 'readv(fd) strings read'); |
| 178 | } |
| 179 | |
| 180 | # |
| 181 | # preadv/pwritev |
| 182 | # |
| 183 | SKIP: { |
| 184 | if (! defined &POSIX::2008::pwritev) { |
| 185 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'pwritev() UNAVAILABLE', 3; |
| 186 | } |
| 187 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 188 | truncatescip-perl cpan POSIX-2008 0.28 main/truncate(). $fh, 0; |
| 189 | syswritescip-perl cpan POSIX-2008 0.28 main/syswrite(). $fh, '......'; |
| 190 | no warnings 'uninitialized'; |
| 191 | cmp_ok(POSIX::2008::pwritev($fh, ['88', '', undef, '8'], 3), '==', 3, 'pwritev(fh) bytes written'); |
| 192 | cmp_ok(POSIX::2008::pwritev($fd, ['7', undef, '', '77'], 0), '==', 3, 'pwritev(fd) bytes written'); |
| 193 | sysseekscip-perl cpan POSIX-2008 0.28 main/sysseek(). $fh, 0, 0; |
| 194 | sysreadscip-perl cpan POSIX-2008 0.28 main/sysread(). $fh, my $buf, 4096; |
| 195 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($buf, 'eq', '777888'); |
| 196 | } |
| 197 | SKIP: { |
| 198 | if (! defined &POSIX::2008::preadv) { |
| 199 | skipscip-perl cpan POSIX-2008 0.28 main/skip(). 'preadv() UNAVAILABLE', 7; |
| 200 | } |
| 201 | my (@buf1, @buf2); |
| 202 | cmp_ok(POSIX::2008::preadv($fh, @buf1, [2, 0, 0, 1], 3), '==', 3, 'preadv(fh) bytes read'); |
| 203 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(scalarscip-perl cpan POSIX-2008 0.28 main/scalar().(@buf1), '==', 4, 'preadv(fh) buffers read'); |
| 204 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(joinscip-perl cpan POSIX-2008 0.28 main/join().('', @buf1), 'eq', '888', 'preadv(fh) strings read'); |
| 205 | cmp_ok(POSIX::2008::preadv($fd, @buf2, [1, 0, 0, 2], 0), '==', 3, 'preadv(fd) bytes read'); |
| 206 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(scalarscip-perl cpan POSIX-2008 0.28 main/scalar().(@buf2), '==', 4, 'preadv(fd) buffers read'); |
| 207 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().(joinscip-perl cpan POSIX-2008 0.28 main/join().('', @buf2), 'eq', '777', 'preadv(fh) strings read'); |
| 208 | |
| 209 | my $rv = POSIX::2008::preadvscip-perl cpan POSIX-2008 0.28 main/preadv().($fd, @buf2, [1, 0, 0, 2], -1); |
| 210 | if (! definedscip-perl cpan POSIX-2008 0.28 main/defined(). $rv) { |
| 211 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($!scip-perl cpan POSIX-2008 0.28 main/`$!`., '==', EINVAL, 'preadv() with negative offset sets errno EINVAL'); |
| 212 | } |
| 213 | else { |
| 214 | # omnios-r151054 returns 0 with a negative offset. |
| 215 | cmp_okscip-perl cpan POSIX-2008 0.28 main/cmp_ok().($rv, '==', 0, 'preadv() with negative offset returns 0'); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | closescip-perl cpan POSIX-2008 0.28 main/close(). $fh; |
| 220 | |
| 221 | END { |
| 222 | rmtreescip-perl cpan POSIX-2008 0.28 main/rmtree().($tmpname) if definedscip-perl cpan POSIX-2008 0.28 main/defined(). $tmpname && $tmpname =~ /^tmp/; |
| 223 | } |