~samwhited/xmpp

ref: dcc6772f31033948a00a7dfb9232a120120be5b7 xmpp/blocklist/blocking_test.go -rw-r--r-- 6.6 KiB
dcc6772fSam Whited all: add strict namespace checking to mux 26 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package blocklist_test

import (
	"context"
	"encoding/xml"
	"errors"
	"reflect"
	"strconv"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/blocklist"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/stanza"
)

var matchTestCases = [...]struct {
	j1, j2 jid.JID
	result bool
}{
	// Full JID match
	0: {j1: jid.MustParse("user@domain/resource"), j2: jid.MustParse("user@domain/resource"), result: true},
	1: {j1: jid.MustParse("otheruser@domain/resource"), j2: jid.MustParse("user@domain/resource"), result: false},
	2: {j1: jid.MustParse("user@otherdomain/resource"), j2: jid.MustParse("user@domain/resource"), result: false},
	3: {j1: jid.MustParse("user@domain/otherresource"), j2: jid.MustParse("user@domain/resource"), result: false},
	4: {j1: jid.MustParse("otherdomain/resource"), j2: jid.MustParse("user@domain/resource"), result: false},
	5: {j1: jid.MustParse("user@domain"), j2: jid.MustParse("user@domain/resource"), result: false},
	6: {j1: jid.MustParse("domain"), j2: jid.MustParse("user@domain/resource"), result: false},

	// Bare JID match
	7:  {j1: jid.MustParse("user@domain"), j2: jid.MustParse("user@domain"), result: true},
	8:  {j1: jid.MustParse("user@domain/res"), j2: jid.MustParse("user@domain"), result: true},
	9:  {j1: jid.MustParse("domain"), j2: jid.MustParse("user@domain"), result: false},
	10: {j1: jid.MustParse("domain/res"), j2: jid.MustParse("user@domain"), result: false},
	11: {j1: jid.MustParse("otheruser@domain"), j2: jid.MustParse("user@domain"), result: false},

	// Full domain match
	12: {j1: jid.MustParse("domain/resource"), j2: jid.MustParse("domain/resource"), result: true},
	13: {j1: jid.MustParse("user@domain/resource"), j2: jid.MustParse("domain/resource"), result: true},
	14: {j1: jid.MustParse("domain"), j2: jid.MustParse("domain/resource"), result: false},
	15: {j1: jid.MustParse("user@domain"), j2: jid.MustParse("domain/resource"), result: false},
	16: {j1: jid.MustParse("otherdomain/resource"), j2: jid.MustParse("domain/resource"), result: false},
	17: {j1: jid.MustParse("domain/otherresource"), j2: jid.MustParse("domain/resource"), result: false},

	// Bare domain match
	18: {j1: jid.MustParse("domain"), j2: jid.MustParse("domain"), result: true},
	19: {j1: jid.MustParse("domain/resource"), j2: jid.MustParse("domain"), result: true},
	20: {j1: jid.MustParse("user@domain"), j2: jid.MustParse("domain"), result: true},
	21: {j1: jid.MustParse("user@domain/resource"), j2: jid.MustParse("domain"), result: true},
	22: {j1: jid.MustParse("otherdomain"), j2: jid.MustParse("domain"), result: false},
	23: {j1: jid.MustParse("user@otherdomain"), j2: jid.MustParse("domain"), result: false},
	24: {j1: jid.MustParse("user@otherdomain/res"), j2: jid.MustParse("domain"), result: false},
	25: {j1: jid.MustParse("otherdomain/res"), j2: jid.MustParse("domain"), result: false},
}

func TestMatch(t *testing.T) {
	for i, tc := range matchTestCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			result := blocklist.Match(tc.j1, tc.j2)
			if tc.result != result {
				t.Errorf("unexpected result: got=%t, want=%t", result, tc.result)
			}
		})
	}
}

var testCases = [...]struct {
	items []jid.JID
	err   error
}{
	0: {},
	1: {
		items: []jid.JID{
			jid.MustParse("juliet@example.com"),
			jid.MustParse("benvolio@example.org"),
		},
	},
}

