2018-02-14 18:49:26 +01:00
|
|
|
package xmppbase
|
2017-10-02 14:38:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test Values for NewJID from RFC7622
|
|
|
|
// https://tools.ietf.org/html/rfc7622
|
|
|
|
func TestNewJID(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
checkList := map[string]*JID{
|
2018-02-14 10:46:43 +01:00
|
|
|
"juliet@example.com": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "juliet",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"juliet@example.com/foo": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "juliet",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"juliet@example.com/foo bar": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "juliet",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo bar",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"juliet@example.com/foo@bar": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "juliet",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo@bar",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"foo\\20bar@example.com": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "foo\\20bar",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"fussball@example.com": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "fussball",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"fußball@example.com": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "fußball",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"π@example.com": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "π",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"Σ@example.com/foo": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "Σ",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"σ@example.com/foo": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "σ",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"ς@example.com/foo": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "ς",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"king@example.com/♚": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "king",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "♚",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"example.com": {
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"example.com/foobar": {
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foobar",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"a.example.com/b@example.net": {
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "a.example.com",
|
|
|
|
Resource: "b@example.net",
|
|
|
|
},
|
|
|
|
"\"juliet\"@example.com": nil,
|
|
|
|
"foo bar@example.com": nil,
|
|
|
|
"juliet@example.com/ foo": nil,
|
|
|
|
"@example.com/": nil,
|
|
|
|
// "henryⅣ@example.com": nil, -- ignore for easier implementation
|
|
|
|
// "♚@example.com": nil,
|
|
|
|
"juliet@": nil,
|
|
|
|
"/foobar": nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
for jidString, jidValid := range checkList {
|
|
|
|
jid := NewJID(jidString)
|
|
|
|
|
|
|
|
if jidValid != nil {
|
|
|
|
assert.NotNil(jid, "this should be a valid JID:"+jidString)
|
|
|
|
if jid == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-15 22:03:49 +01:00
|
|
|
assert.Equal(jidValid.Local, jid.Local, "the local part was not right detectet:"+jidString)
|
2017-10-02 14:38:52 +02:00
|
|
|
assert.Equal(jidValid.Domain, jid.Domain, "the domain part was not right detectet:"+jidString)
|
|
|
|
assert.Equal(jidValid.Resource, jid.Resource, "the resource part was not right detectet:"+jidString)
|
2018-02-15 22:03:49 +01:00
|
|
|
assert.Equal(jidValid.Full().String(), jidString, "the function full of jid did not work")
|
2017-10-02 14:38:52 +02:00
|
|
|
} else {
|
|
|
|
assert.Nil(jid, "this should not be a valid JID:"+jidString)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJIDBare(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
checkList := map[string]*JID{
|
2018-02-14 10:46:43 +01:00
|
|
|
"aaa@example.com": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "aaa",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"aab@example.com": {
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "aab",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo",
|
|
|
|
},
|
2018-02-14 10:46:43 +01:00
|
|
|
"example.com": {
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for jidValid, jid := range checkList {
|
2018-02-15 22:03:49 +01:00
|
|
|
jidBase := jid.Bare().String()
|
2017-10-02 14:38:52 +02:00
|
|
|
assert.Equal(jidValid, jidBase)
|
|
|
|
}
|
2018-02-15 22:03:49 +01:00
|
|
|
// check nil value
|
|
|
|
var jid *JID
|
|
|
|
assert.Equal("", jid.Bare().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClone(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var jid *JID
|
|
|
|
cloneJID := jid.Clone()
|
|
|
|
assert.Nil(jid)
|
|
|
|
assert.Nil(cloneJID)
|
|
|
|
|
|
|
|
originString := "bot@example.org"
|
|
|
|
|
|
|
|
jid = NewJID(originString)
|
|
|
|
cloneJID = jid.Clone()
|
|
|
|
cloneJID.Resource = "notebook"
|
|
|
|
|
|
|
|
assert.Equal(originString, jid.String())
|
|
|
|
assert.NotEqual(cloneJID.String(), jid.String())
|
|
|
|
assert.Equal(cloneJID.Bare().String(), jid.String())
|
|
|
|
|
2017-10-02 14:38:52 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 00:54:14 +02:00
|
|
|
func TestMarshal(t *testing.T) {
|
2017-10-02 14:38:52 +02:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
jid := &JID{}
|
2018-02-07 19:32:11 +01:00
|
|
|
err := jid.UnmarshalText([]byte("juliet@example.com/foo"))
|
2017-10-02 14:38:52 +02:00
|
|
|
|
|
|
|
assert.NoError(err)
|
2018-02-15 22:03:49 +01:00
|
|
|
assert.Equal(jid.Local, "juliet")
|
2017-10-02 14:38:52 +02:00
|
|
|
assert.Equal(jid.Domain, "example.com")
|
|
|
|
assert.Equal(jid.Resource, "foo")
|
|
|
|
|
2018-02-07 19:32:11 +01:00
|
|
|
err = jid.UnmarshalText([]byte("juliet@example.com/ foo"))
|
2017-10-02 14:38:52 +02:00
|
|
|
|
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
jid = &JID{
|
2018-02-15 22:03:49 +01:00
|
|
|
Local: "romeo",
|
2017-10-02 14:38:52 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
Resource: "bar",
|
|
|
|
}
|
2018-02-07 19:32:11 +01:00
|
|
|
jidString, err := jid.MarshalText()
|
2017-10-02 14:38:52 +02:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal("romeo@example.com/bar", string(jidString))
|
|
|
|
}
|