From c62a2573ead80f3ae1047970ed91e3dd98f8d204 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Tue, 6 Mar 2018 21:29:35 +0100 Subject: [PATCH] [TEST] connect.go startStream --- client/comm_test.go | 16 ++--- client/connect.go | 2 +- client/connect_test.go | 129 +++++++++++++++++++++++++++++++++++++++++ client/start.go | 2 +- xmpp/utils_test.go | 2 +- 5 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 client/connect_test.go diff --git a/client/comm_test.go b/client/comm_test.go index 37e9b52..8b6d06a 100644 --- a/client/comm_test.go +++ b/client/comm_test.go @@ -66,8 +66,8 @@ func TestDecode(t *testing.T) { go server.Write([]byte(``)) wgWait.Add(1) go func() { - _, err := read(xml.NewDecoder(server)) - assert.NoError(err) + _, inlineErr := read(xml.NewDecoder(server)) + assert.NoError(inlineErr) wgWait.Done() }() @@ -107,8 +107,8 @@ func TestSend(t *testing.T) { wgWait.Add(1) go func() { - err := client.Send(&xmpp.MessageClient{To: xmppbase.NewJID("a@a.de")}) - assert.NoError(err) + inlineErr := client.Send(&xmpp.MessageClient{To: xmppbase.NewJID("a@a.de")}) + assert.NoError(inlineErr) wgWait.Done() }() @@ -120,8 +120,8 @@ func TestSend(t *testing.T) { wgWait.Add(1) go func() { - err := client.Send(&xmpp.IQClient{Type: xmpp.IQTypeGet}) - assert.NoError(err) + inlineErr := client.Send(&xmpp.IQClient{Type: xmpp.IQTypeGet}) + assert.NoError(inlineErr) wgWait.Done() }() @@ -133,8 +133,8 @@ func TestSend(t *testing.T) { wgWait.Add(1) go func() { - err := client.Send(&xmpp.PresenceClient{Type: xmpp.PresenceTypeSubscribe}) - assert.NoError(err) + inlineErr := client.Send(&xmpp.PresenceClient{Type: xmpp.PresenceTypeSubscribe}) + assert.NoError(inlineErr) wgWait.Done() }() diff --git a/client/connect.go b/client/connect.go index 9fc933d..356811f 100644 --- a/client/connect.go +++ b/client/connect.go @@ -33,7 +33,7 @@ func (client *Client) startStream() (*xmpp.StreamFeatures, error) { return nil, err } if element.Name.Space != xmpp.NSStream || element.Name.Local != "stream" { - return nil, errors.New("is not stream") + return nil, errors.New("is no stream") } f := &xmpp.StreamFeatures{} if err := client.ReadDecode(f); err != nil { diff --git a/client/connect_test.go b/client/connect_test.go new file mode 100644 index 0000000..5a9e10a --- /dev/null +++ b/client/connect_test.go @@ -0,0 +1,129 @@ +package client + +import ( + "encoding/xml" + "fmt" + "net" + "sync" + "testing" + + log "github.com/sirupsen/logrus" + + "github.com/stretchr/testify/assert" + + "dev.sum7.eu/genofire/yaja/xmpp" + "dev.sum7.eu/genofire/yaja/xmpp/base" +) + +func TestStartStream(t *testing.T) { + assert := assert.New(t) + + server, clientConn := net.Pipe() + client := &Client{ + JID: xmppbase.NewJID("a@example.com"), + Logging: log.WithField("test", "startStream"), + } + client.setConnection(clientConn) + wgWait := &sync.WaitGroup{} + + // complete connection + wgWait.Add(1) + go func() { + decoder := xml.NewDecoder(server) + elm, inlineErr := read(decoder) + assert.NoError(inlineErr) + assert.Equal("http://etherx.jabber.org/streams", elm.Name.Space) + + _, inlineErr = fmt.Fprintf(server, "\n"+ + "\n", + "example.com", xmpp.NSClient, xmpp.NSStream) + assert.NoError(inlineErr) + + _, inlineErr = server.Write([]byte(` + + + + + PLAIN + notworking + + + `)) + assert.NoError(inlineErr) + wgWait.Done() + }() + + _, err := client.startStream() + wgWait.Wait() + assert.NoError(err) + + // no features first rechieve + wgWait.Add(1) + go func() { + decoder := xml.NewDecoder(server) + elm, inlineErr := read(decoder) + assert.NoError(inlineErr) + assert.Equal("http://etherx.jabber.org/streams", elm.Name.Space) + + _, inlineErr = fmt.Fprintf(server, "\n"+ + "\n", + "example.com", xmpp.NSClient, xmpp.NSStream) + assert.NoError(inlineErr) + + _, inlineErr = server.Write([]byte(` + + `)) + assert.NoError(inlineErr) + wgWait.Done() + }() + + _, err = client.startStream() + wgWait.Wait() + assert.Error(err) + assert.Contains(err.Error(), "") + + // no stream receive + wgWait.Add(1) + go func() { + decoder := xml.NewDecoder(server) + elm, inlineErr := read(decoder) + assert.NoError(inlineErr) + assert.Equal("http://etherx.jabber.org/streams", elm.Name.Space) + + _, inlineErr = fmt.Fprintf(server, "") + assert.NoError(inlineErr) + + wgWait.Done() + }() + + _, err = client.startStream() + wgWait.Wait() + assert.Error(err) + assert.Contains(err.Error(), "is no stream") + + // client disconnect after stream start + wgWait.Add(1) + go func() { + decoder := xml.NewDecoder(server) + elm, inlineErr := read(decoder) + assert.NoError(inlineErr) + assert.Equal("http://etherx.jabber.org/streams", elm.Name.Space) + + client.Close() + + wgWait.Done() + }() + + _, err = client.startStream() + wgWait.Wait() + assert.Error(err) + assert.Contains(err.Error(), "closed pipe") + + // client disconnect before stream start + _, err = client.startStream() + wgWait.Wait() + assert.Error(err) + assert.Contains(err.Error(), "closed pipe") +} diff --git a/client/start.go b/client/start.go index 9362077..16f5a26 100644 --- a/client/start.go +++ b/client/start.go @@ -55,7 +55,7 @@ func (client *Client) Start() error { } else { if ch, ok := client.reply[iq.ID]; ok { delete(client.reply, iq.ID) - //TODO is this usefull? + //TODO is this useful? go func() { ch <- iq }() continue } diff --git a/xmpp/utils_test.go b/xmpp/utils_test.go index 502047f..7412980 100644 --- a/xmpp/utils_test.go +++ b/xmpp/utils_test.go @@ -19,7 +19,7 @@ func TestStartElementToString(t *testing.T) { Space: "jabber:client", }, Attr: []xml.Attr{ - xml.Attr{ + { Name: xml.Name{ Local: "foo", },