spec/util_uuid_spec.lua
author Kim Alvefur <zash@zash.se>
Sat, 23 Mar 2024 20:48:19 +0100
changeset 13465 c673ff1075bd
parent 13321 e6a5f196fc1f
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.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     1
-- This tests the format, not the randomness
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     3
local uuid = require "util.uuid";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     4
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     5
describe("util.uuid", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     6
	describe("#generate()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     7
		it("should work follow the UUID pattern", function()
12608
bd9e006a7a74 various: Update IETF RFC URLs for tools.ietf.org transition
Kim Alvefur <zash@zash.se>
parents: 8244
diff changeset
     8
			-- https://www.rfc-editor.org/rfc/rfc4122.html#section-4.4
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     9
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
			local pattern = "^" .. table.concat({
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    11
				string.rep("%x", 8),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    12
				string.rep("%x", 4),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
				"4" .. -- version
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
				string.rep("%x", 3),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
				"[89ab]" .. -- reserved bits of 1 and 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    16
				string.rep("%x", 3),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
				string.rep("%x", 12),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
			}, "%-") .. "$";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    20
			for _ = 1, 100 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    21
				assert.is_string(uuid.generate():match(pattern));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    22
			end
13321
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    23
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    24
			assert.truthy(uuid.generate() ~= uuid.generate(), "does not generate the same UUIDv4 twice")
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    25
		end);
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    26
	end);
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    27
	describe("#v7", function()
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    28
		it("should also follow the UUID pattern", function()
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    29
			local pattern = "^" .. table.concat({
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    30
					string.rep("%x", 8),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    31
					string.rep("%x", 4),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    32
					"7" .. -- version
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    33
					string.rep("%x", 3),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    34
					"[89ab]" .. -- reserved bits of 1 and 0
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    35
					string.rep("%x", 3),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    36
					string.rep("%x", 12),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    37
				}, "%-") .. "$";
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    38
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    39
			local one = uuid.v7(); -- one before the loop to ensure some time passes
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    40
			for _ = 1, 100 do
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    41
				assert.is_string(uuid.v7():match(pattern));
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    42
			end
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    43
			-- one after the loop when some time should have passed
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    44
			assert.truthy(one < uuid.v7(), "should be ordererd")
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    45
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    46
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    47
end);