Skip to content

Commit

Permalink
namesys/namesys_test: Excercise mpns.ResolveN
Browse files Browse the repository at this point in the history
Shifting the generic testResolution helper from the protocol-specific
dns_test.go to the generic namesys_test.go.
  • Loading branch information
wking committed May 20, 2015
1 parent c9fceeb commit ce015d2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
16 changes: 0 additions & 16 deletions namesys/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package namesys
import (
"fmt"
"testing"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
)

type mockDNS struct {
Expand Down Expand Up @@ -92,20 +90,6 @@ func newMockDNS() *mockDNS {
}
}

func testResolution(t *testing.T, resolver Resolver, name string, depth int, expected string, expError error) {
p, err := resolver.ResolveN(context.Background(), name, 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 TestDNSResolution(t *testing.T) {
mock := newMockDNS()
r := &DNSResolver{lookupTXT: mock.lookupTXT}
Expand Down
71 changes: 71 additions & 0 deletions namesys/namesys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package namesys

import (
"fmt"
"testing"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

path "github.com/ipfs/go-ipfs/path"
)

type mockResolver struct {
entries map[string]string
}

func testResolution(t *testing.T, resolver Resolver, name string, depth int, expected string, expError error) {
p, err := resolver.ResolveN(context.Background(), name, 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) (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{
"one": mockResolverOne(),
"two": mockResolverTwo(),
},
}

testResolution(t, r, "Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
testResolution(t, r, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", 1, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion)
testResolution(t, r, "/ipns/ipfs.io", 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", 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)
}

0 comments on commit ce015d2

Please sign in to comment.