~samwhited/xmpp

xmpp/receipts/receipts_test.go -rw-r--r-- 6.7 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 8 days 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// 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 receipts_test

import (
	"bytes"
	"context"
	"encoding/xml"
	"strconv"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/receipts"
	"mellium.im/xmpp/stanza"
)

var (
	_ xml.Marshaler         = receipts.Requested{}
	_ xmlstream.Marshaler   = receipts.Requested{}
	_ xmlstream.WriterTo    = receipts.Requested{}
	_ xml.Unmarshaler       = (*receipts.Requested)(nil)
	_ xmlstream.Transformer = receipts.Request
)

var requestTestCases = [...]struct {
	in  string
	out string
}{
	0: {},
	1: {
		in:  `<message xmlns="jabber:client"/>`,
		out: `<message xmlns="jabber:client"><request xmlns="urn:xmpp:receipts"></request></message>`,
	},
	2: {
		in:  `<message xmlns="jabber:server"/><message xmlns="jabber:client"><body>test</body></message>`,
		out: `<message xmlns="jabber:server"><request xmlns="urn:xmpp:receipts"></request></message><message xmlns="jabber:client"><body xmlns="jabber:client">test</body><request xmlns="urn:xmpp:receipts"></request></message>`,
	},
	3: {
		in:  `<message xmlns="jabber:badns"/>`,
		out: `<message xmlns="jabber:badns"></message>`,
	},
	4: {
		in:  `<message xmlns="jabber:client" type="error"/>`,
		out: `<message xmlns="jabber:client" type="error"></message>`,
	},
	5: {
		in:  `<message xmlns="jabber:server" type="error"/>`,
		out: `<message xmlns="jabber:server" type="error"></message>`,
	},
	6: {
		in:  `<message xmlns="jabber:server" type="chat"><receipt xmlns="urn:xmpp:receipts"></receipt></message>`,
		out: `<message xmlns="jabber:server" type="chat"><receipt xmlns="urn:xmpp:receipts"></receipt></message>`,
	},
	7: {
		in:  `<message xmlns="jabber:server" type="chat"><body>Test</body><receipt xmlns="urn:xmpp:receipts"></receipt></message>`,
		out: `<message xmlns="jabber:server" type="chat"><body xmlns="jabber:server">Test</body><receipt xmlns="urn:xmpp:receipts"></receipt></message>`,
	},
	8: {
		in:  `<message xmlns="jabber:server" type="chat"><receipt xmlns="urn:xmpp:receipts"></receipt></message><message xmlns="jabber:client" type="chat"></message>`,
		out: `<message xmlns="jabber:server" type="chat"><receipt xmlns="urn:xmpp:receipts"></receipt></message><message xmlns="jabber:client" type="chat"><request xmlns="urn:xmpp:receipts"></request></message>`,
	},
}

func TestRequest(t *testing.T) {
	for i, tc := range requestTestCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			r := receipts.Request(xml.NewDecoder(strings.NewReader(tc.in)))
			// Prevent duplicate xmlns attributes. See https://mellium.im/issue/75
			r = xmlstream.RemoveAttr(func(start xml.StartElement, attr xml.Attr) bool {
				return (start.Name.Local == "message" || start.Name.Local == "iq" || start.Name.Local == "receipt") && attr.Name.Local == "xmlns"
			})(r)
			var buf strings.Builder
			e := xml.NewEncoder(&buf)
			_, err := xmlstream.Copy(e, r)
			if err != nil {
				t.Fatalf("error encoding: %v", err)
			}
			if err = e.Flush(); err != nil {
				t.Fatalf("error flushing: %v", err)
			}

			if out := buf.String(); tc.out != out {
				t.Errorf("wrong output:\nwant=%s,\n got=%s", tc.out, out)
			}
		})
	}
}

