-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport_test.go
88 lines (73 loc) · 2.2 KB
/
transport_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
// SPDX-License-Identifier: GPL-3.0-or-later
package dnscore
import (
"context"
"errors"
"testing"
"github.com/miekg/dns"
)
func TestTransportQuery(t *testing.T) {
// create a canceled context so that we do not actually perform the query
ctx, cancel := context.WithCancel(context.Background())
cancel()
tests := []struct {
protocol Protocol
expectErr error
}{
{protocol: ProtocolUDP, expectErr: context.Canceled},
{protocol: ProtocolTCP, expectErr: context.Canceled},
{protocol: ProtocolDoT, expectErr: context.Canceled},
{protocol: ProtocolDoH, expectErr: context.Canceled},
{protocol: "", expectErr: ErrNoSuchTransportProtocol},
}
for _, tt := range tests {
t.Run(string(tt.protocol), func(t *testing.T) {
txp := &Transport{}
query := &dns.Msg{}
addr := NewServerAddr(tt.protocol, "")
resp, err := txp.Query(ctx, addr, query)
if !errors.Is(err, tt.expectErr) {
t.Errorf("expected %v error, got %v", tt.expectErr, err)
}
if resp != nil {
t.Errorf("expected nil response, got %v", resp)
}
})
}
}
func TestTransportQueryWithDuplicates(t *testing.T) {
// create a canceled context so that we do not actually perform the query
ctx, cancel := context.WithCancel(context.Background())
cancel()
tests := []struct {
protocol Protocol
expectErr error
}{
{protocol: ProtocolUDP, expectErr: context.Canceled},
{protocol: ProtocolTCP, expectErr: ErrTransportCannotReceiveDuplicates},
{protocol: ProtocolDoT, expectErr: ErrTransportCannotReceiveDuplicates},
{protocol: ProtocolDoH, expectErr: ErrTransportCannotReceiveDuplicates},
}
for _, tt := range tests {
t.Run(string(tt.protocol), func(t *testing.T) {
txp := &Transport{}
query := &dns.Msg{}
addr := NewServerAddr(tt.protocol, "")
out := txp.QueryWithDuplicates(ctx, addr, query)
var results []*MessageOrError
for result := range out {
results = append(results, result)
}
if len(results) != 1 {
t.Errorf("expected 1 result, got %d", len(results))
}
r0 := results[0]
if !errors.Is(r0.Err, tt.expectErr) {
t.Errorf("expected %v error, got %v", tt.expectErr, r0.Err)
}
if r0.Msg != nil {
t.Errorf("expected nil response, got %v", r0.Msg)
}
})
}
}