2018-01-01 17:12:33 +00:00
|
|
|
package xmpp // import "fluux.io/xmpp"
|
2016-02-15 17:33:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2018-01-16 21:33:21 +00:00
|
|
|
"fmt"
|
2016-02-15 17:33:51 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalIqs(t *testing.T) {
|
2017-10-04 20:51:28 +00:00
|
|
|
//var cs1 = new(iot.ControlSet)
|
2016-02-15 17:33:51 +00:00
|
|
|
var tests = []struct {
|
|
|
|
iqString string
|
2018-01-13 17:50:17 +00:00
|
|
|
parsedIQ IQ
|
2016-02-15 17:33:51 +00:00
|
|
|
}{
|
2018-01-13 17:50:17 +00:00
|
|
|
{"<iq id=\"1\" type=\"set\" to=\"test@localhost\"/>", IQ{XMLName: xml.Name{Space: "", Local: "iq"}, PacketAttrs: PacketAttrs{To: "test@localhost", Type: "set", Id: "1"}}},
|
|
|
|
//{"<iq xmlns=\"jabber:client\" id=\"2\" type=\"set\" to=\"test@localhost\" from=\"server\"><set xmlns=\"urn:xmpp:iot:control\"/></iq>", IQ{XMLName: xml.Name{Space: "jabber:client", Local: "iq"}, PacketAttrs: PacketAttrs{To: "test@localhost", From: "server", Type: "set", Id: "2"}, Payload: cs1}},
|
2016-02-15 17:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2018-01-13 17:50:17 +00:00
|
|
|
var parsedIQ = new(IQ)
|
2016-02-15 17:33:51 +00:00
|
|
|
err := xml.Unmarshal([]byte(test.iqString), parsedIQ)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", test.iqString)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(parsedIQ, &test.parsedIQ) {
|
|
|
|
t.Errorf("Unmarshal(%s) expecting result %+v = %+v", test.iqString, parsedIQ, &test.parsedIQ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 11:28:34 +00:00
|
|
|
|
|
|
|
func TestGenerateIq(t *testing.T) {
|
|
|
|
iq := NewIQ("get", "admin@localhost", "test@localhost", "1", "en")
|
|
|
|
payload := Node{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "http://jabber.org/protocol/disco#info",
|
|
|
|
Local: "query",
|
|
|
|
},
|
|
|
|
Nodes: []Node{
|
|
|
|
{XMLName: xml.Name{
|
2018-01-15 11:47:26 +00:00
|
|
|
Space: "http://jabber.org/protocol/disco#info",
|
2018-01-15 11:28:34 +00:00
|
|
|
Local: "identity",
|
|
|
|
},
|
|
|
|
Attrs: []xml.Attr{
|
|
|
|
{Name: xml.Name{Local: "category"}, Value: "gateway"},
|
2018-01-16 21:33:21 +00:00
|
|
|
{Name: xml.Name{Local: "type"}, Value: "mqtt"},
|
2018-01-15 11:28:34 +00:00
|
|
|
{Name: xml.Name{Local: "name"}, Value: "Test Gateway"},
|
|
|
|
},
|
|
|
|
Nodes: nil,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
iq.AddPayload(&payload)
|
|
|
|
data, err := xml.Marshal(iq)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal xml structure")
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:33:21 +00:00
|
|
|
fmt.Printf("XML Struct: %s\n", data)
|
|
|
|
|
|
|
|
var parsedIQ = new(IQ)
|
|
|
|
if err = xml.Unmarshal(data, parsedIQ); err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(parsedIQ.Payload[0], iq.Payload[0]) {
|
|
|
|
t.Errorf("expecting result %+v = %+v", parsedIQ.Payload[0], iq.Payload[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("ParsedIQ", parsedIQ)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateIqNew(t *testing.T) {
|
|
|
|
iq := NewIQ("get", "admin@localhost", "test@localhost", "1", "en")
|
|
|
|
payload := DiscoInfo{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "http://jabber.org/protocol/disco#info",
|
|
|
|
Local: "query",
|
|
|
|
},
|
|
|
|
Identity: Identity{
|
|
|
|
XMLName: xml.Name{
|
|
|
|
Space: "http://jabber.org/protocol/disco#info",
|
|
|
|
Local: "identity",
|
|
|
|
},
|
|
|
|
Name: "Test Gateway",
|
|
|
|
Category: "gateway",
|
|
|
|
Type: "mqtt",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
iq.AddPayload(&payload)
|
|
|
|
data, err := xml.Marshal(iq)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cannot marshal xml structure")
|
|
|
|
}
|
|
|
|
|
2018-01-15 11:28:34 +00:00
|
|
|
var parsedIQ = new(IQ)
|
|
|
|
if err = xml.Unmarshal(data, parsedIQ); err != nil {
|
|
|
|
t.Errorf("Unmarshal(%s) returned error", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(parsedIQ.Payload[0], iq.Payload[0]) {
|
|
|
|
t.Errorf("expecting result %+v = %+v", parsedIQ.Payload[0], iq.Payload[0])
|
|
|
|
}
|
|
|
|
}
|