Debian Codegraph @

test/lua/unit/hash.lua

raw
1 local hash = require 'rspamd_cryptobox_hash'
2
3 context("Cryptobox hash tests", function()
4
5 local function hash_value(value)
6 local h = hash.create()
7 h:update(value)
8 return h:hex()
9 end
10
11 local function compare_hashes(val1, val2)
12 return hash_value(val1) == hash_value(val2)
13 end
14
15 context("Basic type hashing", function()
16 test("Handles strings", function()
17 local h1 = hash_value("test")
18 local h2 = hash_value("test")
19 assert_equal(h1, h2, "Same strings should hash to same value")
20
21 assert_not_equal(hash_value("test"), hash_value("test2"),
22 "Different strings should hash differently")
23 end)
24
25 test("Handles numbers", function()
26 -- Integer tests
27 assert_equal(hash_value(123), hash_value(123))
28 assert_not_equal(hash_value(123), hash_value(124))
29
30 -- Float tests
31 assert_equal(hash_value(123.45), hash_value(123.45))
32 assert_not_equal(hash_value(123.45), hash_value(123.46))
33
34 -- Different number types should hash differently
35 assert_not_equal(hash_value(123), hash_value(123.1))
36 end)
37
38 test("Handles booleans", function()
39 assert_equal(hash_value(true), hash_value(true))
40 assert_equal(hash_value(false), hash_value(false))
41 assert_not_equal(hash_value(true), hash_value(false))
42 end)
43
44 test("Handles nil", function()
45 local h1 = hash.create()
46 local h2 = hash.create()
47 h1:update(nil)
48 h2:update(nil)
49 assert_equal(h1:hex(), h2:hex())
50 end)
51 end)
52
53 context("Table hashing", function()
54 test("Handles array tables", function()
55 assert_equal(hash_value({ 1, 2, 3 }), hash_value({ 1, 2, 3 }))
56 assert_not_equal(hash_value({ 1, 2, 3 }), hash_value({ 1, 2, 4 }))
57 assert_not_equal(hash_value({ 1, 2, 3 }), hash_value({ 1, 2 }))
58 end)
59
60 test("Handles key-value tables", function()
61 assert_equal(
62 hash_value({ foo = "bar", baz = 123 }),
63 hash_value({ foo = "bar", baz = 123 })
64 )
65 assert_not_equal(
66 hash_value({ foo = "bar" }),
67 hash_value({ foo = "baz" })
68 )
69 end)
70
71 test("Handles mixed tables", function()
72 assert_equal(
73 hash_value({ 1, 2, foo = "bar" }),
74 hash_value({ 1, 2, foo = "bar" })
75 )
76 assert_not_equal(
77 hash_value({ 1, 2, foo = "bar" }),
78 hash_value({ 1, 2, foo = "baz" })
79 )
80 end)
81
82 test("Handles nested tables", function()
83 assert_equal(
84 hash_value({ 1, { 2, 3 }, foo = { bar = "baz" } }),
85 hash_value({ 1, { 2, 3 }, foo = { bar = "baz" } })
86 )
87 assert_not_equal(
88 hash_value({ 1, { 2, 3 } }),
89 hash_value({ 1, { 2, 4 } })
90 )
91 end)
92 end)
93
94 context("Complex scenarios", function()
95 test("Handles multiple updates", function()
96 local h1 = hash.create()
97 h1:update("test")
98 h1:update(123)
99 h1:update({ foo = "bar" })
100
101 local h2 = hash.create()
102 h2:update("test")
103 h2:update(123)
104 h2:update({ foo = "bar" })
105
106 assert_equal(h1:hex(), h2:hex())
107 end)
108
109 test("Order matters for updates", function()
110 local h1 = hash.create()
111 h1:update("a")
112 h1:update("b")
113
114 local h2 = hash.create()
115 h2:update("b")
116 h2:update("a")
117
118 assert_not_equal(h1:hex(), h2:hex())
119 end)
120
121 test("Handles all types together", function()
122 local complex = {
123 str = "test",
124 num = 123,
125 float = 123.45,
126 bool = true,
127 arr = { 1, 2, 3 },
128 nested = {
129 foo = {
130 bar = "baz"
131 }
132 }
133 }
134
135 assert_equal(hash_value(complex), hash_value(complex))
136 end)
137 end)
138
139 context("Error conditions", function()
140 test("Prevents update after finalization", function()
141 local h = hash.create()
142 h:update("test")
143 local _ = h:hex() -- finalize
144 assert_error(function()
145 h:update("more")
146 end)
147 end)
148
149 test("Handles function values", function()
150 local h = hash.create()
151 local f = function()
152 end
153 assert_not_error(function()
154 h:update(f)
155 end)
156 end)
157 end)
158
159 context("Determinism tests", function()
160 test("Same input always produces same hash", function()
161 local inputs = {
162 "test string",
163 123,
164 true,
165 { 1, 2, 3 },
166 { foo = "bar", nested = { 1, 2, 3 } },
167 }
168
169 for _, input in ipairs(inputs) do
170 local h1 = hash_value(input)
171 local h2 = hash_value(input)
172 assert_equal(h1, h2, "Hash should be deterministic for: " .. type(input))
173 end
174 end)
175 end)
176 end)