2 # tests whether tainting works with UTF-8
12 # How to identify taint when you see it
14 not eval { join("",@_), kill 0; 1 };
21 plan(tests => 3*10 + 3*8 + 2*16 + 2);
23 my $arg = $ENV{PATH}; # a tainted value
24 use constant UTF8 => "\x{1234}";
26 *is_utf8 = \&utf8::is_utf8;
28 for my $ary ([ascii => 'perl'], [latin1 => "\xB6"], [utf8 => "\x{100}"]) {
29 my $encode = $ary->[0];
30 my $string = $ary->[1];
32 my $taint = $arg; substr($taint, 0) = $ary->[1];
34 is(tainted($taint), tainted($arg), "tainted: $encode, before test");
38 is($lconcat, $string.UTF8, "compare: $encode, concat left");
40 is(tainted($lconcat), tainted($arg), "tainted: $encode, concat left");
44 is($rconcat, UTF8.$string, "compare: $encode, concat right");
46 is(tainted($rconcat), tainted($arg), "tainted: $encode, concat right");
48 my $ljoin = join('!', $taint, UTF8);
49 is($ljoin, join('!', $string, UTF8), "compare: $encode, join left");
51 is(tainted($ljoin), tainted($arg), "tainted: $encode, join left");
53 my $rjoin = join('!', UTF8, $taint);
54 is($rjoin, join('!', UTF8, $string), "compare: $encode, join right");
56 is(tainted($rjoin), tainted($arg), "tainted: $encode, join right");
58 is(tainted($taint), tainted($arg), "tainted: $encode, after test");
62 for my $ary ([ascii => 'perl'], [latin1 => "\xB6"], [utf8 => "\x{100}"]) {
63 my $encode = $ary->[0];
65 my $utf8 = pack('U*') . $ary->[1];
66 my $byte = unpack('U0a*', $utf8);
68 my $taint = $arg; substr($taint, 0) = $utf8;
71 is($taint, $byte, "compare: $encode, encode utf8");
73 is(pack('a*',$taint), pack('a*',$byte), "bytecmp: $encode, encode utf8");
75 ok(!is_utf8($taint), "is_utf8: $encode, encode utf8");
77 is(tainted($taint), tainted($arg), "tainted: $encode, encode utf8");
79 my $taint = $arg; substr($taint, 0) = $byte;
82 is($taint, $utf8, "compare: $encode, decode byte");
84 is(pack('a*',$taint), pack('a*',$utf8), "bytecmp: $encode, decode byte");
86 is(is_utf8($taint), ($encode ne 'ascii'), "is_utf8: $encode, decode byte");
88 is(tainted($taint), tainted($arg), "tainted: $encode, decode byte");
92 for my $ary ([ascii => 'perl'], [latin1 => "\xB6"]) {
93 my $encode = $ary->[0];
95 my $up = pack('U*') . $ary->[1];
96 my $down = pack("a*", $ary->[1]);
98 my $taint = $arg; substr($taint, 0) = $up;
99 utf8::upgrade($taint);
101 is($taint, $up, "compare: $encode, upgrade up");
103 is(pack('a*',$taint), pack('a*',$up), "bytecmp: $encode, upgrade up");
105 ok(is_utf8($taint), "is_utf8: $encode, upgrade up");
107 is(tainted($taint), tainted($arg), "tainted: $encode, upgrade up");
109 my $taint = $arg; substr($taint, 0) = $down;
110 utf8::upgrade($taint);
112 is($taint, $up, "compare: $encode, upgrade down");
114 is(pack('a*',$taint), pack('a*',$up), "bytecmp: $encode, upgrade down");
116 ok(is_utf8($taint), "is_utf8: $encode, upgrade down");
118 is(tainted($taint), tainted($arg), "tainted: $encode, upgrade down");
120 my $taint = $arg; substr($taint, 0) = $up;
121 utf8::downgrade($taint);
123 is($taint, $down, "compare: $encode, downgrade up");
125 is(pack('a*',$taint), pack('a*',$down), "bytecmp: $encode, downgrade up");
127 ok(!is_utf8($taint), "is_utf8: $encode, downgrade up");
129 is(tainted($taint), tainted($arg), "tainted: $encode, downgrade up");
131 my $taint = $arg; substr($taint, 0) = $down;
132 utf8::downgrade($taint);
134 is($taint, $down, "compare: $encode, downgrade down");
136 is(pack('a*',$taint), pack('a*',$down), "bytecmp: $encode, downgrade down");
138 ok(!is_utf8($taint), "is_utf8: $encode, downgrade down");
140 is(tainted($taint), tainted($arg), "tainted: $encode, downgrade down");
144 fresh_perl_is('$a = substr $^X, 0, 0; /\x{100}/i; /$a\x{100}/i || print q,ok,',
145 'ok', {switches => ["-T", "-l"]},
146 "matching a regexp is taint agnostic");
148 fresh_perl_is('$a = substr $^X, 0, 0; /$a\x{100}/i || print q,ok,',
149 'ok', {switches => ["-T", "-l"]},
150 "therefore swash_init should be taint agnostic");