~samwhited/xmpp

ref: a2ffaf710170d692a9289c2b428a4efa90c8e703 xmpp/jid/jid_test.go -rw-r--r-- 9.0 KiB
a2ffaf71Sam Whited all: update deps 3 years 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2015 Sam Whited.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package jid_test

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"net"
	"strings"
	"testing"

	"mellium.im/xmpp/jid"
)

// Compile time checks to make sure that JID and *jid.JID match several interfaces.
var (
	_ fmt.Stringer        = (*jid.JID)(nil)
	_ xml.MarshalerAttr   = (*jid.JID)(nil)
	_ xml.UnmarshalerAttr = (*jid.JID)(nil)
	_ xml.Marshaler       = (*jid.JID)(nil)
	_ xml.Unmarshaler     = (*jid.JID)(nil)
	_ net.Addr            = (*jid.JID)(nil)
)

func TestValidJIDs(t *testing.T) {
	for i, tc := range [...]struct {
		jid, lp, dp, rp string
	}{
		0: {"example.net", "", "example.net", ""},
		1: {"example.net/rp", "", "example.net", "rp"},
		2: {"mercutio@example.net", "mercutio", "example.net", ""},
		3: {"mercutio@example.net/rp", "mercutio", "example.net", "rp"},
		4: {"mercutio@example.net/rp@rp", "mercutio", "example.net", "rp@rp"},
		5: {"mercutio@example.net/rp@rp/rp", "mercutio", "example.net", "rp@rp/rp"},
		6: {"mercutio@example.net/@", "mercutio", "example.net", "@"},
		7: {"mercutio@example.net//@", "mercutio", "example.net", "/@"},
		8: {"mercutio@example.net//@//", "mercutio", "example.net", "/@//"},
		9: {"[::1]", "", "[::1]", ""},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			j, err := jid.Parse(tc.jid)
			if err != nil {
				t.Fatal(err)
			}
			if j.Domainpart() != tc.dp {
				t.Errorf("Got domainpart %s but expected %s", j.Domainpart(), tc.dp)
			}
			if j.Localpart() != tc.lp {
				t.Errorf("Got localpart %s but expected %s", j.Localpart(), tc.lp)
			}
			if j.Resourcepart() != tc.rp {
				t.Errorf("Got resourcepart %s but expected %s", j.Resourcepart(), tc.rp)
			}
		})
	}
}

var invalidutf8 = string([]byte{0xff, 0xfe, 0xfd})

func TestInvalidParseJIDs(t *testing.T) {
	for i, tc := range [...]string{
		0:  "test@/test",
		1:  invalidutf8 + "@example.com/rp",
		2:  invalidutf8 + "/rp",
		3:  invalidutf8,
		4:  "example.com/" + invalidutf8,
		5:  "lp@/rp",
		6:  `b"d@example.net`,
		7:  `b&d@example.net`,
		8:  `b'd@example.net`,
		9:  `b:d@example.net`,
		10: `b<d@example.net`,
		11: `b>d@example.net`,
		12: `e@example.net/`,
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			_, err := jid.Parse(tc)
			if err == nil {
				t.Errorf("Expected JID %s to fail", tc)
			}
		})
	}
}

func TestInvalidNewJIDs(t *testing.T) {
	for i, tc := range [...]struct {
		lp, dp, rp string
	}{
		0: {strings.Repeat("a", 1024), "example.net", ""},
		1: {"e", "example.net", strings.Repeat("a", 1024)},
		2: {"b/d", "example.net", ""},
		3: {"b@d", "example.net", ""},
		4: {"e", "[example.net]", ""},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			_, err := jid.New(tc.lp, tc.dp, tc.rp)
			if err == nil {
				t.Errorf("Expected composition of JID parts %s to fail", tc)
			}
		})
	}
}

func TestMarshalAttrEmpty(t *testing.T) {
	attr, err := ((*jid.JID)(nil)).MarshalXMLAttr(xml.Name{})
	switch {
	case err != nil:
		t.Logf("Marshaling an empty JID to an attr should not error but got %v\n", err)
		t.Fail()
	case attr != xml.Attr{}:
		t.Logf("Error marshaling empty JID expected Attr{} but got: %+v\n", err)
		t.Fail()
	}
}

func TestMustParsePanics(t *testing.T) {
	for i, tc := range [...]struct {
		jid         string
		shouldPanic bool
	}{
		0: {"@me", true},
		1: {"@`me", true},
		2: {"e@example.net", false},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			defer func() {
				r := recover()
				switch {
				case tc.shouldPanic && r == nil:
					t.Error("Must parse should panic on invalid JID")
				case !tc.shouldPanic && r != nil:
					t.Error("Must parse should not panic on valid JID")
				}
			}()
			jid.MustParse(tc.jid)
		})
	}
}

func TestEqual(t *testing.T) {
	m := jid.MustParse("mercutio@example.net/test")
	for i, tc := range [...]struct {
		j1, j2 *jid.JID
		eq     bool
	}{
		0: {m, jid.MustParse("mercutio@example.net/test"), true},
		1: {m.Bare(), jid.MustParse("mercutio@example.net"), true},
		2: {m.Domain(), jid.MustParse("example.net"), true},
		3: {m, jid.MustParse("mercutio@example.net/nope"), false},
		4: {m, jid.MustParse("mercutio@e.com/test"), false},
		5: {m, jid.MustParse("m@example.net/test"), false},
		6: {(*jid.JID)(nil), (*jid.JID)(nil), true},
		7: {m, (*jid.JID)(nil), false},
		8: {(*jid.JID)(nil), m, false},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			switch {
			case tc.eq && !tc.j1.Equal(tc.j2):
				t.Errorf("JIDs %s and %s should be equal", tc.j1, tc.j2)
			case !tc.eq && tc.j1.Equal(tc.j2):
				t.Errorf("JIDs %s and %s should not be equal", tc.j1, tc.j2)
			}
		})
	}
}

