From 6872ed8d1bf1aab94f1ddba4c2674f336d8fd72e Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Tue, 9 Jul 2019 17:53:49 +0200 Subject: [PATCH] Add builder & test on software version helpers --- stanza/iq_disco_test.go | 10 ++++++++-- stanza/iq_version.go | 20 ++++++++++++++++++++ stanza/iq_version_test.go | 40 +++++++++++++++++++++++++++++++++++++++ stanza/xmpp_test.go | 2 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 stanza/iq_version_test.go diff --git a/stanza/iq_disco_test.go b/stanza/iq_disco_test.go index 08ccc68..d659cde 100644 --- a/stanza/iq_disco_test.go +++ b/stanza/iq_disco_test.go @@ -14,7 +14,7 @@ func TestDiscoInfo_Builder(t *testing.T) { disco.AddIdentity("Test Component", "gateway", "service") disco.AddFeatures(stanza.NSDiscoInfo, stanza.NSDiscoItems, "jabber:iq:version", "urn:xmpp:delegation:1") - parsedIQ, err := marshallUnmarshall(t, iq) + parsedIQ, err := checkMarshalling(t, iq) if err != nil { return } @@ -57,7 +57,7 @@ func TestDiscoItems_Builder(t *testing.T) { AddItem("catalog.shakespeare.lit", "clothing", "Wear your literary taste with pride"). AddItem("catalog.shakespeare.lit", "music", "Music from the time of Shakespeare") - parsedIQ, err := marshallUnmarshall(t, iq) + parsedIQ, err := checkMarshalling(t, iq) if err != nil { return } @@ -79,6 +79,12 @@ func TestDiscoItems_Builder(t *testing.T) { if item.JID != items[i].JID { t.Errorf("JID Mismatch (expected: %s): %s", items[i].JID, item.JID) } + if item.Node != items[i].Node { + t.Errorf("Node Mismatch (expected: %s): %s", items[i].JID, item.JID) + } + if item.Name != items[i].Name { + t.Errorf("Name Mismatch (expected: %s): %s", items[i].JID, item.JID) + } } } } diff --git a/stanza/iq_version.go b/stanza/iq_version.go index ebf4452..4cfbfce 100644 --- a/stanza/iq_version.go +++ b/stanza/iq_version.go @@ -17,6 +17,26 @@ func (v *Version) Namespace() string { return v.XMLName.Space } +// --------------- +// Builder helpers + +// Version builds a default software version payload +func (iq *IQ) Version() *Version { + d := Version{ + XMLName: xml.Name{Space: "jabber:iq:version", Local: "query"}, + } + iq.Payload = &d + return &d +} + +// Set all software version info +func (v *Version) SetInfo(name, version, os string) *Version { + v.Name = name + v.Version = version + v.OS = os + return v +} + // ============================================================================ // Registry init diff --git a/stanza/iq_version_test.go b/stanza/iq_version_test.go new file mode 100644 index 0000000..45d68f7 --- /dev/null +++ b/stanza/iq_version_test.go @@ -0,0 +1,40 @@ +package stanza_test + +import ( + "testing" + + "gosrc.io/xmpp/stanza" +) + +// Build a Software Version reply +// https://xmpp.org/extensions/xep-0092.html#example-2 +func TestVersion_Builder(t *testing.T) { + name := "Exodus" + version := "0.7.0.4" + os := "Windows-XP 5.01.2600" + iq := stanza.NewIQ(stanza.Attrs{Type: "result", From: "romeo@montague.net/orchard", + To: "juliet@capulet.com/balcony", Id: "version_1"}) + iq.Version().SetInfo(name, version, os) + + parsedIQ, err := checkMarshalling(t, iq) + if err != nil { + return + } + + // Check result + pp, ok := parsedIQ.Payload.(*stanza.Version) + if !ok { + t.Errorf("Parsed stanza does not contain correct IQ payload") + } + + // Check version info + if pp.Name != name { + t.Errorf("Name Mismatch (expected: %s): %s", name, pp.Name) + } + if pp.Version != version { + t.Errorf("Version Mismatch (expected: %s): %s", version, pp.Version) + } + if pp.OS != os { + t.Errorf("OS Mismatch (expected: %s): %s", os, pp.OS) + } +} diff --git a/stanza/xmpp_test.go b/stanza/xmpp_test.go index 7295b7b..420a053 100644 --- a/stanza/xmpp_test.go +++ b/stanza/xmpp_test.go @@ -11,7 +11,7 @@ import ( // ============================================================================ // Marshaller / unmarshaller test -func marshallUnmarshall(t *testing.T, iq stanza.IQ) (*stanza.IQ, error) { +func checkMarshalling(t *testing.T, iq stanza.IQ) (*stanza.IQ, error) { // Marshall data, err := xml.Marshal(iq) if err != nil {