Blame t/taint/basic.t

Packit Service d987f3
#!/usr/bin/perl -T
Packit Service d987f3
#
Packit Service d987f3
# Check that Term::ANSIColor untaints generated constants.
Packit Service d987f3
#
Packit Service d987f3
# It's possible that the name of the constant function that we're calling
Packit Service d987f3
# could be tained (such as by loading the name of the constant function from
Packit Service d987f3
# an environment variable).  Term::ANSIColor does the work to untaint it; be
Packit Service d987f3
# sure that the taint flag is properly cleared.
Packit Service d987f3
#
Packit Service d987f3
# Copyright 2012 Russ Allbery <rra@cpan.org>
Packit Service d987f3
#
Packit Service d987f3
# This program is free software; you may redistribute it and/or modify it
Packit Service d987f3
# under the same terms as Perl itself.
Packit Service d987f3
Packit Service d987f3
use strict;
Packit Service d987f3
use warnings;
Packit Service d987f3
Packit Service d987f3
use Test::More tests => 4;
Packit Service d987f3
Packit Service d987f3
# Load the module.
Packit Service d987f3
BEGIN {
Packit Service d987f3
    delete $ENV{ANSI_COLORS_ALIASES};
Packit Service d987f3
    delete $ENV{ANSI_COLORS_DISABLED};
Packit Service d987f3
    use_ok('Term::ANSIColor', qw(:pushpop));
Packit Service d987f3
}
Packit Service d987f3
Packit Service d987f3
# Generate a tainted constant name.  PATH is always tainted, and tainting is
Packit Service d987f3
# sticky, so we can prepend the name to whatever PATH holds and then chop it
Packit Service d987f3
# off again.
Packit Service d987f3
my $constant = substr 'BOLD' . $ENV{PATH}, 0, length 'BOLD';
Packit Service d987f3
Packit Service d987f3
# Using that as a constant should now work without any tainting problems.
Packit Service d987f3
## no critic (TestingAndDebugging::ProhibitNoStrict)
Packit Service d987f3
{
Packit Service d987f3
    no strict 'refs';
Packit Service d987f3
    is(&{$constant}(), "\e[1m", 'Constant subs are not tainted');
Packit Service d987f3
    is(BOLD(),         "\e[1m", '...and we can call the sub again');
Packit Service d987f3
    ok(defined(&Term::ANSIColor::BOLD), '...and it is now defined');
Packit Service d987f3
}