From 6a3ee5b0a5311fd80f0150ee13dd867a126f0f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?CORNIERE=20R=C3=A9mi?= Date: Thu, 9 Apr 2020 10:02:11 +0200 Subject: [PATCH] Support for XEP-0334 --- go.sum | 2 +- stanza/msg_hint.go | 36 +++++++++++++++++++++ stanza/msg_hint_test.go | 72 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 stanza/msg_hint.go create mode 100644 stanza/msg_hint_test.go diff --git a/go.sum b/go.sum index 38fd55b..e51d671 100644 --- a/go.sum +++ b/go.sum @@ -201,7 +201,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools/gotestsum v0.3.5/go.mod h1:Mnf3e5FUzXbkCfynWBGOwLssY7gTQgCHObK9tMpAriY= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/stanza/msg_hint.go b/stanza/msg_hint.go new file mode 100644 index 0000000..205d0ef --- /dev/null +++ b/stanza/msg_hint.go @@ -0,0 +1,36 @@ +package stanza + +import "encoding/xml" + +/* +Support for: +- XEP-0334: Message Processing Hints: https://xmpp.org/extensions/xep-0334.html +Pointers should be used to keep consistent with unmarshal. Eg : +msg.Extensions = append(msg.Extensions, &stanza.HintNoCopy{}, &stanza.HintStore{}) +*/ + +type HintNoPermanentStore struct { + MsgExtension + XMLName xml.Name `xml:"urn:xmpp:hints no-permanent-store"` +} + +type HintNoStore struct { + MsgExtension + XMLName xml.Name `xml:"urn:xmpp:hints no-store"` +} + +type HintNoCopy struct { + MsgExtension + XMLName xml.Name `xml:"urn:xmpp:hints no-copy"` +} +type HintStore struct { + MsgExtension + XMLName xml.Name `xml:"urn:xmpp:hints store"` +} + +func init() { + TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-permanent-store"}, HintNoPermanentStore{}) + TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-store"}, HintNoStore{}) + TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-copy"}, HintNoCopy{}) + TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "store"}, HintStore{}) +} diff --git a/stanza/msg_hint_test.go b/stanza/msg_hint_test.go new file mode 100644 index 0000000..b86d2f9 --- /dev/null +++ b/stanza/msg_hint_test.go @@ -0,0 +1,72 @@ +package stanza_test + +import ( + "encoding/xml" + "gosrc.io/xmpp/stanza" + "reflect" + "strings" + "testing" +) + +const msg_const = ` + + V unir avtugf pybnx gb uvqr zr sebz gurve fvtug + + + + +` + +func TestSerializationHint(t *testing.T) { + msg := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"}) + msg.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug" + msg.Extensions = append(msg.Extensions, stanza.HintNoCopy{}, stanza.HintNoPermanentStore{}, stanza.HintNoStore{}, stanza.HintStore{}) + data, _ := xml.Marshal(msg) + if strings.ReplaceAll(strings.Join(strings.Fields(msg_const), ""), "\n", "") != strings.Join(strings.Fields(string(data)), "") { + t.Fatalf("marshalled message does not match expected message") + } +} + +func TestUnmarshalHints(t *testing.T) { + // Init message as in the const value + msgConst := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"}) + msgConst.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug" + msgConst.Extensions = append(msgConst.Extensions, &stanza.HintNoCopy{}, &stanza.HintNoPermanentStore{}, &stanza.HintNoStore{}, &stanza.HintStore{}) + + // Compare message with the const value + msg := stanza.Message{} + err := xml.Unmarshal([]byte(msg_const), &msg) + if err != nil { + t.Fatal(err) + } + + if msgConst.XMLName.Local != msg.XMLName.Local { + t.Fatalf("message tags do not match. Expected: %s, Actual: %s", msgConst.XMLName.Local, msg.XMLName.Local) + } + if msgConst.Body != msg.Body { + t.Fatalf("message bodies do not match. Expected: %s, Actual: %s", msgConst.Body, msg.Body) + } + + if !reflect.DeepEqual(msgConst.Attrs, msg.Attrs) { + t.Fatalf("attributes do not match") + } + + if !reflect.DeepEqual(msgConst.Error, msg.Error) { + t.Fatalf("attributes do not match") + } + var found bool + for _, ext := range msgConst.Extensions { + for _, strExt := range msg.Extensions { + if reflect.TypeOf(ext) == reflect.TypeOf(strExt) { + found = true + break + } + } + if !found { + t.Fatalf("extensions do not match") + } + found = false + } +}