~samwhited/xmpp

ref: be25ce7b23f96712a76b65c28360b2f1de2514e6 xmpp/send_test.go -rw-r--r-- 6.4 KiB
be25ce7bSam Whited all: rework API for SendIQ and SendIQElement 1 year, 9 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2019 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 xmpp_test

import (
	"bytes"
	"context"
	"encoding/xml"
	"errors"
	"io"
	"strconv"
	"strings"
	"testing"
	"time"

	"mellium.im/xmlstream"
	"mellium.im/xmpp"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
)

const (
	testIQID = "123"
)

type errReader struct{ err error }

func (r errReader) Token() (xml.Token, error) {
	return nil, r.err
}

type toksReader []xml.Token

func (r *toksReader) Token() (xml.Token, error) {
	if len(*r) == 0 {
		return nil, io.EOF
	}

	var t xml.Token
	t, *r = (*r)[0], (*r)[1:]
	return t, nil
}

var (
	errExpected = errors.New("Expected error")
	to          = jid.MustParse("test@example.net")
)

var sendIQTests = [...]struct {
	iq         stanza.IQ
	payload    xml.TokenReader
	err        error
	writesBody bool
	resp       xml.TokenReader
}{
	0: {
		iq:         stanza.IQ{ID: testIQID, Type: stanza.GetIQ},
		writesBody: true,
		resp:       stanza.WrapIQ(stanza.IQ{ID: testIQID, Type: stanza.ResultIQ}, nil),
	},
	1: {
		iq:         stanza.IQ{ID: testIQID, Type: stanza.SetIQ},
		writesBody: true,
		resp:       stanza.WrapIQ(stanza.IQ{ID: testIQID, Type: stanza.ErrorIQ}, nil),
	},
	2: {
		iq:         stanza.IQ{Type: stanza.ResultIQ, ID: testIQID},
		writesBody: true,
	},
	3: {
		iq:         stanza.IQ{Type: stanza.ErrorIQ, ID: testIQID},
		writesBody: true,
	},
}

func TestSendIQ(t *testing.T) {
	for i, tc := range sendIQTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			br := &bytes.Buffer{}
			if tc.resp != nil {
				e := xml.NewEncoder(br)
				_, err := xmlstream.Copy(e, tc.resp)
				if err != nil {
					t.Logf("error responding: %q", err)
				}
				err = e.Flush()
				if err != nil {
					t.Logf("error flushing after responding: %q", err)
				}
			}

			t.Run("SendIQElement", func(t *testing.T) {
				bw := &bytes.Buffer{}
				s := xmpptest.NewSession(0, struct {
					io.Reader
					io.Writer
				}{
					Reader: strings.NewReader(br.String()),
					Writer: bw,
				})
				defer func() {
					if err := s.Close(); err != nil {
						t.Errorf("Error closing session: %q", err)
					}
				}()

				go func() {
					err := s.Serve(nil)
					if err != nil && err != io.EOF {
						panic(err)
					}
				}()

				ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
				defer cancel()

				resp, err := s.SendIQElement(ctx, tc.payload, tc.iq)
				if err != tc.err {
					t.Errorf("Unexpected error, want=%q, got=%q", tc.err, err)
				}
				if resp != nil {
					defer func() {
						if err := resp.Close(); err != nil {
							t.Errorf("Error closing response: %q", err)
						}
					}()
				}
				if empty := bw.Len() != 0; tc.writesBody != empty {
					t.Errorf("Unexpected body, want=%t, got=%t", tc.writesBody, empty)
				}
				switch {
				case resp == nil && tc.resp != nil:
					t.Errorf("Expected response, but got none")
				case resp != nil && tc.resp == nil:
					buf := &bytes.Buffer{}
					_, err := xmlstream.Copy(xml.NewEncoder(buf), resp)
					if err != nil {
						t.Errorf("Error encoding unexpected response")
					}
					t.Errorf("Did not expect response, but got: %s", buf.String())
				}
			})
			t.Run("SendIQ", func(t *testing.T) {
				bw := &bytes.Buffer{}
				s := xmpptest.NewSession(0, struct {
					io.Reader
					io.Writer
				}{
					Reader: strings.NewReader(br.String()),
					Writer: bw,
				})
				defer func() {
					if err := s.Close(); err != nil {
						t.Errorf("Error closing session: %q", err)
					}
				}()

				go func() {
					err := s.Serve(nil)
					if err != nil && err != io.EOF {
						panic(err)
					}
				}()

				ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
				defer cancel()

				resp, err := s.SendIQ(ctx, stanza.WrapIQ(tc.iq, tc.payload))
				if err != tc.err {
					t.Errorf("Unexpected error, want=%q, got=%q", tc.err, err)
				}
				if resp != nil {
					defer func() {
						if err := resp.Close(); err != nil {
							t.Errorf("Error closing response: %q", err)
						}
					}()
				}
				if empty := bw.Len() != 0; tc.writesBody != empty {
					t.Errorf("Unexpected body, want=%t, got=%t", tc.writesBody, empty)
				}
				switch {
				case resp == nil && tc.resp != nil:
					t.Errorf("Expected response, but got none")
				case resp != nil && tc.resp == nil:
					buf := &bytes.Buffer{}
					_, err := xmlstream.Copy(xml.NewEncoder(buf), resp)
					if err != nil {
						t.Errorf("Error encoding unexpected response")
					}
					t.Errorf("Did not expect response, but got: %s", buf.String())
				}
			})
		})
	}
}

var sendTests = [...]struct {
	r          xml.TokenReader
	err        error
	resp       xml.TokenReader
	writesBody bool
}{
	0: {
		r:   errReader{err: errExpected},
		err: errExpected,
	},
	1: {
		r: &toksReader{
			xml.EndElement{Name: xml.Name{Local: "iq"}},
		},
		err: xmpp.ErrNotStart,
	},
	2: {
		r:          stanza.WrapMessage(to, stanza.NormalMessage, nil),
		writesBody: true,
	},
	3: {
		r:          stanza.WrapPresence(to, stanza.AvailablePresence, nil),
		writesBody: true,
	},
	4: {
		r:          stanza.WrapIQ(stanza.IQ{Type: stanza.ResultIQ}, nil),
		writesBody: true,
	},
	5: {
		r:          stanza.WrapIQ(stanza.IQ{Type: stanza.ErrorIQ}, nil),
		writesBody: true,
	},
}

func TestSend(t *testing.T) {
	for i, tc := range sendTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			br := &bytes.Buffer{}
			bw := &bytes.Buffer{}
			s := xmpptest.NewSession(0, struct {
				io.Reader
				io.Writer
			}{
				Reader: br,
				Writer: bw,
			})
			defer func() {
				if err := s.Close(); err != nil {
					t.Errorf("Error closing session: %q", err)
				}
			}()
			if tc.resp != nil {
				e := xml.NewEncoder(br)
				_, err := xmlstream.Copy(e, tc.resp)
				if err != nil {
					t.Logf("error responding: %q", err)
				}
				err = e.Flush()
				if err != nil {
					t.Logf("error flushing after responding: %q", err)
				}
			}

			go func() {
				err := s.Serve(nil)
				if err != nil && err != io.EOF {
					panic(err)
				}
			}()

			ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
			defer cancel()
			err := s.Send(ctx, tc.r)
			if err != tc.err {
				t.Errorf("Unexpected error, want=%q, got=%q", tc.err, err)
			}
			if empty := bw.Len() != 0; tc.writesBody != empty {
				t.Errorf("Unexpected body, want=%t, got=%t", tc.writesBody, empty)
			}
		})
	}
}