-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_keys.lua
More file actions
147 lines (112 loc) · 3.6 KB
/
test_keys.lua
File metadata and controls
147 lines (112 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
--[[
Tests for cryptographic key functionality
Run with: luajit test/test_keys.lua
]]
-- Add paths for running from lua directory: luajit test/test_keys.lua
package.path = "./?/init.lua;./?.lua;./ant_ffi/?.lua;" .. package.path
local function test_secret_key_random()
local ant = require("ant_ffi")
local key = ant.SecretKey.random()
assert(key, "Should create random key")
local hex = key:to_hex()
assert(type(hex) == "string", "to_hex should return string")
assert(#hex > 0, "Hex should not be empty")
print("Random key: " .. hex:sub(1, 32) .. "...")
key:dispose()
print("PASS: secret_key_random")
end
local function test_secret_key_from_hex()
local ant = require("ant_ffi")
-- Create a key, get its hex, then recreate from hex
local key1 = ant.SecretKey.random()
local hex = key1:to_hex()
local key2 = ant.SecretKey.from_hex(hex)
local hex2 = key2:to_hex()
assert(hex == hex2, "Keys should match after round-trip")
key1:dispose()
key2:dispose()
print("PASS: secret_key_from_hex")
end
local function test_public_key()
local ant = require("ant_ffi")
local secret = ant.SecretKey.random()
local public = secret:public_key()
assert(public, "Should get public key from secret key")
local hex = public:to_hex()
assert(type(hex) == "string", "to_hex should return string")
assert(#hex > 0, "Public key hex should not be empty")
print("Public key: " .. hex:sub(1, 32) .. "...")
-- Same secret key should produce same public key
local public2 = secret:public_key()
assert(public:to_hex() == public2:to_hex(), "Same secret should produce same public")
secret:dispose()
public:dispose()
public2:dispose()
print("PASS: public_key")
end
local function test_public_key_from_hex()
local ant = require("ant_ffi")
local secret = ant.SecretKey.random()
local public1 = secret:public_key()
local hex = public1:to_hex()
local public2 = ant.PublicKey.from_hex(hex)
assert(public2:to_hex() == hex, "Public key should round-trip through hex")
secret:dispose()
public1:dispose()
public2:dispose()
print("PASS: public_key_from_hex")
end
local function test_multiple_keys()
local ant = require("ant_ffi")
-- Create multiple different keys
local keys = {}
for i = 1, 5 do
keys[i] = ant.SecretKey.random()
end
-- All keys should be unique
local hexes = {}
for i, key in ipairs(keys) do
hexes[i] = key:to_hex()
end
for i = 1, #hexes do
for j = i + 1, #hexes do
assert(hexes[i] ~= hexes[j], "Keys should be unique")
end
end
-- Cleanup
for _, key in ipairs(keys) do
key:dispose()
end
print("PASS: multiple_keys")
end
local function test_key_garbage_collection()
local ant = require("ant_ffi")
-- Create keys and let them be GC'd
for i = 1, 10 do
local key = ant.SecretKey.random()
local _ = key:to_hex()
-- Key goes out of scope and should be collected
end
-- Force garbage collection
collectgarbage("collect")
collectgarbage("collect")
-- If we got here without crash, GC cleanup works
print("PASS: key_garbage_collection")
end
-- Run tests
print("\n=== Key Tests ===\n")
local tests = {
test_secret_key_random,
test_secret_key_from_hex,
test_public_key,
test_public_key_from_hex,
test_multiple_keys,
test_key_garbage_collection,
}
for _, test in ipairs(tests) do
local ok, err = pcall(test)
if not ok then
print("FAIL: " .. tostring(err))
end
end
print("\n=== Tests Complete ===\n")