forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_test.go
93 lines (88 loc) · 2.47 KB
/
dns_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func stringPointerForString(s string) *string {
return &s
}
func Test_dnsMapFailure(t *testing.T) {
tests := []struct {
name string
failure *string
want *string
}{{
name: "nil",
failure: nil,
want: nil,
}, {
name: "nxdomain",
failure: stringPointerForString(netxlite.FailureDNSNXDOMAINError),
want: stringPointerForString(model.THDNSNameError),
}, {
name: "no answer",
failure: stringPointerForString(netxlite.FailureDNSNoAnswer),
want: nil,
}, {
name: "non recoverable failure",
failure: stringPointerForString(netxlite.FailureDNSNonRecoverableFailure),
want: stringPointerForString("dns_server_failure"),
}, {
name: "refused",
failure: stringPointerForString(netxlite.FailureDNSRefusedError),
want: stringPointerForString("dns_server_failure"),
}, {
name: "server misbehaving",
failure: stringPointerForString(netxlite.FailureDNSServerMisbehaving),
want: stringPointerForString("dns_server_failure"),
}, {
name: "temporary failure",
failure: stringPointerForString(netxlite.FailureDNSTemporaryFailure),
want: stringPointerForString("dns_server_failure"),
}, {
name: "anything else",
failure: stringPointerForString(netxlite.FailureEOFError),
want: stringPointerForString("unknown_error"),
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := dnsMapFailure(tt.failure)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Fatal(diff)
}
})
}
}
func TestDNSDo(t *testing.T) {
t.Run("returns non-nil addresses list on nxdomin", func(t *testing.T) {
ctx := context.Background()
config := &dnsConfig{
Domain: "www.ooni.nonexistent",
Logger: model.DiscardLogger,
NewResolver: func(model.Logger) model.Resolver {
return &mocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return nil, netxlite.ErrOODNSNoSuchHost
},
MockCloseIdleConnections: func() {
// nothing
},
}
},
Out: make(chan model.THDNSResult, 1),
Wg: &sync.WaitGroup{},
}
config.Wg.Add(1)
dnsDo(ctx, config)
config.Wg.Wait()
resp := <-config.Out
if resp.Addrs == nil || len(resp.Addrs) != 0 {
t.Fatal("returned nil Addrs or Addrs containing replies")
}
})
}