-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsignature_test.go
89 lines (65 loc) · 2.23 KB
/
signature_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
package neofscrypto_test
import (
"testing"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test"
"github.com/nspcc-dev/neofs-sdk-go/proto/refs"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
const anyUnsupportedScheme = -1
func TestSignatureLifecycle(t *testing.T) {
data := []byte("Hello, world!")
signer := neofscryptotest.Signer()
scheme := signer.Scheme()
pubKey := signer.Public()
bPubKey := neofscrypto.PublicKeyBytes(pubKey)
var clientSig neofscrypto.Signature
err := clientSig.Calculate(signer, data)
require.NoError(t, err)
testSig := func(sig neofscrypto.Signature) {
require.Equal(t, signer.Scheme(), sig.Scheme())
require.Equal(t, signer.Public(), sig.PublicKey())
require.Equal(t, bPubKey, sig.PublicKeyBytes())
require.NotEmpty(t, sig.Value())
require.True(t, sig.Verify(data))
}
testSig(clientSig)
m := clientSig.ProtoMessage()
require.Equal(t, refs.SignatureScheme(scheme), m.GetScheme())
require.Equal(t, bPubKey, m.GetKey())
require.Equal(t, clientSig.Value(), m.GetSign())
// m transmitted to server over the network
var serverSig neofscrypto.Signature
err = serverSig.FromProtoMessage(m)
require.NoError(t, err)
testSig(serverSig)
// break the message in different ways
for i, breakSig := range []func(*refs.Signature){
func(sigV2 *refs.Signature) { sigV2.Scheme = refs.SignatureScheme(anyUnsupportedScheme) },
} {
m := proto.Clone(m).(*refs.Signature)
breakSig(m)
err = serverSig.FromProtoMessage(m)
require.Errorf(t, err, "break func #%d", i)
}
}
func TestNewSignature(t *testing.T) {
signer := neofscryptotest.Signer()
scheme := signer.Scheme()
pubKey := signer.Public()
val := []byte("Hello, world!") // may be any for this test
sig := neofscrypto.NewSignature(scheme, pubKey, val)
checkFields := func(sig neofscrypto.Signature) {
require.Equal(t, scheme, sig.Scheme())
require.Equal(t, pubKey, sig.PublicKey())
require.Equal(t, neofscrypto.PublicKeyBytes(pubKey), sig.PublicKeyBytes())
require.Equal(t, val, sig.Value())
}
checkFields(sig)
m := sig.ProtoMessage()
var sig2 neofscrypto.Signature
err := sig2.FromProtoMessage(m)
require.NoError(t, err)
checkFields(sig2)
}