Debian Codegraph @

xmpp_test.go

raw
1 package xmpp
2
3 import (
4 "bytes"
5 "encoding/xml"
6 "io"
7 "net"
8 "reflect"
9 "strings"
10 "testing"
11 "time"
12 )
13
14 type localAddr struct{}
15
16 func (a *localAddr) Network() string {
17 return "tcp"
18 }
19
20 func (addr *localAddr) String() string {
21 return "localhost:5222"
22 }
23
24 type testConn struct {
25 *bytes.Buffer
26 }
27
28 func tConnect(s string) net.Conn {
29 var conn testConn
30 conn.Buffer = bytes.NewBufferString(s)
31 return &conn
32 }
33
34 func (*testConn) Close() error {
35 return nil
36 }
37
38 func (*testConn) LocalAddr() net.Addr {
39 return &localAddr{}
40 }
41
42 func (*testConn) RemoteAddr() net.Addr {
43 return &localAddr{}
44 }
45
46 func (*testConn) SetDeadline(time.Time) error {
47 return nil
48 }
49
50 func (*testConn) SetReadDeadline(time.Time) error {
51 return nil
52 }
53
54 func (*testConn) SetWriteDeadline(time.Time) error {
55 return nil
56 }
57
58 var text = strings.TrimSpace(`
59 <message xmlns="jabber:client" id="3" type="error" to="[email protected]/ABC">
60 <gcm xmlns="google:mobile:data">
61 {"random": "&lt;text&gt;"}
62 </gcm>
63 <error code="400" type="modify">
64 <bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
65 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
66 InvalidJson: JSON_PARSING_ERROR : Missing Required Field: message_id\n
67 </text>
68 </error>
69 </message>
70 `)
71
72 func TestStanzaError(t *testing.T) {
73 var c Client
74 c.conn = tConnect(text)
75 c.p = xml.NewDecoder(c.conn)
76 v, err := c.Recv()
77 if err != nil {
78 t.Fatalf("Recv() = %v", err)
79 }
80
81 chat := Chat{
82 Type: "error",
83 Other: []string{
84 "\n\t\t{\"random\": \"<text>\"}\n\t",
85 "\n\t\t\n\t\t\n\t",
86 },
87 OtherElem: []XMLElement{
88 {
89 XMLName: xml.Name{Space: "google:mobile:data", Local: "gcm"},
90 Attr: []xml.Attr{{Name: xml.Name{Space: "", Local: "xmlns"}, Value: "google:mobile:data"}},
91 InnerXML: "\n\t\t{\"random\": \"&lt;text&gt;\"}\n\t",
92 },
93 {
94 XMLName: xml.Name{Space: "jabber:client", Local: "error"},
95 Attr: []xml.Attr{{Name: xml.Name{Space: "", Local: "code"}, Value: "400"}, {Name: xml.Name{Space: "", Local: "type"}, Value: "modify"}},
96 InnerXML: `
97 <bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
98 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
99 InvalidJson: JSON_PARSING_ERROR : Missing Required Field: message_id\n
100 </text>
101 `,
102 },
103 },
104 }
105 if !reflect.DeepEqual(v, chat) {
106 t.Errorf("Recv() = %#v; want %#v", v, chat)
107 }
108 }
109
110 func TestEOFError(t *testing.T) {
111 var c Client
112 c.conn = tConnect("")
113 c.p = xml.NewDecoder(c.conn)
114 _, err := c.Recv()
115 if err != io.EOF {
116 t.Errorf("Recv() did not return io.EOF on end of input stream")
117 }
118 }
119
120 var emptyPubSub = strings.TrimSpace(`
121 <iq xmlns="jabber:client" type='result' from='[email protected]' id='items3'>
122 <pubsub xmlns='http://jabber.org/protocol/pubsub'>
123 <items node='urn:xmpp:avatar:data'></items>
124 </pubsub>
125 </iq>
126 `)
127
128 func TestEmptyPubsub(t *testing.T) {
129 var c Client
130 c.itemsIDs = append(c.itemsIDs, "items3")
131 c.conn = tConnect(emptyPubSub)
132 c.p = xml.NewDecoder(c.conn)
133 m, err := c.Recv()
134
135 switch m.(type) {
136 case AvatarData:
137 if err == nil {
138 t.Errorf("Expected an error to be returned")
139 }
140 default:
141 t.Errorf("Recv() = %v", m)
142 t.Errorf("Expected a return value of AvatarData")
143 }
144 }
145
146 // https://xmpp.org/extensions/xep-0363.html#example-6
147 var exampleSlot = strings.TrimSpace(`
148 <iq from='upload.montague.tld'
149 xmlns="jabber:client"
150 id='abcdef'
151 to='[email protected]/garden'
152 type='result'>
153 <slot xmlns='urn:xmpp:http:upload:0'>
154 <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg'>
155 <header name='Authorization'>Basic Base64String==</header>
156 <header name='Cookie'>foo=bar; user=romeo</header>
157 </put>
158 <get url='https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg' />
159 </slot>
160 </iq>
161 `)
162
163 func TestUploadSlot(t *testing.T) {
164 var c Client
165 // c.itemsIDs = append(c.itemsIDs, "step_03")
166 c.conn = tConnect(exampleSlot)
167 c.p = xml.NewDecoder(c.conn)
168 m, err := c.Recv()
169 if err != nil {
170 panic(err)
171 }
172 t.Logf("Recv() = %v", m)
173
174 switch m.(type) {
175 case Slot:
176 v, _ := m.(Slot)
177
178 if v.ID != "abcdef" {
179 t.Errorf("Invalid ID: %s", v.ID)
180 }
181
182 if v.Put.Url != "https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg" {
183 t.Errorf("Invalid PUT URL: %s", v.Put.Url)
184 }
185 if v.Get.Url != "https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg" {
186 t.Errorf("Invalid GET URL: %s", v.Get.Url)
187 }
188
189 foundAuthorization := false
190 foundCookie := false
191 for _, header := range v.Put.Headers {
192 if header.Name == "Authorization" && header.Value == "Basic Base64String==" {
193 foundAuthorization = true
194 continue
195 }
196
197 if header.Name == "Cookie" && header.Value == "foo=bar; user=romeo" {
198 foundCookie = true
199 continue
200 }
201
202 t.Errorf("Unknown header: %s: %s", header.Name, header.Value)
203 }
204
205 if !foundAuthorization {
206 t.Errorf("Authorization header not found")
207 }
208 if !foundCookie {
209 t.Errorf("Cookie header not found")
210 }
211 default:
212 t.Errorf("Recv() = %V", m)
213 t.Errorf("Expected a return value of Slot")
214 }
215 }
216
217 // https://xmpp.org/extensions/xep-0066.html#example-5
218 var exampleOOB = strings.TrimSpace(`
219 <message from='[email protected]/work'
220 to='[email protected]/home'
221 xmlns='jabber:client'>
222 <body>Yeah, but do you have a license to Jabber?</body>
223 <x xmlns='jabber:x:oob'>
224 <url>http://www.jabber.org/images/psa-license.jpg</url>
225 </x>
226 </message>
227 `)
228
229 func TestChatOOB(t *testing.T) {
230 var c Client
231 c.conn = tConnect(exampleOOB)
232 c.p = xml.NewDecoder(c.conn)
233 m, err := c.Recv()
234 if err != nil {
235 panic(err)
236 }
237 t.Logf("Recv() = %v", m)
238
239 switch m.(type) {
240 case Chat:
241 v, _ := m.(Chat)
242
243 if v.Oob.Url != "http://www.jabber.org/images/psa-license.jpg" {
244 t.Errorf("Wrong URL, found: `%s`", v.Oob.Url)
245 }
246
247 if v.Oob.Desc != "" {
248 t.Errorf("Should not find Desc: `%s`", v.Oob.Desc)
249 }
250 default:
251 t.Errorf("Recv() = %v", m)
252 t.Errorf("Expected a return value of AvatarData")
253 }
254 }
255
256 var exampleNoOOB = strings.TrimSpace(`
257 <message from='[email protected]/work'
258 to='[email protected]/home'
259 xmlns='jabber:client'>
260 <body>Yeah, but do you have a license to Jabber?</body>
261 </message>
262 `)
263
264 func TestChatNoOOB(t *testing.T) {
265 var c Client
266 c.conn = tConnect(exampleNoOOB)
267 c.p = xml.NewDecoder(c.conn)
268 m, err := c.Recv()
269 if err != nil {
270 panic(err)
271 }
272 t.Logf("Recv() = %v", m)
273
274 switch m.(type) {
275 case Chat:
276 v, _ := m.(Chat)
277
278 if v.Oob.Url != "" {
279 t.Errorf("Should not find URL: `%s`", v.Oob.Url)
280 }
281
282 if v.Oob.Desc != "" {
283 t.Errorf("Should not find Desc: `%s`", v.Oob.Desc)
284 }
285 default:
286 t.Errorf("Recv() = %v", m)
287 t.Errorf("Expected a return value of AvatarData")
288 }
289 }
290
291 var rawOob = strings.TrimSpace(`
292 <x xmlns='jabber:x:oob'>
293 <url>http://www.jabber.org/images/psa-license.jpg</url>
294 </x>
295 `)
296
297 func TestRawOob(t *testing.T) {
298 var s Oob
299 err := xml.Unmarshal([]byte(rawOob), &s)
300 if err != nil {
301 t.Errorf("%v", err)
302 }
303
304 if s.Url != "http://www.jabber.org/images/psa-license.jpg" {
305 t.Errorf("Wrong URL: %s", s.Url)
306 }
307 }