sum7
/
yaja
Archived
1
0
Fork 0

[TEST] connect.go startStream

This commit is contained in:
Martin/Geno 2018-03-06 21:29:35 +01:00
parent 64ee706b4a
commit c62a2573ea
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
5 changed files with 140 additions and 11 deletions

View File

@ -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>`)) go server.Write([]byte(`<iq xmlns="jabber:client" type="get"><ping xmlns="urn:xmpp:ping"/></iq>`))
wgWait.Add(1) wgWait.Add(1)
go func() { go func() {
_, err := read(xml.NewDecoder(server)) _, inlineErr := read(xml.NewDecoder(server))
assert.NoError(err) assert.NoError(inlineErr)
wgWait.Done() wgWait.Done()
}() }()
@ -107,8 +107,8 @@ func TestSend(t *testing.T) {
wgWait.Add(1) wgWait.Add(1)
go func() { go func() {
err := client.Send(&xmpp.MessageClient{To: xmppbase.NewJID("a@a.de")}) inlineErr := client.Send(&xmpp.MessageClient{To: xmppbase.NewJID("a@a.de")})
assert.NoError(err) assert.NoError(inlineErr)
wgWait.Done() wgWait.Done()
}() }()
@ -120,8 +120,8 @@ func TestSend(t *testing.T) {
wgWait.Add(1) wgWait.Add(1)
go func() { go func() {
err := client.Send(&xmpp.IQClient{Type: xmpp.IQTypeGet}) inlineErr := client.Send(&xmpp.IQClient{Type: xmpp.IQTypeGet})
assert.NoError(err) assert.NoError(inlineErr)
wgWait.Done() wgWait.Done()
}() }()
@ -133,8 +133,8 @@ func TestSend(t *testing.T) {
wgWait.Add(1) wgWait.Add(1)
go func() { go func() {
err := client.Send(&xmpp.PresenceClient{Type: xmpp.PresenceTypeSubscribe}) inlineErr := client.Send(&xmpp.PresenceClient{Type: xmpp.PresenceTypeSubscribe})
assert.NoError(err) assert.NoError(inlineErr)
wgWait.Done() wgWait.Done()
}() }()

View File

@ -33,7 +33,7 @@ func (client *Client) startStream() (*xmpp.StreamFeatures, error) {
return nil, err return nil, err
} }
if element.Name.Space != xmpp.NSStream || element.Name.Local != "stream" { 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{} f := &xmpp.StreamFeatures{}
if err := client.ReadDecode(f); err != nil { if err := client.ReadDecode(f); err != nil {

129
client/connect_test.go Normal file
View File

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

View File

@ -55,7 +55,7 @@ func (client *Client) Start() error {
} else { } else {
if ch, ok := client.reply[iq.ID]; ok { if ch, ok := client.reply[iq.ID]; ok {
delete(client.reply, iq.ID) delete(client.reply, iq.ID)
//TODO is this usefull? //TODO is this useful?
go func() { ch <- iq }() go func() { ch <- iq }()
continue continue
} }

View File

@ -19,7 +19,7 @@ func TestStartElementToString(t *testing.T) {
Space: "jabber:client", Space: "jabber:client",
}, },
Attr: []xml.Attr{ Attr: []xml.Attr{
xml.Attr{ {
Name: xml.Name{ Name: xml.Name{
Local: "foo", Local: "foo",
}, },