-
Notifications
You must be signed in to change notification settings - Fork 659
/
test_signer.go
75 lines (59 loc) · 1.99 KB
/
test_signer.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package warp
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
)
// SignerTests is a list of all signer tests
var SignerTests = map[string]func(t *testing.T, s Signer, sk *bls.SecretKey, networkID uint32, chainID ids.ID){
"WrongChainID": TestWrongChainID,
"WrongNetworkID": TestWrongNetworkID,
"Verifies": TestVerifies,
}
// Test that using a random SourceChainID results in an error
func TestWrongChainID(t *testing.T, s Signer, _ *bls.SecretKey, _ uint32, _ ids.ID) {
require := require.New(t)
msg, err := NewUnsignedMessage(
constants.UnitTestID,
ids.GenerateTestID(),
[]byte("payload"),
)
require.NoError(err)
_, err = s.Sign(msg)
// TODO: require error to be ErrWrongSourceChainID
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
}
// Test that using a different networkID results in an error
func TestWrongNetworkID(t *testing.T, s Signer, _ *bls.SecretKey, networkID uint32, blockchainID ids.ID) {
require := require.New(t)
msg, err := NewUnsignedMessage(
networkID+1,
blockchainID,
[]byte("payload"),
)
require.NoError(err)
_, err = s.Sign(msg)
// TODO: require error to be ErrWrongNetworkID
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
}
// Test that a signature generated with the signer verifies correctly
func TestVerifies(t *testing.T, s Signer, sk *bls.SecretKey, networkID uint32, chainID ids.ID) {
require := require.New(t)
msg, err := NewUnsignedMessage(
networkID,
chainID,
[]byte("payload"),
)
require.NoError(err)
sigBytes, err := s.Sign(msg)
require.NoError(err)
sig, err := bls.SignatureFromBytes(sigBytes)
require.NoError(err)
pk := bls.PublicFromSecretKey(sk)
msgBytes := msg.Bytes()
require.True(bls.Verify(pk, sig, msgBytes))
}