aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--spec/util_stanza_spec.lua63
-rw-r--r--util/stanza.lua50
2 files changed, 69 insertions, 44 deletions
diff --git a/spec/util_stanza_spec.lua b/spec/util_stanza_spec.lua
index f7759385..cd5a0dc3 100644
--- a/spec/util_stanza_spec.lua
+++ b/spec/util_stanza_spec.lua
@@ -165,38 +165,49 @@ describe("util.stanza", function()
end);
end);
- describe("#invalid", function ()
- it("name should be rejected", function ()
- assert.has_error(function ()
- st.stanza(1234);
+ describe("should reject #invalid", function ()
+ local invalid_names = {
+ ["empty string"] = "", ["characters"] = "<>";
+ }
+ local invalid_data = {
+ ["number"] = 1234, ["table"] = {};
+ ["utf8"] = string.char(0xF4, 0x90, 0x80, 0x80);
+ };
+
+ for value_type, value in pairs(invalid_names) do
+ it(value_type.." in tag names", function ()
+ assert.error_matches(function ()
+ st.stanza(value);
+ end, value_type);
end);
- assert.has_error(function ()
- st.stanza({});
+ it(value_type.." in attribute names", function ()
+ assert.error_matches(function ()
+ st.stanza("valid", { [value] = "valid" });
+ end, value_type);
end);
- assert.has_error(function ()
- st.stanza();
+ end
+ for value_type, value in pairs(invalid_data) do
+ it(value_type.." in tag names", function ()
+ assert.error_matches(function ()
+ st.stanza(value);
+ end, value_type);
end);
- assert.has_error(function ()
- st.stanza("");
+ it(value_type.." in attribute names", function ()
+ assert.error_matches(function ()
+ st.stanza("valid", { [value] = "valid" });
+ end, value_type);
end);
- assert.has_error(function ()
- st.stanza(string.char(0xC0));
+ it(value_type.." in attribute values", function ()
+ assert.error_matches(function ()
+ st.stanza("valid", { valid = value });
+ end, value_type);
end);
- assert.has_error(function ()
- st.stanza(string.char(0xF4, 0x90, 0x80, 0x80));
+ it(value_type.." in text node", function ()
+ assert.error_matches(function ()
+ st.stanza("valid"):text(value);
+ end, value_type);
end);
- assert.has_error(function ()
- st.stanza("<>");
- end);
- assert.has_error(function ()
- st.stanza("&");
- end);
- end);
- it("UTF-8 should be rejected", function ()
- assert.has_error(function ()
- st.stanza("tag"):text("hello "..string.char(0xF4, 0x90, 0x80, 0x80).." world");
- end);
- end);
+ end
end);
describe("#is_stanza", function ()
diff --git a/util/stanza.lua b/util/stanza.lua
index 50dc32bb..d16fc947 100644
--- a/util/stanza.lua
+++ b/util/stanza.lua
@@ -7,7 +7,7 @@
--
-local assert = assert;
+local error = error;
local t_insert = table.insert;
local t_remove = table.remove;
local t_concat = table.concat;
@@ -45,31 +45,45 @@ local _ENV = nil;
local stanza_mt = { __name = "stanza" };
stanza_mt.__index = stanza_mt;
-local function check_name(name)
- assert(type(name) == "string", "tag name is not a string, "..type(name));
- assert(#name > 0, "tag name is empty");
- assert(not s_find(name, "[<>& '\"]"), "tag name contains invalid characters");
- assert(valid_utf8(name), "tag name is invalid utf8");
+local function check_name(name, name_type)
+ if type(name) ~= "string" then
+ error("invalid "..name_type.." name: expected string, got "..type(name));
+ elseif #name == 0 then
+ error("invalid "..name_type.." name: empty string");
+ elseif s_find(name, "[<>& '\"]") then
+ error("invalid "..name_type.." name: contains invalid characters");
+ elseif not valid_utf8(name) then
+ error("invalid "..name_type.." name: contains invalid utf8");
+ end
+end
+
+local function check_text(text, text_type)
+ if type(text) ~= "string" then
+ error("invalid "..text_type.." value: expected string, got "..type(text));
+ elseif not valid_utf8(text) then
+ error("invalid "..text_type.." value: contains invalid utf8");
+ end
end
+
local function check_attr(attr)
if attr ~= nil then
- assert(type(attr) == "table", "attribute is not a table");
+ if type(attr) ~= "table" then
+ error("invalid attributes, expected table got "..type(attr));
+ end
for k, v in pairs(attr) do
- assert(type(k) == "string", "non-string key in attributes");
- assert(valid_utf8(k), "attribute name is not valid utf8");
- assert(type(v) == "string", "non-string value in attributes");
- assert(valid_utf8(v), "attribute value is not valid utf8");
+ check_name(k, "attribute");
+ check_text(v, "attribute");
+ if type(v) ~= "string" then
+ error("invalid attribute value for '"..k.."': expected string, got "..type(v));
+ elseif not valid_utf8(v) then
+ error("invalid attribute value for '"..k.."': contains invalid utf8");
+ end
end
end
end
-local function check_text(text)
- assert(type(text) == "string", "text is not a string");
- assert(valid_utf8(text), "text is not valid utf8");
-end
local function new_stanza(name, attr, namespaces)
- assert(name)
- check_name(name);
+ check_name(name, "tag");
check_attr(attr);
local stanza = { name = name, attr = attr or {}, namespaces = namespaces, tags = {} };
return setmetatable(stanza, stanza_mt);
@@ -97,7 +111,7 @@ function stanza_mt:tag(name, attr, namespaces)
end
function stanza_mt:text(text)
- check_text(text);
+ check_text(text, "text");
local last_add = self.last_add;
(last_add and last_add[#last_add] or self):add_direct_child(text);
return self;