"ddos challenge" style script #4
1 changed files with 68 additions and 45 deletions
|
|
@ -27,10 +27,10 @@ local function createMockSharedDict()
|
||||||
end
|
end
|
||||||
|
|
||||||
function TestDDoSProtectionChallenge:setup()
|
function TestDDoSProtectionChallenge:setup()
|
||||||
-- First reset ngx to get the base ngx object
|
-- Reset ngx
|
||||||
resetNgx()
|
resetNgx()
|
||||||
|
|
||||||
-- Create mock shared dictionaries
|
-- Create mock shared dictionaries AFTER resetNgx
|
||||||
ngx.shared = {
|
ngx.shared = {
|
||||||
aproxy_bans = createMockSharedDict(),
|
aproxy_bans = createMockSharedDict(),
|
||||||
aproxy_tokens = createMockSharedDict()
|
aproxy_tokens = createMockSharedDict()
|
||||||
|
|
@ -39,19 +39,10 @@ function TestDDoSProtectionChallenge:setup()
|
||||||
-- Mock ngx.time for consistent testing
|
-- Mock ngx.time for consistent testing
|
||||||
ngx.time = function() return 1000000 end
|
ngx.time = function() return 1000000 end
|
||||||
|
|
||||||
-- Add request method support
|
-- Setup module manually (can't use setupTest because it calls resetNgx)
|
||||||
ngx.var.request_method = 'GET'
|
local ctx = require('ctx')
|
||||||
|
local config = require('config')
|
||||||
-- Add header setting support
|
local test_config = {
|
||||||
ngx.header = {}
|
|
||||||
|
|
||||||
-- Add POST body reading support
|
|
||||||
ngx.req.read_body = function() end
|
|
||||||
ngx.req.get_post_args = function()
|
|
||||||
return ngx._post_args or {}
|
|
||||||
end
|
|
||||||
|
|
||||||
self.mod = setupTest('scripts.ddos_protection_challenge', {
|
|
||||||
ban_duration = 3600,
|
ban_duration = 3600,
|
||||||
token_duration = 86400,
|
token_duration = 86400,
|
||||||
cookie_name = 'aproxy_token',
|
cookie_name = 'aproxy_token',
|
||||||
|
|
@ -60,7 +51,17 @@ function TestDDoSProtectionChallenge:setup()
|
||||||
protected_paths = {},
|
protected_paths = {},
|
||||||
challenge_type = 'button',
|
challenge_type = 'button',
|
||||||
pow_difficulty = 4
|
pow_difficulty = 4
|
||||||
})
|
}
|
||||||
|
|
||||||
|
self.mod = require('scripts.ddos_protection_challenge')
|
||||||
|
local schema_errors = config.validateSchema(self.mod.config, test_config)
|
||||||
|
local count = table.pprint(schema_errors)
|
||||||
|
lu.assertIs(count, 0)
|
||||||
|
|
||||||
|
local state = self.mod.init(test_config)
|
||||||
|
ctx.compiled_chain = {
|
||||||
|
{self.mod, test_config, state}
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function TestDDoSProtectionChallenge:testNoTokenShowsChallenge()
|
function TestDDoSProtectionChallenge:testNoTokenShowsChallenge()
|
||||||
|
|
@ -212,25 +213,21 @@ end
|
||||||
TestDDoSProtectionChallengePaths = {}
|
TestDDoSProtectionChallengePaths = {}
|
||||||
|
|
||||||
function TestDDoSProtectionChallengePaths:setup()
|
function TestDDoSProtectionChallengePaths:setup()
|
||||||
-- First reset ngx to get the base ngx object
|
-- Reset ngx
|
||||||
resetNgx()
|
resetNgx()
|
||||||
|
|
||||||
-- Create mock shared dictionaries
|
-- Create mock shared dictionaries AFTER resetNgx
|
||||||
ngx.shared = {
|
ngx.shared = {
|
||||||
aproxy_bans = createMockSharedDict(),
|
aproxy_bans = createMockSharedDict(),
|
||||||
aproxy_tokens = createMockSharedDict()
|
aproxy_tokens = createMockSharedDict()
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx.time = function() return 1000000 end
|
ngx.time = function() return 1000000 end
|
||||||
ngx.var.request_method = 'GET'
|
|
||||||
ngx.header = {}
|
|
||||||
ngx.req.read_body = function() end
|
|
||||||
ngx.req.get_post_args = function()
|
|
||||||
return ngx._post_args or {}
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Configure with specific protected paths
|
-- Setup module manually
|
||||||
self.mod = setupTest('scripts.ddos_protection_challenge', {
|
local ctx = require('ctx')
|
||||||
|
local config = require('config')
|
||||||
|
local test_config = {
|
||||||
ban_duration = 3600,
|
ban_duration = 3600,
|
||||||
token_duration = 86400,
|
token_duration = 86400,
|
||||||
cookie_name = 'aproxy_token',
|
cookie_name = 'aproxy_token',
|
||||||
|
|
@ -239,7 +236,17 @@ function TestDDoSProtectionChallengePaths:setup()
|
||||||
protected_paths = {'/api/.*', '/search'},
|
protected_paths = {'/api/.*', '/search'},
|
||||||
challenge_type = 'button',
|
challenge_type = 'button',
|
||||||
pow_difficulty = 4
|
pow_difficulty = 4
|
||||||
})
|
}
|
||||||
|
|
||||||
|
self.mod = require('scripts.ddos_protection_challenge')
|
||||||
|
local schema_errors = config.validateSchema(self.mod.config, test_config)
|
||||||
|
local count = table.pprint(schema_errors)
|
||||||
|
lu.assertIs(count, 0)
|
||||||
|
|
||||||
|
local state = self.mod.init(test_config)
|
||||||
|
ctx.compiled_chain = {
|
||||||
|
{self.mod, test_config, state}
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function TestDDoSProtectionChallengePaths:testProtectedPathShowsChallenge()
|
function TestDDoSProtectionChallengePaths:testProtectedPathShowsChallenge()
|
||||||
|
|
@ -363,23 +370,21 @@ end
|
||||||
TestDDoSProtectionChallengeQuestion = {}
|
TestDDoSProtectionChallengeQuestion = {}
|
||||||
|
|
||||||
function TestDDoSProtectionChallengeQuestion:setup()
|
function TestDDoSProtectionChallengeQuestion:setup()
|
||||||
-- First reset ngx to get the base ngx object
|
-- Reset ngx
|
||||||
resetNgx()
|
resetNgx()
|
||||||
|
|
||||||
|
-- Create mock shared dictionaries AFTER resetNgx
|
||||||
ngx.shared = {
|
ngx.shared = {
|
||||||
aproxy_bans = createMockSharedDict(),
|
aproxy_bans = createMockSharedDict(),
|
||||||
aproxy_tokens = createMockSharedDict()
|
aproxy_tokens = createMockSharedDict()
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx.time = function() return 1000000 end
|
ngx.time = function() return 1000000 end
|
||||||
ngx.var.request_method = 'GET'
|
|
||||||
ngx.header = {}
|
|
||||||
ngx.req.read_body = function() end
|
|
||||||
ngx.req.get_post_args = function()
|
|
||||||
return ngx._post_args or {}
|
|
||||||
end
|
|
||||||
|
|
||||||
self.mod = setupTest('scripts.ddos_protection_challenge', {
|
-- Setup module manually
|
||||||
|
local ctx = require('ctx')
|
||||||
|
local config = require('config')
|
||||||
|
local test_config = {
|
||||||
ban_duration = 3600,
|
ban_duration = 3600,
|
||||||
token_duration = 86400,
|
token_duration = 86400,
|
||||||
cookie_name = 'aproxy_token',
|
cookie_name = 'aproxy_token',
|
||||||
|
|
@ -388,7 +393,17 @@ function TestDDoSProtectionChallengeQuestion:setup()
|
||||||
protected_paths = {},
|
protected_paths = {},
|
||||||
challenge_type = 'question',
|
challenge_type = 'question',
|
||||||
pow_difficulty = 4
|
pow_difficulty = 4
|
||||||
})
|
}
|
||||||
|
|
||||||
|
self.mod = require('scripts.ddos_protection_challenge')
|
||||||
|
local schema_errors = config.validateSchema(self.mod.config, test_config)
|
||||||
|
local count = table.pprint(schema_errors)
|
||||||
|
lu.assertIs(count, 0)
|
||||||
|
|
||||||
|
local state = self.mod.init(test_config)
|
||||||
|
ctx.compiled_chain = {
|
||||||
|
{self.mod, test_config, state}
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function TestDDoSProtectionChallengeQuestion:testQuestionChallengeShown()
|
function TestDDoSProtectionChallengeQuestion:testQuestionChallengeShown()
|
||||||
|
|
@ -480,23 +495,21 @@ end
|
||||||
TestDDoSProtectionChallengePow = {}
|
TestDDoSProtectionChallengePow = {}
|
||||||
|
|
||||||
function TestDDoSProtectionChallengePow:setup()
|
function TestDDoSProtectionChallengePow:setup()
|
||||||
-- First reset ngx to get the base ngx object
|
-- Reset ngx
|
||||||
resetNgx()
|
resetNgx()
|
||||||
|
|
||||||
|
-- Create mock shared dictionaries AFTER resetNgx
|
||||||
ngx.shared = {
|
ngx.shared = {
|
||||||
aproxy_bans = createMockSharedDict(),
|
aproxy_bans = createMockSharedDict(),
|
||||||
aproxy_tokens = createMockSharedDict()
|
aproxy_tokens = createMockSharedDict()
|
||||||
}
|
}
|
||||||
|
|
||||||
ngx.time = function() return 1000000 end
|
ngx.time = function() return 1000000 end
|
||||||
ngx.var.request_method = 'GET'
|
|
||||||
ngx.header = {}
|
|
||||||
ngx.req.read_body = function() end
|
|
||||||
ngx.req.get_post_args = function()
|
|
||||||
return ngx._post_args or {}
|
|
||||||
end
|
|
||||||
|
|
||||||
self.mod = setupTest('scripts.ddos_protection_challenge', {
|
-- Setup module manually
|
||||||
|
local ctx = require('ctx')
|
||||||
|
local config = require('config')
|
||||||
|
local test_config = {
|
||||||
ban_duration = 3600,
|
ban_duration = 3600,
|
||||||
token_duration = 86400,
|
token_duration = 86400,
|
||||||
cookie_name = 'aproxy_token',
|
cookie_name = 'aproxy_token',
|
||||||
|
|
@ -505,7 +518,17 @@ function TestDDoSProtectionChallengePow:setup()
|
||||||
protected_paths = {},
|
protected_paths = {},
|
||||||
challenge_type = 'pow',
|
challenge_type = 'pow',
|
||||||
pow_difficulty = 4
|
pow_difficulty = 4
|
||||||
})
|
}
|
||||||
|
|
||||||
|
self.mod = require('scripts.ddos_protection_challenge')
|
||||||
|
local schema_errors = config.validateSchema(self.mod.config, test_config)
|
||||||
|
local count = table.pprint(schema_errors)
|
||||||
|
lu.assertIs(count, 0)
|
||||||
|
|
||||||
|
local state = self.mod.init(test_config)
|
||||||
|
ctx.compiled_chain = {
|
||||||
|
{self.mod, test_config, state}
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function TestDDoSProtectionChallengePow:testPowChallengeShown()
|
function TestDDoSProtectionChallengePow:testPowChallengeShown()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue