6805d48abf980edc4802f4808904e71ddaca1424
[urisagit/Perl-Docs.git] / t / tainted.t
1 #!perl -T
2
3 use strict;
4 use Test::More;
5 use File::Slurp;
6
7 plan 'skip_all', "Scalar::Util not available" unless
8         eval 'use Scalar::Util qw(tainted) ; tainted($0) ; 1';
9
10 plan 'tests', 5;
11
12 my $path = "data.txt";
13 my $data = "random junk\nline2";
14
15 SKIP: {
16     # write something to that file
17     open(FILE, ">$path") or skip 4, "can't write to '$path': $!";
18     print FILE $data;
19     close(FILE);
20
21     # read the file using File::Slurp in scalar context
22     my $content = eval { read_file($path) };
23     is( $@, '', "read_file() in scalar context" );
24     ok( tainted($content), "  => returned content should be tainted" );
25
26
27 #         # reconstruct the full lines by merging items by pairs
28 #         for my $k (0..int($#lines/2)) {
29 #             my $i = $k * 2;
30 #             $lines[$k] = (defined $lines[$i]   ? $lines[$i]   : '') 
31 #                        . (defined $lines[$i+1] ? $lines[$i+1] : '');
32 #         }
33
34 #         # remove the rest of the items
35 #         splice(@lines, int($#lines/2)+1);
36 #         pop @lines unless $lines[-1];
37
38 #       $_ .= $/ for @lines ;
39
40 #         my @lines = split m{$/}, $content, -1;
41 #       my @parts = split m{($/)}, $content, -1;
42
43 # #         my @parts = $content =~ m{.+?(?:$/)?}g ;
44
45 # my @lines ;
46 #       while( @parts > 2 ) {
47
48 #               my( $line, $sep ) = splice( @parts, 0, 2 ) ;
49 #               push @lines, "$line$sep" ;
50 #       }
51
52 #       push @lines, shift @parts if @parts ;
53
54 # #    ok( tainted($lines[0]), "  text => returned content should be tainted" );
55
56     # read the file using File::Slurp in list context
57    my @content = eval { read_file($path) };
58    is( $@, '', "read_file() in list context" );
59    ok( tainted($content[0]), "  => returned content should be tainted" );
60
61         my $text = join( '', @content ) ;
62
63         is( $text, $content, "list eq scalar" );
64
65
66 #    ok( tainted($lines[0]), "  => returned content should be tainted" );
67 }
68
69 unlink $path;