~samwhited/xmpp

ref: faef9ff39736dc97922b2c8bfd5dd4581c73b460 xmpp/stanza/iq_test.go -rw-r--r-- 6.8 KiB
faef9ff3Sam Whited styling: minor typo fix in fuzz tests 1 year, 5 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
// Copyright 2016 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 stanza_test

import (
	"bytes"
	"encoding"
	"encoding/xml"
	"fmt"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/attr"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
)

var _ encoding.TextMarshaler = stanza.IQType("")

type iqTest struct {
	to      string
	typ     stanza.IQType
	payload xml.TokenReader
	out     string
	err     error
}

var iqTests = [...]iqTest{
	0: {
		to:      "new@example.net",
		payload: &testReader{},
	},
	1: {
		to:      "new@example.org",
		payload: &testReader{start, start.End()},
		out:     `<ping></ping>`,
		typ:     stanza.GetIQ,
	},
}

func TestIQ(t *testing.T) {
	for i, tc := range iqTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			b := new(bytes.Buffer)
			e := xml.NewEncoder(b)
			iq := stanza.IQ{To: jid.MustParse(tc.to), Type: tc.typ}.Wrap(tc.payload)
			if _, err := xmlstream.Copy(e, iq); err != tc.err {
				t.Errorf("Unexpected error: want=`%v', got=`%v'", tc.err, err)
			}
			if err := e.Flush(); err != nil {
				t.Fatalf("Error flushing: %q", err)
			}

			o := b.String()
			jidattr := fmt.Sprintf(`to="%s"`, tc.to)
			if !strings.Contains(o, jidattr) {
				t.Errorf("Expected output to have attr `%s',\ngot=`%s'", jidattr, o)
			}
			typeattr := fmt.Sprintf(`type="%s"`, string(tc.typ))
			if !strings.Contains(o, typeattr) {
				t.Errorf("Expected output to have attr `%s',\ngot=`%s'", typeattr, o)
			}
			if !strings.Contains(o, tc.out) {
				t.Errorf("Expected output to contain payload `%s',\ngot=`%s'", tc.out, o)
			}
		})
	}
}

func TestMarshalIQTypeAttr(t *testing.T) {
	for i, tc := range [...]struct {
		iqtype stanza.IQType
		value  string
	}{
		0: {stanza.IQType(""), "get"},
		1: {stanza.GetIQ, "get"},
		2: {stanza.SetIQ, "set"},
		3: {stanza.ResultIQ, "result"},
		4: {stanza.ErrorIQ, "error"},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			b, err := xml.Marshal(stanza.IQ{Type: tc.iqtype})
			if err != nil {
				t.Fatal("Got unexpected error while marshaling IQ:", err)
			}

			if err == nil && !bytes.Contains(b, []byte(fmt.Sprintf(`type="%s"`, tc.value))) {
				t.Errorf(`Expected output to contain type="%s", found: %s`, tc.value, b)
			}
		})
	}
}

func TestUnmarshalIQTypeAttr(t *testing.T) {
	for i, tc := range [...]struct {
		iq     string
		iqtype stanza.IQType
	}{
		0: {`<iq/>`, stanza.IQType("")},
		1: {`<iq type=""/>`, stanza.IQType("")},
		2: {`<iq type="get"/>`, stanza.GetIQ},
		3: {`<iq type="error"/>`, stanza.ErrorIQ},
		4: {`<iq type="result"/>`, stanza.ResultIQ},
		5: {`<iq type="set"/>`, stanza.SetIQ},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			iq := stanza.IQ{}
			switch err := xml.Unmarshal([]byte(tc.iq), &iq); {
			case err != nil:
				t.Errorf("Got unexpected error while unmarshaling IQ: %v", err)
			case tc.iqtype != iq.Type:
				t.Errorf("Wrong type when unmarshaling IQ: want=%s, got=%s", tc.iqtype, iq.Type)
			}
		})
	}
}

func TestIQResult(t *testing.T) {
	iq := stanza.IQ{
		ID:   "123",
		To:   jid.MustParse("to@example.net"),
		From: jid.MustParse("from@example.net"),
		Type: stanza.SetIQ,
	}
	reply := iq.Result(xmlstream.Wrap(nil, xml.StartElement{Name: xml.Name{Local: "foo"}}))

	var b strings.Builder
	e := xml.NewEncoder(&b)
	_, err := xmlstream.Copy(e, reply)
	if err != nil {
		t.Fatalf("error copying tokens: %v", err)
	}
	err = e.Flush()
	if err != nil {
		t.Fatalf("error flushing encoder: %v", err)
	}

	const expected = `<iq type="result" to="from@example.net" from="to@example.net" id="123"><foo></foo></iq>`
	out := b.String()
	if out != expected {
		t.Errorf("want=%q, got=%q", expected, out)
	}
}

