xmpp_test.go
author Chris Jones <chris@cjones.org>
Sun, 08 Jan 2012 13:04:50 -0700
branch20120108-close
changeset 70 bb629d22148a
parent 56 74686b8c9146
child 72 53f15893a1a7
permissions -rw-r--r--
Closing this branch.
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
// Copyright 2011 The Go Authors.  All rights reserved.
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     2
// Use of this source code is governed by a BSD-style
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     3
// license that can be found in the LICENSE file.
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     4
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     5
package xmpp
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     6
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     7
import (
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     8
	"bytes"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
     9
	"reflect"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    10
	"strings"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    11
	"sync"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    12
	"testing"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    13
	"xml"
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    14
)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    15
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    16
func TestReadError(t *testing.T) {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    17
	r := strings.NewReader(`<stream:error><bad-foo/></stream:error>`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    18
	ch := make(chan interface{})
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    19
	go readXml(r, ch, make(map[string] func(*xml.Name) interface{}))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    20
	x := <- ch
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    21
	se, ok := x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    22
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    23
		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    24
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    25
	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    26
	assertEquals(t, "", se.Any.XMLName.Space)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    27
	if se.Text != nil {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    28
		t.Errorf("text not nil: %v", se.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    29
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    30
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    31
	r = strings.NewReader(`<stream:error><bad-foo/>` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    32
		`<text xml:lang="en" xmlns="` + NsStreams +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    33
		`">Error text</text></stream:error>`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    34
	ch = make(chan interface{})
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    35
	go readXml(r, ch, make(map[string] func(*xml.Name) interface{}))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    36
	x = <- ch
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    37
	se, ok = x.(*streamError)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    38
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    39
		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    40
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    41
	assertEquals(t, "bad-foo", se.Any.XMLName.Local)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    42
	assertEquals(t, "", se.Any.XMLName.Space)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    43
	assertEquals(t, "Error text", se.Text.Text)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    44
	assertEquals(t, "en", se.Text.Lang)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    45
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    46
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    47
func TestReadStream(t *testing.T) {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    48
	r := strings.NewReader(`<stream:stream to="foo.com" ` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    49
		`from="bar.org" id="42"` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    50
		`xmlns="jabber:client" xmlns:stream="` + NsStream +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    51
		`" version="1.0">`)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    52
	ch := make(chan interface{})
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    53
	go readXml(r, ch, make(map[string] func(*xml.Name) interface{}))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    54
	x := <- ch
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    55
	ss, ok := x.(*stream)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    56
	if !ok {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    57
		t.Fatalf("not stream: %v", reflect.TypeOf(x))
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    58
	}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    59
	assertEquals(t, "foo.com", ss.To)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    60
	assertEquals(t, "bar.org", ss.From)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    61
	assertEquals(t, "42", ss.Id)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    62
	assertEquals(t, "1.0", ss.Version)
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
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    65
func testWrite(obj interface{}) string {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    66
	w := bytes.NewBuffer(nil)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    67
	ch := make(chan interface{})
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    68
	var wg sync.WaitGroup
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    69
	wg.Add(1)
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()
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    72
		writeXml(w, ch)
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) {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    81
	se := &streamError{Any: Generic{XMLName: xml.Name{Local:
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    82
				"blah"}}}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    83
	str := testWrite(se)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    84
	exp := `<stream:error><blah></blah></stream:error>`
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    85
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    86
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    87
	se = &streamError{Any: Generic{XMLName: xml.Name{Space:
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    88
				NsStreams, Local: "foo"}}, Text:
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    89
		&errText{Lang: "ru", Text: "Пошёл ты"}}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    90
	str = testWrite(se)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    91
	exp = `<stream:error><foo xmlns="` + NsStreams +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    92
		`"></foo><text xmlns="` + NsStreams +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    93
		`" xml:lang="ru">Пошёл ты</text></stream:error>`
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    94
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    95
}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    96
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    97
func TestWriteStream(t *testing.T) {
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    98
	ss := &stream{To: "foo.org", From: "bar.com", Id: "42", Lang:
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
    99
		"en", Version: "1.0"}
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   100
	str := testWrite(ss)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   101
	exp := `<stream:stream xmlns="jabber:client"` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   102
		` xmlns:stream="` + NsStream + `" to="foo.org"` +
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   103
		` 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
   104
	assertEquals(t, exp, str)
74686b8c9146 Another null change for eol stuff.
Chris Jones <chris@cjones.org>
parents: 38
diff changeset
   105
}