~samwhited/xmpp

ref: 65f402284d99ad3918ac019d219b67de5a4d8e21 xmpp/jid_test.go -rw-r--r-- 4.2 KiB
65f40228Sam Whited Use BSD license and move to Bitbucket 6 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
// Copyright 2014 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"
	"testing"
)

// Trying to create a new JID with an invalid UTF8 string should fail.
func TestNewInvalidUtf8Jid(t *testing.T) {
	invalid := string([]byte{0xff, 0xfe, 0xfd})
	_, err := NewJID(invalid + "@example.com/resourcepart")
	if err == nil || err != ErrorInvalidString {
		t.FailNow()
	}
}

// Trying to create a JID with an empty localpart should error.
func TestNewEmptyLocalPart(t *testing.T) {
	_, err := NewJID("@example.com/resourcepart")
	if err == nil || err != ErrorEmptyPart {
		t.FailNow()
	}
}

// Trying to create a JID with no localpart should work.
func TestNewNoLocalPart(t *testing.T) {
	jid, err := NewJID("example.com/resourcepart")
	if err != nil || jid.LocalPart() != "" {
		t.FailNow()
	}
}

// Trying to create a JID with no domainpart should error.
func TestNewNoDomainPart(t *testing.T) {
	_, err := NewJID("text@/resourcepart")
	if err == nil {
		t.FailNow()
	}
}

// Trying to create a JID with no anything should error.
func TestNewNoAnything(t *testing.T) {
	_, err := NewJID("@/")
	if err == nil {
		t.FailNow()
	}
}

// Trying to create a JID with '@' or '/' in the resourcepart should work.
func TestNewJidInResourcePart(t *testing.T) {
	_, err := NewJID("this/is@/fine")
	if err != nil {
		fmt.Println(err)
		t.FailNow()
	}
}

// Trying to create a JID with an empty resource part should error.
func TestNewEmptyResourcePart(t *testing.T) {
	_, err := NewJID("text@example.com/")
	if err == nil || err != ErrorEmptyPart {
		t.FailNow()
	}
}

// Trying to create a new bare JID (no resource part) should work.
func TestNewBareJID(t *testing.T) {
	jid, err := NewJID("barejid@example.com")
	if err != nil || jid.ResourcePart() != "" {
		t.FailNow()
	}
}

// New JIDs should strip whitespace from inputs.
func TestNewSurroundingWhitespace(t *testing.T) {
	jid, err := NewJID("  localpart@example.com/resourcepart	 ")
	if err != nil {
		t.FailNow()
	}
	str := jid.String()
	if str != "localpart@example.com/resourcepart" {
		t.FailNow()
	}
}

// New JIDs should not allow `\t`.
func TestNewHasTab(t *testing.T) {
	_, err := NewJID("localpart	@example.com/resourcepart")
	if err == nil || err != ErrorIllegalSpace {
		t.FailNow()
	}
}

// New JIDs should not allow spaces.
func TestNewHasSpace(t *testing.T) {
	_, err := NewJID("localpart@exampl e.com/resourcepart")
	if err == nil || err != ErrorIllegalSpace {
		t.FailNow()
	}
}

// New JIDs should not be empty strings.
func TestNewEmpty(t *testing.T) {
	_, err := NewJID("")
	if err == nil {
		t.FailNow()
	}
}

// Creating a new JID from a valid JID string should work and contain all the
// correct parts.
func TestNewValid(t *testing.T) {
	s := "jid@example.com/resourcepart"
	jid, err := NewJID(s)
	if err != nil {
		t.FailNow()
	}
	dp, err := jid.DomainPart()
	switch {
	case err != nil:
		t.FailNow()
	case jid.LocalPart() != "jid":
		t.FailNow()
	case dp != "example.com":
		t.FailNow()
	case jid.ResourcePart() != "resourcepart":
		t.FailNow()
	}
}

// Two identical JIDs should be equal.
func TestEqualJIDs(t *testing.T) {
	jid := JID{"newjid", "example.com", "equal"}
	jid2 := JID{"newjid", "example.com", "equal"}
	if !jid.Equals(jid2) {
		t.FailNow()
	}
}

// Two different JIDs should not be equal.
func TestNotEqualJIDs(t *testing.T) {
	jid := JID{"newjid", "example.com", "notequal"}
	jid2 := JID{"newjid2", "example.com", "notequal"}
	if jid.Equals(jid2) {
		t.FailNow()
	}
}

// Two JIDs with similar looking unicode characters should be equal.
func TestEqualsUnicodeNorm(t *testing.T) {
	// U+2126 Ω ohm sign
	jid, err := NewJID("Ω@example.com/res")
	if err != nil {
		t.Fail()
	}
	// U+03A9 Ω greek capital letter omega
	jid2, err := NewJID("Ω@example.com/res")
	if err != nil {
		t.Fail()
	}
	if !jid.Equals(jid2) {
		t.FailNow()
	}
}

// Test marshalling JID into an XML attribute
func TestMarshal(t *testing.T) {
	jid := JID{"newjid", "example.com", "marshal"}
	attr, err := jid.MarshalXMLAttr(xml.Name{Space: "", Local: "to"})

	if err != nil || attr.Name.Local != "to" || attr.Name.Space != "" || attr.Value != "newjid@example.com/marshal" {
		t.FailNow()
	}
}