add tests for pubkey mismatch and bad pubkey
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jun 5, 2018 at 07:51 UTC
c66c5c64bb74161caef0fa25c69083f24de8acfd
2 files changed
+48
-20
namesys/ipns_validate_test.go
+44
-19
@@ -4,10 +4,12 @@ import (
4
"context"
5
"fmt"
6
"math/rand"
7
+ "strings"
8
"testing"
9
"time"
10
11
opts "github.com/ipfs/go-ipfs/namesys/opts"
12
+ pb "github.com/ipfs/go-ipfs/namesys/pb"
13
path "github.com/ipfs/go-ipfs/path"
14
15
u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
@@ -27,6 +29,25 @@ import (
29
func testValidatorCase(t *testing.T, priv ci.PrivKey, kbook pstore.KeyBook, key string, val []byte, eol time.Time, exp error) {
30
t.Helper()
31
32
+ match := func(t *testing.T, err error) {
33
+ t.Helper()
34
+ if err != exp {
35
+ params := fmt.Sprintf("key: %s\neol: %s\n", key, eol)
36
+ if exp == nil {
37
+ t.Fatalf("Unexpected error %s for params %s", err, params)
38
+ } else if err == nil {
39
+ t.Fatalf("Expected error %s but there was no error for params %s", exp, params)
40
+ } else {
41
+ t.Fatalf("Expected error %s but got %s for params %s", exp, err, params)
42
+ }
43
+ }
44
+ }
45
+
46
+ testValidatorCaseMatchFunc(t, priv, kbook, key, val, eol, match)
47
+}
48
+
49
+func testValidatorCaseMatchFunc(t *testing.T, priv ci.PrivKey, kbook pstore.KeyBook, key string, val []byte, eol time.Time, matchf func(*testing.T, error)) {
50
+ t.Helper()
51
validator := IpnsValidator{kbook}
52
53
data := val
@@ -43,17 +64,7 @@ func testValidatorCase(t *testing.T, priv ci.PrivKey, kbook pstore.KeyBook, key
64
}
65
}
66
46
- err := validator.Validate(key, data)
47
- if err != exp {
48
- params := fmt.Sprintf("key: %s\neol: %s\n", key, eol)
49
- if exp == nil {
50
- t.Fatalf("Unexpected error %s for params %s", err, params)
51
- } else if err == nil {
52
- t.Fatalf("Expected error %s but there was no error for params %s", exp, params)
53
- } else {
54
- t.Fatalf("Expected error %s but got %s for params %s", exp, err, params)
55
- }
56
- }
67
+ matchf(t, validator.Validate(key, data))
68
}
69
70
func TestValidator(t *testing.T) {
@@ -76,6 +87,15 @@ func TestValidator(t *testing.T) {
87
testValidatorCase(t, priv, kbook, "/wrong/"+string(id), nil, ts.Add(time.Hour), ErrInvalidPath)
88
}
89
90
+func mustMarshal(t *testing.T, entry *pb.IpnsEntry) []byte {
91
+ t.Helper()
92
+ data, err := proto.Marshal(entry)
93
+ if err != nil {
94
+ t.Fatal(err)
95
+ }
96
+ return data
97
+}
98
+
99
func TestEmbeddedPubKeyValidate(t *testing.T) {
100
goodeol := time.Now().Add(time.Hour)
101
kbook := pstore.NewPeerstore()
@@ -89,12 +109,7 @@ func TestEmbeddedPubKeyValidate(t *testing.T) {
109
t.Fatal(err)
110
}
111
92
- dataNoKey, err := proto.Marshal(entry)
93
- if err != nil {
94
- t.Fatal(err)
95
- }
96
-
97
- testValidatorCase(t, priv, kbook, ipnsk, dataNoKey, goodeol, ErrPublicKeyNotFound)
112
+ testValidatorCase(t, priv, kbook, ipnsk, mustMarshal(t, entry), goodeol, ErrPublicKeyNotFound)
113
114
pubkb, err := priv.GetPublic().Bytes()
115
if err != nil {
@@ -102,13 +117,23 @@ func TestEmbeddedPubKeyValidate(t *testing.T) {
117
}
118
119
entry.PubKey = pubkb
120
+ testValidatorCase(t, priv, kbook, ipnsk, mustMarshal(t, entry), goodeol, nil)
121
+
122
+ entry.PubKey = []byte("probably not a public key")
123
+ testValidatorCaseMatchFunc(t, priv, kbook, ipnsk, mustMarshal(t, entry), goodeol, func(t *testing.T, err error) {
124
+ if !strings.Contains(err.Error(), "unmarshaling pubkey in record:") {
125
+ t.Fatal("expected pubkey unmarshaling to fail")
126
+ }
127
+ })
128
106
- dataWithKey, err := proto.Marshal(entry)
129
+ opriv, _, _, _ := genKeys(t)
130
+ wrongkeydata, err := opriv.GetPublic().Bytes()
131
if err != nil {
132
t.Fatal(err)
133
}
134
111
- testValidatorCase(t, priv, kbook, ipnsk, dataWithKey, goodeol, nil)
135
+ entry.PubKey = wrongkeydata
136
+ testValidatorCase(t, priv, kbook, ipnsk, mustMarshal(t, entry), goodeol, ErrPublicKeyMismatch)
137
}
138
139
func TestPeerIDPubKeyValidate(t *testing.T) {
namesys/validator.go
+4
-1
@@ -44,6 +44,8 @@ var ErrKeyFormat = errors.New("record key could not be parsed into peer ID")
44
// from the peer store
45
var ErrPublicKeyNotFound = errors.New("public key not found in peer store")
46
47
+var ErrPublicKeyMismatch = errors.New("public key in record did not match expected pubkey")
48
+
49
type IpnsValidator struct {
50
KeyBook pstore.KeyBook
51
}
@@ -104,13 +106,14 @@ func (v IpnsValidator) getPublicKey(pid peer.ID, entry *pb.IpnsEntry) (ic.PubKey
106
log.Debugf("public key in ipns record failed to parse: ", err)
107
return nil, fmt.Errorf("unmarshaling pubkey in record: %s", err)
108
}
109
+
110
expPid, err := peer.IDFromPublicKey(pk)
111
if err != nil {
112
return nil, fmt.Errorf("could not regenerate peerID from pubkey: %s", err)
113
}
114
115
if pid != expPid {
113
- return nil, fmt.Errorf("pubkey in record did not match expected pubkey")
116
+ return nil, ErrPublicKeyMismatch
117
}
118
119
return pk, nil