-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreader_writer_test.go
200 lines (150 loc) · 7.95 KB
/
reader_writer_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
/*
* 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"
"testing"
"github.com/amzn/ion-go/ion"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// This test writes a nested struct {a:{b:1}} where the Ion Writer writes the outer struct
// and the HashWriter writes the inner struct.
// We then read the struct back following a similar pattern where the Ion Reader reads the outer struct
// and the HashReader reads the inner struct.
// We then confirm that the HashReader reads the same hash written by the HashWriter.
func TestFieldNameAsymmetry(t *testing.T) {
buf := bytes.Buffer{}
writer := ion.NewBinaryWriter(&buf)
tihp := newTestIonHasherProvider("identity")
hw, err := NewHashWriter(writer, tihp.getInstance())
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")
// Writing a nested struct: {a:{b:1}}
// We use the ion writer to write the outer struct (ie. {a:_})
assert.NoError(t, writer.BeginStruct(), "Something went wrong executing writer.BeginStruct()")
assert.NoError(t, writer.FieldName(ion.NewSymbolTokenFromString("a")), "Something went wrong executing writer.FieldName(...)")
// We use the ion hash writer to write the inner struct (ie. {b:1} inside {a:{b:1}})
assert.NoError(t, ionHashWriter.BeginStruct(), "Something went wrong executing ionHashWriter.BeginStruct()")
assert.NoError(t, ionHashWriter.FieldName(ion.NewSymbolTokenFromString("b")), "Something went wrong executing ionHashWriter.FieldName(...)")
assert.NoError(t, ionHashWriter.WriteInt(1), "Something went wrong executing ionHashWriter.WriteInt(1)")
assert.NoError(t, ionHashWriter.EndStruct(), "Something went wrong executing ionHashWriter.EndStruct()")
assert.NoError(t, writer.EndStruct(), "Something went wrong executing writer.EndStruct()")
writeHash, err := ionHashWriter.Sum(nil)
require.NoError(t, err, "Something went wrong executing ionHashWriter.Sum(nil)")
assert.NoError(t, ionHashWriter.Finish(), "Something went wrong executing ionHashWriter.Finish()")
assert.NoError(t, writer.Finish(), "Something went wrong executing writer.Finish()")
reader := ion.NewReaderBytes(buf.Bytes())
tihp = newTestIonHasherProvider("identity")
hr, err := NewHashReader(reader, tihp.getInstance())
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")
// We are reading the nested struct that we just wrote: {a:{b:1}}
// We use the ion reader to read the outer struct (ie. {a:_})
if !reader.Next() {
assert.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
}
assert.NoError(t, reader.StepIn(), "Something went wrong executing reader.StepIn()")
if !reader.Next() {
assert.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
}
// We use the ion hash reader to read the inner struct (ie. {b:1} inside {a:{b:1}})
assert.NoError(t, ionHashReader.StepIn(), "Something went wrong executing ionHashReader.StepIn()")
if !ionHashReader.Next() {
assert.NoError(t, ionHashReader.Err(), "Something went wrong executing ionHashReader.Next()")
}
assert.NoError(t, ionHashReader.StepOut(), "Something went wrong executing ionHashReader.StepOut()")
assert.NoError(t, reader.StepOut(), "Something went wrong executing reader.StepOut()")
sum, err := ionHashReader.Sum(nil)
require.NoError(t, err, "Something went wrong executing ionHashReader.Sum(nil)")
assert.Equal(t, writeHash, sum, "sum did not match expectation")
}
func TestNoFieldNameInCurrentHash(t *testing.T) {
AssertNoFieldnameInCurrentHash(t, "null", []byte{0x0b, 0x0f, 0x0e})
AssertNoFieldnameInCurrentHash(t, "false", []byte{0x0b, 0x10, 0x0e})
AssertNoFieldnameInCurrentHash(t, "5", []byte{0x0b, 0x20, 0x05, 0x0e})
AssertNoFieldnameInCurrentHash(
t,
"2e0",
[]byte{0x0b, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e})
AssertNoFieldnameInCurrentHash(t, "1234.500", []byte{0x0b, 0x50, 0xc3, 0x12, 0xd6, 0x44, 0x0e})
AssertNoFieldnameInCurrentHash(t, "hi", []byte{0x0b, 0x70, 0x68, 0x69, 0x0e})
AssertNoFieldnameInCurrentHash(t, "\"hi\"", []byte{0x0b, 0x80, 0x68, 0x69, 0x0e})
AssertNoFieldnameInCurrentHash(t, "{{\"hi\"}}", []byte{0x0b, 0x90, 0x68, 0x69, 0x0e})
AssertNoFieldnameInCurrentHash(t, "{{aGVsbG8=}}", []byte{0x0b, 0xa0, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0e})
AssertNoFieldnameInCurrentHash(
t,
"[1,2,3]",
[]byte{0x0b, 0xb0, 0x0b, 0x20, 0x01, 0x0e, 0x0b, 0x20, 0x02, 0x0e, 0x0b, 0x20, 0x03, 0x0e, 0x0e})
AssertNoFieldnameInCurrentHash(
t,
"(1 2 3)",
[]byte{0x0b, 0xc0, 0x0b, 0x20, 0x01, 0x0e, 0x0b, 0x20, 0x02, 0x0e, 0x0b, 0x20, 0x03, 0x0e, 0x0e})
AssertNoFieldnameInCurrentHash(
t,
"{a:1,b:2,c:3}",
[]byte{
0x0b, 0xd0, 0x0c, 0x0b, 0x70, 0x61, 0x0c, 0x0e, 0x0c, 0x0b, 0x20, 0x01, 0x0c,
0x0e, 0x0c, 0x0b, 0x70, 0x62, 0x0c, 0x0e, 0x0c, 0x0b, 0x20, 0x02, 0x0c, 0x0e,
0x0c, 0x0b, 0x70, 0x63, 0x0c, 0x0e, 0x0c, 0x0b, 0x20, 0x03, 0x0c, 0x0e, 0x0e})
AssertNoFieldnameInCurrentHash(
t,
"hi::7",
[]byte{0x0b, 0xe0, 0x0b, 0x70, 0x68, 0x69, 0x0e, 0x0b, 0x20, 0x07, 0x0e, 0x0e})
}
func AssertNoFieldnameInCurrentHash(t *testing.T, value string, expectedBytes []byte) {
var err error
reader := ion.NewReaderString(value)
buf := bytes.Buffer{}
writer := ion.NewBinaryWriter(&buf)
assert.NoError(t, writer.BeginStruct(), "Something went wrong executing writer.BeginStruct()")
tihp := newTestIonHasherProvider("identity")
hw, err := NewHashWriter(writer, tihp.getInstance())
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")
assert.NoError(t, ionHashWriter.FieldName(ion.NewSymbolTokenFromString("field_name")),
"Something went wrong executing ionHashWriter.FieldName(...)")
writeFromReaderToWriter(t, reader, ionHashWriter, false)
actualBytes, err := ionHashWriter.Sum(nil)
require.NoError(t, err, "Something went wrong executing ionHashWriter.Sum(nil)")
assert.Equal(t, expectedBytes, actualBytes, "sum did not match expectation")
assert.NoError(t, writer.EndStruct(), "Something went wrong executing writer.EndStruct()")
assert.NoError(t, ionHashWriter.Finish(), "Something went wrong executing ionHashWriter.Finish()")
assert.NoError(t, writer.Finish(), "Something went wrong executing writer.Finish()")
reader = ion.NewReaderBytes(buf.Bytes())
if !reader.Next() {
assert.NoError(t, reader.Err(), "Something went wrong executing reader.Next()")
}
assert.NoError(t, reader.StepIn(), "Something went wrong executing reader.StepIn()")
tihp = newTestIonHasherProvider("identity")
hr, err := NewHashReader(reader, tihp.getInstance())
require.NoError(t, err, "Expected NewHashReader() to successfully create a HashReader")
ionHashReader := hr.(*hashReader)
require.True(t, ok, "Expected hr to be of type hashReader")
// List
if !ionHashReader.Next() {
assert.NoError(t, ionHashReader.Err(), "Something went wrong executing ionHashReader.Next()")
}
// None
if !ionHashReader.Next() {
assert.NoError(t, ionHashReader.Err(), "Something went wrong executing ionHashReader.Next()")
}
actualBytes, err = ionHashReader.Sum(nil)
require.NoError(t, err, "Something went wrong executing ionHashReader.Sum(nil)")
assert.Equal(t, expectedBytes, actualBytes, "sum did not match expectation")
}