hash_test.go raw
1 package chainhash
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "testing"
7 )
8
9 // mainNetGenesisHash is the hash of the first block in the block chain for the main network (genesis block).
10 var mainNetGenesisHash = Hash([HashSize]byte{ // Make go vet happy.
11 0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
12 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
13 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
14 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
15 },
16 )
17
18 // TestHash tests the Hash API.
19 func TestHash(t *testing.T) {
20 // Hash of block 234439.
21 blockHashStr := "14a0810ac680a3eb3f82edc878cea25ec41d6b790744e5daeef"
22 blockHash, e := NewHashFromStr(blockHashStr)
23 if e != nil {
24 t.Errorf("NewHashFromStr: %v", e)
25 }
26 // Hash of block 234440 as byte slice.
27 buf := []byte{
28 0x79, 0xa6, 0x1a, 0xdb, 0xc6, 0xe5, 0xa2, 0xe1,
29 0x39, 0xd2, 0x71, 0x3a, 0x54, 0x6e, 0xc7, 0xc8,
30 0x75, 0x63, 0x2e, 0x75, 0xf1, 0xdf, 0x9c, 0x3f,
31 0xa6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 }
33 hash, e := NewHash(buf)
34 if e != nil {
35 t.Errorf("NewHash: unexpected error %v", e)
36 }
37 // Ensure proper size.
38 if len(hash) != HashSize {
39 t.Errorf("NewHash: hash length mismatch - got: %v, want: %v",
40 len(hash), HashSize,
41 )
42 }
43 // Ensure contents match.
44 if !bytes.Equal(hash[:], buf) {
45 t.Errorf("NewHash: hash contents mismatch - got: %v, want: %v",
46 hash[:], buf,
47 )
48 }
49 // Ensure contents of hash of block 234440 don't match 234439.
50 if hash.IsEqual(blockHash) {
51 t.Errorf("IsEqual: hash contents should not match - got: %v, want: %v",
52 hash, blockHash,
53 )
54 }
55 // Set hash from byte slice and ensure contents match.
56 e = hash.SetBytes(blockHash.CloneBytes())
57 if e != nil {
58 t.Errorf("SetBytes: %v", e)
59 }
60 if !hash.IsEqual(blockHash) {
61 t.Errorf("IsEqual: hash contents mismatch - got: %v, want: %v",
62 hash, blockHash,
63 )
64 }
65 // Ensure nil hashes are handled properly.
66 if !(*Hash)(nil).IsEqual(nil) {
67 t.Error("IsEqual: nil hashes should match")
68 }
69 if hash.IsEqual(nil) {
70 t.Error("IsEqual: non-nil hash matches nil hash")
71 }
72 // Invalid size for SetBytes.
73 e = hash.SetBytes([]byte{0x00})
74 if e == nil {
75 t.Errorf("SetBytes: failed to received expected e - got: nil")
76 }
77 // Invalid size for NewHash.
78 invalidHash := make([]byte, HashSize+1)
79 _, e = NewHash(invalidHash)
80 if e == nil {
81 t.Errorf("NewHash: failed to received expected e - got: nil")
82 }
83 }
84
85 // TestHashString tests the stringized output for hashes.
86 func TestHashString(t *testing.T) {
87 // Block 100000 hash.
88 wantStr := "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
89 hash := Hash([HashSize]byte{ // Make go vet happy.
90 0x06, 0xe5, 0x33, 0xfd, 0x1a, 0xda, 0x86, 0x39,
91 0x1f, 0x3f, 0x6c, 0x34, 0x32, 0x04, 0xb0, 0xd2,
92 0x78, 0xd4, 0xaa, 0xec, 0x1c, 0x0b, 0x20, 0xaa,
93 0x27, 0xba, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
94 },
95 )
96 hashStr := hash.String()
97 if hashStr != wantStr {
98 t.Errorf("String: wrong hash string - got %v, want %v",
99 hashStr, wantStr,
100 )
101 }
102 }
103
104 // TestNewHashFromStr executes tests against the NewHashFromStr function.
105 func TestNewHashFromStr(t *testing.T) {
106 tests := []struct {
107 in string
108 want Hash
109 e error
110 }{
111 // Genesis hash.
112 {
113 "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
114 mainNetGenesisHash,
115 nil,
116 },
117 // Genesis hash with stripped leading zeros.
118 {
119 "19d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
120 mainNetGenesisHash,
121 nil,
122 },
123 // Empty string.
124 {
125 "",
126 Hash{},
127 nil,
128 },
129 // Single digit hash.
130 {
131 "1",
132 Hash([HashSize]byte{ // Make go vet happy.
133 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 },
138 ),
139 nil,
140 },
141 // Block 203707 with stripped leading zeros.
142 {
143 "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc",
144 Hash([HashSize]byte{ // Make go vet happy.
145 0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
146 0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
147 0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
148 0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149 },
150 ),
151 nil,
152 },
153 // Hash string that is too long.
154 {
155 "01234567890123456789012345678901234567890123456789012345678912345",
156 Hash{},
157 ErrHashStrSize,
158 },
159 // Hash string that is contains non-hex chars.
160 {
161 "abcdefg",
162 Hash{},
163 hex.InvalidByteError('g'),
164 },
165 }
166 unexpectedErrStr := "NewHashFromStr #%d failed to detect expected error - got: %v want: %v"
167 unexpectedResultStr := "NewHashFromStr #%d got: %v want: %v"
168 t.Logf("Running %d tests", len(tests))
169 for i, test := range tests {
170 result, e := NewHashFromStr(test.in)
171 if e != test.e {
172 t.Errorf(unexpectedErrStr, i, e, test.e)
173 continue
174 } else if e != nil {
175
176 // Got expected error. Move on to the next test.
177 continue
178 }
179 if !test.want.IsEqual(result) {
180 t.Errorf(unexpectedResultStr, i, result, &test.want)
181 continue
182 }
183 }
184 }
185