File: basic.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (40 lines) | stat: -rw-r--r-- 1,312 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/perl -T
#
# Check that Term::ANSIColor untaints generated constants.
#
# It's possible that the name of the constant function that we're calling
# could be tained (such as by loading the name of the constant function from
# an environment variable).  Term::ANSIColor does the work to untaint it; be
# sure that the taint flag is properly cleared.
#
# Copyright 2012, 2020 Russ Allbery <rra@cpan.org>
#
# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl

use 5.008;
use strict;
use warnings;

use Test::More tests => 4;

# Load the module.
BEGIN {
    delete $ENV{ANSI_COLORS_ALIASES};
    delete $ENV{ANSI_COLORS_DISABLED};
    delete $ENV{NO_COLOR};
    use_ok('Term::ANSIColor', qw(:pushpop));
}

# Generate a tainted constant name.  PATH is always tainted, and tainting is
# sticky, so we can prepend the name to whatever PATH holds and then chop it
# off again.
my $constant = substr('BOLD' . $ENV{PATH}, 0, length('BOLD'));

# Using that as a constant should now work without any tainting problems.
## no critic (TestingAndDebugging::ProhibitNoStrict)
{
    no strict 'refs';
    is(&{$constant}(), "\e[1m", 'Constant subs are not tainted');
    is(BOLD(),         "\e[1m", '...and we can call the sub again');
    ok(defined(&Term::ANSIColor::BOLD), '...and it is now defined');
}