func TestMarshal(t *testing.T) {
	var buf strings.Builder
	e := xml.NewEncoder(&buf)
	err := e.Encode(struct {
		stanza.Message

		Requested receipts.Requested
	}{})
	if err != nil {
		t.Fatalf("error encoding: %v", err)
	}
	if err = e.Flush(); err != nil {
		t.Fatalf("error flushing: %v", err)
	}

	const expected = `<message to="" from=""><request xmlns="urn:xmpp:receipts"></request></message>`
	if out := buf.String(); expected != out {
		t.Errorf("wrong output:\nwant=%s,\n got=%s", expected, out)
	}
}

var unmarshalTestCases = [...]struct {
	in  string
	out bool
}{
	0: {
		in:  `<message><request xmlns="urn:xmpp:receipts"/></message>`,
		out: true,
	},
	1: {in: `<message><wrong xmlns="urn:xmpp:receipts"/></message>`},
	2: {in: `<message><request xmlns="urn:xmpp:wrongns"/></message>`},
}

func TestUnmarshal(t *testing.T) {
	for i, tc := range unmarshalTestCases {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			m := struct {
				stanza.Message
				Requested receipts.Requested
			}{}
			err := xml.NewDecoder(strings.NewReader(tc.in)).Decode(&m)
			if err != nil {
				t.Errorf("error decoding: %v", err)
			}
			if m.Requested.Value != tc.out {
				t.Errorf("bad decode: want=%t, got=%t", tc.out, m.Requested.Value)
			}
		})
	}
}

func TestClosedDoesNotPanic(t *testing.T) {
	h := &receipts.Handler{}

	bw := &bytes.Buffer{}
	s := xmpptest.NewClientSession(0, bw)
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	err := h.SendMessageElement(ctx, s, nil, stanza.Message{
		ID: "123",
	})
	if err != context.Canceled {
		t.Fatalf("unexpected error: %v", err)
	}

	msg := stanza.Message{
		XMLName: xml.Name{Space: ns.Client, Local: "message"},
		Type:    stanza.ChatMessage,
	}
	r := msg.Wrap(xmlstream.Wrap(nil, xml.StartElement{
		Name: xml.Name{Local: "received", Space: receipts.NS},
		Attr: []xml.Attr{{Name: xml.Name{Local: "id"}, Value: "123"}},
	}))

	bw = &bytes.Buffer{}
	e := xml.NewEncoder(bw)
	// If the has not been removed from handling when the context is canceled,
	// this will panic (effectively failing the test).
	err = h.HandleMessage(msg, struct {
		xml.TokenReader
		xmlstream.Encoder
	}{
		TokenReader: r,
		Encoder:     e,
	})
	if err != nil {
		t.Fatalf("error handling response: %v", err)
	}
}

// TODO: find a way to test that SendMessageElement actually matches up the
// response correctly (ie. don't timeout, use the test server).
func TestRoundTrip(t *testing.T) {
	h := &receipts.Handler{}

	var req bytes.Buffer
	s := xmpptest.NewClientSession(0, &req)

	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	err := h.SendMessageElement(ctx, s, nil, stanza.Message{
		ID:   "123",
		Type: stanza.NormalMessage,
	})
	if err != context.Canceled {
		t.Fatalf("unexpected error: %v", err)
	}

	d := xml.NewDecoder(strings.NewReader(req.String()))
	tok, _ := d.Token()
	start := tok.(xml.StartElement)
	var b strings.Builder
	e := xml.NewEncoder(&b)

	m := mux.New(ns.Client, receipts.Handle(h))
	err = m.HandleXMPP(struct {
		xml.TokenReader
		xmlstream.Encoder
	}{
		TokenReader: d,
		Encoder:     e,
	}, &start)
	if err != nil {
		t.Errorf("unexpected error in handler: %v", err)
	}
	err = e.Flush()
	if err != nil {
		t.Errorf("unexpected error flushing encoder: %v", err)
	}

	out := b.String()
	const expected = `<message xmlns="jabber:client" type="normal"><received xmlns="urn:xmpp:receipts" id="123"></received></message>`
	if out != expected {
		t.Errorf("wrong output:\nwant=%s,\n got=%s", expected, out)
	}
}