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