-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
195 lines (185 loc) · 4.87 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package markdown
import (
"testing"
)
func TestUnescapeAll(t *testing.T) {
type testCase struct {
in string
want string
}
testCases := []testCase{
{"", ""},
{`foo \`, `foo \`},
{`\f\o\o`, `\f\o\o`},
{`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`, "!\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~"},
{`\foo b\#a\r`, `\foo b#a\r`},
{"&", "&"},
{`\&`, "&"},
{""&"", `"&"`},
{"≪⃒≫⃒", "\u226a\u20d2\u226b\u20d2"},
}
for _, tc := range testCases {
got := unescapeAll(tc.in)
if got != tc.want {
t.Errorf("unescapeAll(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestNormalizeInlineCode(t *testing.T) {
type testCase struct {
in string
want string
}
testCases := []testCase{
{"", ""},
{" ", ""},
{"\n", ""},
{"\n\n", ""},
{"\nfoo\n", "foo"},
{"foo bar", "foo bar"},
{"foo\nbar", "foo bar"},
{"foo\n\nbar", "foo bar"},
{"foo bar", "foo bar"},
{"foo \nbar", "foo bar"},
{" foo ", "foo"},
{" foo bar\n baz\n foo\n\n", "foo bar baz foo"},
}
for _, tc := range testCases {
got := normalizeInlineCode(tc.in)
if got != tc.want {
t.Errorf("normalizeInlineCode(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestValidateLink(t *testing.T) {
type testCase struct {
in string
want bool
}
testCases := []testCase{
{"", true},
{"http://google.com", true},
{"javascript:alert(1)", false},
{"Javascript:alert(1)", false},
{" javascript:alert(1)", false},
{"\x09javascript:alert(1)", false},
{"\x00javascript:alert(1)", false},
// {"	javascript:alert(1)", false},
{"javascript:alert(1)", true},
// {"javascript:alert(1)", false},
// {"Javascript:alert(1)", false},
{"&#x6a;avascript:alert(1)", true},
// {"javascript:alert(1)", false},
{"vbscript:alert(1)", false},
{"file:///home", false},
{"data:text/html;base64,", false},
{"data:image/jpeg;base64,", true},
{"data:image/gif;base64,", true},
{"data:image/png;base64,", true},
{"data:image/webp;base64,", true},
}
for _, tc := range testCases {
got := validateLink(tc.in)
if got != tc.want {
t.Errorf("validateLink(%q) = %v, want %v", tc.in, got, tc.want)
}
}
}
func TestIsMarkdownPunct(t *testing.T) {
type testCase struct {
in string
want bool
}
testCases := []testCase{
{"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", true},
{" \x7f0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZабвгдеёжзийклмнопрстуфхцчшщьыъэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЫЪЭЮЯ", false},
}
for _, tc := range testCases {
for _, r := range tc.in {
got := isMdAsciiPunct(r)
if got != tc.want {
t.Errorf("isMarkdownPunct(%c) = %v, want %v", r, got, tc.want)
}
}
}
}
func TestNormalizeLink(t *testing.T) {
type testCase struct {
in string
want string
}
testCases := []testCase{
{"", ""},
// {"http://google.com/s%65arch", "http://google.com/search"},
{"http://google.com/search?query=%5B%5D", "http://google.com/search?query=%5B%5D"},
{"//google.com", "//google.com"},
{"http://%XX", "http://%25XX"},
}
for _, tc := range testCases {
got := normalizeLink(tc.in)
if got != tc.want {
t.Errorf("normalizeLink(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestNormalizeLinkText(t *testing.T) {
type testCase struct {
in string
want string
}
testCases := []testCase{
{"", ""},
{"http://google.com/s%65arch", "http://google.com/search"},
{"http://google.com/search%XX", "http://google.com/search%XX"},
// {"http://google.com/search%80", "http://google.com/search%80"},
}
for _, tc := range testCases {
got := normalizeLinkText(tc.in)
if got != tc.want {
t.Errorf("normalizeLinkText(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestNormalizeReference(t *testing.T) {
type testCase struct {
in string
want string
}
testCases := []testCase{
{"", ""},
{" r e f ", "r e f"},
{" ref ", "ref"},
{" ref ", "ref"},
{"ref", "ref"},
{"REF", "ref"},
{"r\u00a0e\u00a0f", "r e f"},
}
for _, tc := range testCases {
got := normalizeReference(tc.in)
if got != tc.want {
t.Errorf("normalizeReference(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestRemoveSpecial(t *testing.T) {
type testCase struct {
in string
want string
}
testCases := []testCase{
{"", ""},
{"javascript", "javascript"},
{"javascript\x00", "javascript"},
{"\x00javascript", "javascript"},
{"\x00java\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x20script\x7f", "javascript"},
}
for _, tc := range testCases {
got := removeSpecial(tc.in)
if got != tc.want {
t.Errorf("removeSpecial(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}