2
0

safe2.t 2.5 KB

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