Debian Codegraph @

test/lua/unit/cryptobox.fast_hash64.lua

raw
1 local cr = require 'rspamd_cryptobox'
2
3 context("Cryptobox - fast_hash64", function()
4
5 test('Returns two numbers', function()
6 local hi, lo = cr.fast_hash64("test")
7 assert_not_nil(hi)
8 assert_not_nil(lo)
9 assert_equal(type(hi), "number")
10 assert_equal(type(lo), "number")
11 end)
12
13 test('Same input produces same output', function()
14 local hi1, lo1 = cr.fast_hash64("hello world")
15 local hi2, lo2 = cr.fast_hash64("hello world")
16 assert_equal(hi1, hi2)
17 assert_equal(lo1, lo2)
18 end)
19
20 test('Different input produces different output', function()
21 local hi1, lo1 = cr.fast_hash64("hello")
22 local hi2, lo2 = cr.fast_hash64("world")
23 -- At least one of hi/lo should differ
24 assert_true(hi1 ~= hi2 or lo1 ~= lo2, "Different inputs should produce different hashes")
25 end)
26
27 test('Seed affects output', function()
28 local hi1, lo1 = cr.fast_hash64("test", 0)
29 local hi2, lo2 = cr.fast_hash64("test", 12345)
30 assert_true(hi1 ~= hi2 or lo1 ~= lo2, "Different seeds should produce different hashes")
31 end)
32
33 test('Empty string is valid input', function()
34 local hi, lo = cr.fast_hash64("")
35 assert_not_nil(hi)
36 assert_not_nil(lo)
37 end)
38
39 test('Long string is valid input', function()
40 local long_str = string.rep("x", 10000)
41 local hi, lo = cr.fast_hash64(long_str)
42 assert_not_nil(hi)
43 assert_not_nil(lo)
44 end)
45
46 test('XOR accumulation produces order-independent result', function()
47 local bit = require "bit"
48
49 local function hash_and_xor(strings)
50 local acc_hi, acc_lo = 0, 0
51 for _, s in ipairs(strings) do
52 local hi, lo = cr.fast_hash64(s)
53 acc_hi = bit.bxor(acc_hi, hi)
54 acc_lo = bit.bxor(acc_lo, lo)
55 end
56 return acc_hi, acc_lo
57 end
58
59 -- Same strings in different order
60 local hi1, lo1 = hash_and_xor({"alpha", "beta", "gamma"})
61 local hi2, lo2 = hash_and_xor({"gamma", "alpha", "beta"})
62 local hi3, lo3 = hash_and_xor({"beta", "gamma", "alpha"})
63
64 assert_equal(hi1, hi2)
65 assert_equal(lo1, lo2)
66 assert_equal(hi1, hi3)
67 assert_equal(lo1, lo3)
68 end)
69
70 end)