test to ensure embedding the key in the record works
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jun 5, 2018 at 04:52 UTC
af68a380330c9147f5d70c2004e4c99a8c199255
2 files changed
+77
-9
namesys/ipns_validate_test.go
+75
-7
@@ -3,6 +3,7 @@ package namesys
3
import (
4
"context"
5
"fmt"
6
+ "math/rand"
7
"testing"
8
"time"
9
@@ -28,20 +29,21 @@ func testValidatorCase(t *testing.T, priv ci.PrivKey, kbook pstore.KeyBook, key
29
30
validator := IpnsValidator{kbook}
31
31
- p := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
32
- entry, err := CreateRoutingEntryData(priv, p, 1, eol)
33
- if err != nil {
34
- t.Fatal(err)
35
- }
36
-
32
data := val
33
if data == nil {
34
+ p := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
35
+ entry, err := CreateRoutingEntryData(priv, p, 1, eol)
36
+ if err != nil {
37
+ t.Fatal(err)
38
+ }
39
+
40
data, err = proto.Marshal(entry)
41
if err != nil {
42
t.Fatal(err)
43
}
44
}
44
- err = validator.Validate(key, data)
45
+
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 {
@@ -74,6 +76,72 @@ func TestValidator(t *testing.T) {
76
testValidatorCase(t, priv, kbook, "/wrong/"+string(id), nil, ts.Add(time.Hour), ErrInvalidPath)
77
}
78
79
+func TestEmbeddedPubKeyValidate(t *testing.T) {
80
+ goodeol := time.Now().Add(time.Hour)
81
+ kbook := pstore.NewPeerstore()
82
+
83
+ pth := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
84
+
85
+ priv, _, _, ipnsk := genKeys(t)
86
+
87
+ entry, err := CreateRoutingEntryData(priv, pth, 1, goodeol)
88
+ if err != nil {
89
+ t.Fatal(err)
90
+ }
91
+
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)
98
+
99
+ pubkb, err := priv.GetPublic().Bytes()
100
+ if err != nil {
101
+ t.Fatal(err)
102
+ }
103
+
104
+ entry.PubKey = pubkb
105
+
106
+ dataWithKey, err := proto.Marshal(entry)
107
+ if err != nil {
108
+ t.Fatal(err)
109
+ }
110
+
111
+ testValidatorCase(t, priv, kbook, ipnsk, dataWithKey, goodeol, nil)
112
+}
113
+
114
+func TestPeerIDPubKeyValidate(t *testing.T) {
115
+ goodeol := time.Now().Add(time.Hour)
116
+ kbook := pstore.NewPeerstore()
117
+
118
+ pth := path.Path("/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG")
119
+
120
+ sk, pk, err := ci.GenerateEd25519Key(rand.New(rand.NewSource(42)))
121
+ if err != nil {
122
+ t.Fatal(err)
123
+ }
124
+
125
+ pid, err := peer.IDFromPublicKey(pk)
126
+ if err != nil {
127
+ t.Fatal(err)
128
+ }
129
+
130
+ ipnsk := "/ipns/" + string(pid)
131
+
132
+ entry, err := CreateRoutingEntryData(sk, pth, 1, goodeol)
133
+ if err != nil {
134
+ t.Fatal(err)
135
+ }
136
+
137
+ dataNoKey, err := proto.Marshal(entry)
138
+ if err != nil {
139
+ t.Fatal(err)
140
+ }
141
+
142
+ testValidatorCase(t, sk, kbook, ipnsk, dataNoKey, goodeol, nil)
143
+}
144
+
145
func TestResolverValidation(t *testing.T) {
146
ctx := context.Background()
147
rid := testutil.RandIdentityOrFatal(t)
namesys/validator.go
+2
-2
@@ -70,7 +70,7 @@ func (v IpnsValidator) Validate(key string, value []byte) error {
70
71
pubk, err := v.getPublicKey(pid, entry)
72
if err != nil {
73
- return fmt.Errorf("getting public key failed: %s", err)
73
+ return err
74
}
75
76
// Check the ipns record signature with the public key
@@ -102,7 +102,7 @@ func (v IpnsValidator) getPublicKey(pid peer.ID, entry *pb.IpnsEntry) (ic.PubKey
102
if err != nil {
103
// TODO: i think this counts as a 'malformed record' and should be discarded
104
log.Debugf("public key in ipns record failed to parse: ", err)
105
- return nil, err
105
+ return nil, fmt.Errorf("unmarshaling pubkey in record: %s", err)
106
}
107
expPid, err := peer.IDFromPublicKey(pk)
108
if err != nil {