From b63e7562de86424a969e208f4e942aa3cf5d2d68 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 11:30:27 +0000 Subject: [PATCH] fix(config): reject unsupported proxy modes Co-Authored-By: Virgil --- config_runtime.go | 5 +++++ config_runtime_test.go | 39 +++++++++++++++++++++++++++++++++++++++ proxy_runtime_test.go | 4 ++-- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 config_runtime_test.go diff --git a/config_runtime.go b/config_runtime.go index 6af1e7a..dbcdd3a 100644 --- a/config_runtime.go +++ b/config_runtime.go @@ -43,6 +43,11 @@ func (c *Config) Validate() error { if len(c.Pools) == 0 { return errors.New("pool list is empty") } + switch c.Mode { + case "nicehash", "simple": + default: + return errors.New("unsupported mode") + } for _, poolConfig := range c.Pools { if poolConfig.Enabled && strings.TrimSpace(poolConfig.URL) == "" { diff --git a/config_runtime_test.go b/config_runtime_test.go new file mode 100644 index 0000000..301dc95 --- /dev/null +++ b/config_runtime_test.go @@ -0,0 +1,39 @@ +package proxy + +import "testing" + +func TestConfig_Validate_Good(t *testing.T) { + cfg := &Config{ + Mode: "nicehash", + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, + } + + if errorValue := cfg.Validate(); errorValue != nil { + t.Fatalf("expected valid config, got %v", errorValue) + } +} + +func TestConfig_Validate_Bad(t *testing.T) { + cfg := &Config{ + Mode: "noop", + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, + } + + if errorValue := cfg.Validate(); errorValue == nil { + t.Fatal("expected unsupported mode to fail validation") + } +} + +func TestConfig_Validate_Ugly(t *testing.T) { + cfg := &Config{ + Mode: "simple", + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{Enabled: true}}, + } + + if errorValue := cfg.Validate(); errorValue == nil { + t.Fatal("expected empty enabled pool URL to fail validation") + } +} diff --git a/proxy_runtime_test.go b/proxy_runtime_test.go index 3b52f5e..f757a1a 100644 --- a/proxy_runtime_test.go +++ b/proxy_runtime_test.go @@ -4,7 +4,7 @@ import "testing" func TestProxy_Reload_Good(t *testing.T) { cfg := &Config{ - Mode: "noop", + Mode: "nicehash", Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, CustomDiff: 100, @@ -41,7 +41,7 @@ func TestProxy_Reload_Good(t *testing.T) { func TestProxy_CurrentMiners_Good(t *testing.T) { cfg := &Config{ - Mode: "noop", + Mode: "nicehash", Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, Pools: []PoolConfig{{URL: "pool-a:3333", Enabled: true}}, Workers: WorkersDisabled,