forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjid_test.go
114 lines (101 loc) · 3.74 KB
/
jid_test.go
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
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package xml_test
import (
"testing"
"github.com/ortuman/jackal/xml"
"github.com/stretchr/testify/require"
)
func TestBadJID(t *testing.T) {
_, err := xml.NewJIDString("ortuman@", false)
require.NotNil(t, err)
longStr := ""
for i := 0; i < 1074; i++ {
longStr += "a"
}
_, err2 := xml.NewJID(longStr, "example.org", "res", false)
require.NotNil(t, err2)
_, err3 := xml.NewJID("ortuman", longStr, "res", false)
require.NotNil(t, err3)
_, err4 := xml.NewJID("ortuman", "example.org", longStr, false)
require.NotNil(t, err4)
}
func TestNewJID(t *testing.T) {
j1, err := xml.NewJID("ortuman", "example.org", "res", false)
require.Nil(t, err)
require.Equal(t, "ortuman", j1.Node())
require.Equal(t, "example.org", j1.Domain())
require.Equal(t, "res", j1.Resource())
j2, err := xml.NewJID("ortuman", "example.org", "res", true)
require.Nil(t, err)
require.Equal(t, "ortuman", j2.Node())
require.Equal(t, "example.org", j2.Domain())
require.Equal(t, "res", j2.Resource())
}
func TestEmptyJID(t *testing.T) {
j, err := xml.NewJIDString("", true)
require.Nil(t, err)
require.Equal(t, "", j.Node())
require.Equal(t, "", j.Domain())
require.Equal(t, "", j.Resource())
}
func TestNewJIDString(t *testing.T) {
j, err := xml.NewJIDString("ortuman@jackal.im/res", false)
require.Nil(t, err)
require.Equal(t, "ortuman", j.Node())
require.Equal(t, "jackal.im", j.Domain())
require.Equal(t, "res", j.Resource())
require.Equal(t, "ortuman@jackal.im", j.ToBareJID().String())
require.Equal(t, "ortuman@jackal.im/res", j.String())
}
func TestServerJID(t *testing.T) {
j1, _ := xml.NewJIDString("example.org", false)
j2, _ := xml.NewJIDString("user@example.org", false)
j3, _ := xml.NewJIDString("example.org/res", false)
require.True(t, j1.IsServer())
require.False(t, j2.IsServer())
require.True(t, j3.IsServer() && j3.IsFull())
}
func TestBareJID(t *testing.T) {
j1, _ := xml.NewJID("ortuman", "example.org", "res", false)
require.True(t, j1.ToBareJID().IsBare())
j2, _ := xml.NewJIDString("example.org/res", false)
require.False(t, j2.ToBareJID().IsBare())
}
func TestFullJID(t *testing.T) {
j1, _ := xml.NewJID("ortuman", "example.org", "res", false)
j2, _ := xml.NewJID("", "example.org", "res", false)
require.True(t, j1.IsFullWithUser())
require.True(t, j2.IsFullWithServer())
}
func TestMatchesJID(t *testing.T) {
j1, _ := xml.NewJIDString("ortuman@example.org/res1", false)
j2, _ := xml.NewJIDString("ortuman@example.org", false)
j3, _ := xml.NewJIDString("example.org", false)
j4, _ := xml.NewJIDString("example.org/res1", false)
j6, _ := xml.NewJIDString("ortuman@example2.org/res2", false)
require.True(t, j1.Matches(j1, xml.JIDMatchesNode|xml.JIDMatchesDomain|xml.JIDMatchesResource))
require.True(t, j1.Matches(j2, xml.JIDMatchesNode|xml.JIDMatchesDomain))
require.True(t, j1.Matches(j3, xml.JIDMatchesDomain))
require.True(t, j1.Matches(j4, xml.JIDMatchesDomain|xml.JIDMatchesResource))
require.False(t, j1.Matches(j2, xml.JIDMatchesNode|xml.JIDMatchesDomain|xml.JIDMatchesResource))
require.False(t, j6.Matches(j2, xml.JIDMatchesNode|xml.JIDMatchesDomain))
require.False(t, j6.Matches(j3, xml.JIDMatchesDomain))
require.False(t, j6.Matches(j4, xml.JIDMatchesDomain|xml.JIDMatchesResource))
}
func TestBadPrep(t *testing.T) {
badNode := string([]byte{255, 255, 255})
badDomain := "\U0001f480"
basResource := string([]byte{255, 255, 255})
j, err := xml.NewJID(badNode, "example.org", "res", false)
require.Nil(t, j)
require.NotNil(t, err)
j2, err := xml.NewJID("ortuman", badDomain, "res", false)
require.Nil(t, j2)
require.NotNil(t, err)
j3, err := xml.NewJID("ortuman", "example.org", basResource, false)
require.Nil(t, j3)
require.NotNil(t, err)
}