~samwhited/xmpp

ref: e8c09b3ff1c1489c21d96a0f7f8f0e8728fc095a xmpp/stanza/error_test.go -rw-r--r-- 8.4 KiB
e8c09b3fSam Whited design: fix typo in design doc template 1 year, 4 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
// 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 (
	"encoding/xml"
	"errors"
	"fmt"
	"reflect"
	"testing"

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

var (
	_ error               = stanza.Error{}
	_ xmlstream.WriterTo  = stanza.Error{}
	_ xmlstream.Marshaler = stanza.Error{}

	simpleText = map[string]string{
		"": "test",
	}
)

var cmpTests = [...]struct {
	err    error
	target error
	is     bool
}{
	0: {
		err:    stanza.Error{},
		target: errors.New("test"),
	},
	1: {
		err:    stanza.Error{},
		target: stanza.Error{},
		is:     true,
	},
	2: {
		err:    stanza.Error{Type: stanza.Cancel},
		target: stanza.Error{},
		is:     true,
	},
	3: {
		err:    stanza.Error{Condition: stanza.UnexpectedRequest},
		target: stanza.Error{},
		is:     true,
	},
	4: {
		err:    stanza.Error{Type: stanza.Auth, Condition: stanza.UndefinedCondition},
		target: stanza.Error{},
		is:     true,
	},
	5: {
		err:    stanza.Error{Type: stanza.Cancel},
		target: stanza.Error{Type: stanza.Auth},
	},
	6: {
		err:    stanza.Error{Type: stanza.Auth},
		target: stanza.Error{Type: stanza.Auth},
		is:     true,
	},
	7: {
		err:    stanza.Error{Type: stanza.Continue, Condition: stanza.SubscriptionRequired},
		target: stanza.Error{Type: stanza.Continue},
		is:     true,
	},
	8: {
		err:    stanza.Error{Type: stanza.Continue},
		target: stanza.Error{Type: stanza.Continue, Condition: stanza.SubscriptionRequired},
	},
	9: {
		err:    stanza.Error{Condition: stanza.BadRequest},
		target: stanza.Error{Condition: stanza.Conflict},
	},
	10: {
		err:    stanza.Error{Condition: stanza.FeatureNotImplemented},
		target: stanza.Error{Condition: stanza.FeatureNotImplemented},
		is:     true,
	},
	11: {
		err:    stanza.Error{Type: stanza.Continue, Condition: stanza.Forbidden},
		target: stanza.Error{Condition: stanza.Forbidden},
		is:     true,
	},
	12: {
		err:    stanza.Error{Condition: stanza.Forbidden},
		target: stanza.Error{Type: stanza.Continue, Condition: stanza.Forbidden},
	},
}

func TestCmp(t *testing.T) {
	for i, tc := range cmpTests {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			is := errors.Is(tc.err, tc.target)
			if is != tc.is {
				t.Errorf("unexpected comparison, want=%t, got=%t", tc.is, is)
			}
		})
	}
}

func TestErrorReturnsCondition(t *testing.T) {
	s := stanza.Error{Condition: "leprosy"}
	if string(s.Condition) != s.Error() {
		t.Errorf("Expected stanza error to return condition `leprosy` but got %s", s.Error())
	}
	s = stanza.Error{Condition: "nope", Text: map[string]string{
		"": "Text",
	}}
	if string(s.Condition) != s.Error() {
		t.Errorf("Expected stanza error to return text `Text` but got %s", s.Error())
	}
}

