Software /
code /
prosody
Comparison
spec/util_xmppstream_spec.lua @ 9021:548ba4090012
util.xmppstream: Add tests for various XML features forbidden by the RFC
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 11 Jul 2018 11:58:25 +0100 |
parent | 8236:4878e4159e12 |
comparison
equal
deleted
inserted
replaced
9020:19e51d8f8947 | 9021:548ba4090012 |
---|---|
1 | 1 |
2 local xmppstream = require "util.xmppstream"; | 2 local xmppstream = require "util.xmppstream"; |
3 | 3 |
4 describe("util.xmppstream", function() | 4 describe("util.xmppstream", function() |
5 local function test(xml, expect_success, ex) | |
6 local stanzas = {}; | |
7 local session = { notopen = true }; | |
8 local callbacks = { | |
9 stream_ns = "streamns"; | |
10 stream_tag = "stream"; | |
11 default_ns = "stanzans"; | |
12 streamopened = function (_session) | |
13 assert.are.equal(session, _session); | |
14 assert.are.equal(session.notopen, true); | |
15 _session.notopen = nil; | |
16 return true; | |
17 end; | |
18 handlestanza = function (_session, stanza) | |
19 assert.are.equal(session, _session); | |
20 assert.are.equal(_session.notopen, nil); | |
21 table.insert(stanzas, stanza); | |
22 end; | |
23 streamclosed = function (_session) | |
24 assert.are.equal(session, _session); | |
25 assert.are.equal(_session.notopen, nil); | |
26 _session.notopen = nil; | |
27 end; | |
28 } | |
29 if type(ex) == "table" then | |
30 for k, v in pairs(ex) do | |
31 if k ~= "_size_limit" then | |
32 callbacks[k] = v; | |
33 end | |
34 end | |
35 end | |
36 local stream = xmppstream.new(session, callbacks, ex and ex._size_limit or nil); | |
37 local ok, err = pcall(function () | |
38 assert(stream:feed(xml)); | |
39 end); | |
40 | |
41 if ok and type(expect_success) == "function" then | |
42 expect_success(stanzas); | |
43 end | |
44 assert.are.equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure")); | |
45 end | |
46 | |
47 local function test_stanza(stanza, expect_success, ex) | |
48 return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex); | |
49 end | |
50 | |
5 describe("#new()", function() | 51 describe("#new()", function() |
6 it("should work", function() | 52 it("should work", function() |
7 local function test(xml, expect_success, ex) | |
8 local stanzas = {}; | |
9 local session = { notopen = true }; | |
10 local callbacks = { | |
11 stream_ns = "streamns"; | |
12 stream_tag = "stream"; | |
13 default_ns = "stanzans"; | |
14 streamopened = function (_session) | |
15 assert.are.equal(session, _session); | |
16 assert.are.equal(session.notopen, true); | |
17 _session.notopen = nil; | |
18 return true; | |
19 end; | |
20 handlestanza = function (_session, stanza) | |
21 assert.are.equal(session, _session); | |
22 assert.are.equal(_session.notopen, nil); | |
23 table.insert(stanzas, stanza); | |
24 end; | |
25 streamclosed = function (_session) | |
26 assert.are.equal(session, _session); | |
27 assert.are.equal(_session.notopen, nil); | |
28 _session.notopen = nil; | |
29 end; | |
30 } | |
31 if type(ex) == "table" then | |
32 for k, v in pairs(ex) do | |
33 if k ~= "_size_limit" then | |
34 callbacks[k] = v; | |
35 end | |
36 end | |
37 end | |
38 local stream = xmppstream.new(session, callbacks, size_limit); | |
39 local ok, err = pcall(function () | |
40 assert(stream:feed(xml)); | |
41 end); | |
42 | |
43 if ok and type(expect_success) == "function" then | |
44 expect_success(stanzas); | |
45 end | |
46 assert.are.equal(not not ok, not not expect_success, "Expected "..(expect_success and ("success ("..tostring(err)..")") or "failure")); | |
47 end | |
48 | |
49 local function test_stanza(stanza, expect_success, ex) | |
50 return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza, expect_success, ex); | |
51 end | |
52 | |
53 test([[<stream:stream xmlns:stream="streamns"/>]], true); | 53 test([[<stream:stream xmlns:stream="streamns"/>]], true); |
54 test([[<stream xmlns="streamns"/>]], true); | 54 test([[<stream xmlns="streamns"/>]], true); |
55 | 55 |
56 -- Incorrect stream tag name should be rejected | |
56 test([[<stream1 xmlns="streamns"/>]], false); | 57 test([[<stream1 xmlns="streamns"/>]], false); |
58 -- Incorrect stream namespace should be rejected | |
57 test([[<stream xmlns="streamns1"/>]], false); | 59 test([[<stream xmlns="streamns1"/>]], false); |
60 -- Invalid XML should be rejected | |
58 test("<>", false); | 61 test("<>", false); |
59 | 62 |
60 test_stanza("<message/>", function (stanzas) | 63 test_stanza("<message/>", function (stanzas) |
61 assert.are.equal(#stanzas, 1); | 64 assert.are.equal(#stanzas, 1); |
62 assert.are.equal(stanzas[1].name, "message"); | 65 assert.are.equal(stanzas[1].name, "message"); |
85 | 88 |
86 assert.are.equal(s.namespaces, nil); | 89 assert.are.equal(s.namespaces, nil); |
87 end); | 90 end); |
88 end); | 91 end); |
89 end); | 92 end); |
93 | |
94 it("should allow an XML declaration", function () | |
95 test([[<?xml version="1.0" encoding="UTF-8"?><stream xmlns="streamns"/>]], true); | |
96 test([[<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><stream xmlns="streamns"/>]], true); | |
97 test([[<?xml version="1.0" encoding="utf-8" ?><stream xmlns="streamns"/>]], true); | |
98 end); | |
99 | |
100 it("should not accept XML versions other than 1.0", function () | |
101 test([[<?xml version="1.1" encoding="utf-8" ?><stream xmlns="streamns"/>]], false); | |
102 end); | |
103 | |
104 it("should not allow a misplaced XML declaration", function () | |
105 test([[<stream xmlns="streamns"><?xml version="1.0" encoding="UTF-8"?></stream>]], false); | |
106 end); | |
107 | |
108 describe("should forbid restricted XML:", function () | |
109 it("comments", function () | |
110 test_stanza("<!-- hello world -->", false); | |
111 end); | |
112 it("DOCTYPE", function () | |
113 test([[<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE stream SYSTEM "mydtd.dtd">]], false); | |
114 end); | |
115 it("incorrect encoding specification", function () | |
116 -- This is actually caught by the underlying XML parser | |
117 test([[<?xml version="1.0" encoding="UTF-16"?><stream xmlns="streamns"/>]], false); | |
118 end); | |
119 it("non-UTF8 encodings: ISO-8859-1", function () | |
120 test([[<?xml version="1.0" encoding="ISO-8859-1"?><stream xmlns="streamns"/>]], false); | |
121 end); | |
122 it("non-UTF8 encodings: UTF-16", function () | |
123 -- <?xml version="1.0" encoding="UTF-16"?><stream xmlns="streamns"/> | |
124 -- encoded into UTF-16 | |
125 local hx = ([[fffe3c003f0078006d006c002000760065007200730069006f006e003d00 | |
126 220031002e0030002200200065006e0063006f00640069006e0067003d00 | |
127 22005500540046002d003100360022003f003e003c007300740072006500 | |
128 61006d00200078006d006c006e0073003d00220073007400720065006100 | |
129 6d006e00730022002f003e00]]):gsub("%x%x", function (c) return string.char(tonumber(c, 16)); end); | |
130 test(hx, false); | |
131 end); | |
132 it("processing instructions", function () | |
133 test([[<stream xmlns="streamns"><?xml-stylesheet type="text/xsl" href="style.xsl"?></stream>]], false); | |
134 end); | |
135 end); | |
90 end); | 136 end); |