func TestNetwork(t *testing.T) {
	if jid.MustParse("test").Network() != "xmpp" {
		t.Error("Network should be `xmpp`")
	}
}

func TestCopy(t *testing.T) {
	m := jid.MustParse("mercutio@example.net/test")
	m2 := m.Copy()
	switch {
	case !m.Equal(m2):
		t.Error("Copying a JID should still result in equal JIDs")
	case m == m2:
		t.Error("Copying a JID should result in a different JID pointer")
	}
}

func TestWithResource(t *testing.T) {
	for i, tc := range [...]struct {
		jid string
		res string
		err bool
	}{
		0: {"mercutio@example.net/test", "new", false},
		1: {"mercutio@example.net/test", invalidutf8, true},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			old := jid.MustParse(tc.jid)
			new, err := old.WithResource(tc.res)
			switch {
			case (err != nil) && !tc.err:
				t.Fatal("Unexpected error", err)
			case tc.err:
				return
			case old == new:
				t.Fatal("Expected different pointers for JID copy")
			}
			if old.String() != tc.jid {
				t.Fatalf("WithResource should clone data")
			}
			if r := new.Resourcepart(); r != tc.res {
				t.Errorf("Unexpected resourcepart: want=`%s', got=`%s'", tc.res, r)
			}
			if new.Domainpart() != old.Domainpart() {
				t.Errorf("Unexpected domainpart mutation: want=`%s', got=`%s'", old.Domainpart(), new.Domainpart())
			}
			if new.Localpart() != old.Localpart() {
				t.Errorf("Unexpected localpart mutation: want=`%s', got=`%s'", old.Localpart(), new.Localpart())
			}
		})
	}
}

func TestMarshalXML(t *testing.T) {
	// Test default marshaling
	j := jid.MustParse("feste@shakespeare.lit")
	b, err := xml.Marshal(j)
	switch expected := `<JID>feste@shakespeare.lit</JID>`; {
	case err != nil:
		t.Error(err)
	case string(b) != expected:
		t.Errorf("Error marshaling JID, expected `%s` but got `%s`", expected, string(b))
	}

	// Test encoding with a custom element
	j = jid.MustParse("feste@shakespeare.lit/ilyria")
	var buf bytes.Buffer
	start := xml.StartElement{Name: xml.Name{Space: "", Local: "item"}, Attr: []xml.Attr{}}
	e := xml.NewEncoder(&buf)
	err = e.EncodeElement(j, start)
	switch expected := `<item>feste@shakespeare.lit/ilyria</item>`; {
	case err != nil:
		t.Error(err)
	case buf.String() != expected:
		t.Errorf("Error encoding JID, expected `%s` but got `%s`", expected, buf.String())
	}

	// Test encoding a nil JID
	j = (*jid.JID)(nil)
	b, err = xml.Marshal(j)
	switch expected := ``; {
	case err != nil:
		t.Error(err)
	case string(b) != expected:
		t.Errorf("Error marshaling JID, expected `%s` but got `%s`", expected, string(b))
	}
}

func TestUnmarshal(t *testing.T) {
	for i, test := range [...]struct {
		xml string
		jid *jid.JID
		err bool
	}{
		0: {`<item>feste@shakespeare.lit/ilyria</item>`, jid.MustParse("feste@shakespeare.lit/ilyria"), false},
		1: {`<jid>feste@shakespeare.lit</jid>`, jid.MustParse("feste@shakespeare.lit"), false},
		2: {`<oops>feste@shakespeare.lit</bad>`, nil, true},
		3: {`<item></item>`, nil, true},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			j := &jid.JID{}
			err := xml.Unmarshal([]byte(test.xml), j)
			switch {
			case test.err && err == nil:
				t.Errorf("Expected unmarshaling `%s` as a JID to return an error", test.xml)
			case !test.err && err != nil:
				t.Error("Unexpected error:", err)
			case err != nil:
				return
			case !test.jid.Equal(j):
				t.Errorf("Expected JID to unmarshal to `%s` but got `%s`", test.jid, j)
			}
		})
	}
}

func TestString(t *testing.T) {
	for i, tc := range [...]string{
		0: "example.com",
		1: "feste@example.com",
		2: "feste@example.com/testabc",
		3: "example.com/test",
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			j := jid.MustParse(tc)

			// Check that String() and jid.Parse() are inverse operations
			if js := j.String(); js != tc {
				t.Errorf("want=%s, got=%s", tc, js)
			}

			// Check that String() does not allocate

			// If the code is instrumented for coverage, allocations that happen there
			// break this test. This is annoying, but I'm not sure of a better way to
			// fix it.
			var okallocs float64
			if testing.CoverMode() != "" {
				okallocs = 3.0
			}

			if n := testing.AllocsPerRun(1000, func() { _ = j.String() }); n > okallocs {
				t.Errorf("got %f allocs, want %f", n, okallocs)
			}
		})
	}
}

// Malloc tests may be flakey under GCC until it improves its escape analysis.

func TestSplitMallocs(t *testing.T) {
	n := testing.AllocsPerRun(1000, func() {
		jid.SplitString("olivia@example.net/ilyria")
	})
	if n > 0 {
		t.Errorf("got %f allocs, want 0", n)
	}
}