func TestFetch(t *testing.T) {
	var IQ = stanza.IQ{ID: "123"}
	for i, tc := range testCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			var list []jid.JID
			h := blocklist.Handler{
				Block: func(j jid.JID) {
					list = append(list, j)
				},
				Unblock: func(j jid.JID) {
					b := list[:0]
					for _, x := range list {
						if !j.Equal(x) {
							b = append(b, x)
						}
					}
					list = b
				},
				UnblockAll: func() {
					list = list[:0]
				},
				List: func(j chan<- jid.JID) {
					for _, jj := range list {
						j <- jj
					}
				},
			}
			m := mux.New(ns.Client, blocklist.Handle(h))
			cs := xmpptest.NewClientServer(xmpptest.ServerHandler(m))

			err := blocklist.AddIQ(context.Background(), IQ, cs.Client, tc.items...)
			if err != nil {
				t.Fatalf("error setting the blocklist: %v", err)
			}

			iter := blocklist.FetchIQ(context.Background(), IQ, cs.Client)
			items := make([]jid.JID, 0, len(tc.items))
			for iter.Next() {
				items = append(items, iter.JID())
			}
			if err := iter.Err(); err != tc.err {
				t.Errorf("wrong error after iter: want=%v, got=%v", tc.err, err)
			}
			iter.Close()

			// Don't try to compare nil and empty slice with DeepEqual
			if len(tc.items) == 0 {
				tc.items = make([]jid.JID, 0)
			}

			if !reflect.DeepEqual(items, tc.items) {
				t.Errorf("wrong items:\nwant=\n%+v,\ngot=\n%+v", tc.items, items)
			}

			// Test removing one item.
			if len(tc.items) > 0 {
				err = blocklist.RemoveIQ(context.Background(), IQ, cs.Client, tc.items[0])
				if err != nil {
					t.Errorf("error removing first blocklist item: %v", err)
				}
				if !reflect.DeepEqual(list, tc.items[1:]) {
					t.Errorf("wrong items after removing %s:\nwant=\n%+v,\ngot=\n%+v", tc.items[0], tc.items[1:], list)
				}
			}

			// Test removing all items.
			err = blocklist.RemoveIQ(context.Background(), IQ, cs.Client)
			if err != nil {
				t.Errorf("error removing remaining blocklist items: %v", err)
			}
			if len(list) > 0 {
				t.Errorf("failed to remove remaining items")
			}
		})
	}
}

func TestFetchNoStart(t *testing.T) {
	cs := xmpptest.NewClientServer(
		xmpptest.ServerHandlerFunc(func(e xmlstream.TokenReadEncoder, start *xml.StartElement) error {
			const resp = `<iq id="123" type="result"><blocklist xmlns='urn:xmpp:blocklist'><!-- comment --></blocklist></iq>`
			_, err := xmlstream.Copy(e, xml.NewDecoder(strings.NewReader(resp)))
			return err
		}),
	)
	iter := blocklist.FetchIQ(context.Background(), stanza.IQ{ID: "123"}, cs.Client)
	for iter.Next() {
		// Just iterate
	}
	if err := iter.Err(); err != nil {
		t.Errorf("Wrong error after iter: want=nil, got=%q", err)
	}
	iter.Close()
}

type errReadWriter struct{}

func (errReadWriter) Write([]byte) (int, error) {
	return 0, errors.New("called Write on errReadWriter")
}

func (errReadWriter) Read([]byte) (int, error) {
	return 0, errors.New("called Read on errReadWriter")
}

func TestErroredDoesNotPanic(t *testing.T) {
	s := xmpptest.NewClientSession(0, errReadWriter{})
	iter := blocklist.Fetch(context.Background(), s)
	if iter.Next() {
		t.Errorf("expected false from call to next")
	}
	if err := iter.Close(); err != nil {
		t.Errorf("got unexpected error closing iter: %v", err)
	}
}