~samwhited/xmpp

ref: faef9ff39736dc97922b2c8bfd5dd4581c73b460 xmpp/internal/stream/reader_test.go -rw-r--r-- 3.7 KiB
faef9ff3Sam Whited styling: minor typo fix in fuzz tests 1 year, 6 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2020 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 (
	"bytes"
	"encoding/xml"
	"reflect"
	"strconv"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/stream"
	"mellium.im/xmpp/internal/xmpptest"
	streamerr "mellium.im/xmpp/stream"
)

var readerTestCases = [...]struct {
	in      string
	skip    int
	err     error
	errType error
}{
	0: {},
	1: {
		in: `<stream></stream>`,
	},
	2: {
		in: `<stream:stream
					version='1.0'
					xmlns='jabber:client'
					xmlns:stream='http://wrong.namespace.example.org/'/>`,
	},
	3: {
		in: `<other:stream
					version='1.0'
					xmlns='jabber:client'
					xmlns:other='http://etherx.jabber.org/streams'/>`,
		err: stream.ErrUnexpectedRestart,
	},
	4: {
		in: `<stream:stream
					version='1.0'
					xmlns='jabber:client'
					xmlns:stream='http://etherx.jabber.org/streams'/>`,
		err: stream.ErrUnexpectedRestart,
	},
	5: {
		in: `<stream:unknown
					version='1.0'
					xmlns='jabber:client'
					xmlns:stream='http://etherx.jabber.org/streams'/>`,
		err: stream.ErrUnknownStreamElement,
	},
	6: {
		in: `<stream:error/>`,
	},
	7: {
		in:  `<stream:error xmlns:stream='http://etherx.jabber.org/streams'/>`,
		err: streamerr.Error{},
	},
	8: {
		in:  `<error xmlns='http://etherx.jabber.org/streams'/>`,
		err: streamerr.Error{},
	},
	9: {
		in: `<stream:error xmlns:stream='http://etherx.jabber.org/streams'>
	<not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
</stream:error>`,
		err: streamerr.NotWellFormed,
	},
	10: {
		in: `<stream:error xmlns:stream='http://etherx.jabber.org/streams'>
	</notwellformed>
</stream:error>`,
		errType: &xml.SyntaxError{},
	},
	11: {
		in:   `<stream:stream xmlns:stream='http://etherx.jabber.org/streams'></stream:stream>`,
		skip: 1,
	},
}

func TestReader(t *testing.T) {
	for i, tc := range readerTestCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			var out strings.Builder
			d := xml.NewDecoder(strings.NewReader(tc.in))
			e := xml.NewEncoder(&out)
			for tc.skip > 0 {
				// Throw away any tokens that we need to make the stream well-formed,
				// but that we don't want in the actual test output.
				tok, err := d.Token()
				if err != nil {
					t.Fatalf("error skipping tokens: %v", err)
				}
				err = e.EncodeToken(tok)
				if err != nil {
					t.Fatalf("error encoding skipped tokens: %v", err)
				}
				tc.skip--
			}
			_, err := xmlstream.Copy(e, stream.Reader(d))
			switch {
			case tc.errType != nil:
				if reflect.TypeOf(tc.errType) != reflect.TypeOf(err) {
					t.Errorf("unexpected error type: want=%T, got=%T", tc.err, err)
				}
			case err != tc.err:
				t.Errorf("unexpected error: want=%v, got=%#v", tc.err, err)
			}
			if err = e.Flush(); err != nil {
				t.Fatalf("error flushing output to buffer: %v", err)
			}
			t.Logf("output: %q", out.String())
		})
	}
}

func TestBadFormat(t *testing.T) {
	toks := &xmpptest.Tokens{
		xml.EndElement{Name: xml.Name{Local: "error", Space: streamerr.NS}},
	}
	r := stream.Reader(toks)
	var out strings.Builder
	e := xml.NewEncoder(&out)
	_, err := xmlstream.Copy(e, r)
	if err != streamerr.BadFormat {
		t.Errorf("unexpected error: want=%v, got=%v", streamerr.BadFormat, err)
	}
	err = e.Flush()
	if err != nil {
		t.Errorf("error flushing: %v", err)
	}
	t.Logf("output: %q", out.String())
}

func TestDisallowedTokenType(t *testing.T) {
	comment := xml.Comment("foo")
	toks := &xmpptest.Tokens{comment}
	r := stream.Reader(toks)
	tok, err := r.Token()
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if c, ok := tok.(xml.Comment); !ok || !bytes.Equal(c, comment) {
		t.Errorf("expected unknown token type to be passed through: want=%#v, got=%#v", comment, tok)
	}
}