Skip to content

Commit ca2d3b7

Browse files
authored
Remove multiple interface conversions in tests (#181)
* fix:remove multiple interface conversions * fix:remove redunant skipping prefix at t.Skip logs * move binaryappender tests to interface casting
1 parent e402ef5 commit ca2d3b7

File tree

1 file changed

+79
-61
lines changed

1 file changed

+79
-61
lines changed

hash_test.go

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestHash(t *testing.T) {
5959
t.Run(ch.String(), func(t *testing.T) {
6060
t.Parallel()
6161
if !openssl.SupportsHash(ch) {
62-
t.Skip("skipping: not supported")
62+
t.Skip("not supported")
6363
}
6464
h := cryptoToHash(ch)()
6565
initSum := h.Sum(nil)
@@ -92,26 +92,36 @@ func TestHash_BinaryMarshaler(t *testing.T) {
9292
t.Run(ch.String(), func(t *testing.T) {
9393
t.Parallel()
9494
if !openssl.SupportsHash(ch) {
95-
t.Skip("skipping: not supported")
95+
t.Skip("hash not supported")
9696
}
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")
100104
}
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)
104108
}
105-
state, err := h.(encoding.BinaryMarshaler).MarshalBinary()
109+
110+
state, err := hashMarshaler.MarshalBinary()
106111
if err != nil {
107-
t.Errorf("could not marshal: %v", err)
112+
t.Fatalf("MarshalBinary failed: %v", err)
108113
}
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)
112121
}
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)
115125
}
116126
})
117127
}
@@ -122,46 +132,54 @@ func TestHash_BinaryAppender(t *testing.T) {
122132
t.Run(ch.String(), func(t *testing.T) {
123133
t.Parallel()
124134
if !openssl.SupportsHash(ch) {
125-
t.Skip("skipping: not supported")
135+
t.Skip("not supported")
126136
}
127-
h := cryptoToHash(ch)()
128-
if h, ok := h.(interface {
137+
138+
hashWithBinaryAppender, ok := cryptoToHash(ch)().(interface {
139+
hash.Hash
129140
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+
}
138152

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...)
141155

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+
}
147166

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:]
152169

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+
}
155177

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)
165183
}
166184
})
167185
}
@@ -173,11 +191,11 @@ func TestHash_Clone(t *testing.T) {
173191
t.Run(ch.String(), func(t *testing.T) {
174192
t.Parallel()
175193
if !openssl.SupportsHash(ch) {
176-
t.Skip("skipping: not supported")
194+
t.Skip("not supported")
177195
}
178196
h := cryptoToHash(ch)()
179197
if _, ok := h.(encoding.BinaryMarshaler); !ok {
180-
t.Skip("skipping: not supported")
198+
t.Skip("not supported")
181199
}
182200
_, err := h.Write(msg)
183201
if err != nil {
@@ -201,20 +219,21 @@ func TestHash_Clone(t *testing.T) {
201219
func TestHash_ByteWriter(t *testing.T) {
202220
msg := []byte("testing")
203221
for _, ch := range hashes {
204-
ch := ch
205222
t.Run(ch.String(), func(t *testing.T) {
206223
t.Parallel()
207224
if !openssl.SupportsHash(ch) {
208-
t.Skip("skipping: not supported")
225+
t.Skip("not supported")
209226
}
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)
213232
for i := range len(msg) {
214-
bw.WriteByte(msg[i])
233+
bwh.WriteByte(msg[i])
215234
}
216-
h.Reset()
217-
sum := h.Sum(nil)
235+
bwh.Reset()
236+
sum := bwh.Sum(nil)
218237
if !bytes.Equal(sum, initSum) {
219238
t.Errorf("got:%x want:%x", sum, initSum)
220239
}
@@ -225,11 +244,10 @@ func TestHash_ByteWriter(t *testing.T) {
225244
func TestHash_StringWriter(t *testing.T) {
226245
msg := []byte("testing")
227246
for _, ch := range hashes {
228-
ch := ch
229247
t.Run(ch.String(), func(t *testing.T) {
230248
t.Parallel()
231249
if !openssl.SupportsHash(ch) {
232-
t.Skip("skipping: not supported")
250+
t.Skip("not supported")
233251
}
234252
h := cryptoToHash(ch)()
235253
initSum := h.Sum(nil)
@@ -289,7 +307,7 @@ func TestHash_OneShot(t *testing.T) {
289307
for _, tt := range tests {
290308
t.Run(tt.h.String(), func(t *testing.T) {
291309
if !openssl.SupportsHash(tt.h) {
292-
t.Skip("skipping: not supported")
310+
t.Skip("not supported")
293311
}
294312
got := tt.oneShot(msg)
295313
h := cryptoToHash(tt.h)()

0 commit comments

Comments
 (0)