fix(config): validate mode and workers enums

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 17:14:54 +00:00
parent 3535e4b006
commit 9e997554fa
2 changed files with 29 additions and 2 deletions

View file

@ -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")
}

View file

@ -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}},