mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 11:31:54 +08:00

* go-bitswap: fix some race conditions. * go-libp2p-kad-dht: fix a goroutine leak. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
148 lines
3.7 KiB
Go
148 lines
3.7 KiB
Go
package namesys
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
peer "gx/ipfs/QmTu65MVbemtUxJEWgsTtzv9Zv9P8rvmqNA4eG9TrTRGYc/go-libp2p-peer"
|
|
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
|
|
dssync "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore/sync"
|
|
path "gx/ipfs/QmaVydE6qtiiMhRUraPrzL6dvtfHPNR6Y9JJZcTwDAw9dY/go-path"
|
|
mockrouting "gx/ipfs/QmcjqHcsk8E1Gd8RbuaUawWC7ogDtaVcdjLvZF8ysCCiPn/go-ipfs-routing/mock"
|
|
ipns "gx/ipfs/QmdboayjE53q27kq6zGk5vkx4u7LDGdcbVoi5NXMCtfiKS/go-ipns"
|
|
testutil "gx/ipfs/QmeFVdhzY13YZPWxCiQvmLercrumFRoQZFQEYw2BtzyiQc/go-testutil"
|
|
)
|
|
|
|
func TestRoutingResolve(t *testing.T) {
|
|
dstore := dssync.MutexWrap(ds.NewMapDatastore())
|
|
serv := mockrouting.NewServer()
|
|
id := testutil.RandIdentityOrFatal(t)
|
|
d := serv.ClientWithDatastore(context.Background(), id, dstore)
|
|
|
|
resolver := NewIpnsResolver(d)
|
|
publisher := NewIpnsPublisher(d, dstore)
|
|
|
|
privk, pubk, err := testutil.RandTestKeyPair(512)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
h := path.FromString("/ipfs/QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN")
|
|
err = publisher.Publish(context.Background(), privk, h)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
pid, err := peer.IDFromPublicKey(pubk)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
res, err := resolver.Resolve(context.Background(), pid.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(), testutil.RandIdentityOrFatal(t), dstore)
|
|
|
|
resolver := NewIpnsResolver(d)
|
|
publisher := NewIpnsPublisher(d, dstore)
|
|
|
|
privk, pubk, err := testutil.RandTestKeyPair(512)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
id, err := peer.IDFromPublicKey(pubk)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// 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(privk, []byte(h), 0, eol)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = PutRecordToRouting(context.Background(), d, pubk, 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(), privk, h)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = verifyCanResolve(resolver, 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(), testutil.RandIdentityOrFatal(t), dstore)
|
|
|
|
resolver := NewIpnsResolver(d)
|
|
publisher := NewIpnsPublisher(d, dstore)
|
|
|
|
privk, pubk, err := testutil.RandTestKeyPair(512)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
id, err := peer.IDFromPublicKey(pubk)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// 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(privk, []byte(h), 0, eol)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = PutRecordToRouting(context.Background(), d, pubk, 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(), privk, h)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = verifyCanResolve(resolver, 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
|
|
}
|