func TestIQStartElement(t *testing.T) {
	to := jid.MustParse("to@example.net")
	from := jid.MustParse("from@example.net")
	msg := stanza.IQ{
		XMLName: xml.Name{Space: "ns", Local: "badname"},
		ID:      "123",
		To:      to,
		From:    from,
		Lang:    "te",
		Type:    stanza.SetIQ,
	}

	start := msg.StartElement()
	if start.Name.Local != "iq" || start.Name.Space != testNS {
		t.Errorf("wrong value for name: want=%v, got=%v", xml.Name{Space: testNS, Local: "iq"}, start.Name)
	}
	if _, v := attr.Get(start.Attr, "id"); v != msg.ID {
		t.Errorf("wrong value for id: want=%q, got=%q", msg.ID, v)
	}
	if _, v := attr.Get(start.Attr, "to"); v != msg.To.String() {
		t.Errorf("wrong value for to: want=%q, got=%q", msg.To, v)
	}
	if _, v := attr.Get(start.Attr, "from"); v != msg.From.String() {
		t.Errorf("wrong value for from: want=%q, got=%q", msg.From, v)
	}
	if i, v := attr.Get(start.Attr, "lang"); v != msg.Lang || start.Attr[i].Name.Space != ns.XML {
		t.Errorf("wrong value for xml:lang: want=%q, got=%q", xml.Attr{
			Name:  xml.Name{Space: ns.XML, Local: "lang"},
			Value: msg.Lang,
		}, xml.Attr{
			Name:  start.Attr[i].Name,
			Value: v,
		})
	}
	if _, v := attr.Get(start.Attr, "type"); v != string(msg.Type) {
		t.Errorf("wrong value for type: want=%q, got=%q", msg.Type, v)
	}
}

func TestIQFromStartElement(t *testing.T) {
	for i, tc := range [...]xml.StartElement{
		0: {
			// Make sure that we're not validating the name.
			Name: xml.Name{Local: "message", Space: testNS},
			Attr: []xml.Attr{
				{Name: xml.Name{Local: "id"}, Value: "123"},
				{Name: xml.Name{Local: "to"}, Value: "to@example.com"},
				{Name: xml.Name{Local: "from"}, Value: "from@example.com"},
				{Name: xml.Name{Space: ns.XML, Local: "lang"}, Value: "lo"},
				{Name: xml.Name{Local: "lang"}, Value: "de"},
				{Name: xml.Name{Local: "type"}, Value: "chat"},
			},
		},
		1: {
			Name: xml.Name{Local: "msg", Space: testNS},
			Attr: []xml.Attr{
				{Name: xml.Name{Local: "to"}, Value: ""},
				{Name: xml.Name{Local: "from"}, Value: ""},
			},
		},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			msg, err := stanza.NewIQ(tc)
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}

			if msg.XMLName.Local != tc.Name.Local {
				t.Errorf("wrong localname value: want=%q, got=%q", tc.Name.Local, msg.XMLName.Local)
			}
			if msg.XMLName.Space != testNS {
				t.Errorf("wrong namespace value: want=%q, got=%q", testNS, msg.XMLName.Space)
			}
			if _, v := attr.Get(tc.Attr, "id"); v != msg.ID {
				t.Errorf("wrong value for id: want=%q, got=%q", v, msg.ID)
			}
			if _, v := attr.Get(tc.Attr, "to"); v != msg.To.String() {
				t.Errorf("wrong value for to: want=%q, got=%q", v, msg.To)
			}
			if _, v := attr.Get(tc.Attr, "from"); v != msg.From.String() {
				t.Errorf("wrong value for from: want=%q, got=%q", v, msg.From)
			}
			langAttr := getLangAttr(tc)
			if langAttr.Value != msg.Lang {
				t.Errorf("wrong value for xml:lang: want=%q, got=%q", langAttr.Value, msg.Lang)
			}
			if _, v := attr.Get(tc.Attr, "type"); v != string(msg.Type) {
				t.Errorf("wrong value for type: want=%q, got=%q", v, msg.Type)
			}
		})
	}
}