source: code/trunk/morty_test.go@ 59

Last change on this file since 59 was 55, checked in by asciimoo, 9 years ago

[enh] add anchor url parse test

File size: 3.1 KB
Line 
1package main
2
3import (
4 "bytes"
5 "net/url"
6 "testing"
7)
8
9type AttrTestCase struct {
10 AttrName []byte
11 AttrValue []byte
12 ExpectedOutput []byte
13}
14
15type StringTestCase struct {
16 Input string
17 ExpectedOutput string
18}
19
20var attrTestData []*AttrTestCase = []*AttrTestCase{
21 &AttrTestCase{
22 []byte("href"),
23 []byte("./x"),
24 []byte(` href="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fx"`),
25 },
26 &AttrTestCase{
27 []byte("src"),
28 []byte("http://x.com/y"),
29 []byte(` src="./?mortyurl=http%3A%2F%2Fx.com%2Fy"`),
30 },
31 &AttrTestCase{
32 []byte("action"),
33 []byte("/z"),
34 []byte(` action="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fz"`),
35 },
36 &AttrTestCase{
37 []byte("onclick"),
38 []byte("console.log(document.cookies)"),
39 nil,
40 },
41}
42
43var urlTestData []*StringTestCase = []*StringTestCase{
44 &StringTestCase{
45 "http://x.com/",
46 "./?mortyurl=http%3A%2F%2Fx.com%2F",
47 },
48 &StringTestCase{
49 "http://a@x.com/",
50 "./?mortyurl=http%3A%2F%2Fa%40x.com%2F",
51 },
52 &StringTestCase{
53 "#a",
54 "#a",
55 },
56}
57
58func TestAttrSanitizer(t *testing.T) {
59 u, _ := url.Parse("http://127.0.0.1/")
60 rc := &RequestConfig{BaseURL: u}
61 for _, testCase := range attrTestData {
62 out := bytes.NewBuffer(nil)
63 sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue, testCase.AttrValue)
64 res, _ := out.ReadBytes(byte(0))
65 if !bytes.Equal(res, testCase.ExpectedOutput) {
66 t.Errorf(
67 `Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: "%s"`,
68 testCase.AttrName,
69 testCase.AttrValue,
70 testCase.ExpectedOutput,
71 res,
72 )
73 }
74 }
75}
76
77func TestURLProxifier(t *testing.T) {
78 u, _ := url.Parse("http://127.0.0.1/")
79 rc := &RequestConfig{BaseURL: u}
80 for _, testCase := range urlTestData {
81 newUrl, err := rc.ProxifyURI(testCase.Input)
82 if err != nil {
83 t.Errorf("Failed to parse URL: %s", testCase.Input)
84 }
85 if newUrl != testCase.ExpectedOutput {
86 t.Errorf(
87 `URL proxifier error. Expected: "%s", Got: "%s"`,
88 testCase.ExpectedOutput,
89 newUrl,
90 )
91 }
92 }
93}
94
95var BENCH_SIMPLE_HTML []byte = []byte(`<!doctype html>
96<html>
97 <head>
98 <title>test</title>
99 </head>
100 <body>
101 <h1>Test heading</h1>
102 </body>
103</html>`)
104
105func BenchmarkSanitizeSimpleHTML(b *testing.B) {
106 u, _ := url.Parse("http://127.0.0.1/")
107 rc := &RequestConfig{BaseURL: u}
108 b.ResetTimer()
109 for i := 0; i < b.N; i++ {
110 out := bytes.NewBuffer(nil)
111 sanitizeHTML(rc, out, BENCH_SIMPLE_HTML)
112 }
113}
114
115var BENCH_COMPLEX_HTML []byte = []byte(`<!doctype html>
116<html>
117 <head>
118 <noscript><meta http-equiv="refresh" content="0; URL=./xy"></noscript>
119 <title>test 2</title>
120 <script> alert('xy'); </script>
121 <link rel="stylesheet" href="./core.bundle.css">
122 <style>
123 html { background: url(./a.jpg); }
124 </style
125 </head>
126 <body>
127 <h1>Test heading</h1>
128 <img src="b.png" alt="imgtitle" />
129 <form action="/z">
130 <input type="submit" style="background: url(http://aa.bb/cc)" >
131 </form>
132 </body>
133</html>`)
134
135func BenchmarkSanitizeComplexHTML(b *testing.B) {
136 u, _ := url.Parse("http://127.0.0.1/")
137 rc := &RequestConfig{BaseURL: u}
138 b.ResetTimer()
139 for i := 0; i < b.N; i++ {
140 out := bytes.NewBuffer(nil)
141 sanitizeHTML(rc, out, BENCH_COMPLEX_HTML)
142 }
143}
Note: See TracBrowser for help on using the repository browser.