xref: /freebsd/crypto/openssl/external/perl/Text-Template-1.56/t/safe2.t (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
1#!perl
2#
3# test apparatus for Text::Template module
4# still incomplete.
5
6use strict;
7use warnings;
8use Test::More;
9
10unless (eval { require Safe; 1 }) {
11    plan skip_all => 'Safe.pm is required for this test';
12}
13else {
14    plan tests => 12;
15}
16
17use_ok 'Text::Template' or exit 1;
18
19my $c = Safe->new or die;
20
21# Test handling of packages and importing.
22$c->reval('$P = "safe root"');
23our $P = 'main';
24$Q::P = $Q::P = 'Q';
25
26# How to effectively test the gensymming?
27
28my $t = Text::Template->new(
29    TYPE   => 'STRING',
30    SOURCE => 'package is {$P}') or die;
31
32# (1) Default behavior: Inherit from calling package, `main' in this case.
33my $text = $t->fill_in();
34is $text, 'package is main';
35
36# (2) When a package is specified, we should use that package instead.
37$text = $t->fill_in(PACKAGE => 'Q');
38is $text, 'package is Q';
39
40# (3) When no package is specified in safe mode, we should use the
41# default safe root.
42$text = $t->fill_in(SAFE => $c);
43is $text, 'package is safe root';
44
45# (4) When a package is specified in safe mode, we should use the
46# default safe root, after aliasing to the specified package
47TODO: {
48    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
49    $text = $t->fill_in(SAFE => $c, PACKAGE => 'Q');
50    is $text, 'package is Q';
51}
52
53# Now let's see if hash vars are installed properly into safe templates
54$t = Text::Template->new(
55    TYPE   => 'STRING',
56    SOURCE => 'hash is {$H}') or die;
57
58# (5) First in default mode
59$text = $t->fill_in(HASH => { H => 'good5' });
60is $text, 'hash is good5';
61
62# suppress "once" warnings
63$Q::H = $Q2::H = undef;
64
65# (6) Now in packages
66$text = $t->fill_in(HASH => { H => 'good6' }, PACKAGE => 'Q');
67is $text, 'hash is good6';
68
69# (7) Now in the default root of the safe compartment
70TODO: {
71    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
72    $text = $t->fill_in(HASH => { H => 'good7' }, SAFE => $c);
73    is $text, 'hash is good7';
74}
75
76# (8) Now in the default root after aliasing to a package that
77# got the hash stuffed in
78our $H;
79TODO: {
80    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
81    $text = $t->fill_in(HASH => { H => 'good8' }, SAFE => $c, PACKAGE => 'Q2');
82    is $text, 'hash is good8';
83}
84
85# Now let's make sure that none of the packages leaked on each other.
86# (9) This var should NOT have been installed into the main package
87ok !defined $H;
88$H = $H;
89
90# (11) this value overwrote the one from test 6.
91is $Q::H, 'good7';
92
93# (12)
94is $Q2::H, 'good8';
95