func readAuth(t *testing.T, decoder *xml.Decoder) string {
	se, err := stanza.NextStart(decoder)
	if err != nil {
		t.Errorf("cannot read auth: %s", err)
		return ""
	}
	var nv interface{}
	nv = &stanza.SASLAuth{}
	if err = decoder.DecodeElement(nv, &se); err != nil {
		t.Errorf("cannot decode auth: %s", err)
		return ""
	}
	switch v := nv.(type) {
	case *stanza.SASLAuth:
		return v.Value
	}
	return ""
}
TODO return err in case of error reading the auth params
package xmpp
import (
	"encoding/xml"
	"fmt"
	"gosrc.io/xmpp/stanza"
	"net"
	"testing"
	"time"
)
const (
	defaultTimeout       = 2 * time.Second
	testComponentDomain  = "localhost"
	defaultServerName    = "testServer"
	defaultStreamID      = "91bd0bba-012f-4d92-bb17-5fc41e6fe545"
	defaultComponentName = "Test Component"
	serverStreamOpen     = "<?xml version='1.0'?><stream:stream to='%s' id='%s' xmlns='%s' xmlns:stream='%s' version='1.0'>"
	testHandshakePort = iota + 15222
	testDecoderPort
	testSendIqPort
	testSendIqFailPort
	testSendRawPort
	testDisconnectPort
	testSManDisconnectPort
	testClientBasePort
	testClientRawPort
	testClientIqPort
	testClientIqFailPort
	testClientPostConnectHook
	testClientStreamManagement
)
type ClientHandler func(t *testing.T, serverConn *ServerConn)
type ServerMock struct {
	t                 *testing.T
	handler           ClientHandler
	listener          net.Listener
	serverConnections []*ServerConn
	done              chan struct{}
}
type ServerConn struct {
	connection net.Conn
	decoder    *xml.Decoder
}
func (mock *ServerMock) Start(t *testing.T, addr string, handler ClientHandler) {
	mock.t = t
	mock.handler = handler
	if err := mock.init(addr); err != nil {
		return
	}
	go mock.loop()
}
func (mock *ServerMock) Stop() {
	close(mock.done)
	if mock.listener != nil {
		mock.listener.Close()
	}
	for _, c := range mock.serverConnections {
		c.connection.Close()
	}
}
func (mock *ServerMock) init(addr string) error {
	mock.done = make(chan struct{})
	l, err := net.Listen("tcp", addr)
	if err != nil {
		mock.t.Errorf("TCPServerMock cannot listen on address: %q", addr)
		return err
	}
	mock.listener = l
	return nil
}
func (mock *ServerMock) loop() {
	listener := mock.listener
	for {
		conn, err := listener.Accept()
		serverConn := &ServerConn{conn, xml.NewDecoder(conn)}
		if err != nil {
			select {
			case <-mock.done:
				return
			default:
				mock.t.Error("TCPServerMock accept error:", err.Error())
			}
			return
		}
		mock.serverConnections = append(mock.serverConnections, serverConn)
		go mock.handler(mock.t, serverConn)
	}
}
func respondToIQ(t *testing.T, sc *ServerConn) {
	iqReq, err := receiveIq(sc)
	if err != nil {
		t.Fatalf("failed to receive IQ : %s", err.Error())
	}
	if vld, _ := iqReq.IsValid(); !vld {
		mockIQError(sc.connection)
		return
	}
	iqResp, err := stanza.NewIQ(stanza.Attrs{Type: stanza.IQTypeResult, From: iqReq.To, To: iqReq.From, Id: iqReq.Id, Lang: "en"})
	if err != nil {
		t.Fatalf("failed to create iqResp: %v", err)
	}
	disco := iqResp.DiscoInfo()
	disco.AddFeatures("vcard-temp",
		`http: 
	disco.AddIdentity("Multicast", "service", "multicast")
	iqResp.Payload = disco
	mResp, err := xml.Marshal(iqResp)
	_, err = fmt.Fprintln(sc.connection, string(mResp))
	if err != nil {
		t.Errorf("Could not send response stanza : %s", err)
	}
	return
}
func discardPresence(t *testing.T, sc *ServerConn) {
	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[:])  
	if err != nil {
		if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
			t.Errorf("read timeout: %s", err)
		} else {
			t.Errorf("read error: %s", err)
		}
	}
	err = xml.Unmarshal(recvBuf, &presenceStz)
	if err != nil {
		t.Errorf("Expected presence but this happened : %s", err.Error())
	}
}
func receiveIq(sc *ServerConn) (*stanza.IQ, error) {
	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)
	if err != nil {
		return nil, err
	}
	return &iqStz, nil
}
func mockIQError(c net.Conn) {
	s := stanza.StreamError{
		XMLName: xml.Name{Local: "stream:error"},
		Error:   xml.Name{Local: "xml-not-well-formed"},
		Text:    `XML was not well-formed`,
	}
	raw, _ := xml.Marshal(s)
	fmt.Fprintln(c, string(raw))
	fmt.Fprintln(c, `</stream:stream>`)
}
func sendStreamFeatures(t *testing.T, sc *ServerConn) {
	features := `<stream:features>
  <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
    <mechanism>PLAIN</mechanism>
  </mechanisms>
</stream:features>`
	if _, err := fmt.Fprintln(sc.connection, features); err != nil {
		t.Errorf("cannot send stream feature: %s", err)
	}
}