This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
385d6610c5d3459bb24fbbe895b5d8112b9602b8
[perl5.git] / dist / Safe / t / safe1.t
1 #!./perl -w
2 $|=1;
3 BEGIN {
4     require Config; import Config;
5     if ($Config{'extensions'} !~ /\bOpcode\b/ && $Config{'osname'} ne 'VMS') {
6         print "1..0\n";
7         exit 0;
8     }
9
10 }
11
12 # Tests Todo:
13 #       'main' as root
14
15 package test;   # test from somewhere other than main
16
17 use vars qw($bar);
18
19 use Opcode 1.00, qw(opdesc opset opset_to_ops opset_to_hex
20         opmask_add full_opset empty_opset opcodes opmask define_optag);
21
22 use Safe 1.00;
23
24 my $last_test; # initalised at end
25 print "1..$last_test\n";
26
27 my $t = 1;
28 my $cpt;
29 # create and destroy some automatic Safe compartments first
30 $cpt = new Safe or die;
31 $cpt = new Safe or die;
32 $cpt = new Safe or die;
33
34 $cpt = new Safe "Root" or die;
35
36 foreach(1..3) {
37         $foo = 42;
38
39         $cpt->share(qw($foo));
40
41         print ${$cpt->varglob('foo')}       == 42 ? "ok $t\n" : "not ok $t\n"; $t++;
42
43         ${$cpt->varglob('foo')} = 9;
44
45         print $foo == 9 ? "ok $t\n" : "not ok $t\n"; $t++;
46
47         print $cpt->reval('$foo')       == 9    ? "ok $t\n" : "not ok $t\n"; $t++;
48         # check 'main' has been changed:
49         print $cpt->reval('$::foo')     == 9    ? "ok $t\n" : "not ok $t\n"; $t++;
50         print $cpt->reval('$main::foo') == 9    ? "ok $t\n" : "not ok $t\n"; $t++;
51         # check we can't see our test package:
52         print $cpt->reval('$test::foo')         ? "not ok $t\n" : "ok $t\n"; $t++;
53         print $cpt->reval('${"test::foo"}')             ? "not ok $t\n" : "ok $t\n"; $t++;
54
55         $cpt->erase;    # erase the compartment, e.g., delete all variables
56
57         print $cpt->reval('$foo') ? "not ok $t\n" : "ok $t\n"; $t++;
58
59         # Note that we *must* use $cpt->varglob here because if we used
60         # $Root::foo etc we would still see the original values!
61         # This seems to be because the compiler has created an extra ref.
62
63         print ${$cpt->varglob('foo')} ? "not ok $t\n" : "ok $t\n"; $t++;
64 }
65
66 print "ok $last_test\n";
67 BEGIN { $last_test = 28 }