Repository navigation
Expand file tree
/
Copy pathcommon_test.go
More file actions
115 lines (105 loc) · 3.34 KB
/
Copy pathcommon_test.go
File metadata and controls
115 lines (105 loc) · 3.34 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
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package verifier
import (
"math/big"
"testing"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/pkg/errors"
. "github.com/smartystreets/goconvey/convey"
)
var (
MockHash = []byte{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
}
)
type MockHeader struct{}
func (*MockHeader) MarshalHash() ([]byte, error) {
return MockHash, nil
}
type MockObject struct {
MockHeader
HSV DefaultHashSignVerifierImpl
}
func (o *MockObject) Sign(signer *asymmetric.PrivateKey) error {
return o.HSV.Sign(&o.MockHeader, signer)
}
func (o *MockObject) Verify() error {
return o.HSV.Verify(&o.MockHeader)
}
func TestDefaultHashSignVerifierImpl(t *testing.T) {
Convey("Given a dummy object and a pair of keys", t, func() {
var (
obj = &MockObject{}
priv, _, err = asymmetric.GenSecp256k1KeyPair()
)
So(err, ShouldBeNil)
So(priv, ShouldNotBeNil)
Convey("When the object is signed by the key pair", func() {
err = obj.Sign(priv)
So(err, ShouldBeNil)
Convey("The object should be verifiable", func() {
err = obj.Verify()
So(err, ShouldBeNil)
})
Convey("The object should have data hash", func() {
So(obj.HSV.Hash(), ShouldEqual, hash.THashH(MockHash))
})
Convey("When the hash is modified", func() {
obj.HSV.DataHash = hash.Hash{0x0, 0x0, 0x0, 0x1}
Convey("The verifier should return hash value not match error", func() {
err = errors.Cause(obj.Verify())
So(err, ShouldEqual, ErrHashValueNotMatch)
})
})
Convey("When the signee is not set", func() {
obj.HSV.Signee = nil
Convey("The verifier should return signature not match error", func() {
err = errors.Cause(obj.Verify())
So(err, ShouldEqual, ErrSignatureNotMatch)
})
})
Convey("When the signature is not set", func() {
obj.HSV.Signature = nil
Convey("The verifier should return signature not match error", func() {
err = errors.Cause(obj.Verify())
So(err, ShouldEqual, ErrSignatureNotMatch)
})
})
Convey("When the signee is modified", func() {
var _, pub, err = asymmetric.GenSecp256k1KeyPair()
So(err, ShouldBeNil)
obj.HSV.Signee = pub
Convey("The verifier should return signature not match error", func() {
err = errors.Cause(obj.Verify())
So(err, ShouldEqual, ErrSignatureNotMatch)
})
})
Convey("When the signature is modified", func() {
var val = obj.HSV.Signature.R
val.Add(val, big.NewInt(1))
Convey("The verifier should return signature not match error", func() {
err = errors.Cause(obj.Verify())
So(err, ShouldEqual, ErrSignatureNotMatch)
})
})
})
})
}