File

spec/util_dataforms_spec.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 12636:e8934ce6ea0f
line wrap: on
line source

local dataforms = require "util.dataforms";
local st = require "util.stanza";
local jid = require "util.jid";
local iter = require "util.iterators";

describe("util.dataforms", function ()
	local some_form, xform;
	setup(function ()
		some_form = dataforms.new({
			title = "form-title",
			instructions = "form-instructions",
			{
				type = "hidden",
				name = "FORM_TYPE",
				value = "xmpp:prosody.im/spec/util.dataforms#1",
			};
			{
				type = "fixed";
				value = "Fixed field";
			},
			{
				type = "boolean",
				label = "boolean-label",
				name = "boolean-field",
				value = true,
			},
			{
				type = "fixed",
				label = "fixed-label",
				name = "fixed-field",
				value = "fixed-value",
			},
			{
				type = "hidden",
				label = "hidden-label",
				name = "hidden-field",
				value = "hidden-value",
			},
			{
				type = "jid-multi",
				label = "jid-multi-label",
				name = "jid-multi-field",
				value = {
					"jid@multi/value#1",
					"jid@multi/value#2",
				},
			},
			{
				type = "jid-single",
				label = "jid-single-label",
				name = "jid-single-field",
				value = "jid@single/value",
			},
			{
				type = "list-multi",
				label = "list-multi-label",
				name = "list-multi-field",
				value = {
					"list-multi-option-value#1",
					"list-multi-option-value#3",
				},
				options = {
					{
						label = "list-multi-option-label#1",
						value = "list-multi-option-value#1",
						default = true,
					},
					{
						label = "list-multi-option-label#2",
						value = "list-multi-option-value#2",
						default = false,
					},
					{
						label = "list-multi-option-label#3",
						value = "list-multi-option-value#3",
						default = true,
					},
				}
			},
			{
				type = "list-single",
				label = "list-single-label",
				name = "list-single-field",
				value = "list-single-value",
				options = {
					"list-single-value",
					"list-single-value#2",
					"list-single-value#3",
				}
			},
			{
				type = "text-multi",
				label = "text-multi-label",
				name = "text-multi-field",
				value = "text\nmulti\nvalue",
			},
			{
				type = "text-private",
				label = "text-private-label",
				name = "text-private-field",
				value = "text-private-value",
			},
			{
				type = "text-single",
				label = "text-single-label",
				name = "text-single-field",
				value = "text-single-value",
			},
			{
				-- XEP-0221
				-- TODO Validate the XML produced by this.
				type = "text-single",
				label = "text-single-with-media-label",
				name = "text-single-with-media-field",
				media = {
					height = 24,
					width = 32,
					{
						type = "image/png",
						uri = "data:",
					},
				},
			},
		});
		xform = some_form:form();
	end);

	it("XML serialization looks like it should", function ()
		assert.truthy(xform);
		assert.truthy(st.is_stanza(xform));
		assert.equal("x", xform.name);
		assert.equal("jabber:x:data", xform.attr.xmlns);
		assert.equal("FORM_TYPE", xform:get_child_attr("field", nil, "var"));
		assert.equal("xmpp:prosody.im/spec/util.dataforms#1", xform:find("field/value#"));
		local allowed_direct_children = {
			title = true,
			instructions = true,
			field = true,
		}
		for tag in xform:childtags() do
			assert.truthy(allowed_direct_children[tag.name], "unknown direct child");
		end
	end);

	it("produced boolean field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "boolean-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("boolean-field", f.attr.var);
		assert.equal("boolean", f.attr.type);
		assert.equal("boolean-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		local val = f:get_child_text("value");
		assert.truthy(val == "true" or val == "1");
	end);

	it("produced fixed field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "fixed-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("fixed-field", f.attr.var);
		assert.equal("fixed", f.attr.type);
		assert.equal("fixed-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("fixed-value", f:get_child_text("value"));
	end);

	it("produced hidden field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "hidden-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("hidden-field", f.attr.var);
		assert.equal("hidden", f.attr.type);
		assert.equal("hidden-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("hidden-value", f:get_child_text("value"));
	end);

	it("produced jid-multi field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "jid-multi-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("jid-multi-field", f.attr.var);
		assert.equal("jid-multi", f.attr.type);
		assert.equal("jid-multi-label", f.attr.label);
		assert.equal(2, iter.count(f:childtags("value")));

		local i = 0;
		for value in f:childtags("value") do
			i = i + 1;
			assert.equal(("jid@multi/value#%d"):format(i), value:get_text());
		end
	end);

	it("produced jid-single field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "jid-single-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("jid-single-field", f.attr.var);
		assert.equal("jid-single", f.attr.type);
		assert.equal("jid-single-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("jid@single/value", f:get_child_text("value"));
		assert.truthy(jid.prep(f:get_child_text("value")));
	end);

	it("produced list-multi field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "list-multi-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("list-multi-field", f.attr.var);
		assert.equal("list-multi", f.attr.type);
		assert.equal("list-multi-label", f.attr.label);
		assert.equal(2, iter.count(f:childtags("value")));
		assert.equal("list-multi-option-value#1", f:get_child_text("value"));
		assert.equal(3, iter.count(f:childtags("option")));
	end);

	it("produced list-single field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "list-single-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("list-single-field", f.attr.var);
		assert.equal("list-single", f.attr.type);
		assert.equal("list-single-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("list-single-value", f:get_child_text("value"));
		assert.equal(3, iter.count(f:childtags("option")));
	end);

	it("produced text-multi field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "text-multi-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("text-multi-field", f.attr.var);
		assert.equal("text-multi", f.attr.type);
		assert.equal("text-multi-label", f.attr.label);
		assert.equal(3, iter.count(f:childtags("value")));
	end);

	it("produced text-private field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "text-private-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("text-private-field", f.attr.var);
		assert.equal("text-private", f.attr.type);
		assert.equal("text-private-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("text-private-value", f:get_child_text("value"));
	end);

	it("produced text-single field correctly", function ()
		local f;
		for field in xform:childtags("field") do
			if field.attr.var == "text-single-field" then
				f = field;
				break;
			end
		end

		assert.truthy(st.is_stanza(f));
		assert.equal("text-single-field", f.attr.var);
		assert.equal("text-single", f.attr.type);
		assert.equal("text-single-label", f.attr.label);
		assert.equal(1, iter.count(f:childtags("value")));
		assert.equal("text-single-value", f:get_child_text("value"));
	end);

	describe("get_type()", function ()
		it("identifes dataforms", function ()
			assert.equal(nil, dataforms.get_type(nil));
			assert.equal(nil, dataforms.get_type(""));
			assert.equal(nil, dataforms.get_type({}));
			assert.equal(nil, dataforms.get_type(st.stanza("no-a-form")));
			assert.equal("xmpp:prosody.im/spec/util.dataforms#1", dataforms.get_type(xform));
		end);
	end);

	describe(":data", function ()
		it("returns something", function ()
			assert.truthy(some_form:data(xform));
		end);
	end);

	describe("issue1177", function ()
		local form_with_stuff;
		setup(function ()
			form_with_stuff = dataforms.new({
				{
					type = "list-single";
					name = "abtest";
					label = "A or B?";
					options = {
						{ label = "A", value = "a", default = true },
						{ label = "B", value = "b" },
					};
				},
			});
		end);

		it("includes options when value is included", function ()
			local f = form_with_stuff:form({ abtest = "a" });
			assert.truthy(f:find("field/option"));
		end);

		it("includes options when value is excluded", function ()
			local f = form_with_stuff:form({});
			assert.truthy(f:find("field/option"));
		end);
	end);

	describe("using current values in place of missing fields", function ()
		it("gets back the previous values when given an empty form", function ()
			local current = {
				["list-multi-field"] = {
					"list-multi-option-value#2";
				};
				["list-single-field"] = "list-single-value#2";
				["hidden-field"] = "hidden-value";
				["boolean-field"] = false;
				["text-multi-field"] = "words\ngo\nhere";
				["jid-single-field"] = "alice@example.com";
				["text-private-field"] = "hunter2";
				["text-single-field"] = "text-single-value";
				["jid-multi-field"] = {
					"bob@example.net";
				};
			};
			local expect = {
				-- FORM_TYPE = "xmpp:prosody.im/spec/util.dataforms#1"; -- does this need to be included?
				["list-multi-field"] = {
					"list-multi-option-value#2";
				};
				["list-single-field"] = "list-single-value#2";
				["hidden-field"] = "hidden-value";
				["boolean-field"] = false;
				["text-multi-field"] = "words\ngo\nhere";
				["jid-single-field"] = "alice@example.com";
				["text-private-field"] = "hunter2";
				["text-single-field"] = "text-single-value";
				["jid-multi-field"] = {
					"bob@example.net";
				};
			};
			local data, err = some_form:data(st.stanza("x", {xmlns="jabber:x:data"}), current);
			assert.is.table(data, err);
			assert.same(expect, data, "got back the same data");
		end);
	end);

	describe("field 'var' property", function ()
		it("works as expected", function ()
			local f = dataforms.new {
				{
					var = "someprefix#the-field",
					name = "the_field",
					type = "text-single",
				}
			};
			local x = f:form({the_field = "hello"});
			assert.equal("someprefix#the-field", x:find"field@var");
			assert.equal("hello", x:find"field/value#");
		end);
	end);

	describe("number handling", function()
		it("handles numbers as booleans", function()
			local f = dataforms.new { { name = "boolean"; type = "boolean" } };
			local x = f:form({ boolean = 0 });
			assert.equal("0", x:find "field/value#");
			x = f:form({ boolean = 1 });
			assert.equal("1", x:find "field/value#");
		end);
	end)

	describe("datatype validation", function ()
		describe("integer", function ()

			local f = dataforms.new {
				{
					name = "number",
					type = "text-single",
					datatype = "xs:integer",
					range_min = -10,
					range_max = 10,
				},
			};

			it("roundtrip works", function ()
				local d = f:data(f:form({number = 1}));
				assert.equal(1, d.number);
			end);

			it("error handling works", function ()
				local d,e = f:data(f:form({number = "nan"}));
				assert.not_equal(1, d.number);
				assert.table(e);
				assert.string(e.number);
			end);

			it("bounds-checking work works", function ()
				local d,e = f:data(f:form({number = 100}));
				assert.not_equal(100, d.number);
				assert.table(e);
				assert.string(e.number);
			end);

			it("serializes larger ints okay", function ()
				local x = f:form{number=1125899906842624}
				assert.equal("1125899906842624", x:find("field/value#"))
			end);

		end)

		describe("datetime", function ()
			local f = dataforms.new { { name = "when"; type = "text-single"; datatype = "xs:dateTime" } }

			it("works", function ()
				local x = f:form({ when = 1219439340 });
				assert.equal("2008-08-22T21:09:00Z", x:find("field/value#"))
				local d, e = f:data(x);
				assert.is_nil(e);
				assert.same({ when = 1219439340 }, d);
			end);

		end)

	end);
	describe("media element", function ()
		it("produced media element correctly", function ()
			local f;
			for field in xform:childtags("field") do
				if field.attr.var == "text-single-with-media-field" then
					f = field;
					break;
				end
			end

			assert.truthy(st.is_stanza(f));
			assert.equal("text-single-with-media-field", f.attr.var);
			assert.equal("text-single", f.attr.type);
			assert.equal("text-single-with-media-label", f.attr.label);
			assert.equal(0, iter.count(f:childtags("value")));

			local m = f:get_child("media", "urn:xmpp:media-element");
			assert.truthy(st.is_stanza(m));
			assert.equal("24", m.attr.height);
			assert.equal("32", m.attr.width);
			assert.equal(1, iter.count(m:childtags("uri")));

			local u = m:get_child("uri");
			assert.truthy(st.is_stanza(u));
			assert.equal("image/png", u.attr.type);
			assert.equal("data:", u:get_text());
		end);
	end);
end);