func TestMarshalStanzaError(t *testing.T) {
	for i, data := range [...]struct {
		se  stanza.Error
		xml string
		err bool
	}{
		0: {stanza.Error{}, "", true},
		1: {stanza.Error{Condition: stanza.UnexpectedRequest}, `<error><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		2: {stanza.Error{Type: stanza.Cancel, Condition: stanza.UnexpectedRequest}, `<error type="cancel"><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		3: {stanza.Error{Type: stanza.Wait, Condition: stanza.UndefinedCondition}, `<error type="wait"><undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></undefined-condition></error>`, false},
		4: {stanza.Error{Type: stanza.Modify, By: jid.MustParse("test@example.net"), Condition: stanza.SubscriptionRequired}, `<error type="modify" by="test@example.net"><subscription-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></subscription-required></error>`, false},
		5: {stanza.Error{Type: stanza.Continue, Condition: stanza.ServiceUnavailable, Text: simpleText}, `<error type="continue"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></service-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">test</text></error>`, false},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			b, err := xml.Marshal(data.se)
			switch {
			case data.err && err == nil:
				t.Errorf("Expected an error when marshaling stanza error %v", data.se)
			case !data.err && err != nil:
				t.Error(err)
			case err != nil:
				return
			case string(b) != data.xml:
				t.Errorf("Expected marshaling stanza error '%v' to be:\n`%s`\nbut got:\n`%s`.", data.se, data.xml, string(b))
			}
		})
	}
}

func TestUnmarshalStanzaError(t *testing.T) {
	for i, data := range [...]struct {
		xml string
		se  stanza.Error
		err bool
	}{
		0: {"", stanza.Error{}, true},
		1: {`<error><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`,
			stanza.Error{Condition: stanza.UnexpectedRequest}, false},
		2: {`<error type="cancel"><registration-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></registration-required></error>`,
			stanza.Error{Type: stanza.Cancel, Condition: stanza.RegistrationRequired}, false},
		3: {`<error type="cancel"><redirect xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></redirect></error>`,
			stanza.Error{Type: stanza.Cancel, Condition: stanza.Redirect}, false},
		4: {`<error type="wait"><undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></undefined-condition></error>`,
			stanza.Error{Type: stanza.Wait, Condition: stanza.UndefinedCondition}, false},
		5: {`<error type="modify" by="test@example.net"><subscription-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></subscription-required></error>`,
			stanza.Error{Type: stanza.Modify, By: jid.MustParse("test@example.net"), Condition: stanza.SubscriptionRequired}, false},
		6: {`<error type="continue"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></service-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">test</text></error>`,
			stanza.Error{Type: stanza.Continue, Condition: stanza.ServiceUnavailable, Text: simpleText}, false},
		7: {`<error type="auth"><resource-constraint xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></resource-constraint><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">test</text></error>`,
			stanza.Error{Type: stanza.Auth, Condition: stanza.ResourceConstraint, Text: map[string]string{
				"en": "test",
			}}, false},
		8: {`<error type="auth"><resource-constraint xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></resource-constraint><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">test</text><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="de">German</text></error>`,
			stanza.Error{Type: stanza.Auth, Condition: stanza.ResourceConstraint, Text: map[string]string{
				"en": "test",
				"de": "German",
			}}, false},
		9: {`<error type="auth"><remote-server-timeout xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></remote-server-timeout><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">test</text><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="es">Spanish</text></error>`,
			stanza.Error{Type: stanza.Auth, Condition: stanza.RemoteServerTimeout, Text: map[string]string{
				"en": "test",
				"es": "Spanish",
			}}, false},
		10: {`<error by=""><remote-server-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></remote-server-not-found></error>`,
			stanza.Error{By: jid.JID{}, Condition: stanza.RemoteServerNotFound}, false},
		11: {`<error><other xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></other></error>`,
			stanza.Error{Condition: stanza.Condition("other")}, false},
		12: {`<error><recipient-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></recipient-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="ac-u">test</text></error>`,
			stanza.Error{Condition: stanza.RecipientUnavailable, Text: map[string]string{
				"ac-u": "test",
			}}, false},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			se2 := stanza.Error{}
			err := xml.Unmarshal([]byte(data.xml), &se2)
			j1, j2 := data.se.By, se2.By
			data.se.By = jid.JID{}
			se2.By = jid.JID{}
			switch {
			case data.err && err == nil:
				t.Errorf("Expected an error when unmarshaling stanza error `%s`", data.xml)
			case !data.err && err != nil:
				t.Error(err)
			case err != nil:
				return
			case !j1.Equal(j2):
				t.Errorf(`Expected by="%v" but got by="%v"`, j1, j2)
			case !reflect.DeepEqual(data.se, se2):
				t.Errorf("Expected unmarshaled stanza error:\n`%#v`\nbut got:\n`%#v`", data.se, se2)
			}
		})
	}
}