sum7
/
yaja
Archived
1
0
Fork 0

[TEST] improve xmpp struct library

This commit is contained in:
Martin/Geno 2018-02-16 08:29:35 +01:00
parent 3e39507f5a
commit 255c2ef9ad
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
9 changed files with 90 additions and 21 deletions

View File

@ -19,7 +19,7 @@ type StreamFeatures struct {
// TLSStartTLS implements RFC 6120 - A.3 StartTLS Namespace // TLSStartTLS implements RFC 6120 - A.3 StartTLS Namespace
type TLSStartTLS struct { type TLSStartTLS struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls starttls"` 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 // TLSProceed implements RFC 6120 - A.3 StartTLS Namespace

View File

@ -8,7 +8,7 @@ type StanzaErrorGroup struct {
Conflict *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas conflict"` 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"` 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"` 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"` 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"` 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"` 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"` 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"` 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"` 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"` 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"` 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"` RemoteServerTimeout *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas remote-server-timeout"`

View File

@ -23,7 +23,7 @@ type StreamErrorGroup struct {
Reset *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams reset"` 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"` 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"` 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"` 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"` 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"` UnsupportedEncoding *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams unsupported-encoding"`

View File

@ -9,7 +9,7 @@ import (
// DiscoQueryInfo implements XEP 0030: Service Discovery - 11.1 disco#info // DiscoQueryInfo implements XEP 0030: Service Discovery - 11.1 disco#info
type DiscoQueryInfo struct { type DiscoQueryInfo struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"` XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
Node *string `xml:"node,attr"` Node string `xml:"node,attr,omitempty"`
Identities []*DiscoIdentity Identities []*DiscoIdentity
Features []*DiscoFeature Features []*DiscoFeature
} }
@ -18,7 +18,7 @@ type DiscoQueryInfo struct {
type DiscoIdentity struct { type DiscoIdentity struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info identity"` XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info identity"`
Category string `xml:"category"` //required Category string `xml:"category"` //required
Name *string `xml:"name"` Name string `xml:"name,omitempty"`
Type string `xml:"type"` //required Type string `xml:"type"` //required
} }
@ -31,7 +31,7 @@ type DiscoFeature struct {
// DiscoQueryItem implements XEP 0030: Service Discovery - 11.2 disco#items // DiscoQueryItem implements XEP 0030: Service Discovery - 11.2 disco#items
type DiscoQueryItem struct { type DiscoQueryItem struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"` XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"`
Node *string `xml:"node,attr"` Node string `xml:"node,attr,omitempty"`
Items []*DiscoItem Items []*DiscoItem
} }
@ -39,6 +39,6 @@ type DiscoQueryItem struct {
type DiscoItem struct { type DiscoItem struct {
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items item"` XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items item"`
JID *xmppbase.JID `xml:"jid"` JID *xmppbase.JID `xml:"jid"`
Node *string `xml:"node"` Node string `xml:"node,omitempty"`
Name *string `xml:"name"` Name string `xml:"name,omitempty"`
} }

7
xmpp/iq/test.go Normal file
View File

@ -0,0 +1,7 @@
package xmppiq
// just to validate, there is no functions in here
func init() {
a := 1
a++
}

5
xmpp/iq/test_test.go Normal file
View File

@ -0,0 +1,5 @@
package xmppiq
import "testing"
func Test(t *testing.T) {}

View File

@ -9,5 +9,5 @@ type Version struct {
XMLName xml.Name `xml:"jabber:iq:version query"` XMLName xml.Name `xml:"jabber:iq:version query"`
Name string `xml:"name"` //required Name string `xml:"name"` //required
Version string `xml:"version"` //required Version string `xml:"version"` //required
OS *string `xml:"os"` OS string `xml:"os,omitempty"`
} }

View File

@ -22,22 +22,13 @@ func XMLStartElementToString(element *xml.StartElement) string {
func XMLChildrenString(o interface{}) (result string) { func XMLChildrenString(o interface{}) (result string) {
val := reflect.ValueOf(o) val := reflect.ValueOf(o)
if val.Kind() == reflect.Interface && !val.IsNil() { if val.Kind() == reflect.Ptr {
elm := val.Elem() val = val.Elem()
if elm.Kind() == reflect.Ptr && !elm.IsNil() && elm.Elem().Kind() == reflect.Ptr {
val = elm
}
} }
if val.Kind() == reflect.Struct { if val.Kind() == reflect.Struct {
first := true first := true
for i := 0; i < val.NumField(); i++ { for i := 0; i < val.NumField(); i++ {
valueField := val.Field(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 xmlElement, ok := valueField.Interface().(*xml.Name); ok && xmlElement != nil {
if first { if first {

66
xmpp/utils_test.go Normal file
View File

@ -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("<nil>", 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(`<iq xmlns="jabber:client" foo="bar">`, 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)
}