88 "encoding"
99 "hash"
1010 "io"
11- "strings"
1211 "testing"
1312
1413 "github.com/golang-fips/openssl/v2"
@@ -44,27 +43,30 @@ func cryptoToHash(h crypto.Hash) func() hash.Hash {
4443
4544func TestSha (t * testing.T ) {
4645 msg := []byte ("testing" )
47- var tests = []crypto.Hash {
48- crypto .MD4 ,
49- crypto .MD5 ,
50- crypto .SHA1 ,
51- crypto .SHA224 ,
52- crypto .SHA256 ,
53- crypto .SHA384 ,
54- crypto .SHA512 ,
55- crypto .SHA3_224 ,
56- crypto .SHA3_256 ,
57- crypto .SHA3_384 ,
58- crypto .SHA3_512 ,
46+ var tests = []struct {
47+ h crypto.Hash
48+ hasMarshaler bool
49+ }{
50+ {crypto .MD4 , false },
51+ {crypto .MD5 , true },
52+ {crypto .SHA1 , true },
53+ {crypto .SHA224 , true },
54+ {crypto .SHA256 , true },
55+ {crypto .SHA384 , true },
56+ {crypto .SHA512 , true },
57+ {crypto .SHA3_224 , false },
58+ {crypto .SHA3_256 , false },
59+ {crypto .SHA3_384 , false },
60+ {crypto .SHA3_512 , false },
5961 }
6062 for _ , tt := range tests {
6163 tt := tt
62- t .Run (tt .String (), func (t * testing.T ) {
64+ t .Run (tt .h . String (), func (t * testing.T ) {
6365 t .Parallel ()
64- if ! openssl .SupportsHash (tt ) {
66+ if ! openssl .SupportsHash (tt . h ) {
6567 t .Skip ("skipping: not supported" )
6668 }
67- h := cryptoToHash (tt )()
69+ h := cryptoToHash (tt . h )()
6870 initSum := h .Sum (nil )
6971 n , err := h .Write (msg )
7072 if err != nil {
@@ -80,12 +82,12 @@ func TestSha(t *testing.T) {
8082 if bytes .Equal (sum , initSum ) {
8183 t .Error ("Write didn't change internal hash state" )
8284 }
83- if name := tt .String (); name != "MD4" && ! strings . HasPrefix ( name , "SHA3-" ) {
85+ if tt .hasMarshaler {
8486 state , err := h .(encoding.BinaryMarshaler ).MarshalBinary ()
8587 if err != nil {
8688 t .Errorf ("could not marshal: %v" , err )
8789 }
88- h2 := cryptoToHash (tt )()
90+ h2 := cryptoToHash (tt . h )()
8991 if err := h2 .(encoding.BinaryUnmarshaler ).UnmarshalBinary (state ); err != nil {
9092 t .Errorf ("could not unmarshal: %v" , err )
9193 }
0 commit comments