~samwhited/xmpp

29e21e81079f25441428670750ad0d7de6e6135e — Sam Whited 3 years ago 80255bc
stream: restructure tests

Move them into a new package and make it easier to see which subtest a
failed.
1 files changed, 73 insertions(+), 59 deletions(-)

M stream/error_test.go
M stream/error_test.go => stream/error_test.go +73 -59
@@ 2,82 2,96 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package stream
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 _ error = (*Error)(nil)
var _ error = Error{}
var _ xml.Marshaler = (*Error)(nil)
var _ xml.Marshaler = Error{}
var _ xml.Unmarshaler = (*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")}, `<stream:error><see-other-host xmlns="urn:ietf:params:xml:ns:xmpp-streams">[::1]</see-other-host></stream:error>`, false},
	1: {&net.IPAddr{IP: net.ParseIP("127.0.0.1")}, `<stream:error><see-other-host xmlns="urn:ietf:params:xml:ns:xmpp-streams">127.0.0.1</see-other-host></stream:error>`, false},
}

func TestMarshalSeeOtherHost(t *testing.T) {
	for _, test := range []struct {
		ipaddr net.Addr
		xml    string
		err    bool
	}{
		// see-other-host errors should wrap IPv6 addresses in brackets.
		{&net.IPAddr{IP: net.ParseIP("::1")}, `<stream:error><see-other-host xmlns="urn:ietf:params:xml:ns:xmpp-streams">[::1]</see-other-host></stream:error>`, false},
		{&net.IPAddr{IP: net.ParseIP("127.0.0.1")}, `<stream:error><see-other-host xmlns="urn:ietf:params:xml:ns:xmpp-streams">127.0.0.1</see-other-host></stream:error>`, false},
	} {
		soh := SeeOtherHostError(test.ipaddr)
		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)
			continue
		case !test.err && err != nil:
			t.Error(err)
			continue
		case err != nil:
			continue
		case xbs != test.xml:
			t.Logf("Expected `%s` but got `%s`", test.xml, xbs)
			t.Fail()
		}
	for i, test := range marshalSeeOtherHostTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			soh := stream.SeeOtherHostError(test.ipaddr)
			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 _, test := range []struct {
		xml string
		se  Error
		err bool
	}{
		{
			`<stream:error><restricted-xml xmlns="urn:ietf:params:xml:ns:xmpp-streams"></restricted-xml></stream:error>`,
			RestrictedXML, false,
		},
		{
			`<stream:error></a>`,
			RestrictedXML, true,
		},
	} {
		s := 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)
			continue
		case !test.err && err != nil:
			t.Error(err)
			continue
		case err != nil:
			continue
		case s.Err != test.se.Err || string(s.InnerXML) != string(test.se.InnerXML):
			t.Errorf("Expected `%#v` but got `%#v`", test.se, s)
		}
	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 RestrictedXML.Error() != "restricted-xml" {
	if stream.RestrictedXML.Error() != "restricted-xml" {
		t.Error("Error should return the name of the err")
	}
}