This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathresolve_test.go
122 lines (98 loc) · 3.17 KB
/
resolve_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
package namesys
import (
"context"
"errors"
"testing"
"time"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
mockrouting "github.com/ipfs/go-ipfs-routing/mock"
ipns "github.com/ipfs/go-ipns"
path "github.com/ipfs/go-path"
tnet "github.com/libp2p/go-libp2p-testing/net"
)
func TestRoutingResolve(t *testing.T) {
dstore := dssync.MutexWrap(ds.NewMapDatastore())
serv := mockrouting.NewServer()
id := tnet.RandIdentityOrFatal(t)
d := serv.ClientWithDatastore(context.Background(), id, dstore)
resolver := NewIpnsResolver(d)
publisher := NewIpnsPublisher(d, dstore)
identity := tnet.RandIdentityOrFatal(t)
h := path.FromString("/ipfs/QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN")
err := publisher.Publish(context.Background(), identity.PrivateKey(), h)
if err != nil {
t.Fatal(err)
}
res, err := resolver.Resolve(context.Background(), identity.ID().Pretty())
if err != nil {
t.Fatal(err)
}
if res != h {
t.Fatal("Got back incorrect value.")
}
}
func TestPrexistingExpiredRecord(t *testing.T) {
dstore := dssync.MutexWrap(ds.NewMapDatastore())
d := mockrouting.NewServer().ClientWithDatastore(context.Background(), tnet.RandIdentityOrFatal(t), dstore)
resolver := NewIpnsResolver(d)
publisher := NewIpnsPublisher(d, dstore)
identity := tnet.RandIdentityOrFatal(t)
// Make an expired record and put it in the datastore
h := path.FromString("/ipfs/QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN")
eol := time.Now().Add(time.Hour * -1)
entry, err := ipns.Create(identity.PrivateKey(), []byte(h), 0, eol, 0)
if err != nil {
t.Fatal(err)
}
err = PutRecordToRouting(context.Background(), d, identity.PublicKey(), entry)
if err != nil {
t.Fatal(err)
}
// Now, with an old record in the system already, try and publish a new one
err = publisher.Publish(context.Background(), identity.PrivateKey(), h)
if err != nil {
t.Fatal(err)
}
err = verifyCanResolve(resolver, identity.ID().Pretty(), h)
if err != nil {
t.Fatal(err)
}
}
func TestPrexistingRecord(t *testing.T) {
dstore := dssync.MutexWrap(ds.NewMapDatastore())
d := mockrouting.NewServer().ClientWithDatastore(context.Background(), tnet.RandIdentityOrFatal(t), dstore)
resolver := NewIpnsResolver(d)
publisher := NewIpnsPublisher(d, dstore)
identity := tnet.RandIdentityOrFatal(t)
// Make a good record and put it in the datastore
h := path.FromString("/ipfs/QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN")
eol := time.Now().Add(time.Hour)
entry, err := ipns.Create(identity.PrivateKey(), []byte(h), 0, eol, 0)
if err != nil {
t.Fatal(err)
}
err = PutRecordToRouting(context.Background(), d, identity.PublicKey(), entry)
if err != nil {
t.Fatal(err)
}
// Now, with an old record in the system already, try and publish a new one
err = publisher.Publish(context.Background(), identity.PrivateKey(), h)
if err != nil {
t.Fatal(err)
}
err = verifyCanResolve(resolver, identity.ID().Pretty(), h)
if err != nil {
t.Fatal(err)
}
}
func verifyCanResolve(r Resolver, name string, exp path.Path) error {
res, err := r.Resolve(context.Background(), name)
if err != nil {
return err
}
if res != exp {
return errors.New("got back wrong record")
}
return nil
}