namesys/namesys_test: Excercise mpns.ResolveN
Shifting the generic testResolution helper from the protocol-specific dns_test.go to the generic namesys_test.go.
W. Trevor King committed
May 19, 2015 at 12:48 UTC
ce015d241b799defe2d54ef5c86e0aed3805bbb9
2 files changed
+71
-16
namesys/dns_test.go
-16
@@ -3,8 +3,6 @@ package namesys
3
import (
4
"fmt"
5
"testing"
6
-
7
- context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
6
)
7
8
type mockDNS struct {
@@ -92,20 +90,6 @@ func newMockDNS() *mockDNS {
90
}
91
}
92
95
-func testResolution(t *testing.T, resolver Resolver, name string, depth int, expected string, expError error) {
96
- p, err := resolver.ResolveN(context.Background(), name, depth)
97
- if err != expError {
98
- t.Fatal(fmt.Errorf(
99
- "Expected %s with a depth of %d to have a '%s' error, but got '%s'",
100
- name, depth, expError, err))
101
- }
102
- if p.String() != expected {
103
- t.Fatal(fmt.Errorf(
104
- "%s with depth %d resolved to %s != %s",
105
- name, depth, p.String(), expected))
106
- }
107
-}
108
-
93
func TestDNSResolution(t *testing.T) {
94
mock := newMockDNS()
95
r := &DNSResolver{lookupTXT: mock.lookupTXT}
namesys/namesys_test.go
new
+71
@@ -0,0 +1,71 @@
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
+}