-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathhashing_test.go
More file actions
166 lines (133 loc) · 4.27 KB
/
Copy pathhashing_test.go
File metadata and controls
166 lines (133 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package types
import (
"bytes"
"crypto/sha256"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestHeaderHash tests the Hash method of the Header.
func TestHeaderHash(t *testing.T) {
// Create a sample header
header := &Header{
BaseHeader: BaseHeader{
Height: 1,
Time: 1234567890,
},
DataHash: []byte("datahash"),
}
hash1 := header.Hash()
assert.Nil(t, header.cachedHash, "Hash() should not memoize")
memoizedHash := header.MemoizeHash()
assert.Equal(t, hash1, memoizedHash)
assert.NotNil(t, header.cachedHash, "MemoizeHash() should store the computed value")
headerBytes, err := header.MarshalBinary()
require.NoError(t, err)
expectedHash := sha256.Sum256(headerBytes)
assert.NotNil(t, hash1)
assert.Len(t, hash1, sha256.Size)
assert.Equal(t, Hash(expectedHash[:]), hash1, "Header hash should match manual calculation")
header.BaseHeader.Height = 2
header.InvalidateHash()
assert.Nil(t, header.cachedHash)
hash2 := header.Hash()
assert.NotEqual(t, hash1, hash2, "Different headers should have different hashes")
}
func TestHeaderHashLegacy(t *testing.T) {
legacyFields := &LegacyHeaderFields{
LastCommitHash: bytes.Repeat([]byte{0x01}, 32),
ConsensusHash: bytes.Repeat([]byte{0x02}, 32),
LastResultsHash: bytes.Repeat([]byte{0x03}, 32),
}
header := &Header{
BaseHeader: BaseHeader{
Height: 10,
Time: 987654321,
ChainID: "legacy-chain",
},
DataHash: bytes.Repeat([]byte{0x04}, 32),
Legacy: legacyFields,
}
hash := header.Hash()
legacyBytes, err := header.MarshalBinaryLegacy()
require.NoError(t, err)
expected := sha256.Sum256(legacyBytes)
assert.NotNil(t, hash)
assert.Len(t, hash, sha256.Size)
assert.Equal(t, Hash(expected[:]), hash, "Header hash should prefer legacy encoding when legacy fields are present")
}
// TestDataHash tests the Hash method of the Data.
func TestDataHash(t *testing.T) {
data := &Data{
Txs: Txs{Tx("tx1"), Tx("tx2")},
Metadata: &Metadata{
ChainID: "test-chain",
Height: 1,
Time: 1234567890,
LastDataHash: []byte("lastdatahash"),
},
}
hash1 := data.Hash()
dataBytes, err := data.MarshalBinary()
require.NoError(t, err)
hasher := sha256.New()
hasher.Write(leafPrefix)
hasher.Write(dataBytes)
expectedHash := hasher.Sum(nil)
assert.NotNil(t, hash1)
assert.Len(t, hash1, sha256.Size)
assert.Equal(t, Hash(expectedHash), hash1, "Data hash should match manual calculation with prefix")
data.Txs = Txs{Tx("tx3")}
hash2 := data.Hash()
assert.NotEqual(t, hash1, hash2, "Different data (Txs) should have different hashes")
data.Metadata.Height = 2
hash3 := data.Hash()
assert.NotEqual(t, hash1, hash3, "Different data (Metadata) should have different hashes")
assert.NotEqual(t, hash2, hash3)
}
// TestLeafHashOpt tests the helper function leafHashOpt directly.
func TestLeafHashOpt(t *testing.T) {
data := []byte("some data")
hasher := sha256.New()
hash1 := leafHashOpt(hasher, data)
hasher.Reset()
hasher.Write(leafPrefix)
hasher.Write(data)
expectedHash := hasher.Sum(nil)
assert.Equal(t, expectedHash, hash1)
assert.Len(t, hash1, sha256.Size)
emptyData := []byte{}
hash2 := leafHashOpt(hasher, emptyData)
hasher.Reset()
hasher.Write(leafPrefix)
hasher.Write(emptyData)
expectedHash2 := hasher.Sum(nil)
assert.Equal(t, expectedHash2, hash2)
assert.NotEqual(t, hash1, hash2)
}
// TestHeaderHashWithBytes tests the HeaderHash function directly
func TestHeaderHashWithBytes(t *testing.T) {
header := &Header{
BaseHeader: BaseHeader{Height: 1, Time: 1234567890},
DataHash: []byte("datahash"),
}
// Hash using the method
hash1 := header.Hash()
// Hash using the function directly
headerBytes, err := header.MarshalBinary()
require.NoError(t, err)
var targetHeader Header
require.NoError(t, targetHeader.UnmarshalBinary(headerBytes))
assert.Equal(t, hash1, targetHeader.Hash(), "HeaderHash should produce same result as Header.Hash()")
}
func TestHeaderCloneDropsCachedHash(t *testing.T) {
header := &Header{
BaseHeader: BaseHeader{Height: 1, Time: 1234567890},
DataHash: []byte("datahash"),
}
header.MemoizeHash()
require.NotNil(t, header.cachedHash)
clone := header.Clone()
assert.Nil(t, clone.cachedHash, "Clone should not copy memoized hash state")
assert.Equal(t, header.Hash(), clone.Hash())
}