2020-01-09 14:33:11 +00:00
|
|
|
package stanza
|
2016-01-06 16:59:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-01-06 18:21:37 +00:00
|
|
|
func TestValidJids(t *testing.T) {
|
2019-06-05 08:02:24 +00: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 09:40:10 +00:00
|
|
|
{jidstr: "domain.com", expected: Jid{"", "domain.com", ""}},
|
2019-06-05 08:02:24 +00:00
|
|
|
}
|
2016-01-06 16:59:44 +00:00
|
|
|
|
2019-06-05 08:02:24 +00: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 17:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if jid == nil {
|
|
|
|
t.Error("jid should not be nil")
|
2016-01-06 18:21:37 +00:00
|
|
|
}
|
2016-01-06 16:59:44 +00:00
|
|
|
|
2019-06-07 13:56:41 +00:00
|
|
|
if jid.Node != tt.expected.Node {
|
|
|
|
t.Errorf("incorrect jid Node (%s): %s", tt.expected.Node, jid.Node)
|
2016-01-06 18:21:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 13:56:41 +00:00
|
|
|
if jid.Node != tt.expected.Node {
|
|
|
|
t.Errorf("incorrect jid domain (%s): %s", tt.expected.Domain, jid.Domain)
|
2016-01-06 18:21:37 +00:00
|
|
|
}
|
2016-01-06 16:59:44 +00:00
|
|
|
|
2019-06-07 13:56:41 +00:00
|
|
|
if jid.Resource != tt.expected.Resource {
|
|
|
|
t.Errorf("incorrect jid resource (%s): %s", tt.expected.Resource, jid.Resource)
|
2016-01-06 18:21:37 +00:00
|
|
|
}
|
2016-01-06 16:59:44 +00:00
|
|
|
}
|
2016-01-06 18:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncorrectJids(t *testing.T) {
|
2019-06-05 08:02:24 +00:00
|
|
|
badJids := []string{
|
2019-06-07 09:40:10 +00:00
|
|
|
"",
|
|
|
|
"user@",
|
|
|
|
"@domain.com",
|
2019-06-05 08:02:24 +00:00
|
|
|
"user:name@domain.com",
|
|
|
|
"user<name@domain.com",
|
|
|
|
"test@domain.com@otherdomain.com",
|
|
|
|
"test@domain com/resource",
|
|
|
|
}
|
2016-01-06 16:59:44 +00:00
|
|
|
|
2016-01-06 18:21:37 +00:00
|
|
|
for _, sjid := range badJids {
|
|
|
|
if _, err := NewJid(sjid); err == nil {
|
|
|
|
t.Error("parsing incorrect jid should return error: " + sjid)
|
|
|
|
}
|
2016-01-06 16:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:25:18 +00:00
|
|
|
|
|
|
|
func TestFull(t *testing.T) {
|
2019-11-05 23:27:37 +00:00
|
|
|
fullJids := []string{
|
|
|
|
"test@domain.com/my resource",
|
2022-05-24 20:26:48 +00:00
|
|
|
"domain.com/my resource",
|
2019-11-05 23:27:37 +00:00
|
|
|
"test@domain.com",
|
|
|
|
"domain.com",
|
2019-06-07 14:25:18 +00:00
|
|
|
}
|
2019-11-05 23:27:37 +00:00
|
|
|
for _, sjid := range fullJids {
|
|
|
|
parsedJid, err := NewJid(sjid)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not parse jid: %v", err)
|
|
|
|
}
|
|
|
|
fullJid := parsedJid.Full()
|
|
|
|
if fullJid != sjid {
|
|
|
|
t.Errorf("incorrect full jid: %s", fullJid)
|
|
|
|
}
|
2019-06-07 14:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBare(t *testing.T) {
|
2019-11-05 23:27:37 +00:00
|
|
|
tests := []struct {
|
|
|
|
jidstr string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{jidstr: "test@domain.com", expected: "test@domain.com"},
|
|
|
|
{jidstr: "test@domain.com/resource", expected: "test@domain.com"},
|
|
|
|
{jidstr: "domain.com", expected: "domain.com"},
|
2019-06-07 14:25:18 +00:00
|
|
|
}
|
2019-11-05 23:27:37 +00:00
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
parsedJid, err := NewJid(tt.jidstr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not parse jid: %v", err)
|
|
|
|
}
|
|
|
|
bareJid := parsedJid.Bare()
|
|
|
|
if bareJid != tt.expected {
|
|
|
|
t.Errorf("incorrect bare jid: %s", bareJid)
|
|
|
|
}
|
2019-06-07 14:25:18 +00:00
|
|
|
}
|
|
|
|
}
|