xref: /freebsd-src/crypto/openssl/external/perl/Text-Template-1.56/t/safe.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#!perl
2*e0c4386eSCy Schubert#
3*e0c4386eSCy Schubert# test apparatus for Text::Template module
4*e0c4386eSCy Schubert# still incomplete.
5*e0c4386eSCy Schubert
6*e0c4386eSCy Schubertuse strict;
7*e0c4386eSCy Schubertuse warnings;
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubertuse Test::More;
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubertunless (eval { require Safe; 1 }) {
12*e0c4386eSCy Schubert    plan skip_all => 'Safe.pm is required for this test';
13*e0c4386eSCy Schubert}
14*e0c4386eSCy Schubertelse {
15*e0c4386eSCy Schubert    plan tests => 20;
16*e0c4386eSCy Schubert}
17*e0c4386eSCy Schubert
18*e0c4386eSCy Schubertuse_ok 'Text::Template' or exit 1;
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubertmy ($BADOP, $FAILURE);
21*e0c4386eSCy Schubertif ($^O eq 'MacOS') {
22*e0c4386eSCy Schubert    $BADOP   = qq{};
23*e0c4386eSCy Schubert    $FAILURE = q{};
24*e0c4386eSCy Schubert}
25*e0c4386eSCy Schubertelse {
26*e0c4386eSCy Schubert    $BADOP   = qq{kill 0};
27*e0c4386eSCy Schubert    $FAILURE = q{Program fragment at line 1 delivered error ``kill trapped by operation mask''};
28*e0c4386eSCy Schubert}
29*e0c4386eSCy Schubert
30*e0c4386eSCy Schubertour $v = 119;
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubertmy $c = Safe->new or die;
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubertmy $goodtemplate = q{This should succeed: { $v }};
35*e0c4386eSCy Schubertmy $goodoutput   = q{This should succeed: 119};
36*e0c4386eSCy Schubert
37*e0c4386eSCy Schubertmy $template1 = Text::Template->new(type => 'STRING', source => $goodtemplate);
38*e0c4386eSCy Schubertmy $template2 = Text::Template->new(type => 'STRING', source => $goodtemplate);
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubertmy $text1 = $template1->fill_in();
41*e0c4386eSCy Schubertok defined $text1;
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubertmy $text2 = $template1->fill_in(SAFE => $c);
44*e0c4386eSCy Schubertok defined $text2;
45*e0c4386eSCy Schubert
46*e0c4386eSCy Schubertmy $text3 = $template2->fill_in(SAFE => $c);
47*e0c4386eSCy Schubertok defined $text3;
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert# (4) Safe and non-safe fills of different template objects with the
50*e0c4386eSCy Schubert# same template text should yield the same result.
51*e0c4386eSCy Schubert# print +($text1 eq $text3 ? '' : 'not '), "ok $n\n";
52*e0c4386eSCy Schubert# (4) voided this test:  it's not true, because the unsafe fill
53*e0c4386eSCy Schubert# uses package main, while the safe fill uses the secret safe package.
54*e0c4386eSCy Schubert# We could alias the secret safe package to be identical to main,
55*e0c4386eSCy Schubert# but that wouldn't be safe.  If you want the aliasing, you have to
56*e0c4386eSCy Schubert# request it explicitly with `PACKAGE'.
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubert# (5) Safe and non-safe fills of the same template object
59*e0c4386eSCy Schubert# should yield the same result.
60*e0c4386eSCy Schubert# (5) voided this test for the same reason as #4.
61*e0c4386eSCy Schubert# print +($text1 eq $text2 ? '' : 'not '), "ok $n\n";
62*e0c4386eSCy Schubert
63*e0c4386eSCy Schubert# (6) Make sure the output was actually correct
64*e0c4386eSCy Schubertis $text1, $goodoutput;
65*e0c4386eSCy Schubert
66*e0c4386eSCy Schubertmy $badtemplate     = qq{This should fail: { $BADOP; 'NOFAIL' }};
67*e0c4386eSCy Schubertmy $badnosafeoutput = q{This should fail: NOFAIL};
68*e0c4386eSCy Schubertmy $badsafeoutput =
69*e0c4386eSCy Schubert    q{This should fail: Program fragment delivered error ``kill trapped by operation mask at template line 1.''};
70*e0c4386eSCy Schubert
71*e0c4386eSCy Schubert$template1 = Text::Template->new('type' => 'STRING', 'source' => $badtemplate);
72*e0c4386eSCy Schubertisa_ok $template1, 'Text::Template';
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert$template2 = Text::Template->new('type' => 'STRING', 'source' => $badtemplate);
75*e0c4386eSCy Schubertisa_ok $template2, 'Text::Template';
76*e0c4386eSCy Schubert
77*e0c4386eSCy Schubert# none of these should fail
78*e0c4386eSCy Schubert$text1 = $template1->fill_in();
79*e0c4386eSCy Schubertok defined $text1;
80*e0c4386eSCy Schubert
81*e0c4386eSCy Schubert$text2 = $template1->fill_in(SAFE => $c);
82*e0c4386eSCy Schubertok defined $text2;
83*e0c4386eSCy Schubert
84*e0c4386eSCy Schubert$text3 = $template2->fill_in(SAFE => $c);
85*e0c4386eSCy Schubertok defined $text3;
86*e0c4386eSCy Schubert
87*e0c4386eSCy Schubertmy $text4 = $template1->fill_in();
88*e0c4386eSCy Schubertok defined $text4;
89*e0c4386eSCy Schubert
90*e0c4386eSCy Schubert# (11) text1 and text4 should be the same (using safe in between
91*e0c4386eSCy Schubert# didn't change anything.)
92*e0c4386eSCy Schubertis $text1, $text4;
93*e0c4386eSCy Schubert
94*e0c4386eSCy Schubert# (12) text2 and text3 should be the same (same template text in different
95*e0c4386eSCy Schubert# objects
96*e0c4386eSCy Schubertis $text2, $text3;
97*e0c4386eSCy Schubert
98*e0c4386eSCy Schubert# (13) text1 should yield badnosafeoutput
99*e0c4386eSCy Schubertis $text1, $badnosafeoutput;
100*e0c4386eSCy Schubert
101*e0c4386eSCy Schubert# (14) text2 should yield badsafeoutput
102*e0c4386eSCy Schubert$text2 =~ s/'kill'/kill/;    # 5.8.1 added quote marks around the op name
103*e0c4386eSCy Schubertis $text2, $badsafeoutput;
104*e0c4386eSCy Schubert
105*e0c4386eSCy Schubertmy $template = q{{$x=1}{$x+1}};
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert$template1 = Text::Template->new('type' => 'STRING', 'source' => $template);
108*e0c4386eSCy Schubertisa_ok $template1, 'Text::Template';
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert$template2 = Text::Template->new('type' => 'STRING', 'source' => $template);
111*e0c4386eSCy Schubertisa_ok $template2, 'Text::Template';
112*e0c4386eSCy Schubert
113*e0c4386eSCy Schubert$text1 = $template1->fill_in();
114*e0c4386eSCy Schubert$text2 = $template1->fill_in(SAFE => Safe->new);
115*e0c4386eSCy Schubert
116*e0c4386eSCy Schubert# (15) Do effects persist in safe compartments?
117*e0c4386eSCy Schubertis $text1, $text2;
118*e0c4386eSCy Schubert
119*e0c4386eSCy Schubert# (16) Try the BROKEN routine in safe compartments
120*e0c4386eSCy Schubertsub my_broken {
121*e0c4386eSCy Schubert    my %a = @_;
122*e0c4386eSCy Schubert    $a{error} =~ s/ at.*//s;
123*e0c4386eSCy Schubert    "OK! text:$a{text} error:$a{error} lineno:$a{lineno} arg:$a{arg}";
124*e0c4386eSCy Schubert}
125*e0c4386eSCy Schubert
126*e0c4386eSCy Schubertmy $templateB = Text::Template->new(TYPE => 'STRING', SOURCE => '{die}');
127*e0c4386eSCy Schubertisa_ok $templateB, 'Text::Template';
128*e0c4386eSCy Schubert
129*e0c4386eSCy Schubert$text1 = $templateB->fill_in(
130*e0c4386eSCy Schubert    BROKEN     => \&my_broken,
131*e0c4386eSCy Schubert    BROKEN_ARG => 'barg',
132*e0c4386eSCy Schubert    SAFE       => Safe->new);
133*e0c4386eSCy Schubert
134*e0c4386eSCy Schubertmy $result1 = qq{OK! text:die error:Died lineno:1 arg:barg};
135*e0c4386eSCy Schubertis $text1, $result1;
136