-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnaughty_strings_test.go
208 lines (161 loc) · 5.78 KB
/
naughty_strings_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
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package ionhash
import (
"bufio"
"encoding/base64"
"os"
"strings"
"testing"
"github.com/amzn/ion-go/ion"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNaughtyStrings(t *testing.T) {
file, err := os.Open("ion-hash-test/big_list_of_naughty_strings.txt")
require.NoError(t, err, "Something went wrong loading big_list_of_naughty_strings.txt")
defer func() {
assert.NoError(t, file.Close(), "Something went wrong executing file.Close()")
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Trim(line, " ") == "" || strings.HasPrefix(line, "#") {
continue
}
tv := newTestValue(line)
NaughtyStrings(t, tv, tv.asSymbol())
NaughtyStrings(t, tv, tv.asString())
NaughtyStrings(t, tv, tv.asLongString())
NaughtyStrings(t, tv, tv.asClob())
NaughtyStrings(t, tv, tv.asBlob())
NaughtyStrings(t, tv, tv.asSymbol()+"::"+tv.asSymbol())
NaughtyStrings(t, tv, tv.asSymbol()+"::"+tv.asString())
NaughtyStrings(t, tv, tv.asSymbol()+"::"+tv.asLongString())
NaughtyStrings(t, tv, tv.asSymbol()+"::"+tv.asClob())
NaughtyStrings(t, tv, tv.asSymbol()+"::"+tv.asBlob())
NaughtyStrings(t, tv, tv.asSymbol()+"::{"+tv.asSymbol()+":"+tv.asSymbol()+"}")
NaughtyStrings(t, tv, tv.asSymbol()+"::{"+tv.asSymbol()+":"+tv.asString()+"}")
NaughtyStrings(t, tv, tv.asSymbol()+"::{"+tv.asSymbol()+":"+tv.asLongString()+"}")
NaughtyStrings(t, tv, tv.asSymbol()+"::{"+tv.asSymbol()+":"+tv.asClob()+"}")
NaughtyStrings(t, tv, tv.asSymbol()+"::{"+tv.asSymbol()+":"+tv.asBlob()+"}")
if tv.validIon {
NaughtyStrings(t, tv, tv.asIon())
NaughtyStrings(t, tv, tv.asSymbol()+"::"+tv.asIon())
NaughtyStrings(t, tv, tv.asSymbol()+"::{"+tv.asSymbol()+":"+tv.asIon()+"}")
NaughtyStrings(t, tv, tv.asSymbol()+"::{"+tv.asSymbol()+":"+tv.asSymbol()+"::"+tv.asIon()+"}")
}
list := tv.asSymbol() + "::[" + tv.asSymbol() + ", " + tv.asString() + ", " + tv.asLongString() + ", " + tv.asClob() + ", " + tv.asBlob() + ", "
if tv.validIon {
list += tv.asIon()
}
list += "]"
NaughtyStrings(t, tv, list)
sexp := tv.asSymbol() + "::(" + tv.asSymbol() + " " + tv.asString() + " " + tv.asLongString() + " " + tv.asClob() + " " + tv.asBlob() + " "
if tv.validIon {
sexp += tv.asIon()
}
sexp += ")"
NaughtyStrings(t, tv, sexp)
// multiple annotations
NaughtyStrings(t, tv, tv.asSymbol()+"::"+tv.asSymbol()+"::"+tv.asSymbol()+"::"+tv.asString())
}
assert.NoError(t, scanner.Err(), "Expected scanner to scan without errors")
}
func NaughtyStrings(t *testing.T, tv testValue, s string) {
hasherProvider := NewCryptoHasherProvider(SHA256)
str := strings.Builder{}
hw, err := NewHashWriter(ion.NewTextWriter(&str), hasherProvider)
require.NoError(t, err, "Expected NewHashWriter() to successfully create a HashWriter")
ionHashWriter, ok := hw.(*hashWriter)
require.True(t, ok, "Expected hw to be of type hashWriter")
writeFromReaderToWriter(t, ion.NewReaderString(s), ionHashWriter, !tv.validIon)
hr, err := NewHashReader(ion.NewReaderString(s), hasherProvider)
require.NoError(t, err, "Expected NewHashReader() to successfully create a HashReader")
ionHashReader, ok := hr.(*hashReader)
require.True(t, ok, "Expected hr to be of type hashReader")
if tv.validIon {
if !ionHashReader.Next() {
assert.NoError(t, ionHashReader.Err(), "Something went wrong executing ionHashReader.Next()")
}
if !ionHashReader.Next() {
assert.NoError(t, ionHashReader.Err(), "Something went wrong executing ionHashReader.Next()")
}
writerSum, err := ionHashWriter.Sum(nil)
require.NoError(t, err, "Something went wrong executing ionHashWriter.Sum(nil)")
readerSum, err := ionHashReader.Sum(nil)
require.NoError(t, err, "Something went wrong executing ionHashReader.Sum(nil)")
assert.Equalf(t, writerSum, readerSum, "Expected reader/writer sums for \"%s\" to match", s)
}
}
type testValue struct {
ionPrefix string
invalidIonPrefix string
ion string
validIon bool
}
func newTestValue(ion string) testValue {
prefix := "ion::"
invalidPrefix := "invalid_ion::"
validIon := false
if strings.HasPrefix(ion, prefix) {
validIon = true
ion = ion[len(prefix):]
} else {
ion = strings.TrimPrefix(ion, invalidPrefix)
}
return testValue{ionPrefix: prefix, invalidIonPrefix: invalidPrefix, ion: ion, validIon: validIon}
}
func (tv *testValue) asIon() string {
return tv.ion
}
func (tv *testValue) asSymbol() string {
s := tv.ion
s = strings.Replace(s, "\\", "\\\\", -1)
s = strings.Replace(s, "'", "\\'", -1)
s = "'" + s + "'"
return s
}
func (tv *testValue) asString() string {
s := tv.ion
s = strings.Replace(s, "\\", "\\\\", -1)
s = strings.Replace(s, "\"", "\\\"", -1)
s = "\"" + s + "\""
return s
}
func (tv *testValue) asLongString() string {
s := tv.ion
s = strings.Replace(s, "\\", "\\\\", -1)
s = strings.Replace(s, "'", "\\'", -1)
s = "'''" + s + "'''"
return s
}
func (tv *testValue) asClob() string {
s := ""
bytes := []byte(tv.asString())
for _, b := range bytes {
c := b & 0xFF
if c >= 128 {
s += "\\x" + string(c)
} else {
s += string(c)
}
}
return s
}
func (tv *testValue) asBlob() string {
bytes := []byte(tv.asIon())
return "{{" + base64.StdEncoding.EncodeToString(bytes) + "}}"
}