-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidators_test.go
220 lines (209 loc) · 6.65 KB
/
validators_test.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package partition
import (
gocrypto "crypto"
"testing"
"github.com/alphabill-org/alphabill/crypto"
testcertificates "github.com/alphabill-org/alphabill/internal/testutils/certificates"
testsig "github.com/alphabill-org/alphabill/internal/testutils/sig"
"github.com/alphabill-org/alphabill/network/protocol/blockproposal"
"github.com/alphabill-org/alphabill/network/protocol/genesis"
testtransaction "github.com/alphabill-org/alphabill/txsystem/testutils/transaction"
"github.com/alphabill-org/alphabill/types"
"github.com/stretchr/testify/require"
)
var systemDescription = &genesis.SystemDescriptionRecord{
SystemIdentifier: 1,
T2Timeout: 2500,
}
func TestNewDefaultUnicityCertificateValidator_NotOk(t *testing.T) {
_, v := testsig.CreateSignerAndVerifier(t)
type args struct {
systemDescription *genesis.SystemDescriptionRecord
rootTrustBase map[string]crypto.Verifier
algorithm gocrypto.Hash
}
tests := []struct {
name string
args args
wantErr error
}{
{
name: "system description record is nil",
args: args{
systemDescription: nil,
rootTrustBase: map[string]crypto.Verifier{"test": v},
algorithm: gocrypto.SHA256,
},
wantErr: genesis.ErrSystemDescriptionIsNil,
},
{
name: "trust base is nil",
args: args{
systemDescription: systemDescription,
rootTrustBase: nil,
algorithm: gocrypto.SHA256,
},
wantErr: types.ErrRootValidatorInfoMissing,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDefaultUnicityCertificateValidator(tt.args.systemDescription, tt.args.rootTrustBase, tt.args.algorithm)
require.ErrorIs(t, err, tt.wantErr)
require.Nil(t, got)
})
}
}
func TestDefaultUnicityCertificateValidator_ValidateNotOk(t *testing.T) {
_, verifier := testsig.CreateSignerAndVerifier(t)
rootTrust := map[string]crypto.Verifier{"test": verifier}
v, err := NewDefaultUnicityCertificateValidator(systemDescription, rootTrust, gocrypto.SHA256)
require.NoError(t, err)
require.ErrorIs(t, v.Validate(nil), types.ErrUnicityCertificateIsNil)
}
func TestDefaultUnicityCertificateValidator_ValidateOk(t *testing.T) {
signer, verifier := testsig.CreateSignerAndVerifier(t)
rootTrust := map[string]crypto.Verifier{"test": verifier}
v, err := NewDefaultUnicityCertificateValidator(systemDescription, rootTrust, gocrypto.SHA256)
require.NoError(t, err)
ir := &types.InputRecord{
PreviousHash: make([]byte, 32),
Hash: make([]byte, 32),
BlockHash: make([]byte, 32),
SummaryValue: make([]byte, 32),
RoundNumber: 1,
}
uc := testcertificates.CreateUnicityCertificate(
t,
signer,
ir,
systemDescription,
1,
make([]byte, 32),
)
require.NoError(t, v.Validate(uc))
}
func TestNewDefaultBlockProposalValidator_NotOk(t *testing.T) {
_, v := testsig.CreateSignerAndVerifier(t)
type args struct {
systemDescription *genesis.SystemDescriptionRecord
trustBase map[string]crypto.Verifier
algorithm gocrypto.Hash
}
tests := []struct {
name string
args args
wantErr error
}{
{
name: "system description record is nil",
args: args{
systemDescription: nil,
trustBase: map[string]crypto.Verifier{"test": v},
algorithm: gocrypto.SHA256,
},
wantErr: genesis.ErrSystemDescriptionIsNil,
},
{
name: "trust base is nil",
args: args{
systemDescription: systemDescription,
trustBase: nil,
algorithm: gocrypto.SHA256,
},
wantErr: types.ErrRootValidatorInfoMissing,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDefaultBlockProposalValidator(tt.args.systemDescription, tt.args.trustBase, tt.args.algorithm)
require.ErrorIs(t, err, tt.wantErr)
require.Nil(t, got)
})
}
}
func TestDefaultNewDefaultBlockProposalValidator_ValidateNotOk(t *testing.T) {
_, verifier := testsig.CreateSignerAndVerifier(t)
rootTrust := map[string]crypto.Verifier{"test": verifier}
v, err := NewDefaultBlockProposalValidator(systemDescription, rootTrust, gocrypto.SHA256)
require.NoError(t, err)
require.ErrorIs(t, v.Validate(nil, nil), blockproposal.ErrBlockProposalIsNil)
}
func TestDefaultNewDefaultBlockProposalValidator_ValidateOk(t *testing.T) {
signer, verifier := testsig.CreateSignerAndVerifier(t)
nodeSigner, nodeVerifier := testsig.CreateSignerAndVerifier(t)
rootTrust := map[string]crypto.Verifier{"test": verifier}
v, err := NewDefaultBlockProposalValidator(systemDescription, rootTrust, gocrypto.SHA256)
require.NoError(t, err)
ir := &types.InputRecord{
PreviousHash: make([]byte, 32),
Hash: make([]byte, 32),
BlockHash: make([]byte, 32),
SummaryValue: make([]byte, 32),
RoundNumber: 1,
}
uc := testcertificates.CreateUnicityCertificate(
t,
signer,
ir,
systemDescription,
1,
make([]byte, 32),
)
bp := &blockproposal.BlockProposal{
SystemIdentifier: uc.UnicityTreeCertificate.SystemIdentifier,
NodeIdentifier: "1",
UnicityCertificate: uc,
Transactions: []*types.TransactionRecord{
{
TransactionOrder: testtransaction.NewTransactionOrder(t),
ServerMetadata: &types.ServerMetadata{
ActualFee: 10,
},
},
},
}
err = bp.Sign(gocrypto.SHA256, nodeSigner)
require.NoError(t, err)
require.NoError(t, v.Validate(bp, nodeVerifier))
}
func TestDefaultTxValidator_ValidateNotOk(t *testing.T) {
tests := []struct {
name string
tx *types.TransactionOrder
latestBlockNumber uint64
expectedSystemIdentifier types.SystemID
errStr string
}{
{
name: "tx is nil",
tx: nil,
latestBlockNumber: 10,
expectedSystemIdentifier: 0x01020304,
errStr: "transaction is nil",
},
{
name: "invalid system identifier",
tx: testtransaction.NewTransactionOrder(t), // default systemID is 0x00000001
latestBlockNumber: 10,
expectedSystemIdentifier: 0x01020304,
errStr: "expected 01020304, got 00000001: invalid transaction system identifier",
},
{
name: "expired transaction",
tx: testtransaction.NewTransactionOrder(t), // default timeout is 10
latestBlockNumber: 11,
expectedSystemIdentifier: 0x00000001,
errStr: "transaction timeout round is 10, current round is 11: transaction has timed out",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dtv := &DefaultTxValidator{
systemIdentifier: tt.expectedSystemIdentifier,
}
err := dtv.Validate(tt.tx, tt.latestBlockNumber)
require.ErrorContains(t, err, tt.errStr)
})
}
}