From 9e997554faddf768c2efbfc33fe6bc2c160ca4f9 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 17:14:54 +0000 Subject: [PATCH] fix(config): validate mode and workers enums Co-Authored-By: Virgil --- config_runtime.go | 13 +++++++++++++ config_runtime_test.go | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/config_runtime.go b/config_runtime.go index 5a3ef64..3d842ee 100644 --- a/config_runtime.go +++ b/config_runtime.go @@ -46,6 +46,19 @@ func (c *Config) Validate() error { if c == nil { return errors.New("config is nil") } + if c.Mode != "" && c.Mode != "nicehash" && c.Mode != "simple" { + return errors.New("mode is invalid") + } + if c.Workers != "" && + c.Workers != WorkersByRigID && + c.Workers != WorkersByUser && + c.Workers != WorkersByPass && + c.Workers != WorkersByPassword && + c.Workers != WorkersByAgent && + c.Workers != WorkersByIP && + c.Workers != WorkersDisabled { + return errors.New("workers mode is invalid") + } if len(c.Bind) == 0 { return errors.New("bind list is empty") } diff --git a/config_runtime_test.go b/config_runtime_test.go index 06732aa..fd78fde 100644 --- a/config_runtime_test.go +++ b/config_runtime_test.go @@ -20,16 +20,30 @@ func TestConfig_Validate_Good(t *testing.T) { func TestConfig_Validate_Bad(t *testing.T) { cfg := &Config{ + Mode: "bogus", 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 missing mode to be allowed, got %v", errorValue) + if errorValue := cfg.Validate(); errorValue == nil { + t.Fatal("expected invalid mode to fail validation") } } func TestConfig_Validate_Ugly(t *testing.T) { + cfg := &Config{ + Mode: "simple", + Workers: WorkersMode("bogus"), + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{Enabled: true}}, + } + + if errorValue := cfg.Validate(); errorValue == nil { + t.Fatal("expected invalid workers mode to fail validation") + } +} + +func TestConfig_Validate_EnabledPoolURL(t *testing.T) { cfg := &Config{ Mode: "simple", Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}},