2 local xmppstream
= require
"util.xmppstream";
4 describe("util.xmppstream", function()
5 local function test(xml
, expect_success
, ex
)
7 local session
= { notopen
= true };
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;
18 handlestanza
= function (_session
, stanza
)
19 assert.are
.equal(session
, _session
);
20 assert.are
.equal(_session
.notopen
, nil);
21 table.insert(stanzas
, stanza
);
23 streamclosed
= function (_session
)
24 assert.are
.equal(session
, _session
);
25 assert.are
.equal(_session
.notopen
, nil);
26 _session
.notopen
= nil;
29 if type(ex
) == "table" then
30 for k
, v
in pairs(ex
) do
31 if k
~= "_size_limit" then
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
));
41 if ok
and type(expect_success
) == "function" then
42 expect_success(stanzas
);
44 assert.are
.equal(not not ok
, not not expect_success
, "Expected "..(expect_success
and ("success ("..tostring(err
)..")") or "failure"));
47 local function test_stanza(stanza
, expect_success
, ex
)
48 return test([[<stream:stream xmlns:stream="streamns" xmlns="stanzans">]]..stanza
, expect_success
, ex
);
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
63 test_stanza("<message/>", function (stanzas
)
64 assert.are
.equal(#stanzas
, 1);
65 assert.are
.equal(stanzas
[1].name
, "message");
67 test_stanza("< message>>>>/>\n", false);
69 test_stanza([[<x xmlns:a="b">
74 </x>]], function (stanzas
)
75 assert.are
.equal(#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);
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);
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);
104 it("should not allow a misplaced XML declaration", function ()
105 test([[<stream xmlns="streamns"><?xml version="1.0" encoding="UTF-8"?></stream>]], false);
108 describe("should forbid restricted XML:", function ()
109 it("comments", function ()
110 test_stanza("<!-- hello world -->", false);
112 it("DOCTYPE", function ()
113 test([[<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE stream SYSTEM "mydtd.dtd">]], false);
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);
119 it("non-UTF8 encodings: ISO-8859-1", function ()
120 test([[<?xml version="1.0" encoding="ISO-8859-1"?><stream xmlns="streamns"/>]], false);
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);
132 it("processing instructions", function ()
133 test([[<stream xmlns="streamns"><?xml-stylesheet type="text/xsl" href="style.xsl"?></stream>]], false);