2018-12-26 09:50:01 -08:00
|
|
|
package xmpp // import "gosrc.io/xmpp"
|
2016-01-06 08:59:44 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-01-06 10:21:37 -08:00
|
|
|
func TestValidJids(t *testing.T) {
|
2019-06-05 01:02:24 -07:00
|
|
|
tests := []struct {
|
|
|
|
jidstr string
|
|
|
|
expected Jid
|
|
|
|
}{
|
|
|
|
{jidstr: "test@domain.com", expected: Jid{"test", "domain.com", ""}},
|
|
|
|
{jidstr: "test@domain.com/resource", expected: Jid{"test", "domain.com", "resource"}},
|
|
|
|
// resource can contain '/' or '@'
|
|
|
|
{jidstr: "test@domain.com/a/b", expected: Jid{"test", "domain.com", "a/b"}},
|
|
|
|
{jidstr: "test@domain.com/a@b", expected: Jid{"test", "domain.com", "a@b"}},
|
2019-06-07 02:40:10 -07:00
|
|
|
{jidstr: "domain.com", expected: Jid{"", "domain.com", ""}},
|
2019-06-05 01:02:24 -07:00
|
|
|
}
|
2016-01-06 08:59:44 -08:00
|
|
|
|
2019-06-05 01:02:24 -07:00
|
|
|
for _, tt := range tests {
|
|
|
|
jid, err := NewJid(tt.jidstr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not parse correct jid: %s", tt.jidstr)
|
|
|
|
continue
|
2019-06-04 10:01:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if jid == nil {
|
|
|
|
t.Error("jid should not be nil")
|
2016-01-06 10:21:37 -08:00
|
|
|
}
|
2016-01-06 08:59:44 -08:00
|
|
|
|
2019-06-05 01:02:24 -07:00
|
|
|
if jid.username != tt.expected.username {
|
|
|
|
t.Errorf("incorrect jid username (%s): %s", tt.expected.username, jid.username)
|
2016-01-06 10:21:37 -08:00
|
|
|
}
|
|
|
|
|
2019-06-05 01:02:24 -07:00
|
|
|
if jid.username != tt.expected.username {
|
|
|
|
t.Errorf("incorrect jid domain (%s): %s", tt.expected.domain, jid.domain)
|
2016-01-06 10:21:37 -08:00
|
|
|
}
|
2016-01-06 08:59:44 -08:00
|
|
|
|
2019-06-05 01:02:24 -07:00
|
|
|
if jid.resource != tt.expected.resource {
|
|
|
|
t.Errorf("incorrect jid resource (%s): %s", tt.expected.resource, jid.resource)
|
2016-01-06 10:21:37 -08:00
|
|
|
}
|
2016-01-06 08:59:44 -08:00
|
|
|
}
|
2016-01-06 10:21:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncorrectJids(t *testing.T) {
|
2019-06-05 01:02:24 -07:00
|
|
|
badJids := []string{
|
2019-06-07 02:40:10 -07:00
|
|
|
"",
|
|
|
|
"user@",
|
|
|
|
"@domain.com",
|
2019-06-05 01:02:24 -07:00
|
|
|
"user:name@domain.com",
|
|
|
|
"user<name@domain.com",
|
|
|
|
"test@domain.com@otherdomain.com",
|
|
|
|
"test@domain com/resource",
|
|
|
|
}
|
2016-01-06 08:59:44 -08:00
|
|
|
|
2016-01-06 10:21:37 -08:00
|
|
|
for _, sjid := range badJids {
|
|
|
|
if _, err := NewJid(sjid); err == nil {
|
|
|
|
t.Error("parsing incorrect jid should return error: " + sjid)
|
|
|
|
}
|
2016-01-06 08:59:44 -08:00
|
|
|
}
|
|
|
|
}
|