...

Source file src/hash/marshal_test.go

Documentation: hash

		 1  // Copyright 2017 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  // Test that the hashes in the standard library implement
		 6  // BinaryMarshaler, BinaryUnmarshaler,
		 7  // and lock in the current representations.
		 8  
		 9  package hash_test
		10  
		11  import (
		12  	"bytes"
		13  	"crypto/md5"
		14  	"crypto/sha1"
		15  	"crypto/sha256"
		16  	"crypto/sha512"
		17  	"encoding"
		18  	"encoding/hex"
		19  	"hash"
		20  	"hash/adler32"
		21  	"hash/crc32"
		22  	"hash/crc64"
		23  	"hash/fnv"
		24  	"testing"
		25  )
		26  
		27  func fromHex(s string) []byte {
		28  	b, err := hex.DecodeString(s)
		29  	if err != nil {
		30  		panic(err)
		31  	}
		32  	return b
		33  }
		34  
		35  var marshalTests = []struct {
		36  	name	 string
		37  	new		func() hash.Hash
		38  	golden []byte
		39  }{
		40  	{"adler32", func() hash.Hash { return adler32.New() }, fromHex("61646c01460a789d")},
		41  	{"crc32", func() hash.Hash { return crc32.NewIEEE() }, fromHex("63726301ca87914dc956d3e8")},
		42  	{"crc64", func() hash.Hash { return crc64.New(crc64.MakeTable(crc64.ISO)) }, fromHex("6372630273ba8484bbcd5def5d51c83c581695be")},
		43  	{"fnv32", func() hash.Hash { return fnv.New32() }, fromHex("666e760171ba3d77")},
		44  	{"fnv32a", func() hash.Hash { return fnv.New32a() }, fromHex("666e76027439f86f")},
		45  	{"fnv64", func() hash.Hash { return fnv.New64() }, fromHex("666e7603cc64e0e97692c637")},
		46  	{"fnv64a", func() hash.Hash { return fnv.New64a() }, fromHex("666e7604c522af9b0dede66f")},
		47  	{"fnv128", func() hash.Hash { return fnv.New128() }, fromHex("666e760561587a70a0f66d7981dc980e2cabbaf7")},
		48  	{"fnv128a", func() hash.Hash { return fnv.New128a() }, fromHex("666e7606a955802b0136cb67622b461d9f91e6ff")},
		49  	{"md5", md5.New, fromHex("6d643501a91b0023007aa14740a3979210b5f024c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		50  	{"sha1", sha1.New, fromHex("736861016dad5acb4dc003952f7a0b352ee5537ec381a228c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		51  	{"sha224", sha256.New224, fromHex("73686102f8b92fc047c9b4d82f01a6370841277b7a0d92108440178c83db855a8e66c2d9c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		52  	{"sha256", sha256.New, fromHex("736861032bed68b99987cae48183b2b049d393d0050868e4e8ba3730e9112b08765929b7c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		53  	{"sha384", sha512.New384, fromHex("736861046f1664d213dd802f7c47bc50637cf93592570a2b8695839148bf38341c6eacd05326452ef1cbe64d90f1ef73bb5ac7d2803565467d0ddb10c5ee3fc050f9f0c1808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		54  	{"sha512_224", sha512.New512_224, fromHex("736861056f1a450ec15af20572d0d1ee6518104d7cbbbe79a038557af5450ed7dbd420b53b7335209e951b4d9aff401f90549b9604fa3d823fbb8581c73582a88aa84022808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		55  	{"sha512_256", sha512.New512_256, fromHex("736861067c541f1d1a72536b1f5dad64026bcc7c508f8a2126b51f46f8b9bff63a26fee70980718031e96832e95547f4fe76160ff84076db53b4549b86354af8e17b5116808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		56  	{"sha512", sha512.New, fromHex("736861078e03953cd57cd6879321270afa70c5827bb5b69be59a8f0130147e94f2aedf7bdc01c56c92343ca8bd837bb7f0208f5a23e155694516b6f147099d491a30b151808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f80000000000000000000000000000f9")},
		57  }
		58  
		59  func TestMarshalHash(t *testing.T) {
		60  	for _, tt := range marshalTests {
		61  		t.Run(tt.name, func(t *testing.T) {
		62  			buf := make([]byte, 256)
		63  			for i := range buf {
		64  				buf[i] = byte(i)
		65  			}
		66  
		67  			h := tt.new()
		68  			h.Write(buf[:256])
		69  			sum := h.Sum(nil)
		70  
		71  			h2 := tt.new()
		72  			h3 := tt.new()
		73  			const split = 249
		74  			for i := 0; i < split; i++ {
		75  				h2.Write(buf[i : i+1])
		76  			}
		77  			h2m, ok := h2.(encoding.BinaryMarshaler)
		78  			if !ok {
		79  				t.Fatalf("Hash does not implement MarshalBinary")
		80  			}
		81  			enc, err := h2m.MarshalBinary()
		82  			if err != nil {
		83  				t.Fatalf("MarshalBinary: %v", err)
		84  			}
		85  			if !bytes.Equal(enc, tt.golden) {
		86  				t.Errorf("MarshalBinary = %x, want %x", enc, tt.golden)
		87  			}
		88  			h3u, ok := h3.(encoding.BinaryUnmarshaler)
		89  			if !ok {
		90  				t.Fatalf("Hash does not implement UnmarshalBinary")
		91  			}
		92  			if err := h3u.UnmarshalBinary(enc); err != nil {
		93  				t.Fatalf("UnmarshalBinary: %v", err)
		94  			}
		95  			h2.Write(buf[split:])
		96  			h3.Write(buf[split:])
		97  			sum2 := h2.Sum(nil)
		98  			sum3 := h3.Sum(nil)
		99  			if !bytes.Equal(sum2, sum) {
	 100  				t.Fatalf("Sum after MarshalBinary = %x, want %x", sum2, sum)
	 101  			}
	 102  			if !bytes.Equal(sum3, sum) {
	 103  				t.Fatalf("Sum after UnmarshalBinary = %x, want %x", sum3, sum)
	 104  			}
	 105  		})
	 106  	}
	 107  }
	 108  

View as plain text