Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package sanitizehtml | |
| 6 | |
| 7 import ( | |
| 8 "bytes" | |
| 9 "strings" | |
| 10 "testing" | |
| 11 | |
| 12 . "github.com/smartystreets/goconvey/convey" | |
| 13 ) | |
| 14 | |
| 15 func TestSanitize(t *testing.T) { | |
| 16 t.Parallel() | |
| 17 | |
| 18 cases := []struct{ in, out string }{ | |
|
hinoka
2017/05/02 00:36:16
Some error test cases would be nice. eg invalid h
nodir
2017/05/04 22:11:01
Done.
| |
| 19 // Scripts | |
| 20 { | |
| 21 `<script src="evil.js"/>`, | |
| 22 ``, | |
| 23 }, | |
| 24 | |
| 25 // Paragraphs | |
| 26 { | |
| 27 `<p style="font-size: 100">hi</p>`, | |
| 28 `<p>hi</p>`, | |
| 29 }, | |
| 30 { | |
| 31 `<P>hi</P>`, | |
| 32 `<p>hi</p>`, | |
| 33 }, | |
| 34 { | |
| 35 `a<br>b`, | |
| 36 `a<br>b`, | |
| 37 }, | |
| 38 | |
| 39 // Lists | |
| 40 { | |
| 41 `<ul foo="bar"> | |
| 42 <li x="y">a</li> | |
| 43 <li>a</li> | |
| 44 </ul>`, | |
| 45 `<ul> | |
| 46 <li>a</li> | |
| 47 <li>a</li> | |
| 48 </ul>`, | |
| 49 }, | |
| 50 | |
| 51 // Links | |
|
Vadim Sh.
2017/05/02 00:16:24
add a test for a relative URL
nodir
2017/05/04 22:11:01
Done.
| |
| 52 { | |
| 53 `<a href="https://ci.chromium.org" alt="x">link</a>`, | |
| 54 `<a href="https://ci.chromium.org" alt="x">link</a>`, | |
| 55 }, | |
| 56 { | |
| 57 `<a href="javascript:evil.js">link</a>`, | |
| 58 `<a href="#non-http-or-http-url-stripped">link</a>`, | |
| 59 }, | |
| 60 { | |
| 61 `<a href="about:blank">link</a>`, | |
| 62 `<a href="#non-http-or-http-url-stripped">link</a>`, | |
| 63 }, | |
| 64 { | |
| 65 `<a href="about:blank">link</a>`, | |
|
Vadim Sh.
2017/05/02 00:16:24
duplication
nodir
2017/05/04 22:11:01
Done.
| |
| 66 `<a href="#non-http-or-http-url-stripped">link</a>`, | |
| 67 }, | |
| 68 { | |
| 69 `<a href="%">link</a>`, | |
| 70 `<a href="#invalid-url-stripped">link</a>`, | |
| 71 }, | |
| 72 | |
| 73 // Tables | |
| 74 { | |
| 75 `<table> | |
| 76 <tr colspan="2x"> | |
| 77 <td rowspan=2>a</td> | |
| 78 </tr> | |
| 79 <tr style=""> | |
| 80 <td>b</td> | |
| 81 <td>c</td> | |
| 82 </tr> | |
| 83 </table>`, | |
| 84 `<table> | |
| 85 <tr colspan="2"> | |
| 86 <td rowspan="2">a</td> | |
| 87 </tr> | |
| 88 <tr> | |
| 89 <td>b</td> | |
| 90 <td>c</td> | |
| 91 </tr> | |
| 92 </table>`, | |
| 93 }, | |
| 94 | |
| 95 // Other | |
| 96 { | |
| 97 `<div><strong>hello</strong></div>`, | |
| 98 `<strong>hello</strong>`, | |
| 99 }, | |
| 100 { | |
| 101 `<`, | |
| 102 `<`, | |
| 103 }, | |
| 104 { | |
| 105 `&foobar;`, | |
| 106 `&foobar;`, | |
| 107 }, | |
| 108 } | |
| 109 | |
| 110 for _, c := range cases { | |
|
Vadim Sh.
2017/05/02 00:16:24
add a test for various malformed HTML, e.g.
<p><a
nodir
2017/05/04 22:11:01
Done.
| |
| 111 c := c | |
| 112 Convey(c.in, t, func() { | |
| 113 buf := &bytes.Buffer{} | |
| 114 err := Sanitize(strings.NewReader(c.in), buf) | |
| 115 So(err, ShouldBeNil) | |
| 116 So(buf.String(), ShouldEqual, c.out) | |
| 117 }) | |
| 118 } | |
| 119 } | |
| OLD | NEW |