~samwhited/xmpp

xmpp/internal/discover/lookup_integration_test.go -rw-r--r-- 2.4 KiB
6ac3e8cd — TAKI MEKHALFA carbons: add Private method 5 days ago
                                                                                
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2018 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

//go:build integration
// +build integration

package discover_test

import (
	"context"
	"net"
	"strconv"
	"testing"

	"mellium.im/xmpp/internal/discover"
	"mellium.im/xmpp/jid"
)

var testConvJID = jid.MustParse("sam@conversations.im")

var lookupTests = [...]struct {
	resolver *net.Resolver
	service  string
	addr     jid.JID
	addrs    []*net.SRV
	err      error
}{
	0: {
		err: discover.ErrInvalidService,
	},
	1: {
		service: "xmpp-client",
		addr:    jid.MustParse("me@example.net"),
		addrs:   discover.FallbackRecords("xmpp-client", "example.net"),
	},
	2: {
		service: "xmpp-client",
		addr:    testConvJID,
		addrs: []*net.SRV{
			{
				Target:   "xmpp.conversations.im.",
				Port:     5222,
				Priority: 5,
				Weight:   0,
			},
			{
				Target:   "xmpps.conversations.im.",
				Port:     80,
				Priority: 10,
				Weight:   0,
			},
		},
	},
	3: {
		service: "xmpp-server",
		addr:    testConvJID,
		addrs: []*net.SRV{
			{
				Target:   "xmpp.conversations.im.",
				Port:     5269,
				Priority: 5,
				Weight:   0,
			},
		},
	},
	4: {
		service: "xmpp-server",
		addr:    jid.MustParse("samwhited.com"),
		addrs: []*net.SRV{
			{
				Target:   "xmpp-hosting.conversations.im.",
				Port:     5269,
				Priority: 1,
				Weight:   1,
			},
		},
	},
}

func TestIntegrationLookupService(t *testing.T) {
	for i, tc := range lookupTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			addrs, err := discover.LookupService(context.Background(), tc.resolver, tc.service, tc.addr)
			switch dnsErr := err.(type) {
			case nil:
				if err != tc.err {
					t.Errorf("got unexpected error: want=%q, got=%q", tc.err, err)
				}
			case *net.DNSError:
				var errStr string
				if tc.err != nil {
					errStr = tc.err.Error()
				}
				if dnsErr.Err != errStr {
					t.Errorf("got unexpected error: want=%q, got=%q", errStr, dnsErr.Error())
				}
			default:
				if err != tc.err {
					t.Errorf("got unexpected error: want=%q, got=%q", tc.err, err)
				}
			}
			if len(tc.addrs) != len(addrs) {
				for _, addr := range addrs {
					t.Logf("got addr: %+v", addr)
				}
				t.Fatalf("unexpected addrs: want=%d, got=%d", len(tc.addrs), len(addrs))
			}
			for i, addr := range tc.addrs {
				if *addr != *addrs[i] {
					t.Fatalf("unexpected addr at %d: want=%v, got=%v", i, *addr, *addrs[i])
				}
			}
		})
	}
}