-
Notifications
You must be signed in to change notification settings - Fork 2
/
dht_namespace_pk_validator_test.go
109 lines (80 loc) · 2.54 KB
/
dht_namespace_pk_validator_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
package crabfs
import (
"fmt"
"testing"
"github.com/golang/mock/gomock"
libp2pRecord "github.com/libp2p/go-libp2p-record"
"github.com/stretchr/testify/assert"
)
func setUpPkValidatorTest(t *testing.T) (libp2pRecord.Validator, *gomock.Controller) {
ctrl := setUpBasicTest(t)
validator := DHTNamespacePKValidatorNew()
return validator, ctrl
}
func setDownPkValidatorTest(ctrl *gomock.Controller) {
setDownBasicTest(ctrl)
}
func TestPkValidatorValidateInvalidKey(t *testing.T) {
assert := assert.New(t)
validator, ctrl := setUpPkValidatorTest(t)
defer setDownPkValidatorTest(ctrl)
assert.NotNil(validator.Validate("", nil))
}
func TestPkValidatorValidateInvalidPublicKey(t *testing.T) {
assert := assert.New(t)
validator, ctrl := setUpPkValidatorTest(t)
defer setDownPkValidatorTest(ctrl)
assert.NotNil(validator.Validate("/crabfs_pk/12345", nil))
assert.NotNil(validator.Validate("/crabfs_pk/12345", []byte("abc")))
}
func TestPkValidatorValidateInvalidPublicKeyHash(t *testing.T) {
assert := assert.New(t)
validator, ctrl := setUpPkValidatorTest(t)
defer setDownPkValidatorTest(ctrl)
privKey, err := GenerateKeyPair()
assert.Nil(err)
publicKey := privKey.GetPublic()
data, err := publicKey.Marshal()
assert.Nil(err)
assert.NotNil(validator.Validate("/crabfs_pk/12345", data))
}
func TestPkValidatorValidateValidPublicKey(t *testing.T) {
assert := assert.New(t)
validator, ctrl := setUpPkValidatorTest(t)
defer setDownPkValidatorTest(ctrl)
privKey, err := GenerateKeyPair()
assert.Nil(err)
publicKey := privKey.GetPublic()
data, err := publicKey.Marshal()
assert.Nil(err)
expectedKey := fmt.Sprintf("/crabfs_pk/%s", publicKey.HashString())
assert.Nil(validator.Validate(expectedKey, data))
}
func TestPkValidatorSelect(t *testing.T) {
assert := assert.New(t)
validator, ctrl := setUpPkValidatorTest(t)
defer setDownPkValidatorTest(ctrl)
privKey, err := GenerateKeyPair()
assert.Nil(err)
publicKey := privKey.GetPublic()
data, err := publicKey.Marshal()
assert.Nil(err)
expectedKey := fmt.Sprintf("/crabfs_pk/%s", publicKey.HashString())
i, err := validator.Select(expectedKey, [][]byte{
data,
data,
})
assert.Nil(err)
assert.Equal(0, i)
}
func TestPkValidatorSelectNoValues(t *testing.T) {
assert := assert.New(t)
validator, ctrl := setUpPkValidatorTest(t)
defer setDownPkValidatorTest(ctrl)
privKey, err := GenerateKeyPair()
assert.Nil(err)
publicKey := privKey.GetPublic()
expectedKey := fmt.Sprintf("/crabfs_pk/%s", publicKey.HashString())
_, err = validator.Select(expectedKey, [][]byte{})
assert.NotNil(err)
}