04-safe.t 4.4 KB

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