xmpp/xmpp_test.go
author Chris Jones <christian.jones@sri.com>
Thu, 20 Feb 2014 15:55:19 -0700
changeset 184 ce49140fe60b
parent 163 3f891f7fe817
permissions -rw-r--r--
Added support for SASL PLAIN, from "Ildar Hizbulin" <hizel@vyborg.ru>.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     1
package xmpp
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     2
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     3
import (
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     4
	"bytes"
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
     5
	"encoding/xml"
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     6
	"reflect"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     7
	"strings"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     8
	"sync"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     9
	"testing"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    10
)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    11
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    12
func TestReadError(t *testing.T) {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    13
	r := strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    14
		`</stream:error>`)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    15
	ch := make(chan interface{})
163
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 147
diff changeset
    16
	cl := &Client{}
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 147
diff changeset
    17
	go cl.recvXml(r, ch, make(map[xml.Name]reflect.Type))
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    18
	x := <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    19
	se, ok := x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    20
	if !ok {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    21
		t.Fatalf("not StreamError: %T", x)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    22
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    23
	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    24
	assertEquals(t, "blah", se.Any.XMLName.Space)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    25
	if se.Text != nil {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    26
		t.Errorf("text not nil: %v", se.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    27
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    28
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    29
	r = strings.NewReader(`<stream:error><bad-foo xmlns="blah"/>` +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    30
		`<text xml:lang="en" xmlns="` + NsStreams +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    31
		`">Error text</text></stream:error>`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    32
	ch = make(chan interface{})
163
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 147
diff changeset
    33
	go cl.recvXml(r, ch, make(map[xml.Name]reflect.Type))
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    34
	x = <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    35
	se, ok = x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    36
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    37
		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    38
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    39
	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    40
	assertEquals(t, "blah", se.Any.XMLName.Space)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    41
	assertEquals(t, "Error text", se.Text.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    42
	assertEquals(t, "en", se.Text.Lang)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    43
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    44
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    45
func TestReadStream(t *testing.T) {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    46
	r := strings.NewReader(`<stream:stream to="foo.com" ` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    47
		`from="bar.org" id="42"` +
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    48
		`xmlns="` + NsClient + `" xmlns:stream="` + NsStream +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    49
		`" version="1.0">`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    50
	ch := make(chan interface{})
163
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 147
diff changeset
    51
	cl := &Client{}
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 147
diff changeset
    52
	go cl.recvXml(r, ch, make(map[xml.Name]reflect.Type))
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    53
	x := <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    54
	ss, ok := x.(*stream)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    55
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    56
		t.Fatalf("not stream: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    57
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    58
	assertEquals(t, "foo.com", ss.To)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    59
	assertEquals(t, "bar.org", ss.From)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    60
	assertEquals(t, "42", ss.Id)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    61
	assertEquals(t, "1.0", ss.Version)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    62
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    63
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    64
func testWrite(obj interface{}) string {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    65
	w := bytes.NewBuffer(nil)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    66
	ch := make(chan interface{})
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    67
	var wg sync.WaitGroup
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    68
	wg.Add(1)
163
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 147
diff changeset
    69
	cl := &Client{}
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    70
	go func() {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    71
		defer wg.Done()
163
3f891f7fe817 Removed the weirdo logging facility. There's now a Debug variable which can be set which replaces the former debug log. NewClient() will return an error if something goes wrong setting up the connection.
Chris Jones <chris@cjones.org>
parents: 147
diff changeset
    72
		cl.sendXml(w, ch)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    73
	}()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    74
	ch <- obj
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    75
	close(ch)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    76
	wg.Wait()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    77
	return w.String()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    78
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    79
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    80
func TestWriteError(t *testing.T) {
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    81
	se := &streamError{Any: Generic{XMLName: xml.Name{Local: "blah"}}}
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    82
	str := testWrite(se)
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 98
diff changeset
    83
	exp := `<error xmlns="` + NsStream + `"><blah></blah></error>`
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    84
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    85
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    86
	se = &streamError{Any: Generic{XMLName: xml.Name{Space: NsStreams, Local: "foo"}}, Text: &errText{Lang: "ru", Text: "Пошёл ты"}}
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    87
	str = testWrite(se)
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 98
diff changeset
    88
	exp = `<error xmlns="` + NsStream + `"><foo xmlns="` + NsStreams +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    89
		`"></foo><text xmlns="` + NsStreams +
114
a058e33c1666 Updated for the latest revision of the encoding/xml fixes: The context object owned by Encoder and Decoder isn't directly accessible.
Chris Jones <christian.jones@sri.com>
parents: 98
diff changeset
    90
		`" xml:lang="ru">Пошёл ты</text></error>`
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    91
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    92
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    93
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    94
func TestWriteStream(t *testing.T) {
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    95
	ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang: "en", Version: "1.0"}
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    96
	str := testWrite(ss)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    97
	exp := `<stream:stream xmlns="` + NsClient +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    98
		`" xmlns:stream="` + NsStream + `" to="foo.org"` +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    99
		` from="bar.com" id="42" xml:lang="en" version="1.0">`
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   100
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   101
}