~samwhited/xmpp

ref: 52be89df867be9fc4e8ba83b0c3880887a6485ba xmpp/jid/jid_test.go -rw-r--r-- 6.5 KiB
52be89dfSam Whited form: Rename Option and FieldOption types 4 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
// 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

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

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

func TestValidJIDs(t *testing.T) {
	for _, jid := range []struct {
		jid, lp, dp, rp string
	}{
		{"example.net", "", "example.net", ""},
		{"example.net/rp", "", "example.net", "rp"},
		{"mercutio@example.net", "mercutio", "example.net", ""},
		{"mercutio@example.net/rp", "mercutio", "example.net", "rp"},
		{"mercutio@example.net/rp@rp", "mercutio", "example.net", "rp@rp"},
		{"mercutio@example.net/rp@rp/rp", "mercutio", "example.net", "rp@rp/rp"},
		{"mercutio@example.net/@", "mercutio", "example.net", "@"},
		{"mercutio@example.net//@", "mercutio", "example.net", "/@"},
		{"mercutio@example.net//@//", "mercutio", "example.net", "/@//"},
		{"[::1]", "", "[::1]", ""},
	} {
		j, err := Parse(jid.jid)
		switch {
		case err != nil:
			t.Error(err)
		case j.Domainpart() != jid.dp:
			t.Errorf("Got domainpart %s but expected %s", j.Domainpart(), jid.dp)
		case j.Localpart() != jid.lp:
			t.Errorf("Got localpart %s but expected %s", j.Localpart(), jid.lp)
		case j.Resourcepart() != jid.rp:
			t.Errorf("Got resourcepart %s but expected %s", j.Resourcepart(), jid.rp)
		}
	}
}

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

func TestInvalidParseJIDs(t *testing.T) {

	for _, jid := range []string{
		"test@/test",
		invalidutf8 + "@example.com/rp",
		invalidutf8 + "/rp",
		invalidutf8,
		"example.com/" + invalidutf8,
		"lp@/rp",
		`b"d@example.net`,
		`b&d@example.net`,
		`b'd@example.net`,
		`b:d@example.net`,
		`b<d@example.net`,
		`b>d@example.net`,
		`e@example.net/`,
	} {
		_, err := Parse(jid)
		if err == nil {
			t.Errorf("Expected JID %s to fail", jid)
		}
	}
}

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

func TestMarshalAttrEmpty(t *testing.T) {
	attr, err := ((*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) {
	handleErr := func(shouldPanic bool) {
		r := recover()
		switch {
		case shouldPanic && r == nil:
			t.Error("Must parse should panic on invalid JID")
		case !shouldPanic && r != nil:
			t.Error("Must parse should not panic on valid JID")
		}
	}
	for _, t := range []struct {
		jid         string
		shouldPanic bool
	}{
		{"@me", true},
		{"@`me", true},
		{"e@example.net", false},
	} {
		func() {
			defer handleErr(t.shouldPanic)
			MustParse(t.jid)
		}()
	}
}

func TestEqual(t *testing.T) {
	m := MustParse("mercutio@example.net/test")
	for _, test := range []struct {
		j1, j2 *JID
		eq     bool
	}{
		{m, MustParse("mercutio@example.net/test"), true},
		{m.Bare(), MustParse("mercutio@example.net"), true},
		{m.Domain(), MustParse("example.net"), true},
		{m, MustParse("mercutio@example.net/nope"), false},
		{m, MustParse("mercutio@e.com/test"), false},
		{m, MustParse("m@example.net/test"), false},
		{(*JID)(nil), (*JID)(nil), true},
		{m, (*JID)(nil), false},
		{(*JID)(nil), m, false},
	} {
		switch {
		case test.eq && !test.j1.Equal(test.j2):
			t.Errorf("JIDs %s and %s should be equal", test.j1, test.j2)
		case !test.eq && test.j1.Equal(test.j2):
			t.Errorf("JIDs %s and %s should not be equal", test.j1, test.j2)
		}
	}
}

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

func TestCopy(t *testing.T) {
	m := 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 TestMarshalXML(t *testing.T) {
	// Test default marshaling
	j := 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 = 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)(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 _, test := range []struct {
		xml string
		jid *JID
		err bool
	}{
		{`<item>feste@shakespeare.lit/ilyria</item>`, MustParse("feste@shakespeare.lit/ilyria"), false},
		{`<jid>feste@shakespeare.lit</jid>`, MustParse("feste@shakespeare.lit"), false},
		{`<oops>feste@shakespeare.lit</bad>`, nil, true},
		{`<item></item>`, nil, true},
	} {
		j := &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)
			continue
		case !test.err && err != nil:
			t.Error(err)
			continue
		case err != nil:
			continue
		case !test.jid.Equal(j):
			t.Errorf("Expected JID to unmarshal to `%s` but got `%s`", test.jid, j)
		}
	}
}

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

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