namesys/dns_test: Add DNS resolution tests with a mock resolver
W. Trevor King committed
May 16, 2015 at 09:54 UTC
c9fceeb19b96ef54e3b46b6301f11e9cfbffde8b
1 file changed
+86
namesys/dns_test.go
+86
@@ -1,9 +1,24 @@
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
10
+type mockDNS struct {
11
+ entries map[string][]string
12
+}
13
+
14
+func (m *mockDNS) lookupTXT(name string) (txt []string, err error) {
15
+ txt, ok := m.entries[name]
16
+ if !ok {
17
+ return nil, fmt.Errorf("No TXT entry for %s", name)
18
+ }
19
+ return txt, nil
20
+}
21
+
22
func TestDnsEntryParsing(t *testing.T) {
23
goodEntries := []string{
24
"QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD",
@@ -40,3 +55,74 @@ func TestDnsEntryParsing(t *testing.T) {
55
}
56
}
57
}
58
+
59
+func newMockDNS() *mockDNS {
60
+ return &mockDNS{
61
+ entries: map[string][]string{
62
+ "multihash.example.com": []string{
63
+ "dnslink=QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD",
64
+ },
65
+ "ipfs.example.com": []string{
66
+ "dnslink=/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD",
67
+ },
68
+ "dns1.example.com": []string{
69
+ "dnslink=/ipns/ipfs.example.com",
70
+ },
71
+ "dns2.example.com": []string{
72
+ "dnslink=/ipns/dns1.example.com",
73
+ },
74
+ "multi.example.com": []string{
75
+ "some stuff",
76
+ "dnslink=/ipns/dns1.example.com",
77
+ "masked dnslink=/ipns/example.invalid",
78
+ },
79
+ "equals.example.com": []string{
80
+ "dnslink=/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD/=equals",
81
+ },
82
+ "loop1.example.com": []string{
83
+ "dnslink=/ipns/loop2.example.com",
84
+ },
85
+ "loop2.example.com": []string{
86
+ "dnslink=/ipns/loop1.example.com",
87
+ },
88
+ "bad.example.com": []string{
89
+ "dnslink=",
90
+ },
91
+ },
92
+ }
93
+}
94
+
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
+
109
+func TestDNSResolution(t *testing.T) {
110
+ mock := newMockDNS()
111
+ r := &DNSResolver{lookupTXT: mock.lookupTXT}
112
+ testResolution(t, r, "multihash.example.com", DefaultDepthLimit, "/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", nil)
113
+ testResolution(t, r, "ipfs.example.com", DefaultDepthLimit, "/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", nil)
114
+ testResolution(t, r, "dns1.example.com", DefaultDepthLimit, "/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", nil)
115
+ testResolution(t, r, "dns1.example.com", 1, "/ipns/ipfs.example.com", ErrResolveRecursion)
116
+ testResolution(t, r, "dns2.example.com", DefaultDepthLimit, "/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", nil)
117
+ testResolution(t, r, "dns2.example.com", 1, "/ipns/dns1.example.com", ErrResolveRecursion)
118
+ testResolution(t, r, "dns2.example.com", 2, "/ipns/ipfs.example.com", ErrResolveRecursion)
119
+ testResolution(t, r, "multi.example.com", DefaultDepthLimit, "/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD", nil)
120
+ testResolution(t, r, "multi.example.com", 1, "/ipns/dns1.example.com", ErrResolveRecursion)
121
+ testResolution(t, r, "multi.example.com", 2, "/ipns/ipfs.example.com", ErrResolveRecursion)
122
+ testResolution(t, r, "equals.example.com", DefaultDepthLimit, "/ipfs/QmY3hE8xgFCjGcz6PHgnvJz5HZi1BaKRfPkn1ghZUcYMjD/=equals", nil)
123
+ testResolution(t, r, "loop1.example.com", 1, "/ipns/loop2.example.com", ErrResolveRecursion)
124
+ testResolution(t, r, "loop1.example.com", 2, "/ipns/loop1.example.com", ErrResolveRecursion)
125
+ testResolution(t, r, "loop1.example.com", 3, "/ipns/loop2.example.com", ErrResolveRecursion)
126
+ testResolution(t, r, "loop1.example.com", DefaultDepthLimit, "/ipns/loop1.example.com", ErrResolveRecursion)
127
+ testResolution(t, r, "bad.example.com", DefaultDepthLimit, "", ErrResolveFailed)
128
+}