2019-06-13 15:22:39 +00:00
|
|
|
package xmpp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gosrc.io/xmpp"
|
|
|
|
)
|
|
|
|
|
2019-06-14 07:37:38 +00:00
|
|
|
// ============================================================================
|
|
|
|
// Test route & matchers
|
2019-06-13 15:22:39 +00:00
|
|
|
|
|
|
|
func TestNameMatcher(t *testing.T) {
|
|
|
|
router := xmpp.NewRouter()
|
2019-06-14 07:37:38 +00:00
|
|
|
router.HandleFunc("message", func(s xmpp.Sender, p xmpp.Packet) {
|
|
|
|
_ = s.SendRaw(successFlag)
|
2019-06-13 15:22:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Check that a message packet is properly matched
|
2019-06-14 07:37:38 +00:00
|
|
|
conn := NewSenderMock()
|
2019-06-13 15:22:39 +00:00
|
|
|
// TODO: We want packet creation code to use struct to use default values
|
|
|
|
msg := xmpp.NewMessage("chat", "", "test@localhost", "1", "")
|
|
|
|
msg.Body = "Hello"
|
2019-06-14 07:37:38 +00:00
|
|
|
router.Route(conn, msg)
|
|
|
|
if conn.String() != successFlag {
|
2019-06-13 15:22:39 +00:00
|
|
|
t.Error("Message was not matched and routed properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that an IQ packet is not matched
|
2019-06-14 07:37:38 +00:00
|
|
|
conn = NewSenderMock()
|
2019-06-13 15:22:39 +00:00
|
|
|
iq := xmpp.NewIQ("get", "", "localhost", "1", "")
|
2019-06-19 08:21:57 +00:00
|
|
|
iq.Payload = &xmpp.DiscoInfo{}
|
2019-06-14 07:37:38 +00:00
|
|
|
router.Route(conn, iq)
|
|
|
|
if conn.String() == successFlag {
|
2019-06-13 15:22:39 +00:00
|
|
|
t.Error("IQ should not have been matched and routed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIQNSMatcher(t *testing.T) {
|
|
|
|
router := xmpp.NewRouter()
|
|
|
|
router.NewRoute().
|
|
|
|
IQNamespaces(xmpp.NSDiscoInfo, xmpp.NSDiscoItems).
|
2019-06-14 07:37:38 +00:00
|
|
|
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
|
|
|
|
_ = s.SendRaw(successFlag)
|
2019-06-13 15:22:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Check that an IQ with proper namespace does match
|
2019-06-14 07:37:38 +00:00
|
|
|
conn := NewSenderMock()
|
2019-06-13 15:22:39 +00:00
|
|
|
iqDisco := xmpp.NewIQ("get", "", "localhost", "1", "")
|
2019-06-19 08:21:57 +00:00
|
|
|
iqDisco.Payload = &xmpp.DiscoInfo{
|
2019-06-13 15:22:39 +00:00
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: xmpp.NSDiscoInfo,
|
|
|
|
Local: "query",
|
2019-06-19 08:21:57 +00:00
|
|
|
}}
|
2019-06-14 07:37:38 +00:00
|
|
|
router.Route(conn, iqDisco)
|
|
|
|
if conn.String() != successFlag {
|
2019-06-13 15:22:39 +00:00
|
|
|
t.Errorf("IQ should have been matched and routed: %v", iqDisco)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that another namespace is not matched
|
2019-06-14 07:37:38 +00:00
|
|
|
conn = NewSenderMock()
|
2019-06-13 15:22:39 +00:00
|
|
|
iqVersion := xmpp.NewIQ("get", "", "localhost", "1", "")
|
2019-06-19 08:21:57 +00:00
|
|
|
iqVersion.Payload = &xmpp.DiscoInfo{
|
2019-06-13 15:22:39 +00:00
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "jabber:iq:version",
|
|
|
|
Local: "query",
|
2019-06-19 08:21:57 +00:00
|
|
|
}}
|
2019-06-14 07:37:38 +00:00
|
|
|
router.Route(conn, iqVersion)
|
|
|
|
if conn.String() == successFlag {
|
2019-06-13 15:22:39 +00:00
|
|
|
t.Errorf("IQ should not have been matched and routed: %v", iqVersion)
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 07:37:38 +00:00
|
|
|
|
2019-06-21 14:48:13 +00:00
|
|
|
func TestTypeMatcher(t *testing.T) {
|
|
|
|
router := xmpp.NewRouter()
|
|
|
|
router.NewRoute().
|
|
|
|
StanzaType("normal").
|
|
|
|
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
|
|
|
|
_ = s.SendRaw(successFlag)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check that a packet with the proper type matches
|
|
|
|
conn := NewSenderMock()
|
|
|
|
message := xmpp.NewMessage("normal", "", "test@localhost", "1", "")
|
|
|
|
message.Body = "hello"
|
|
|
|
router.Route(conn, message)
|
|
|
|
|
|
|
|
if conn.String() != successFlag {
|
|
|
|
t.Errorf("'normal' message should have been matched and routed: %v", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should match on default type 'normal' for message without a type
|
|
|
|
conn = NewSenderMock()
|
|
|
|
message = xmpp.NewMessage("", "", "test@localhost", "1", "")
|
|
|
|
message.Body = "hello"
|
|
|
|
router.Route(conn, message)
|
|
|
|
|
|
|
|
if conn.String() != successFlag {
|
|
|
|
t.Errorf("message should have been matched and routed: %v", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not match on other types
|
|
|
|
conn = NewSenderMock()
|
|
|
|
iqVersion := xmpp.NewIQ("get", "service.localhost", "test@localhost", "1", "")
|
|
|
|
iqVersion.Payload = &xmpp.DiscoInfo{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "jabber:iq:version",
|
|
|
|
Local: "query",
|
|
|
|
}}
|
|
|
|
router.Route(conn, iqVersion)
|
|
|
|
|
|
|
|
if conn.String() == successFlag {
|
|
|
|
t.Errorf("iq get should not have been matched and routed: %v", iqVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompositeMatcher(t *testing.T) {
|
|
|
|
router := xmpp.NewRouter()
|
|
|
|
router.NewRoute().
|
|
|
|
IQNamespaces("jabber:iq:version").
|
|
|
|
StanzaType("get").
|
|
|
|
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
|
|
|
|
_ = s.SendRaw(successFlag)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Data set
|
|
|
|
getVersionIq := xmpp.NewIQ("get", "service.localhost", "test@localhost", "1", "")
|
|
|
|
getVersionIq.Payload = &xmpp.Version{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "jabber:iq:version",
|
|
|
|
Local: "query",
|
|
|
|
}}
|
|
|
|
|
|
|
|
setVersionIq := xmpp.NewIQ("set", "service.localhost", "test@localhost", "1", "")
|
|
|
|
setVersionIq.Payload = &xmpp.Version{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "jabber:iq:version",
|
|
|
|
Local: "query",
|
|
|
|
}}
|
|
|
|
|
|
|
|
GetDiscoIq := xmpp.NewIQ("get", "service.localhost", "test@localhost", "1", "")
|
|
|
|
GetDiscoIq.Payload = &xmpp.DiscoInfo{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "http://jabber.org/protocol/disco#info",
|
|
|
|
Local: "query",
|
|
|
|
}}
|
|
|
|
|
|
|
|
message := xmpp.NewMessage("normal", "", "test@localhost", "1", "")
|
|
|
|
message.Body = "hello"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input xmpp.Packet
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{name: "match get version iq", input: getVersionIq, want: true},
|
|
|
|
{name: "ignore set version iq", input: setVersionIq, want: false},
|
|
|
|
{name: "ignore get discoinfo iq", input: GetDiscoIq, want: false},
|
|
|
|
{name: "ignore message", input: message, want: false},
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(st *testing.T) {
|
|
|
|
conn := NewSenderMock()
|
|
|
|
router.Route(conn, tc.input)
|
|
|
|
|
|
|
|
res := conn.String() == successFlag
|
|
|
|
if tc.want != res {
|
|
|
|
st.Errorf("incorrect result for %#v\nMatch = %#v, expecting %#v", tc.input, res, tc.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A blank route with empty matcher will always match
|
|
|
|
// It can be use to receive all packets that do not match any of the previous route.
|
|
|
|
func TestCatchallMatcher(t *testing.T) {
|
|
|
|
router := xmpp.NewRouter()
|
|
|
|
router.NewRoute().
|
|
|
|
HandlerFunc(func(s xmpp.Sender, p xmpp.Packet) {
|
|
|
|
_ = s.SendRaw(successFlag)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check that we match on several packets
|
|
|
|
conn := NewSenderMock()
|
|
|
|
message := xmpp.NewMessage("chat", "", "test@localhost", "1", "")
|
|
|
|
message.Body = "hello"
|
|
|
|
router.Route(conn, message)
|
|
|
|
|
|
|
|
if conn.String() != successFlag {
|
|
|
|
t.Errorf("chat message should have been matched and routed: %v", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = NewSenderMock()
|
|
|
|
iqVersion := xmpp.NewIQ("get", "service.localhost", "test@localhost", "1", "")
|
|
|
|
iqVersion.Payload = &xmpp.DiscoInfo{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "jabber:iq:version",
|
|
|
|
Local: "query",
|
|
|
|
}}
|
|
|
|
router.Route(conn, iqVersion)
|
|
|
|
|
|
|
|
if conn.String() != successFlag {
|
|
|
|
t.Errorf("iq get should have been matched and routed: %v", iqVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// SenderMock
|
|
|
|
|
2019-06-14 07:37:38 +00:00
|
|
|
var successFlag = "matched"
|
|
|
|
|
|
|
|
type SenderMock struct {
|
|
|
|
buffer *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSenderMock() SenderMock {
|
|
|
|
return SenderMock{buffer: new(bytes.Buffer)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SenderMock) Send(packet xmpp.Packet) error {
|
|
|
|
out, err := xml.Marshal(packet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.buffer.Write(out)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SenderMock) SendRaw(str string) error {
|
|
|
|
s.buffer.WriteString(str)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SenderMock) String() string {
|
|
|
|
return s.buffer.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSenderMock(t *testing.T) {
|
|
|
|
conn := NewSenderMock()
|
|
|
|
msg := xmpp.NewMessage("", "", "test@localhost", "1", "")
|
|
|
|
msg.Body = "Hello"
|
|
|
|
if err := conn.Send(msg); err != nil {
|
|
|
|
t.Error("Could not send message")
|
|
|
|
}
|
|
|
|
if conn.String() != "<message id=\"1\" to=\"test@localhost\"><body>Hello</body></message>" {
|
|
|
|
t.Errorf("Incorrect packet sent: %s", conn.String())
|
|
|
|
}
|
|
|
|
}
|