forked from ipfs/go-namesys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namesys_test.go
184 lines (165 loc) · 6.44 KB
/
namesys_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package namesys
import (
"context"
"errors"
"fmt"
"testing"
"time"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
offroute "github.com/ipfs/go-ipfs-routing/offline"
"github.com/ipfs/go-ipns"
"github.com/ipfs/go-path"
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
record "github.com/libp2p/go-libp2p-record"
ci "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
)
type mockResolver struct {
entries map[string]string
}
func testResolution(t *testing.T, resolver Resolver, name string, depth uint, expected string, expError error) {
t.Helper()
p, err := resolver.Resolve(context.Background(), name, opts.Depth(depth))
if !errors.Is(err, expError) {
t.Fatal(fmt.Errorf(
"expected %s with a depth of %d to have a '%s' error, but got '%s'",
name, depth, expError, err))
}
if p.String() != expected {
t.Fatal(fmt.Errorf(
"%s with depth %d resolved to %s != %s",
name, depth, p.String(), expected))
}
}
func (r *mockResolver) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
p, err := path.ParsePath(r.entries[name])
out := make(chan onceResult, 1)
out <- onceResult{value: p, err: err}
close(out)
return out
}
func mockResolverOne() *mockResolver {
return &mockResolver{
entries: map[string]string{
"QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy": "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj",
"QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n": "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy",
"QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD": "/ipns/ipfs.io",
"QmQ4QZh8nrsczdUEwTyfBope4THUhqxqc1fx6qYhhzZQei": "/ipfs/QmP3ouCnU8NNLsW6261pAx2pNLV2E4dQoisB1sgda12Act",
"12D3KooWFB51PRY9BxcXSH6khFXw1BZeszeLDy7C8GciskqCTZn5": "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", // ed25519+identity multihash
"bafzbeickencdqw37dpz3ha36ewrh4undfjt2do52chtcky4rxkj447qhdm": "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", // cidv1 in base32 with libp2p-key multicodec
},
}
}
func mockResolverTwo() *mockResolver {
return &mockResolver{
entries: map[string]string{
"ipfs.io": "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n",
},
}
}
func TestNamesysResolution(t *testing.T) {
r := &mpns{
ipnsResolver: mockResolverOne(),
dnsResolver: mockResolverTwo(),
}
testResolution(t, r, "Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", opts.DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", opts.DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", opts.DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", 1, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion)
testResolution(t, r, "/ipns/ipfs.io", opts.DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/ipfs.io", 1, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion)
testResolution(t, r, "/ipns/ipfs.io", 2, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion)
testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", opts.DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 1, "/ipns/ipfs.io", ErrResolveRecursion)
testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 2, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion)
testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 3, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion)
testResolution(t, r, "/ipns/12D3KooWFB51PRY9BxcXSH6khFXw1BZeszeLDy7C8GciskqCTZn5", 1, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion)
testResolution(t, r, "/ipns/bafzbeickencdqw37dpz3ha36ewrh4undfjt2do52chtcky4rxkj447qhdm", 1, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion)
}
func TestPublishWithCache0(t *testing.T) {
dst := dssync.MutexWrap(ds.NewMapDatastore())
priv, _, err := ci.GenerateKeyPair(ci.RSA, 2048)
if err != nil {
t.Fatal(err)
}
ps, err := pstoremem.NewPeerstore()
if err != nil {
t.Fatal(err)
}
pid, err := peer.IDFromPrivateKey(priv)
if err != nil {
t.Fatal(err)
}
err = ps.AddPrivKey(pid, priv)
if err != nil {
t.Fatal(err)
}
routing := offroute.NewOfflineRouter(dst, record.NamespacedValidator{
"ipns": ipns.Validator{KeyBook: ps},
"pk": record.PublicKeyValidator{},
})
nsys, err := NewNameSystem(routing, WithDatastore(dst))
if err != nil {
t.Fatal(err)
}
// CID is arbitrary.
p, err := path.ParsePath("QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
if err != nil {
t.Fatal(err)
}
err = nsys.Publish(context.Background(), priv, p)
if err != nil {
t.Fatal(err)
}
}
func TestPublishWithTTL(t *testing.T) {
dst := dssync.MutexWrap(ds.NewMapDatastore())
priv, _, err := ci.GenerateKeyPair(ci.RSA, 2048)
if err != nil {
t.Fatal(err)
}
ps, err := pstoremem.NewPeerstore()
if err != nil {
t.Fatal(err)
}
pid, err := peer.IDFromPrivateKey(priv)
if err != nil {
t.Fatal(err)
}
err = ps.AddPrivKey(pid, priv)
if err != nil {
t.Fatal(err)
}
routing := offroute.NewOfflineRouter(dst, record.NamespacedValidator{
"ipns": ipns.Validator{KeyBook: ps},
"pk": record.PublicKeyValidator{},
})
nsys, err := NewNameSystem(routing, WithDatastore(dst), WithCache(128))
if err != nil {
t.Fatal(err)
}
// CID is arbitrary.
p, err := path.ParsePath("QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
if err != nil {
t.Fatal(err)
}
ttl := 1 * time.Second
eol := time.Now().Add(2 * time.Second)
err = nsys.Publish(context.Background(), priv, p, opts.PublishWithEOL(eol), opts.PublishWithTTL(ttl))
if err != nil {
t.Fatal(err)
}
ientry, ok := nsys.(*mpns).cache.Get(string(pid))
if !ok {
t.Fatal("cache get failed")
}
entry, ok := ientry.(cacheEntry)
if !ok {
t.Fatal("bad cache item returned")
}
if entry.eol.Sub(eol) > 10*time.Millisecond {
t.Fatalf("bad cache ttl: expected %s, got %s", eol, entry.eol)
}
}