~samwhited/xmpp

ref: 8a516e9e064619dcb3d84499b66e1562546be918 xmpp/stream/error_test.go -rw-r--r-- 2.6 KiB
8a516e9eSam Whited .builds: validate license headers 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2015 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package stream_test

import (
	"encoding/xml"
	"fmt"
	"net"
	"testing"

	"mellium.im/xmpp/stream"
)

var (
	_ error           = (*stream.Error)(nil)
	_ error           = stream.Error{}
	_ xml.Marshaler   = (*stream.Error)(nil)
	_ xml.Marshaler   = stream.Error{}
	_ xml.Unmarshaler = (*stream.Error)(nil)
)

var marshalSeeOtherHostTests = [...]struct {
	ipaddr net.Addr
	xml    string
	err    bool
}{
	// see-other-host errors should wrap IPv6 addresses in brackets.
	0: {&net.IPAddr{IP: net.ParseIP("::1")}, `<error xmlns="http://etherx.jabber.org/streams"><see-other-host xmlns="urn:ietf:params:xml:ns:xmpp-streams">[::1]</see-other-host></error>`, false},
	1: {&net.IPAddr{IP: net.ParseIP("127.0.0.1")}, `<error xmlns="http://etherx.jabber.org/streams"><see-other-host xmlns="urn:ietf:params:xml:ns:xmpp-streams">127.0.0.1</see-other-host></error>`, false},
}

func TestMarshalSeeOtherHost(t *testing.T) {
	for i, test := range marshalSeeOtherHostTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			soh := stream.SeeOtherHostError(test.ipaddr, nil)
			xb, err := xml.Marshal(soh)
			switch xbs := string(xb); {
			case test.err && err == nil:
				t.Errorf("Expected marshaling SeeOtherHost error for address `%v` to fail", test.ipaddr)
				return
			case !test.err && err != nil:
				t.Error(err)
				return
			case err != nil:
				return
			case xbs != test.xml:
				t.Errorf("Bad output:\nwant=`%s`,\ngot=`%s`", test.xml, xbs)
			}
		})
	}
}

var unmarshalTests = [...]struct {
	xml string
	se  stream.Error
	err bool
}{
	0: {
		`<stream:error><restricted-xml xmlns="urn:ietf:params:xml:ns:xmpp-streams"></restricted-xml></stream:error>`,
		stream.RestrictedXML, false,
	},
	1: {
		`<stream:error></a>`,
		stream.RestrictedXML, true,
	},
}

func TestUnmarshal(t *testing.T) {
	for i, test := range unmarshalTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			s := stream.Error{}
			err := xml.Unmarshal([]byte(test.xml), &s)
			switch {
			case test.err && err == nil:
				t.Errorf("Expected unmarshaling error for `%v` to fail", test.xml)
				return
			case !test.err && err != nil:
				t.Error(err)
				return
			case err != nil:
				return
			case s.Err != test.se.Err:
				t.Errorf("Expected Err `%#v` but got `%#v`", test.se, s)
				//case string(s.InnerXML) != string(test.se.InnerXML):
				//	t.Errorf("Expected `%#v` but got `%#v`", test.se, s)
			}
		})
	}
}

func TestErrorReturnsErr(t *testing.T) {
	if stream.RestrictedXML.Error() != "restricted-xml" {
		t.Error("Error should return the name of the err")
	}
}