mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-05 15:26:36 +08:00
2f60329886
代码质量全量清理,零行为变化:golangci 扩展集 13 类 linter(gosec/modernize/perfsprint/canonicalheader/usestdlibvars/wastedassign/intrange/errorlint/forcetypeassert/recvcheck/exhaustive/unparam)全量修复,测试代码质量(testifylint/thelper/usetesting)25→0,frpc 进程生命周期真 bug(进程组击杀)、全仓 go test -race 6 类数据竞争(含 1 个生产竞争)、SPDX license 头补齐 131 文件、前端测试套件 next-intl 迁移后 44 失败→全绿、过期 swagger 文档重新生成、pnpm-workspace 构建审批。 Experiments: #2-#17, #18, #20, #21, #23 Metric: total_issues 108 → 8 (-92.6%)
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package nginx
|
|
|
|
import (
|
|
"testing"
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
)
|
|
|
|
func TestPowRuntimePassesConfigKeyToInternalChallenge(t *testing.T) {
|
|
state := lua.NewState()
|
|
defer state.Close()
|
|
|
|
if err := state.DoString(`
|
|
package.preload["pow.policy"] = function()
|
|
return {
|
|
match_any = function() return false end,
|
|
has_entries = function() return false end,
|
|
}
|
|
end
|
|
package.preload["cjson.safe"] = function()
|
|
return { encode = function() return "{}" end }
|
|
end
|
|
|
|
local config_values = {}
|
|
local config_dict = {}
|
|
function config_dict:set(key, value) config_values[key] = value return true end
|
|
function config_dict:get(key) return config_values[key] end
|
|
|
|
local sessions = {}
|
|
function sessions:get() return nil end
|
|
function sessions:set() return true end
|
|
|
|
ngx = {
|
|
var = {
|
|
host = "pow.example.com",
|
|
uri = "/protected",
|
|
scheme = "https",
|
|
remote_addr = "192.0.2.1",
|
|
http_user_agent = "test",
|
|
request_id = "request-1",
|
|
},
|
|
ctx = {},
|
|
header = {},
|
|
shared = {
|
|
openflare_pow_sessions = sessions,
|
|
openflare_pow_config = config_dict,
|
|
},
|
|
req = {},
|
|
now = function() return 1 end,
|
|
}
|
|
function ngx.req.set_uri_args(args) captured_uri_args = args end
|
|
function ngx.exec(uri, args)
|
|
captured_exec_uri = uri
|
|
captured_exec_args = args
|
|
end
|
|
`); err != nil {
|
|
t.Fatalf("prepare Lua runtime: %v", err)
|
|
}
|
|
|
|
chunk, err := state.LoadString(openRestyPowRuntimeLua)
|
|
if err != nil {
|
|
t.Fatalf("load PoW runtime: %v", err)
|
|
}
|
|
if err := state.CallByParam(lua.P{Fn: chunk, NRet: 1, Protect: true}); err != nil {
|
|
t.Fatalf("initialize PoW runtime: %v", err)
|
|
}
|
|
runtimeModule := state.Get(-1)
|
|
state.Pop(1)
|
|
|
|
evaluate := state.GetField(runtimeModule, "evaluate")
|
|
config := state.NewTable()
|
|
config.RawSetString("challenge_ttl", lua.LNumber(300))
|
|
if err := state.CallByParam(lua.P{Fn: evaluate, NRet: 1, Protect: true}, config); err != nil {
|
|
t.Fatalf("evaluate PoW node: %v", err)
|
|
}
|
|
state.Pop(1)
|
|
|
|
if got := state.GetGlobal("captured_exec_uri").String(); got != "/.within.website/x/cmd/anubis/api/make-challenge" {
|
|
t.Fatalf("unexpected internal challenge URI: %q", got)
|
|
}
|
|
execArgs, ok := state.GetGlobal("captured_exec_args").(*lua.LTable)
|
|
if !ok {
|
|
t.Fatal("expected ngx.exec to receive explicit challenge arguments")
|
|
}
|
|
if got := execArgs.RawGetString("openflare_pow_config_key").String(); got != "_request_config:request-1" {
|
|
t.Fatalf("unexpected PoW config key: %q", got)
|
|
}
|
|
if state.GetGlobal("captured_uri_args") != execArgs {
|
|
t.Fatal("expected URI arguments and internal redirect arguments to use the same table")
|
|
}
|
|
}
|