Various fixes

This commit is contained in:
rcorniere 2020-01-31 15:17:59 +01:00
parent 928c1595ef
commit 2083cbf29c
8 changed files with 50 additions and 93 deletions

View File

@ -47,6 +47,9 @@ func (c CustomPayload) Namespace() string {
return c.XMLName.Space
}
func (c CustomPayload) GetSet() *stanza.ResultSet {
return nil
}
func init() {
stanza.TypeRegistry.MapExtension(stanza.PKTIQ, xml.Name{Space: "my:custom:payload", Local: "query"}, CustomPayload{})
}

View File

@ -411,7 +411,7 @@ func closeConn(t *testing.T, sc *ServerConn) {
}
switch cls.(type) {
case stanza.StreamClosePacket:
fmt.Fprintf(sc.connection, stanza.StreamClose)
sc.connection.Write([]byte(stanza.StreamClose))
return
}
}
@ -430,7 +430,7 @@ func handlerClientConnectWithSession(t *testing.T, sc *ServerConn) {
sendStreamFeatures(t, sc) // Send initial features
readAuth(t, sc.decoder)
fmt.Fprintln(sc.connection, "<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>")
sc.connection.Write([]byte("<success xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"/>"))
checkClientOpenStream(t, sc) // Reset stream
sendRFC3921Feature(t, sc) // Send post auth features
@ -439,7 +439,10 @@ func handlerClientConnectWithSession(t *testing.T, sc *ServerConn) {
}
func checkClientOpenStream(t *testing.T, sc *ServerConn) {
sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
err := sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
if err != nil {
t.Fatalf("failed to set deadline: %v", err)
}
defer sc.connection.SetDeadline(time.Time{})
for { // TODO clean up. That for loop is not elegant and I prefer bounded recursion.

View File

@ -60,7 +60,7 @@ func TestGenerateHandshakeId(t *testing.T) {
checkOpenStreamHandshakeID(t, sc, <-uchan)
readHandshakeComponent(t, sc.decoder)
fmt.Fprintln(sc.connection, "<handshake/>") // That's all the server needs to return (see xep-0114)
sc.connection.Write([]byte("<handshake/>")) // That's all the server needs to return (see xep-0114)
return
}
@ -401,7 +401,10 @@ func handlerForComponentIQSend(t *testing.T, sc *ServerConn) {
// Used for ID and handshake related tests
func checkOpenStreamHandshakeID(t *testing.T, sc *ServerConn, streamID string) {
sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
err := sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
if err != nil {
t.Fatalf("failed to set deadline: %v", err)
}
defer sc.connection.SetDeadline(time.Time{})
for { // TODO clean up. That for loop is not elegant and I prefer bounded recursion.
@ -441,7 +444,10 @@ func handlerComponentFailedHandshakeDefaultID(t *testing.T, sc *ServerConn) {
Body: "Fail my handshake.",
}
s, _ := xml.Marshal(me)
fmt.Fprintln(sc.connection, string(s))
_, err := sc.connection.Write(s)
if err != nil {
t.Fatalf("could not write message: %v", err)
}
return
}
@ -469,6 +475,6 @@ func readHandshakeComponent(t *testing.T, decoder *xml.Decoder) {
func handlerForComponentHandshakeDefaultID(t *testing.T, sc *ServerConn) {
checkOpenStreamHandshakeDefaultID(t, sc)
readHandshakeComponent(t, sc.decoder)
fmt.Fprintln(sc.connection, "<handshake/>") // That's all the server needs to return (see xep-0114)
sc.connection.Write([]byte("<handshake/>")) // That's all the server needs to return (see xep-0114)
return
}

View File

@ -119,26 +119,27 @@ func (c *Command) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
case "affiliations":
a := Actions{}
d.DecodeElement(&a, &tt)
err = d.DecodeElement(&a, &tt)
c.CommandElement = &a
case "configure":
nt := Note{}
d.DecodeElement(&nt, &tt)
err = d.DecodeElement(&nt, &tt)
c.CommandElement = &nt
case "x":
f := Form{}
d.DecodeElement(&f, &tt)
err = d.DecodeElement(&f, &tt)
c.CommandElement = &f
default:
n := Node{}
e := d.DecodeElement(&n, &tt)
_ = e
err = d.DecodeElement(&n, &tt)
c.CommandElement = &n
if err != nil {
return err
}
}
if err != nil {
return err
}
case xml.EndElement:
if tt == start.End() {
return nil

View File

@ -226,7 +226,10 @@ func NewApprovePendingSubRequest(serviceId, sessionId, nodeId string) (*IQ, erro
return nil, err
}
var n Node
xml.Unmarshal(data, &n)
err = xml.Unmarshal(data, &n)
if err != nil {
return nil, err
}
iq, err := NewIQ(Attrs{Type: IQTypeSet, To: serviceId})
if err != nil {
@ -406,33 +409,35 @@ func (pso *PubSubOwner) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
case "affiliations":
aff := AffiliationsOwner{}
d.DecodeElement(&aff, &tt)
err = d.DecodeElement(&aff, &tt)
pso.OwnerUseCase = &aff
case "configure":
co := ConfigureOwner{}
d.DecodeElement(&co, &tt)
err = d.DecodeElement(&co, &tt)
pso.OwnerUseCase = &co
case "default":
def := DefaultOwner{}
d.DecodeElement(&def, &tt)
err = d.DecodeElement(&def, &tt)
pso.OwnerUseCase = &def
case "delete":
del := DeleteOwner{}
d.DecodeElement(&del, &tt)
err = d.DecodeElement(&del, &tt)
pso.OwnerUseCase = &del
case "purge":
pu := PurgeOwner{}
d.DecodeElement(&pu, &tt)
err = d.DecodeElement(&pu, &tt)
pso.OwnerUseCase = &pu
case "subscriptions":
subs := SubscriptionsOwner{}
d.DecodeElement(&subs, &tt)
err = d.DecodeElement(&subs, &tt)
pso.OwnerUseCase = &subs
if err != nil {
return err
}
}
if err != nil {
return err
}
case xml.EndElement:
if tt == start.End() {
return nil

View File

@ -20,9 +20,6 @@ func TestNewConfigureNode(t *testing.T) {
t.Fatalf("failed to create a configure node request: %v", err)
}
subR.Id = "config1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -136,9 +133,6 @@ func TestNewRequestDefaultConfig(t *testing.T) {
t.Fatalf("failed to create a default config request: %v", err)
}
subR.Id = "def1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -249,9 +243,6 @@ func TestNewDelNode(t *testing.T) {
t.Fatalf("failed to create a node delete request: %v", err)
}
subR.Id = "delete1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -324,9 +315,6 @@ func TestNewPurgeAllItems(t *testing.T) {
t.Fatalf("failed to create a purge all items request: %v", err)
}
subR.Id = "purge1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -383,9 +371,6 @@ func TestNewApproveSubRequest(t *testing.T) {
t.Fatalf("failed to create a sub approval request: %v", err)
}
subR.Id = "approve1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
frm, ok := subR.Extensions[0].(*stanza.Form)
if !ok {
@ -423,9 +408,6 @@ func TestNewGetPendingSubRequests(t *testing.T) {
t.Fatalf("failed to create a get pending subs request: %v", err)
}
subR.Id = "pending1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -507,9 +489,6 @@ func TestNewApprovePendingSubRequest(t *testing.T) {
t.Fatalf("failed to create a approve pending sub request: %v", err)
}
subR.Id = "pending2"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -549,9 +528,6 @@ func TestNewSubListRqPl(t *testing.T) {
t.Fatalf("failed to create a sub list request: %v", err)
}
subR.Id = "subman1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -627,9 +603,6 @@ func TestNewAffiliationListRequest(t *testing.T) {
t.Fatalf("failed to create an affiliations list request: %v", err)
}
subR.Id = "ent1"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -721,9 +694,6 @@ func TestNewModifAffiliationRequest(t *testing.T) {
t.Fatalf("failed to create a modif affiliation request: %v", err)
}
subR.Id = "ent3"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -866,11 +836,7 @@ func TestNewFormSubmissionOwner(t *testing.T) {
if err != nil {
t.Fatalf("failed to create a form submission request: %v", err)
}
subR.Id = "config2"
if err != nil {
t.Fatalf("Could not create request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)

View File

@ -44,9 +44,6 @@ func TestNewSubRequest(t *testing.T) {
t.Fatalf("failed to create a sub request: %v", err)
}
subR.Id = "sub1"
if err != nil {
t.Fatalf("Could not create a sub request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -103,9 +100,6 @@ func TestNewUnSubRequest(t *testing.T) {
t.Fatalf("failed to create an unsub request: %v", err)
}
subR.Id = "unsub1"
if err != nil {
t.Fatalf("Could not create a sub request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -167,9 +161,6 @@ func TestNewSubOptsRq(t *testing.T) {
t.Fatalf("failed to create a sub options request: %v", err)
}
subR.Id = "options1"
if err != nil {
t.Fatalf("Could not create a sub request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -277,9 +268,6 @@ func TestNewFormSubmission(t *testing.T) {
t.Fatalf("failed to create a form submission request: %v", err)
}
subR.Id = "options2"
if err != nil {
t.Fatalf("Could not create a sub request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -329,9 +317,6 @@ func TestNewSubAndConfig(t *testing.T) {
t.Fatalf("failed to create a sub and config request: %v", err)
}
subR.Id = "sub1"
if err != nil {
t.Fatalf("Could not create a sub request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -501,9 +486,6 @@ func TestNewSpecificItemRequest(t *testing.T) {
t.Fatalf("failed to create a specific item request: %v", err)
}
subR.Id = "items3"
if err != nil {
t.Fatalf("Could not create an items request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated sub request : %s", e)
@ -660,9 +642,6 @@ func TestNewDelItemFromNode(t *testing.T) {
t.Fatalf("failed to create a delete item from node request: %v", err)
}
subR.Id = "retract1"
if err != nil {
t.Fatalf("Could not create a del item request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
@ -702,9 +681,6 @@ func TestNewCreateNode(t *testing.T) {
t.Fatalf("failed to create a create node request: %v", err)
}
subR.Id = "create1"
if err != nil {
t.Fatalf("Could not create a create node request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
@ -777,9 +753,6 @@ func TestNewCreateAndConfigNode(t *testing.T) {
t.Fatalf("failed to create a create and config node request: %v", err)
}
subR.Id = "create1"
if err != nil {
t.Fatalf("Could not create a create node request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
@ -828,9 +801,6 @@ func TestNewRetrieveAllSubsRequest(t *testing.T) {
t.Fatalf("failed to create a get all subs request: %v", err)
}
subR.Id = "subscriptions1"
if err != nil {
t.Fatalf("Could not create a create node request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated del item request : %s", e)
@ -891,9 +861,6 @@ func TestNewRetrieveAllAffilsRequest(t *testing.T) {
t.Fatalf("failed to create a get all affiliations request: %v", err)
}
subR.Id = "affil1"
if err != nil {
t.Fatalf("Could not create retreive all affiliations request : %s", err)
}
if _, e := checkMarshalling(t, subR); e != nil {
t.Fatalf("Failed to check marshalling for generated retreive all affiliations request : %s", e)

View File

@ -159,12 +159,15 @@ func respondToIQ(t *testing.T, sc *ServerConn) {
// When a presence stanza is automatically sent (right now it's the case in the client), we may want to discard it
// and test further stanzas.
func discardPresence(t *testing.T, sc *ServerConn) {
sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
err := sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
if err != nil {
t.Fatalf("failed to set deadline: %v", err)
}
defer sc.connection.SetDeadline(time.Time{})
var presenceStz stanza.Presence
recvBuf := make([]byte, len(InitialPresence))
_, err := sc.connection.Read(recvBuf[:]) // recv data
_, err = sc.connection.Read(recvBuf[:]) // recv data
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
@ -173,7 +176,7 @@ func discardPresence(t *testing.T, sc *ServerConn) {
t.Errorf("read error: %s", err)
}
}
xml.Unmarshal(recvBuf, &presenceStz)
err = xml.Unmarshal(recvBuf, &presenceStz)
if err != nil {
t.Errorf("Expected presence but this happened : %s", err.Error())
@ -182,10 +185,13 @@ func discardPresence(t *testing.T, sc *ServerConn) {
// Reads next request coming from the Component. Expecting it to be an IQ request
func receiveIq(sc *ServerConn) (*stanza.IQ, error) {
sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
err := sc.connection.SetDeadline(time.Now().Add(defaultTimeout))
if err != nil {
return nil, err
}
defer sc.connection.SetDeadline(time.Time{})
var iqStz stanza.IQ
err := sc.decoder.Decode(&iqStz)
err = sc.decoder.Decode(&iqStz)
if err != nil {
return nil, err
}