~samwhited/xmpp

ref: fa99f66baa026a0ad3be622a09cd7d71d22db953 xmpp/jid/jid_test.go -rw-r--r-- 5.0 KiB
fa99f66bSam Whited Remove TODO in bind (I decided to leave it as is) 5 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
// 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 (
	"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 _ 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 TestMarshalEmpty(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},
	} {
		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")
	}
}

const allescaped = `\20\22\26\27\2f\3a\3c\3e\40\5c`

func TestEscape(t *testing.T) {
	for _, test := range []struct {
		unescaped, escaped string
	}{
		{escape, allescaped},
		{`nothingtodohere`, `nothingtodohere`},
		{"", ""},
	} {
		if e := Escape(test.unescaped); e != test.escaped {
			t.Errorf("Escaped localpart should be `%s` but got: `%s`", test.escaped, e)
		}
	}
}

func TestUnescape(t *testing.T) {
	for _, test := range []struct {
		escaped, unescaped string
	}{
		{allescaped, escape},
		{`\20\3c\3C\aa\\\`, ` <<\aa\\\`},
		{"nothingtodohere", "nothingtodohere"},
		{"", ""},
	} {
		if u := Unescape(test.escaped); u != test.unescaped {
			t.Errorf("Unescaped localpart should be `%s` but got: `%s`", test.unescaped, u)
		}
	}
}