xmpp/xmpp_test.go
author Chris Jones <christian.jones@sri.com>
Sun, 15 Sep 2013 11:34:35 -0600
changeset 142 0ff033eed887
parent 128 8342afcffc92
child 147 d7679d991b17
permissions -rw-r--r--
Removed copyright statements.
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{})
128
8342afcffc92 Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents: 126
diff changeset
    16
	go readXml(r, ch, make(map[xml.Name]reflect.Type))
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    17
	x := <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    18
	se, ok := x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    19
	if !ok {
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    20
		t.Fatalf("not StreamError: %T", x)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    21
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    22
	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
    23
	assertEquals(t, "blah", se.Any.XMLName.Space)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    24
	if se.Text != nil {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    25
		t.Errorf("text not nil: %v", se.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    26
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    27
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    28
	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
    29
		`<text xml:lang="en" xmlns="` + NsStreams +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    30
		`">Error text</text></stream:error>`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    31
	ch = make(chan interface{})
128
8342afcffc92 Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents: 126
diff changeset
    32
	go readXml(r, ch, make(map[xml.Name]reflect.Type))
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    33
	x = <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    34
	se, ok = x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    35
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    36
		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    37
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    38
	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
    39
	assertEquals(t, "blah", se.Any.XMLName.Space)
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    40
	assertEquals(t, "Error text", se.Text.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    41
	assertEquals(t, "en", se.Text.Lang)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    42
}
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
func TestReadStream(t *testing.T) {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    45
	r := strings.NewReader(`<stream:stream to="foo.com" ` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    46
		`from="bar.org" id="42"` +
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    47
		`xmlns="` + NsClient + `" xmlns:stream="` + NsStream +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    48
		`" version="1.0">`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    49
	ch := make(chan interface{})
128
8342afcffc92 Use reflection instead of constructor functions to create extended stanza structures.
Chris Jones <christian.jones@sri.com>
parents: 126
diff changeset
    50
	go readXml(r, ch, make(map[xml.Name]reflect.Type))
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    51
	x := <-ch
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    52
	ss, ok := x.(*stream)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    53
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    54
		t.Fatalf("not stream: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    55
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    56
	assertEquals(t, "foo.com", ss.To)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    57
	assertEquals(t, "bar.org", ss.From)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    58
	assertEquals(t, "42", ss.Id)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    59
	assertEquals(t, "1.0", ss.Version)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    60
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    61
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    62
func testWrite(obj interface{}) string {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    63
	w := bytes.NewBuffer(nil)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    64
	ch := make(chan interface{})
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    65
	var wg sync.WaitGroup
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    66
	wg.Add(1)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    67
	go func() {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    68
		defer wg.Done()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    69
		writeXml(w, ch)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    70
	}()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    71
	ch <- obj
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    72
	close(ch)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    73
	wg.Wait()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    74
	return w.String()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    75
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    76
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    77
func TestWriteError(t *testing.T) {
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    78
	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
    79
	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
    80
	exp := `<error xmlns="` + NsStream + `"><blah></blah></error>`
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    81
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    82
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    83
	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
    84
	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
    85
	exp = `<error xmlns="` + NsStream + `"><foo xmlns="` + NsStreams +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    86
		`"></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
    87
		`" xml:lang="ru">Пошёл ты</text></error>`
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    88
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    89
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    90
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    91
func TestWriteStream(t *testing.T) {
72
Chris Jones <christian.jones@sri.com>
parents: 56
diff changeset
    92
	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
    93
	str := testWrite(ss)
98
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    94
	exp := `<stream:stream xmlns="` + NsClient +
c9cc4eda6dce Updated for Go 1.0 + upcoming XML fixes.
Chris Jones <chris@cjones.org>
parents: 72
diff changeset
    95
		`" xmlns:stream="` + NsStream + `" to="foo.org"` +
56
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    96
		` 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
    97
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    98
}