spec/util_encodings_spec.lua
author Kim Alvefur <zash@zash.se>
Tue, 14 May 2024 17:07:47 +0200
changeset 13494 6f840763fc73
parent 8376 af7b41a0ec9e
permissions -rw-r--r--
net.server_epoll: Add support for systemd socket activation Allows creating listening sockets and accepting client connections before Prosody starts. This is unlike normal Prosody dynamic resource management, where ports may added and removed at any time, and the ports defined by the config. Weird things happen if these are closed (e.g. due to reload) so here we prevent closing and ensure sockets are reused when opened again.


local encodings = require "util.encodings";
local utf8 = assert(encodings.utf8, "no encodings.utf8 module");

describe("util.encodings", function ()
	describe("#encode()", function()
		it("should work", function ()
			assert.is.equal(encodings.base64.encode(""), "");
			assert.is.equal(encodings.base64.encode('coucou'), "Y291Y291");
			assert.is.equal(encodings.base64.encode("\0\0\0"), "AAAA");
			assert.is.equal(encodings.base64.encode("\255\255\255"), "////");
		end);
	end);
	describe("#decode()", function()
		it("should work", function ()
			assert.is.equal(encodings.base64.decode(""), "");
			assert.is.equal(encodings.base64.decode("="), "");
			assert.is.equal(encodings.base64.decode('Y291Y291'), "coucou");
			assert.is.equal(encodings.base64.decode("AAAA"), "\0\0\0");
			assert.is.equal(encodings.base64.decode("////"), "\255\255\255");
		end);
	end);
end);
describe("util.encodings.utf8", function()
	describe("#valid()", function()
		it("should work", function()

			for line in io.lines("spec/utf8_sequences.txt") do
				local data = line:match(":%s*([^#]+)"):gsub("%s+", ""):gsub("..", function (c) return string.char(tonumber(c, 16)); end)
				local expect = line:match("(%S+):");

				assert(expect == "pass" or expect == "fail", "unknown expectation: "..line:match("^[^:]+"));

				local valid = utf8.valid(data);
				assert.is.equal(valid, utf8.valid(data.." "));
				assert.is.equal(valid, expect == "pass", line);
			end

		end);
	end);
end);