~samwhited/xmpp

ref: 5a3b4b1a95ad2794ed079fe37d1d9eaf2ebde532 xmpp/jid/jid_test.go -rw-r--r-- 1.9 KiB
5a3b4b1aSam Whited Factor xmpp connections out into their own package 5 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
// Copyright 2015 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"
)

// Compile time check ot make sure that JID and *JID match several interfaces.
var _ fmt.Stringer = (*JID)(nil)
var _ fmt.Stringer = JID{}
var _ xml.MarshalerAttr = (*JID)(nil)
var _ xml.MarshalerAttr = JID{}
var _ xml.UnmarshalerAttr = (*JID)(nil)

func TestValidJIDs(t *testing.T) {
	for _, jid := range []struct {
		jid, lp, dp, rp string
	}{
		{"example.net", "", "example.net", ""},
		{"example.net/rp", "", "example.net", "rp"},
		{"mercutio@example.net", "mercutio", "example.net", ""},
		{"mercutio@example.net/rp", "mercutio", "example.net", "rp"},
		{"mercutio@example.net/rp@rp", "mercutio", "example.net", "rp@rp"},
		{"mercutio@example.net/rp@rp/rp", "mercutio", "example.net", "rp@rp/rp"},
		{"mercutio@example.net/@", "mercutio", "example.net", "@"},
		{"mercutio@example.net//@", "mercutio", "example.net", "/@"},
		{"mercutio@example.net//@//", "mercutio", "example.net", "/@//"},
	} {
		j, err := ParseString(jid.jid)
		switch {
		case err != nil:
			t.Log(err)
			t.Fail()
		case j.Domainpart() != jid.dp:
			t.Logf("Got domainpart %s but expected %s", j.Domainpart(), jid.dp)
			t.Fail()
		case j.Localpart() != jid.lp:
			t.Logf("Got localpart %s but expected %s", j.Localpart(), jid.lp)
			t.Fail()
		case j.Resourcepart() != jid.rp:
			t.Logf("Got resourcepart %s but expected %s", j.Resourcepart(), jid.rp)
			t.Fail()
		}
	}
}

var invalidutf8 = string([]byte{0xff, 0xfe, 0xfd})

func TestInvalidJIDs(t *testing.T) {
	for _, jid := range []string{
		"test@/test",
		invalidutf8 + "@example.com/rp",
		invalidutf8 + "/rp",
		invalidutf8,
		"example.com/" + invalidutf8,
		"lp@/rp",
	} {
		_, err := ParseString(jid)
		if err == nil {
			t.Logf("Expected JID %s to fail", jid)
			t.Fail()
		}
	}
}