[TEST] connect.go startStream
This commit is contained in:
parent
64ee706b4a
commit
c62a2573ea
|
@ -66,8 +66,8 @@ func TestDecode(t *testing.T) {
|
|||
go server.Write([]byte(`<iq xmlns="jabber:client" type="get"><ping xmlns="urn:xmpp:ping"/></iq>`))
|
||||
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()
|
||||
}()
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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, "<?xml version='1.0'?>\n"+
|
||||
"<stream:stream to='%s' xmlns='%s'\n"+
|
||||
" xmlns:stream='%s' version='1.0'>\n",
|
||||
"example.com", xmpp.NSClient, xmpp.NSStream)
|
||||
assert.NoError(inlineErr)
|
||||
|
||||
_, inlineErr = server.Write([]byte(`
|
||||
<features xmlns="http://etherx.jabber.org/streams">
|
||||
<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
|
||||
<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/>
|
||||
<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
|
||||
<mechanism>PLAIN</mechanism>
|
||||
<mechanism>notworking</mechanism>
|
||||
</mechanisms>
|
||||
</features>
|
||||
`))
|
||||
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, "<?xml version='1.0'?>\n"+
|
||||
"<stream:stream to='%s' xmlns='%s'\n"+
|
||||
" xmlns:stream='%s' version='1.0'>\n",
|
||||
"example.com", xmpp.NSClient, xmpp.NSStream)
|
||||
assert.NoError(inlineErr)
|
||||
|
||||
_, inlineErr = server.Write([]byte(`
|
||||
<f>
|
||||
`))
|
||||
assert.NoError(inlineErr)
|
||||
wgWait.Done()
|
||||
}()
|
||||
|
||||
_, err = client.startStream()
|
||||
wgWait.Wait()
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "<features>")
|
||||
|
||||
// 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, "<s>")
|
||||
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")
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ func TestStartElementToString(t *testing.T) {
|
|||
Space: "jabber:client",
|
||||
},
|
||||
Attr: []xml.Attr{
|
||||
xml.Attr{
|
||||
{
|
||||
Name: xml.Name{
|
||||
Local: "foo",
|
||||
},
|
||||
|
|
Reference in New Issue