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") }