mod_admin_telnet: Identify native bidi sessions
[prosody.git] / spec / util_xmppstream_spec.lua
blob38b3cbd20a8ea62ad3d164089ed625d834b5bddb
2 local xmppstream = require "util.xmppstream";
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;
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);
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
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
51 describe("#new()", function()
52 it("should work", function()
53 test([[<stream:stream xmlns:stream="streamns"/>]], true);
54 test([[<stream xmlns="streamns"/>]], true);
56 -- Incorrect stream tag name should be rejected
57 test([[<stream1 xmlns="streamns"/>]], false);
58 -- Incorrect stream namespace should be rejected
59 test([[<stream xmlns="streamns1"/>]], false);
60 -- Invalid XML should be rejected
61 test("<>", false);
63 test_stanza("<message/>", function (stanzas)
64 assert.are.equal(#stanzas, 1);
65 assert.are.equal(stanzas[1].name, "message");
66 end);
67 test_stanza("< message>>>>/>\n", false);
69 test_stanza([[<x xmlns:a="b">
70 <y xmlns:a="c">
71 <a:z/>
72 </y>
73 <a:z/>
74 </x>]], function (stanzas)
75 assert.are.equal(#stanzas, 1);
76 local s = stanzas[1];
77 assert.are.equal(s.name, "x");
78 assert.are.equal(#s.tags, 2);
80 assert.are.equal(s.tags[1].name, "y");
81 assert.are.equal(s.tags[1].attr.xmlns, nil);
83 assert.are.equal(s.tags[1].tags[1].name, "z");
84 assert.are.equal(s.tags[1].tags[1].attr.xmlns, "c");
86 assert.are.equal(s.tags[2].name, "z");
87 assert.are.equal(s.tags[2].attr.xmlns, "b");
89 assert.are.equal(s.namespaces, nil);
90 end);
91 end);
92 end);
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);
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);
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);
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);
136 end);