Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • f2023fb
  • /
  • external
  • /
  • perl
  • /
  • Text-Template-1.56
  • /
  • t
  • /
  • safe2.t
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:cfb997bc798226a56833dce5940c1bab5d65dab5
directory badge Iframe embedding
swh:1:dir:7f3413dd469356106299d911badbf763182377dc
safe2.t
#!perl
#
# test apparatus for Text::Template module
# still incomplete.

use strict;
use warnings;
use Test::More;

unless (eval { require Safe; 1 }) {
    plan skip_all => 'Safe.pm is required for this test';
}
else {
    plan tests => 12;
}

use_ok 'Text::Template' or exit 1;

my $c = Safe->new or die;

# Test handling of packages and importing.
$c->reval('$P = "safe root"');
our $P = 'main';
$Q::P = $Q::P = 'Q';

# How to effectively test the gensymming?

my $t = Text::Template->new(
    TYPE   => 'STRING',
    SOURCE => 'package is {$P}') or die;

# (1) Default behavior: Inherit from calling package, `main' in this case.
my $text = $t->fill_in();
is $text, 'package is main';

# (2) When a package is specified, we should use that package instead.
$text = $t->fill_in(PACKAGE => 'Q');
is $text, 'package is Q';

# (3) When no package is specified in safe mode, we should use the
# default safe root.
$text = $t->fill_in(SAFE => $c);
is $text, 'package is safe root';

# (4) When a package is specified in safe mode, we should use the
# default safe root, after aliasing to the specified package
TODO: {
    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
    $text = $t->fill_in(SAFE => $c, PACKAGE => 'Q');
    is $text, 'package is Q';
}

# Now let's see if hash vars are installed properly into safe templates
$t = Text::Template->new(
    TYPE   => 'STRING',
    SOURCE => 'hash is {$H}') or die;

# (5) First in default mode
$text = $t->fill_in(HASH => { H => 'good5' });
is $text, 'hash is good5';

# suppress "once" warnings
$Q::H = $Q2::H = undef;

# (6) Now in packages
$text = $t->fill_in(HASH => { H => 'good6' }, PACKAGE => 'Q');
is $text, 'hash is good6';

# (7) Now in the default root of the safe compartment
TODO: {
    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
    $text = $t->fill_in(HASH => { H => 'good7' }, SAFE => $c);
    is $text, 'hash is good7';
}

# (8) Now in the default root after aliasing to a package that
# got the hash stuffed in
our $H;
TODO: {
    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
    $text = $t->fill_in(HASH => { H => 'good8' }, SAFE => $c, PACKAGE => 'Q2');
    is $text, 'hash is good8';
}

# Now let's make sure that none of the packages leaked on each other.
# (9) This var should NOT have been installed into the main package
ok !defined $H;
$H = $H;

# (11) this value overwrote the one from test 6.
is $Q::H, 'good7';

# (12)
is $Q2::H, 'good8';

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top