-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathion_hash_test.go
243 lines (197 loc) · 8.47 KB
/
ion_hash_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* 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 (
"bytes"
"io/ioutil"
"strings"
"testing"
"github.com/amzn/ion-go/ion"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIonHash(t *testing.T) {
parameters := ionHashDataSource(t)
for i := range parameters {
ionBinary := parameters[i].testCase
reader := ion.NewReaderBytes(ionBinary)
provider := parameters[i].provider
Traverse(t, reader, provider.getInstance())
if len(parameters[i].expectedHashLog.identityUpdateList) > 0 {
assert.Equal(t, parameters[i].expectedHashLog.identityUpdateList, provider.getUpdateHashLog(), parameters[i].hasherName+" failed")
}
if len(parameters[i].expectedHashLog.identityDigestList) > 0 {
assert.Equal(t, parameters[i].expectedHashLog.identityDigestList, provider.getDigestHashLog(), parameters[i].hasherName+" failed")
}
if len(parameters[i].expectedHashLog.identityFinalDigestList) > 0 {
assert.Equal(t, parameters[i].expectedHashLog.identityFinalDigestList, provider.getFinalDigestHashLog(), parameters[i].hasherName+" failed")
}
if len(parameters[i].expectedHashLog.md5UpdateList) > 0 {
assert.Equal(t, parameters[i].expectedHashLog.md5UpdateList, provider.getUpdateHashLog(), parameters[i].hasherName+" failed")
}
if len(parameters[i].expectedHashLog.md5DigestList) > 0 {
assert.Equal(t, parameters[i].expectedHashLog.md5DigestList, provider.getDigestHashLog(), parameters[i].hasherName+" failed")
}
}
}
func Traverse(t *testing.T, reader ion.Reader, provider IonHasherProvider) {
hr, err := NewHashReader(reader, provider)
require.NoError(t, err, "Something went wrong executing NewHashReader()")
TraverseReader(t, hr)
_, err = hr.Sum(nil)
require.NoError(t, err, "Something went wrong with executing hr.Sum()")
}
func TraverseReader(t *testing.T, hr HashReader) {
for hr.Next() {
if hr.Type() != ion.NoType && hr.IsInStruct() {
require.NoError(t, hr.StepIn(), "Something went wrong executing hr.StepIn()")
TraverseReader(t, hr)
require.NoError(t, hr.StepOut(), "Something went wrong executing hr.StepOut()")
}
}
require.NoError(t, hr.Err(), "Something went wrong executing hr.Next()")
}
func ionHashDataSource(t *testing.T) []testObject {
var dataList []testObject
file, err := ioutil.ReadFile("ion-hash-test/ion_hash_tests.ion")
require.NoError(t, err, "Something went wrong loading ion_hash_tests.ion")
reader := ion.NewReaderBytes(file)
for reader.Next() {
testName := "unknown"
annotations, err := reader.Annotations()
require.NoError(t, err, "Something went wrong executing reader.Annotations()")
if len(annotations) > 0 && annotations[0].Text != nil {
testName = *annotations[0].Text
}
require.NoError(t, reader.StepIn(), "Something went wrong executing reader.StepIn()")
require.True(t, reader.Next()) // Read the initial Ion value.
var testCase []byte
fieldName, err := reader.FieldName()
require.NoError(t, err, "Something went wrong executing reader.FieldName()")
if fieldName != nil && fieldName.Text != nil && *fieldName.Text == "10n" {
require.NoError(t, reader.StepIn(), "Something went wrong executing reader.StepIn()")
testCase = append(testCase, []byte{0xE0, 0x01, 0x00, 0xEA}...)
for reader.Next() {
intValue, err := reader.Int64Value()
require.NoError(t, err, "Something went wrong executing reader.IntValue()")
testCase = append(testCase, byte(*intValue))
}
require.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
require.NoError(t, reader.StepOut(), "Something went wrong executing reader.StepOut()")
} else {
// Create textWriter to set testName to Ion text.
str := strings.Builder{}
textWriter := ion.NewTextWriter(&str)
// Create binaryWriter to set testCase to Ion binary.
buf := bytes.Buffer{}
binaryWriter := ion.NewBinaryWriter(&buf)
writeToWriters(t, reader, textWriter, binaryWriter)
require.NoError(t, textWriter.Finish(), "Something went wrong executing textWriter.Finish().")
require.NoError(t, binaryWriter.Finish(), "Something went wrong executing binaryWriter.Finish().")
if testName == "unknown" {
testName = str.String()
}
testCase = buf.Bytes()
}
require.True(t, reader.Next()) // Iterate through expected/ digest bytes.
fieldName, err = reader.FieldName()
require.NoError(t, err, "Something went wrong executing reader.FieldName()")
if fieldName != nil && fieldName.Text != nil && *fieldName.Text == "expect" {
require.NoError(t, reader.StepIn(), "Something went wrong executing reader.StepIn()")
for reader.Next() {
hasherFieldName, err := reader.FieldName()
require.NoError(t, err, "Something went wrong executing reader.FieldName()")
if hasherFieldName == nil || hasherFieldName.Text == nil {
continue
}
hasherName := *hasherFieldName.Text
var identityUpdateList [][]byte
var identityDigestList [][]byte
var identityFinalDigestList []byte
var md5UpdateList [][]byte
var md5DigestList [][]byte
switch hasherName {
case "identity":
require.NoError(t, reader.StepIn(), "Something went wrong executing reader.StepIn()")
for reader.Next() {
annotations, err := reader.Annotations()
require.NoError(t, err, "Something went wrong executing reader.Annotations()")
if len(annotations) > 0 && annotations[0].Text != nil {
switch *annotations[0].Text {
case "update":
updateBytes := readSexpAndAppendToList(t, reader)
identityUpdateList = append(identityUpdateList, updateBytes)
case "digest":
digestBytes := readSexpAndAppendToList(t, reader)
identityDigestList = append(identityDigestList, digestBytes)
case "final_digest":
identityFinalDigestList = readSexpAndAppendToList(t, reader)
}
}
}
require.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
require.NoError(t, reader.StepOut(), "Something went wrong executing reader.StepOut()")
case "md5":
require.NoError(t, reader.StepIn(), "Something went wrong executing reader.StepIn()")
for reader.Next() {
annotations, err := reader.Annotations()
require.NoError(t, err, "Something went wrong executing reader.Annotations()")
if len(annotations) > 0 && annotations[0].Text != nil {
switch *annotations[0].Text {
case "update":
updateBytes := readSexpAndAppendToList(t, reader)
md5UpdateList = append(md5UpdateList, updateBytes)
case "digest":
digestBytes := readSexpAndAppendToList(t, reader)
md5DigestList = append(md5DigestList, digestBytes)
}
}
}
require.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
require.NoError(t, reader.StepOut(), "Something went wrong executing reader.StepOut()")
}
if hasherName != "identity" {
testName = testName + "." + hasherName
}
expectedHashLog := hashLog{
identityUpdateList: identityUpdateList,
identityDigestList: identityDigestList,
identityFinalDigestList: identityFinalDigestList,
md5UpdateList: md5UpdateList,
md5DigestList: md5DigestList,
}
dataList = append(dataList, testObject{testName, testCase, &expectedHashLog, newTestIonHasherProvider(hasherName)})
}
require.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
require.NoError(t, reader.StepOut(), "Something went wrong executing reader.StepOut()")
}
require.NoError(t, reader.StepOut(), "Something went wrong executing reader.StepOut()")
}
require.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
return dataList
}
type testObject struct {
hasherName string
testCase []byte
expectedHashLog *hashLog
provider *testIonHasherProvider
}
type hashLog struct {
identityUpdateList [][]byte
identityDigestList [][]byte
identityFinalDigestList []byte
md5UpdateList [][]byte
md5DigestList [][]byte
}