github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/namesys/namesys_test.go (about)

     1  package namesys
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
     8  
     9  	path "github.com/ipfs/go-ipfs/path"
    10  )
    11  
    12  type mockResolver struct {
    13  	entries map[string]string
    14  }
    15  
    16  func testResolution(t *testing.T, resolver Resolver, name string, depth int, expected string, expError error) {
    17  	p, err := resolver.ResolveN(context.Background(), name, depth)
    18  	if err != expError {
    19  		t.Fatal(fmt.Errorf(
    20  			"Expected %s with a depth of %d to have a '%s' error, but got '%s'",
    21  			name, depth, expError, err))
    22  	}
    23  	if p.String() != expected {
    24  		t.Fatal(fmt.Errorf(
    25  			"%s with depth %d resolved to %s != %s",
    26  			name, depth, p.String(), expected))
    27  	}
    28  }
    29  
    30  func (r *mockResolver) resolveOnce(ctx context.Context, name string) (path.Path, error) {
    31  	return path.ParsePath(r.entries[name])
    32  }
    33  
    34  func mockResolverOne() *mockResolver {
    35  	return &mockResolver{
    36  		entries: map[string]string{
    37  			"QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy": "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj",
    38  			"QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n": "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy",
    39  			"QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD": "/ipns/ipfs.io",
    40  		},
    41  	}
    42  }
    43  
    44  func mockResolverTwo() *mockResolver {
    45  	return &mockResolver{
    46  		entries: map[string]string{
    47  			"ipfs.io": "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n",
    48  		},
    49  	}
    50  }
    51  
    52  func TestNamesysResolution(t *testing.T) {
    53  	r := &mpns{
    54  		resolvers: map[string]resolver{
    55  			"one": mockResolverOne(),
    56  			"two": mockResolverTwo(),
    57  		},
    58  	}
    59  
    60  	testResolution(t, r, "Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
    61  	testResolution(t, r, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
    62  	testResolution(t, r, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
    63  	testResolution(t, r, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", 1, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion)
    64  	testResolution(t, r, "/ipns/ipfs.io", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
    65  	testResolution(t, r, "/ipns/ipfs.io", 1, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion)
    66  	testResolution(t, r, "/ipns/ipfs.io", 2, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion)
    67  	testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", DefaultDepthLimit, "/ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj", nil)
    68  	testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 1, "/ipns/ipfs.io", ErrResolveRecursion)
    69  	testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 2, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion)
    70  	testResolution(t, r, "/ipns/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", 3, "/ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy", ErrResolveRecursion)
    71  }