@@ -59,7 +59,7 @@ func TestHash(t *testing.T) {
59
59
t .Run (ch .String (), func (t * testing.T ) {
60
60
t .Parallel ()
61
61
if ! openssl .SupportsHash (ch ) {
62
- t .Skip ("skipping: not supported" )
62
+ t .Skip ("not supported" )
63
63
}
64
64
h := cryptoToHash (ch )()
65
65
initSum := h .Sum (nil )
@@ -92,26 +92,36 @@ func TestHash_BinaryMarshaler(t *testing.T) {
92
92
t .Run (ch .String (), func (t * testing.T ) {
93
93
t .Parallel ()
94
94
if ! openssl .SupportsHash (ch ) {
95
- t .Skip ("skipping: not supported" )
95
+ t .Skip ("hash not supported" )
96
96
}
97
- h := cryptoToHash (ch )()
98
- if _ , ok := h .(encoding.BinaryMarshaler ); ! ok {
99
- t .Skip ("skipping: not supported" )
97
+
98
+ hashMarshaler , ok := cryptoToHash (ch )().(interface {
99
+ hash.Hash
100
+ encoding.BinaryMarshaler
101
+ })
102
+ if ! ok {
103
+ t .Skip ("BinaryMarshaler not supported" )
100
104
}
101
- _ , err := h . Write ( msg )
102
- if err != nil {
103
- t .Fatal ( err )
105
+
106
+ if _ , err := hashMarshaler . Write ( msg ); err != nil {
107
+ t .Fatalf ( "Write failed: %v" , err )
104
108
}
105
- state , err := h .(encoding.BinaryMarshaler ).MarshalBinary ()
109
+
110
+ state , err := hashMarshaler .MarshalBinary ()
106
111
if err != nil {
107
- t .Errorf ( "could not marshal : %v" , err )
112
+ t .Fatalf ( "MarshalBinary failed : %v" , err )
108
113
}
109
- h2 := cryptoToHash (ch )()
110
- if err := h2 .(encoding.BinaryUnmarshaler ).UnmarshalBinary (state ); err != nil {
111
- t .Errorf ("could not unmarshal: %v" , err )
114
+
115
+ hashUnmarshaler := cryptoToHash (ch )().(interface {
116
+ hash.Hash
117
+ encoding.BinaryUnmarshaler
118
+ })
119
+ if err := hashUnmarshaler .UnmarshalBinary (state ); err != nil {
120
+ t .Fatalf ("UnmarshalBinary failed: %v" , err )
112
121
}
113
- if actual , actual2 := h .Sum (nil ), h2 .Sum (nil ); ! bytes .Equal (actual , actual2 ) {
114
- t .Errorf ("0x%x != marshaled 0x%x" , actual , actual2 )
122
+
123
+ if actual , actual2 := hashMarshaler .Sum (nil ), hashUnmarshaler .Sum (nil ); ! bytes .Equal (actual , actual2 ) {
124
+ t .Errorf ("0x%x != appended 0x%x" , actual , actual2 )
115
125
}
116
126
})
117
127
}
@@ -122,46 +132,54 @@ func TestHash_BinaryAppender(t *testing.T) {
122
132
t .Run (ch .String (), func (t * testing.T ) {
123
133
t .Parallel ()
124
134
if ! openssl .SupportsHash (ch ) {
125
- t .Skip ("skipping: not supported" )
135
+ t .Skip ("not supported" )
126
136
}
127
- h := cryptoToHash (ch )()
128
- if h , ok := h .(interface {
137
+
138
+ hashWithBinaryAppender , ok := cryptoToHash (ch )().(interface {
139
+ hash.Hash
129
140
AppendBinary (b []byte ) ([]byte , error )
130
- Sum (b []byte ) []byte
131
- }); ok {
132
- // Create a slice with 10 elements
133
- prebuiltSlice := make ([]byte , 10 )
134
- // Fill the slice with some data
135
- for i := range prebuiltSlice {
136
- prebuiltSlice [i ] = byte (i )
137
- }
141
+ })
142
+ if ! ok {
143
+ t .Skip ("not supported" )
144
+ }
145
+
146
+ // Create a slice with 10 elements
147
+ prebuiltSlice := make ([]byte , 10 )
148
+ // Fill the slice with some data
149
+ for i := range prebuiltSlice {
150
+ prebuiltSlice [i ] = byte (i )
151
+ }
138
152
139
- // Clone the prebuilt slice for comparison
140
- prebuiltSliceClone := append ([]byte (nil ), prebuiltSlice ... )
153
+ // Clone the prebuilt slice for comparison
154
+ prebuiltSliceClone := append ([]byte (nil ), prebuiltSlice ... )
141
155
142
- // Append binary data to the prebuilt slice
143
- state , err := h .AppendBinary (prebuiltSlice )
144
- if err != nil {
145
- t .Errorf ("could not append binary: %v" , err )
146
- }
156
+ // Append binary data to the prebuilt slice
157
+ state , err := hashWithBinaryAppender .AppendBinary (prebuiltSlice )
158
+ if err != nil {
159
+ t .Errorf ("could not append binary: %v" , err )
160
+ }
161
+
162
+ // Ensure the first 10 elements are still the same
163
+ if ! bytes .Equal (state [:10 ], prebuiltSliceClone ) {
164
+ t .Errorf ("prebuilt slice modified: got %v, want %v" , state [:10 ], prebuiltSliceClone )
165
+ }
147
166
148
- // Ensure the first 10 elements are still the same
149
- if ! bytes .Equal (state [:10 ], prebuiltSliceClone ) {
150
- t .Errorf ("prebuilt slice modified: got %v, want %v" , state [:10 ], prebuiltSliceClone )
151
- }
167
+ // Use only the newly appended part of the slice
168
+ appendedState := state [10 :]
152
169
153
- // Use only the newly appended part of the slice
154
- appendedState := state [10 :]
170
+ h2 , ok := cryptoToHash (ch )().(interface {
171
+ hash.Hash
172
+ encoding.BinaryUnmarshaler
173
+ })
174
+ if ! ok {
175
+ t .Skip ("not supported" )
176
+ }
155
177
156
- h2 := cryptoToHash (ch )()
157
- if err := h2 .(encoding.BinaryUnmarshaler ).UnmarshalBinary (appendedState ); err != nil {
158
- t .Errorf ("could not unmarshal: %v" , err )
159
- }
160
- if actual , actual2 := h .Sum (nil ), h2 .Sum (nil ); ! bytes .Equal (actual , actual2 ) {
161
- t .Errorf ("0x%x != appended 0x%x" , actual , actual2 )
162
- }
163
- } else {
164
- t .Skip ("skipping: not supported" )
178
+ if err := h2 .UnmarshalBinary (appendedState ); err != nil {
179
+ t .Errorf ("could not unmarshal: %v" , err )
180
+ }
181
+ if actual , actual2 := hashWithBinaryAppender .Sum (nil ), h2 .Sum (nil ); ! bytes .Equal (actual , actual2 ) {
182
+ t .Errorf ("0x%x != appended 0x%x" , actual , actual2 )
165
183
}
166
184
})
167
185
}
@@ -173,11 +191,11 @@ func TestHash_Clone(t *testing.T) {
173
191
t .Run (ch .String (), func (t * testing.T ) {
174
192
t .Parallel ()
175
193
if ! openssl .SupportsHash (ch ) {
176
- t .Skip ("skipping: not supported" )
194
+ t .Skip ("not supported" )
177
195
}
178
196
h := cryptoToHash (ch )()
179
197
if _ , ok := h .(encoding.BinaryMarshaler ); ! ok {
180
- t .Skip ("skipping: not supported" )
198
+ t .Skip ("not supported" )
181
199
}
182
200
_ , err := h .Write (msg )
183
201
if err != nil {
@@ -201,20 +219,21 @@ func TestHash_Clone(t *testing.T) {
201
219
func TestHash_ByteWriter (t * testing.T ) {
202
220
msg := []byte ("testing" )
203
221
for _ , ch := range hashes {
204
- ch := ch
205
222
t .Run (ch .String (), func (t * testing.T ) {
206
223
t .Parallel ()
207
224
if ! openssl .SupportsHash (ch ) {
208
- t .Skip ("skipping: not supported" )
225
+ t .Skip ("not supported" )
209
226
}
210
- h := cryptoToHash (ch )()
211
- initSum := h .Sum (nil )
212
- bw := h .(io.ByteWriter )
227
+ bwh := cryptoToHash (ch )().(interface {
228
+ hash.Hash
229
+ io.ByteWriter
230
+ })
231
+ initSum := bwh .Sum (nil )
213
232
for i := range len (msg ) {
214
- bw .WriteByte (msg [i ])
233
+ bwh .WriteByte (msg [i ])
215
234
}
216
- h .Reset ()
217
- sum := h .Sum (nil )
235
+ bwh .Reset ()
236
+ sum := bwh .Sum (nil )
218
237
if ! bytes .Equal (sum , initSum ) {
219
238
t .Errorf ("got:%x want:%x" , sum , initSum )
220
239
}
@@ -225,11 +244,10 @@ func TestHash_ByteWriter(t *testing.T) {
225
244
func TestHash_StringWriter (t * testing.T ) {
226
245
msg := []byte ("testing" )
227
246
for _ , ch := range hashes {
228
- ch := ch
229
247
t .Run (ch .String (), func (t * testing.T ) {
230
248
t .Parallel ()
231
249
if ! openssl .SupportsHash (ch ) {
232
- t .Skip ("skipping: not supported" )
250
+ t .Skip ("not supported" )
233
251
}
234
252
h := cryptoToHash (ch )()
235
253
initSum := h .Sum (nil )
@@ -289,7 +307,7 @@ func TestHash_OneShot(t *testing.T) {
289
307
for _ , tt := range tests {
290
308
t .Run (tt .h .String (), func (t * testing.T ) {
291
309
if ! openssl .SupportsHash (tt .h ) {
292
- t .Skip ("skipping: not supported" )
310
+ t .Skip ("not supported" )
293
311
}
294
312
got := tt .oneShot (msg )
295
313
h := cryptoToHash (tt .h )()
0 commit comments