mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-01 14:28:02 +08:00
95 lines
3.8 KiB
Go
95 lines
3.8 KiB
Go
package namesys
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
context "context"
|
|
|
|
opts "github.com/ipfs/go-ipfs/namesys/opts"
|
|
path "github.com/ipfs/go-ipfs/path"
|
|
"github.com/ipfs/go-ipfs/unixfs"
|
|
|
|
ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
|
|
dssync "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore/sync"
|
|
offroute "gx/ipfs/QmZRcGYvxdauCd7hHnMYLYqcZRaDjv24c7eUNyJojAcdBb/go-ipfs-routing/offline"
|
|
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
|
|
)
|
|
|
|
type mockResolver struct {
|
|
entries map[string]string
|
|
}
|
|
|
|
func testResolution(t *testing.T, resolver Resolver, name string, depth uint, expected string, expError error) {
|
|
p, err := resolver.Resolve(context.Background(), name, opts.Depth(depth))
|
|
if 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) resolveOnce(ctx context.Context, name string, opts *opts.ResolveOpts) (path.Path, error) {
|
|
return path.ParsePath(r.entries[name])
|
|
}
|
|
|
|
func mockResolverOne() *mockResolver {
|
|
return &mockResolver{
|
|
entries: map[string]string{
|
|
"QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy": "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj",
|
|
"QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n": "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy",
|
|
"QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD": "/ipns/ipfs.io",
|
|
},
|
|
}
|
|
}
|
|
|
|
func mockResolverTwo() *mockResolver {
|
|
return &mockResolver{
|
|
entries: map[string]string{
|
|
"ipfs.io": "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n",
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestNamesysResolution(t *testing.T) {
|
|
r := &mpns{
|
|
resolvers: map[string]resolver{
|
|
"dht": mockResolverOne(),
|
|
"dns": 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)
|
|
}
|
|
|
|
func TestPublishWithCache0(t *testing.T) {
|
|
dst := dssync.MutexWrap(ds.NewMapDatastore())
|
|
priv, _, err := ci.GenerateKeyPair(ci.RSA, 1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
routing := offroute.NewOfflineRouter(dst, priv)
|
|
|
|
nsys := NewNameSystem(routing, dst, 0)
|
|
p, err := path.ParsePath(unixfs.EmptyDirNode().Cid().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
nsys.Publish(context.Background(), priv, p)
|
|
}
|