"ddos challenge" style script #4
3 changed files with 56 additions and 0 deletions
Add mock resty.sha256 and resty.string for testing
The test environment doesn't have OpenResty libraries, so we need to provide mock implementations for testing. Created: - tests/mock_resty_sha256.lua: Uses system sha256sum command to compute SHA-256 hashes. Mimics the resty.sha256 API (new, update, final). - tests/mock_resty_string.lua: Implements to_hex() to convert binary strings to hexadecimal. Updated test.lua to preload these mocks so that when the module or tests require 'resty.sha256' or 'resty.string', they get our mock implementations instead. This allows the PoW verification tests to run and actually verify the SHA-256 proof-of-work.
commit
fbe238d3c1
8
test.lua
8
test.lua
|
|
@ -2,6 +2,14 @@ lu = require('luaunit')
|
||||||
local rex = require('rex_pcre2')
|
local rex = require('rex_pcre2')
|
||||||
require('util')
|
require('util')
|
||||||
|
|
||||||
|
-- Preload mock resty libraries for testing
|
||||||
|
package.preload['resty.sha256'] = function()
|
||||||
|
return require('tests.mock_resty_sha256')
|
||||||
|
end
|
||||||
|
package.preload['resty.string'] = function()
|
||||||
|
return require('tests.mock_resty_string')
|
||||||
|
end
|
||||||
|
|
||||||
function createNgx()
|
function createNgx()
|
||||||
local ngx = {
|
local ngx = {
|
||||||
status = nil
|
status = nil
|
||||||
|
|
|
||||||
35
tests/mock_resty_sha256.lua
Normal file
35
tests/mock_resty_sha256.lua
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
-- Mock implementation of resty.sha256 for testing
|
||||||
|
-- Uses system sha256sum command since we don't have OpenResty libraries
|
||||||
|
|
||||||
|
local sha256 = {}
|
||||||
|
sha256.__index = sha256
|
||||||
|
|
||||||
|
function sha256:new()
|
||||||
|
local obj = {
|
||||||
|
buffer = ""
|
||||||
|
}
|
||||||
|
setmetatable(obj, sha256)
|
||||||
|
return obj
|
||||||
|
end
|
||||||
|
|
||||||
|
function sha256:update(data)
|
||||||
|
self.buffer = self.buffer .. data
|
||||||
|
end
|
||||||
|
|
||||||
|
function sha256:final()
|
||||||
|
-- Use sha256sum command to compute hash
|
||||||
|
local handle = io.popen("echo -n '" .. self.buffer:gsub("'", "'\\''") .. "' | sha256sum")
|
||||||
|
local result = handle:read("*a")
|
||||||
|
handle:close()
|
||||||
|
|
||||||
|
-- Parse hex string into binary
|
||||||
|
local hex = result:match("^(%x+)")
|
||||||
|
local binary = {}
|
||||||
|
for i = 1, #hex, 2 do
|
||||||
|
table.insert(binary, string.char(tonumber(hex:sub(i, i+1), 16)))
|
||||||
|
end
|
||||||
|
|
||||||
|
return table.concat(binary)
|
||||||
|
end
|
||||||
|
|
||||||
|
return sha256
|
||||||
13
tests/mock_resty_string.lua
Normal file
13
tests/mock_resty_string.lua
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
-- Mock implementation of resty.string for testing
|
||||||
|
|
||||||
|
local str = {}
|
||||||
|
|
||||||
|
function str.to_hex(binary)
|
||||||
|
local hex = {}
|
||||||
|
for i = 1, #binary do
|
||||||
|
table.insert(hex, string.format("%02x", string.byte(binary, i)))
|
||||||
|
end
|
||||||
|
return table.concat(hex)
|
||||||
|
end
|
||||||
|
|
||||||
|
return str
|
||||||
Loading…
Add table
Add a link
Reference in a new issue