This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Forgot to add the new files for 29248.
[perl5.git] / ext / Safe / t / safeops.t
CommitLineData
6c52f3eb
RGS
1#!perl
2# Tests that all ops can be trapped by a Safe compartment
3
4BEGIN {
5 if ($ENV{PERL_CORE}) {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8 }
9 else {
10 # this won't work outside of the core, so exit
11 print "1..0\n"; exit 0;
12 }
13}
14use Config;
15BEGIN {
16 if ($Config{'extensions'} !~ /\bOpcode\b/ && $Config{'osname'} ne 'VMS') {
17 print "1..0\n"; exit 0;
18 }
19}
20
21use strict;
22use Test::More tests => 354;
23use Safe;
24
25# Read the op names and descriptions directly from opcode.pl
26my @op;
27my @opname;
28open my $fh, '<', '../opcode.pl' or die "Can't open opcode.pl: $!";
29while (<$fh>) {
30 last if /^__END__/;
31}
32while (<$fh>) {
33 chomp;
34 next if !$_ or /^#/;
35 my ($op, $opname) = split /\t+/;
36 push @op, $op;
37 push @opname, $opname;
38}
39close $fh;
40
41sub testop {
42 my ($op, $opname, $code) = @_;
43 pass("$op : skipped") and return if $code =~ /^SKIP/;
fd9784a1 44 pass("$op : skipped") and return if $code =~ m://: && $] < 5.009; # no dor
6c52f3eb
RGS
45 my $c = new Safe;
46 $c->deny_only($op);
47 $c->reval($code);
48 like($@, qr/'\Q$opname\E' trapped by operation mask/, $op);
49}
50
51my $i = 0;
52while (<DATA>) {
53 testop $op[$i], $opname[$i], $_;
54 ++$i;
55}
56
57# lists op examples, in the same order than opcode.pl
58# things that begin with SKIP are skipped, for various reasons (notably
59# optree modified by the optimizer -- Safe checks are done before the
60# optimizer modifies the optree)
61
62__DATA__
63SKIP # null
64SKIP # stub
65scalar $x # scalar
66print @x # pushmark
67wantarray # wantarray
6842 # const
69SKIP (set by optimizer) $x # gvsv
70SKIP *x # gv
71*x{SCALAR} # gelem
72SKIP my $x # padsv
73SKIP my @x # padav
74SKIP my %x # padhv
75SKIP (not implemented) # padany
76SKIP split /foo/ # pushre
77*x # rv2gv
78$x # rv2sv
79$#x # av2arylen
80f() # rv2cv
81sub { } # anoncode
82prototype 'foo' # prototype
83\($x,$y) # refgen
84SKIP \$x # srefgen
85ref # ref
86bless # bless
87qx/ls/ # backtick
88<*.c> # glob
89<FH> # readline
90SKIP (set by optimizer) $x .= <F> # rcatline
91SKIP (internal) # regcmaybe
92SKIP (internal) # regcreset
93SKIP (internal) # regcomp
94/foo/ # match
95qr/foo/ # qr
96s/foo/bar/ # subst
97SKIP (set by optimizer) # substcont
98y:z:t: # trans
99$x = $y # sassign
100@x = @y # aassign
101chop @foo # chop
102chop # schop
103chomp @foo # chomp
104chomp # schomp
105defined # defined
106undef # undef
107study # study
108pos # pos
109++$i # preinc
110SKIP (set by optimizer) # i_preinc
111--$i # predec
112SKIP (set by optimizer) # i_predec
113$i++ # postinc
114SKIP (set by optimizer) # i_postinc
115$i-- # postdec
116SKIP (set by optimizer) # i_postdec
117$x ** $y # pow
118$x * $y # multiply
119SKIP (set by optimizer) # i_multiply
120$x / $y # divide
121SKIP (set by optimizer) # i_divide
122$x % $y # modulo
123SKIP (set by optimizer) # i_modulo
124$x x $y # repeat
125$x + $y # add
126SKIP (set by optimizer) # i_add
127$x - $y # subtract
128SKIP (set by optimizer) # i_subtract
129$x . $y # concat
130"$x" # stringify
131$x << 1 # left_shift
132$x >> 1 # right_shift
133$x < $y # lt
134SKIP (set by optimizer) # i_lt
135$x > $y # gt
136SKIP (set by optimizer) # i_gt
137$i <= $y # le
138SKIP (set by optimizer) # i_le
139$i >= $y # ge
140SKIP (set by optimizer) # i_ge
141$x == $y # eq
142SKIP (set by optimizer) # i_eq
143$x != $y # ne
144SKIP (set by optimizer) # i_ne
145$i <=> $y # ncmp
146SKIP (set by optimizer) # i_ncmp
147$x lt $y # slt
148$x gt $y # sgt
149$x le $y # sle
150$x ge $y # sge
151$x eq $y # seq
152$x ne $y # sne
153$x cmp $y # scmp
154$x & $y # bit_and
155$x ^ $y # bit_xor
156$x | $y # bit_or
157-$x # negate
158SKIP (set by optimizer) # i_negate
159!$x # not
160~$x # complement
161atan2 1 # atan2
162sin 1 # sin
163cos 1 # cos
164rand # rand
165srand # srand
166exp 1 # exp
167log 1 # log
168sqrt 1 # sqrt
169int # int
170hex # hex
171oct # oct
172abs # abs
173length # length
174substr $x, 1 # substr
175vec # vec
176index # index
177rindex # rindex
178sprintf '%s', 'foo' # sprintf
179formline # formline
180ord # ord
181chr # chr
182crypt 'foo','bar' # crypt
183ucfirst # ucfirst
184lcfirst # lcfirst
185uc # uc
186lc # lc
187quotemeta # quotemeta
188@a # rv2av
189SKIP (set by optimizer) # aelemfast
190$a[1] # aelem
191@a[1,2] # aslice
192each %h # each
193values %h # values
194keys %h # keys
195delete $h{Key} # delete
196exists $h{Key} # exists
197%h # rv2hv
198$h{kEy} # helem
199@h{kEy} # hslice
200unpack # unpack
201pack # pack
202split /foo/ # split
203join $a, @b # join
204@x = (1,2) # list
205SKIP @x[1,2] # lslice
206[1,2] # anonlist
207{ a => 1 } # anonhash
208splice @x, 1, 2, 3 # splice
209push @x, $x # push
210pop @x # pop
211shift @x # shift
212unshift @x # unshift
213sort @x # sort
214reverse @x # reverse
215grep { $_ eq 'foo' } @x # grepstart
216SKIP grep { $_ eq 'foo' } @x # grepwhile
217map $_ + 1, @foo # mapstart
218SKIP (set by optimizer) # mapwhile
219SKIP # range
2201..2 # flip
2211..2 # flop
222$x && $y # and
223$x || $y # or
224$x xor $y # xor
225$x ? 1 : 0 # cond_expr
226$x &&= $y # andassign
227$x ||= $y # orassign
228Foo->$x() # method
229f() # entersub
230sub f{} f() # leavesub
231sub f:lvalue{return $x} f() # leavesublv
232caller # caller
233warn # warn
234die # die
235reset # reset
236SKIP # lineseq
237SKIP # nextstate
238SKIP (needs debugger) # dbstate
239while(0){} # unstack
240SKIP # enter
241SKIP # leave
242SKIP # scope
243SKIP # enteriter
244SKIP # iter
245SKIP # enterloop
246SKIP # leaveloop
247return # return
248last # last
249next # next
250redo THIS # redo
251dump # dump
252goto THERE # goto
253exit 0 # exit
254open FOO # open
255close FOO # close
256pipe FOO,BAR # pipe_op
257fileno FOO # fileno
258umask 0755, 'foo' # umask
259binmode FOO # binmode
260tie # tie
261untie # untie
262tied # tied
263dbmopen # dbmopen
264dbmclose # dbmclose
265SKIP (set by optimizer) # sselect
266select FOO # select
267getc FOO # getc
268read FOO # read
269write # enterwrite
270SKIP # leavewrite
271printf # prtf
272print # print
273sysopen # sysopen
274sysseek # sysseek
275sysread # sysread
276syswrite # syswrite
277send # send
278recv # recv
279eof FOO # eof
280tell # tell
281seek FH, $pos, $whence # seek
282truncate FOO, 42 # truncate
283fcntl # fcntl
284ioctl # ioctl
285flock FOO, 1 # flock
286socket # socket
287socketpair # sockpair
288bind # bind
289connect # connect
290listen # listen
291accept # accept
292shutdown # shutdown
293getsockopt # gsockopt
294setsockopt # ssockopt
295getsockname # getsockname
296getpeername # getpeername
297lstat FOO # lstat
298stat FOO # stat
299-R # ftrread
300-W # ftrwrite
301-X # ftrexec
302-r # fteread
303-w # ftewrite
304-x # fteexec
305-e # ftis
306SKIP -O # fteowned
307SKIP -o # ftrowned
308-z # ftzero
309-s # ftsize
310-M # ftmtime
311-A # ftatime
312-C # ftctime
313-S # ftsock
314-c # ftchr
315-b # ftblk
316-f # ftfile
317-d # ftdir
318-p # ftpipe
319-l # ftlink
320-u # ftsuid
321-g # ftsgid
322-k # ftsvtx
323-t # fttty
324-T # fttext
325-B # ftbinary
326chdir '/' # chdir
327chown # chown
328chroot # chroot
329unlink 'foo' # unlink
330chmod 511, 'foo' # chmod
331utime # utime
332rename 'foo', 'bar' # rename
333link 'foo', 'bar' # link
334symlink 'foo', 'bar' # symlink
335readlink 'foo' # readlink
336mkdir 'foo' # mkdir
337rmdir 'foo' # rmdir
338opendir DIR # open_dir
339readdir DIR # readdir
340telldir DIR # telldir
341seekdir DIR, $pos # seekdir
342rewinddir DIR # rewinddir
343closedir DIR # closedir
344fork # fork
345wait # wait
346waitpid # waitpid
347system # system
348exec # exec
349kill # kill
350getppid # getppid
351getpgrp # getpgrp
352setpgrp # setpgrp
353getpriority # getpriority
354setpriority # setpriority
355time # time
356times # tms
357localtime # localtime
358gmtime # gmtime
359alarm # alarm
360sleep 1 # sleep
361shmget # shmget
362shmctl # shmctl
363shmread # shmread
364shmwrite # shmwrite
365msgget # msgget
366msgctl # msgctl
367msgsnd # msgsnd
368msgrcv # msgrcv
369semget # semget
370semctl # semctl
371semop # semop
372use strict # require
373do 'file' # dofile
374eval "1+1" # entereval
375eval "1+1" # leaveeval
376SKIP eval { 1+1 } # entertry
377SKIP eval { 1+1 } # leavetry
378gethostbyname 'foo' # ghbyname
379gethostbyaddr 'foo' # ghbyaddr
380gethostent # ghostent
381getnetbyname 'foo' # gnbyname
382getnetbyaddr 'foo' # gnbyaddr
383getnetent # gnetent
384getprotobyname 'foo' # gpbyname
385getprotobynumber 42 # gpbynumber
386getprotoent # gprotoent
387getservbyname 'name', 'proto' # gsbyname
388getservbyport 'a', 'b' # gsbyport
389getservent # gservent
390sethostent # shostent
391setnetent # snetent
392setprotoent # sprotoent
393setservent # sservent
394endhostent # ehostent
395endnetent # enetent
396endprotoent # eprotoent
397endservent # eservent
398getpwnam # gpwnam
399getpwuid # gpwuid
400getpwent # gpwent
401setpwent # spwent
402endpwent # epwent
403getgrnam # ggrnam
404getgrgid # ggrgid
405getgrent # ggrent
406setgrent # sgrent
407endgrent # egrent
408getlogin # getlogin
409syscall # syscall
410SKIP # lock
411SKIP # threadsv
412SKIP # setstate
413$x->y() # method_named
414$x // $y # dor
415$x //= $y # dorassign
416SKIP (no way) # custom