From 255c2ef9adf37503b9e0f7801cd112303c04fd6c Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 16 Feb 2018 08:29:35 +0100 Subject: [PATCH] [TEST] improve xmpp struct library --- xmpp/connection.go | 2 +- xmpp/error_stanze.go | 4 +-- xmpp/error_stream.go | 2 +- xmpp/iq/service_discovery.go | 10 +++--- xmpp/iq/test.go | 7 ++++ xmpp/iq/test_test.go | 5 +++ xmpp/iq/version.go | 2 +- xmpp/utils.go | 13 ++----- xmpp/utils_test.go | 66 ++++++++++++++++++++++++++++++++++++ 9 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 xmpp/iq/test.go create mode 100644 xmpp/iq/test_test.go create mode 100644 xmpp/utils_test.go diff --git a/xmpp/connection.go b/xmpp/connection.go index f62c1f7..9758622 100644 --- a/xmpp/connection.go +++ b/xmpp/connection.go @@ -19,7 +19,7 @@ type StreamFeatures struct { // TLSStartTLS implements RFC 6120 - A.3 StartTLS Namespace type TLSStartTLS struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls starttls"` - Required *string `xml:"required"` + Required string `xml:"required,omitempty"` } // TLSProceed implements RFC 6120 - A.3 StartTLS Namespace diff --git a/xmpp/error_stanze.go b/xmpp/error_stanze.go index 9b0a243..64e2d32 100644 --- a/xmpp/error_stanze.go +++ b/xmpp/error_stanze.go @@ -8,7 +8,7 @@ type StanzaErrorGroup struct { Conflict *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas conflict"` FeatureNotImplemented *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas feature-not-implemented"` Forbidden *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas forbidden"` - Gone string `xml:"urn:ietf:params:xml:ns:xmpp-stanzas gone"` + Gone string `xml:"urn:ietf:params:xml:ns:xmpp-stanzas gone,omitempty"` InternalServerError *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas internal-server-error"` ItemNotFound *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas item-not-found"` JIDMalformed *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas jid-malformed"` @@ -17,7 +17,7 @@ type StanzaErrorGroup struct { NotAuthorized *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas not-authorized"` PolicyViolation *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas policy-violation"` RecipientUnavailable *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas recipient-unavailable"` - Redirect string `xml:"urn:ietf:params:xml:ns:xmpp-stanzas redirect"` + Redirect string `xml:"urn:ietf:params:xml:ns:xmpp-stanzas redirect,omitempty"` RegistrationRequired *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas registration-required"` RemoteServerNotFound *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas remote-server-not-found"` RemoteServerTimeout *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas remote-server-timeout"` diff --git a/xmpp/error_stream.go b/xmpp/error_stream.go index ed01f0f..3a47325 100644 --- a/xmpp/error_stream.go +++ b/xmpp/error_stream.go @@ -23,7 +23,7 @@ type StreamErrorGroup struct { Reset *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams reset"` ResourceConstraint *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams resource-constraint"` RestrictedXML *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams restricted-xml"` - SeeOtherHost string `xml:"urn:ietf:params:xml:ns:xmpp-streams see-other-host"` + SeeOtherHost string `xml:"urn:ietf:params:xml:ns:xmpp-streams see-other-host,omitempty"` SystemShutdown *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams system-shutdown"` UndefinedCondition *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams undefined-condition"` UnsupportedEncoding *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams unsupported-encoding"` diff --git a/xmpp/iq/service_discovery.go b/xmpp/iq/service_discovery.go index c312f52..593852e 100644 --- a/xmpp/iq/service_discovery.go +++ b/xmpp/iq/service_discovery.go @@ -9,7 +9,7 @@ import ( // DiscoQueryInfo implements XEP 0030: Service Discovery - 11.1 disco#info type DiscoQueryInfo struct { XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"` - Node *string `xml:"node,attr"` + Node string `xml:"node,attr,omitempty"` Identities []*DiscoIdentity Features []*DiscoFeature } @@ -18,7 +18,7 @@ type DiscoQueryInfo struct { type DiscoIdentity struct { XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info identity"` Category string `xml:"category"` //required - Name *string `xml:"name"` + Name string `xml:"name,omitempty"` Type string `xml:"type"` //required } @@ -31,7 +31,7 @@ type DiscoFeature struct { // DiscoQueryItem implements XEP 0030: Service Discovery - 11.2 disco#items type DiscoQueryItem struct { XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"` - Node *string `xml:"node,attr"` + Node string `xml:"node,attr,omitempty"` Items []*DiscoItem } @@ -39,6 +39,6 @@ type DiscoQueryItem struct { type DiscoItem struct { XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items item"` JID *xmppbase.JID `xml:"jid"` - Node *string `xml:"node"` - Name *string `xml:"name"` + Node string `xml:"node,omitempty"` + Name string `xml:"name,omitempty"` } diff --git a/xmpp/iq/test.go b/xmpp/iq/test.go new file mode 100644 index 0000000..9f51dca --- /dev/null +++ b/xmpp/iq/test.go @@ -0,0 +1,7 @@ +package xmppiq + +// just to validate, there is no functions in here +func init() { + a := 1 + a++ +} diff --git a/xmpp/iq/test_test.go b/xmpp/iq/test_test.go new file mode 100644 index 0000000..d43ceb5 --- /dev/null +++ b/xmpp/iq/test_test.go @@ -0,0 +1,5 @@ +package xmppiq + +import "testing" + +func Test(t *testing.T) {} diff --git a/xmpp/iq/version.go b/xmpp/iq/version.go index 1791f09..1d75f08 100644 --- a/xmpp/iq/version.go +++ b/xmpp/iq/version.go @@ -9,5 +9,5 @@ type Version struct { XMLName xml.Name `xml:"jabber:iq:version query"` Name string `xml:"name"` //required Version string `xml:"version"` //required - OS *string `xml:"os"` + OS string `xml:"os,omitempty"` } diff --git a/xmpp/utils.go b/xmpp/utils.go index 2ae5841..4842c17 100644 --- a/xmpp/utils.go +++ b/xmpp/utils.go @@ -22,22 +22,13 @@ func XMLStartElementToString(element *xml.StartElement) string { func XMLChildrenString(o interface{}) (result string) { val := reflect.ValueOf(o) - if val.Kind() == reflect.Interface && !val.IsNil() { - elm := val.Elem() - if elm.Kind() == reflect.Ptr && !elm.IsNil() && elm.Elem().Kind() == reflect.Ptr { - val = elm - } + if val.Kind() == reflect.Ptr { + val = val.Elem() } if val.Kind() == reflect.Struct { first := true for i := 0; i < val.NumField(); i++ { valueField := val.Field(i) - if valueField.Kind() == reflect.Interface && !valueField.IsNil() { - elm := valueField.Elem() - if elm.Kind() == reflect.Ptr && !elm.IsNil() && elm.Elem().Kind() == reflect.Ptr { - valueField = elm - } - } if xmlElement, ok := valueField.Interface().(*xml.Name); ok && xmlElement != nil { if first { diff --git a/xmpp/utils_test.go b/xmpp/utils_test.go new file mode 100644 index 0000000..502047f --- /dev/null +++ b/xmpp/utils_test.go @@ -0,0 +1,66 @@ +package xmpp + +import ( + "encoding/xml" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStartElementToString(t *testing.T) { + assert := assert.New(t) + + str := XMLStartElementToString(nil) + assert.Equal("", str) + + str = XMLStartElementToString(&xml.StartElement{ + Name: xml.Name{ + Local: "iq", + Space: "jabber:client", + }, + Attr: []xml.Attr{ + xml.Attr{ + Name: xml.Name{ + Local: "foo", + }, + Value: "bar", + }, + }, + }) + + assert.Equal(``, str) +} + +func remarhal(origin StanzaErrorGroup) StanzaErrorGroup { + el := StanzaErrorGroup{} + b, _ := xml.Marshal(origin) + xml.Unmarshal(b, &el) + return el +} + +func TestChildrenString(t *testing.T) { + assert := assert.New(t) + + el := remarhal(StanzaErrorGroup{ + Conflict: &xml.Name{}, + Gone: "a", + Forbidden: &xml.Name{}, + }) + str := XMLChildrenString(el) + assert.Equal("conflict, forbidden", str) + + str = XMLChildrenString(&el) + assert.Equal("conflict, forbidden", str) +} + +func TestCreateCookie(t *testing.T) { + assert := assert.New(t) + + a := CreateCookieString() + assert.NotEqual("", a) + + b := CreateCookieString() + assert.NotEqual("", b) + + assert.NotEqual(a, b) +}