From 21f6a549db77e39b5f643f609a25ed4f22b5a8c1 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Mon, 28 Oct 2019 21:21:35 +0100 Subject: [PATCH] Always add an id to IQ queries --- go.mod | 1 + go.sum | 3 +++ stanza/iq.go | 8 +++++++- stanza/iq_test.go | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3298cfa..f31fe40 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/google/go-cmp v0.3.1 + github.com/google/uuid v1.1.1 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 nhooyr.io/websocket v1.6.5 ) diff --git a/go.sum b/go.sum index cf05d14..ae38d07 100644 --- a/go.sum +++ b/go.sum @@ -21,9 +21,12 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190908185732-236ed259b199/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/knq/sysutil v0.0.0-20181215143952-f05b59f0f307/go.mod h1:BjPj+aVjl9FW/cCGiF3nGh5v+9Gd3VCgBQbod/GlMaQ= diff --git a/stanza/iq.go b/stanza/iq.go index f1663bc..923cf28 100644 --- a/stanza/iq.go +++ b/stanza/iq.go @@ -2,6 +2,8 @@ package stanza import ( "encoding/xml" + + "github.com/google/uuid" ) /* @@ -31,8 +33,12 @@ type IQPayload interface { } func NewIQ(a Attrs) IQ { - // TODO generate IQ ID if not set // TODO ensure that type is set, as it is required + if a.Id == "" { + if id, err := uuid.NewRandom(); err == nil { + a.Id = id.String() + } + } return IQ{ XMLName: xml.Name{Local: "iq"}, Attrs: a, diff --git a/stanza/iq_test.go b/stanza/iq_test.go index 04a868a..93f7ebb 100644 --- a/stanza/iq_test.go +++ b/stanza/iq_test.go @@ -34,6 +34,24 @@ func TestUnmarshalIqs(t *testing.T) { } } +func TestGenerateIqId(t *testing.T) { + t.Parallel() + iq := stanza.NewIQ(stanza.Attrs{Id: "1"}) + if iq.Id != "1" { + t.Errorf("NewIQ replaced id with %s", iq.Id) + } + + iq = stanza.NewIQ(stanza.Attrs{}) + if iq.Id != "1" { + t.Error("NewIQ did not generate an Id") + } + + otherIq := stanza.NewIQ(stanza.Attrs{}) + if iq.Id == otherIq.Id { + t.Errorf("NewIQ generated two identical ids: %s", iq.Id) + } +} + func TestGenerateIq(t *testing.T) { iq := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, From: "admin@localhost", To: "test@localhost", Id: "1"}) payload := stanza.DiscoInfo{