-
Notifications
You must be signed in to change notification settings - Fork 63
/
dotclient_test.go
52 lines (45 loc) · 1.51 KB
/
dotclient_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
package rdns
import (
"crypto/tls"
"encoding/pem"
"os"
"path/filepath"
"testing"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestDoTClientSimple(t *testing.T) {
d, _ := NewDoTClient("test-dot", "dns.google:853", DoTClientOptions{})
q := new(dns.Msg)
q.SetQuestion("cloudflare.com.", dns.TypeA)
r, err := d.Resolve(q, ClientInfo{})
require.NoError(t, err)
require.NotEmpty(t, r.Answer)
}
func TestDoTClientCA(t *testing.T) {
// Read the server certificate from the public server write it to a temp file
conn, err := tls.Dial("tcp", "1.1.1.1:853", nil)
require.NoError(t, err)
state := conn.ConnectionState()
crt := state.PeerCertificates[0]
crtEncoded := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: crt.Raw})
crtFile := filepath.Join(t.TempDir(), "certificate.pem")
err = os.WriteFile(crtFile, crtEncoded, 0644)
require.NoError(t, err)
conn.Close()
// Create a config with CA using the temp file
tlsConfig, err := TLSClientConfig(crtFile, "", "", "")
require.NoError(t, err)
// DoT client with valid CA
d, _ := NewDoTClient("test-dot", "1.1.1.1:853", DoTClientOptions{TLSConfig: tlsConfig})
q := new(dns.Msg)
q.SetQuestion("cloudflare.com.", dns.TypeA)
r, err := d.Resolve(q, ClientInfo{})
require.NoError(t, err)
require.NotEmpty(t, r.Answer)
// DoT client with invalid CA
d, _ = NewDoTClient("test-dot", "dns.google:853", DoTClientOptions{TLSConfig: tlsConfig})
q.SetQuestion("cloudflare.com.", dns.TypeA)
_, err = d.Resolve(q, ClientInfo{})
require.Error(t, err)
}