spec/core_moduleapi_spec.lua
author Kim Alvefur <zash@zash.se>
Sat, 23 Mar 2024 20:48:19 +0100
changeset 13465 c673ff1075bd
parent 13404 786c7707f306
permissions -rw-r--r--
mod_posix: Move everything to util.startup This allows greater control over the order of events. Notably, the internal ordering between daemonization, initialization of libunbound and setup of signal handling is sensitive. libunbound starts a separate thread for processing DNS requests. If this thread is started before signal handling has been set up, it will not inherit the signal handlers and instead behave as it would have before signal handlers were set up, i.e. cause the whole process to immediately exit. libunbound is usually initialized on the first DNS request, usually triggered by an outgoing s2s connection attempt. If daemonization happens before signals have been set up, signals may not be processed at all.


package.loaded["core.configmanager"] = {};
package.loaded["core.statsmanager"] = {};
package.loaded["net.server"] = {};

local set = require "util.set";

_G.prosody = { hosts = {}, core_post_stanza = true };

local api = require "core.moduleapi";

local module = setmetatable({}, {__index = api});
local opt = nil;
function module.log(_self) end
function module.get_option(_self, name)
	if name == "opt" then
		return opt;
	else
		return nil;
	end
end

local function test_option_value(value, returns)
	opt = value;
	assert(module:get_option_number("opt") == returns.number, "number doesn't match");
	assert(module:get_option_string("opt") == returns.string, "string doesn't match");
	assert(module:get_option_boolean("opt") == returns.boolean, "boolean doesn't match");

	if type(returns.array) == "table" then
		local target_array, returned_array = returns.array, module:get_option_array("opt");
		assert(#target_array == #returned_array, "array length doesn't match");
		for i=1,#target_array do
			assert(target_array[i] == returned_array[i], "array item doesn't match");
		end
	else
		assert(module:get_option_array("opt") == returns.array, "array is returned (not nil)");
	end

	if type(returns.set) == "table" then
		local target_items, returned_items = set.new(returns.set), module:get_option_set("opt");
		assert(target_items == returned_items, "set doesn't match");
	else
		assert(module:get_option_set("opt") == returns.set, "set is returned (not nil)");
	end
end

describe("core.moduleapi", function()
	describe("#get_option_*()", function()
		it("should handle missing options", function()
			test_option_value(nil, {});
		end);

		it("should return correctly handle boolean options", function()
			test_option_value(true, { boolean = true, string = "true", array = {true}, set = {true} });
			test_option_value(false, { boolean = false, string = "false", array = {false}, set = {false} });
			test_option_value("true", { boolean = true, string = "true", array = {"true"}, set = {"true"} });
			test_option_value("false", { boolean = false, string = "false", array = {"false"}, set = {"false"} });
			test_option_value(1, { boolean = true, string = "1", array = {1}, set = {1}, number = 1 });
			test_option_value(0, { boolean = false, string = "0", array = {0}, set = {0}, number = 0 });
		end);

		it("should return handle strings", function()
			test_option_value("hello world", { string = "hello world", array = {"hello world"}, set = {"hello world"} });
		end);

		it("should return handle numbers", function()
			test_option_value(1234, { string = "1234", number = 1234, array = {1234}, set = {1234} });
		end);

		it("should return handle arrays", function()
			test_option_value({1, 2, 3}, { boolean = true, string = "1", number = 1, array = {1, 2, 3}, set = {1, 2, 3} });
			test_option_value({1, 2, 3, 3, 4}, {boolean = true, string = "1", number = 1, array = {1, 2, 3, 3, 4}, set = {1, 2, 3, 4} });
			test_option_value({0, 1, 2, 3}, { boolean = false, string = "0", number = 0, array = {0, 1, 2, 3}, set = {0, 1, 2, 3} });
		end);
	end)
end)