File: | t/Scrubber.t |
Coverage: | 92.3% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | #!/usr/bin/perl | |||||
2 | ||||||
3 | 1 1 1 | 1.33283540903592e+15 75 88 | use strict; | |||
4 | 1 1 1 | 59 51 84 | use warnings; | |||
5 | ||||||
6 | 1 1 1 | 406 18539 52 | use Test::More tests => 19; | |||
7 | BEGIN { | |||||
8 | 1 1 1 | 2061 1189 43 | use FindBin; | |||
9 | 1 1 1 | 138 512 34 | use lib $FindBin::Bin; | |||
10 | 1 | 120 | use_ok('C4::Scrubber'); | |||
11 | } | |||||
12 | ||||||
13 | sub pretty_line { | |||||
14 | 12 | 99 | my $max = 54; | |||
15 | 12 | 6248 | (@_) or return "#" x $max . "\n"; | |||
16 | 6 | 65 | my $phrase = " " . shift() . " "; | |||
17 | 6 | 65 | my $half = "#" x (($max - length($phrase))/2); | |||
18 | 6 | 68 | return $half . $phrase . $half . "\n"; | |||
19 | } | |||||
20 | ||||||
21 | 1 | 358772 | my ($scrubber,$html,$result,@types,$collapse); | |||
22 | 1 | 39 | $collapse = 1; | |||
23 | 1 | 33 | @types = qw(default comment tag staff); | |||
24 | 1 | 51 | $html = q| | |||
25 | <![CDATA[selfdestruct]]]> | |||||
26 | <?php echo(" EVIL EVIL EVIL "); ?> <!-- COMMENT --> | |||||
27 | <hr> <!-- TMPL_VAR NAME="password" --> | |||||
28 | <style type="text/css">body{display:none;}</style> | |||||
29 | <link media="screen" type="text/css" rev="stylesheet" rel="stylesheet" href="css.css"> | |||||
30 | <I FAKE="attribute" > I am ITALICS with fake="attribute" </I><br /> | |||||
31 | <em FAKE="attribute" > I am em with fake="attribute" </em><br /> | |||||
32 | <B> I am BOLD </B><br /> | |||||
33 | <span style="background-image: url(http://hackersite.cn/porno.jpg);"> I am a span w/ style. Bad style.</span> | |||||
34 | <span> I am a span trying to inject a link: <a href="badlink.html"> link </a></span> | |||||
35 | <br> | |||||
36 | <A NAME="evil"> | |||||
37 | <A HREF="javascript:alert('OMG YOO R HACKED');">I am a link firing javascript.</A> | |||||
38 | <br /> | |||||
39 | <A HREF="image/bigone.jpg" ONMOUSEOVER="alert('OMG YOO R HACKED');"> | |||||
40 | <IMG SRC="image/smallone.jpg" ALT="ONMOUSEOVER JAVASCRIPT"> | |||||
41 | </A> | |||||
42 | </A> <br> | |||||
43 | At the end here, I actually have some regular text. | |||||
44 | |; | |||||
45 | ||||||
46 | 1 | 28 | print pretty_line("Original HTML:"), $html, "\n", pretty_line(); | |||
47 | 1 | 68 | $collapse and diag "Note: scrubber test output will have whitespace collapsed for readability\n"; | |||
48 | 1 | 508 | ok($scrubber = C4::Scrubber->new(), "Constructor: C4::Scrubber->new()"); | |||
49 | ||||||
50 | 1 | 649 | isa_ok($scrubber, 'HTML::Scrubber', 'Constructor returns HTML::Scrubber object'); | |||
51 | ||||||
52 | 1 | 524 | ok(printf("# scrubber settings: default %s, comment %s, process %s\n", | |||
53 | $scrubber->default(),$scrubber->comment(),$scrubber->process()), | |||||
54 | "Outputting settings from scrubber object (type: [default])" | |||||
55 | ); | |||||
56 | 1 | 642 | ok($result = $scrubber->scrub($html), "Getting scrubbed text (type: [default])"); | |||
57 | 1 | 857 | $collapse and $result =~ s/\s*\n\s*/\n/g; | |||
58 | 1 | 4 | print pretty_line('default'), $result, "\n", pretty_line(); | |||
59 | ||||||
60 | 1 | 9 | foreach(@types) { | |||
61 | 4 | 111 | ok($scrubber = C4::Scrubber->new($_), "testing Constructor: C4::Scrubber->new($_)"); | |||
62 | 4 | 2128 | ok(printf("# scrubber settings: default %s, comment %s, process %s\n", | |||
63 | $scrubber->default(),$scrubber->comment(),$scrubber->process()), | |||||
64 | "Outputting settings from scrubber object (type: $_)" | |||||
65 | ); | |||||
66 | 4 | 2590 | ok($result = $scrubber->scrub($html), "Getting scrubbed text (type: $_)"); | |||
67 | 4 | 4510 | $collapse and $result =~ s/\s*\n\s*/\n/g; | |||
68 | 4 | 47 | print pretty_line($_), $result, "\n", pretty_line(); | |||
69 | } | |||||
70 | ||||||
71 | 1 | 11 | print "\n\n######################################################\nStart of invalid tests\n"; | |||
72 | ||||||
73 | #Test for invalid new entry | |||||
74 | 1 | 2 | eval{ | |||
75 | 1 | 16 | C4::Scrubber->new(""); | |||
76 | 0 | 0 | fail("test should fail on entry of ''\n"); | |||
77 | }; | |||||
78 | 1 | 2363 | pass("Test should have failed on entry of '' (empty string) and it did. YAY!\n"); | |||
79 | ||||||
80 | 1 | 323 | eval{ | |||
81 | 1 | 8 | C4::Scrubber->new("Client"); | |||
82 | 0 | 0 | fail("test should fail on entry of 'Client'\n"); | |||
83 | }; | |||||
84 | 1 | 143 | pass("Test should have failed on entry of 'Client' and it did. YAY!\n"); | |||
85 | ||||||
86 | 1 | 259 | print "######################################################\n"; | |||
87 | ||||||
88 | 1 | 8 | diag "done.\n"; |