From bfa63499c3dfff34af48211c1033fe9844197c2c Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Mon, 9 Feb 2026 19:44:54 -0600 Subject: [PATCH] add basic tests for bad module creation --- internal/module/test/http-client_test.go | 41 +++++++++ internal/module/test/http-server_test.go | 52 +++++++++++ internal/module/test/midi-input_test.go | 52 +++++++++++ internal/module/test/midi-output_test.go | 52 +++++++++++ internal/module/test/mqtt-client_test.go | 93 ++++++++++++++++++++ internal/module/test/nats-client_test.go | 72 +++++++++++++++ internal/module/test/psn-client_test.go | 41 +++++++++ internal/module/test/serial-client_test.go | 80 +++++++++++++++++ internal/module/test/sip-call-server_test.go | 41 +++++++++ internal/module/test/sip-dtmf-server_test.go | 41 +++++++++ internal/module/test/tcp-client_test.go | 72 +++++++++++++++ internal/module/test/tcp-server_test.go | 80 +++++++++++++++++ internal/module/test/time-interval_test.go | 54 ++++++++++++ internal/module/test/time-timer_test.go | 54 ++++++++++++ internal/module/test/udp-client_test.go | 72 +++++++++++++++ internal/module/test/udp-multicast_test.go | 72 +++++++++++++++ internal/module/test/udp-server_test.go | 54 ++++++++++++ 17 files changed, 1023 insertions(+) diff --git a/internal/module/test/http-client_test.go b/internal/module/test/http-client_test.go index 457ae43..e2661cf 100644 --- a/internal/module/test/http-client_test.go +++ b/internal/module/test/http-client_test.go @@ -30,3 +30,44 @@ func TestHTTPClientFromRegistry(t *testing.T) { t.Fatalf("http.client module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadHTTPClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["http.client"] + if !ok { + t.Fatalf("http.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "http.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("http.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("http.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("http.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/http-server_test.go b/internal/module/test/http-server_test.go index d0a7f94..b61284b 100644 --- a/internal/module/test/http-server_test.go +++ b/internal/module/test/http-server_test.go @@ -33,3 +33,55 @@ func TestHTTPServerFromRegistry(t *testing.T) { t.Fatalf("http.server module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadHTTPServer(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{}, + errorString: "http.server requires a port parameter", + }, + { + name: "non-numeric port", + params: map[string]any{"port": "3000"}, + errorString: "http.server port must be a number", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["http.server"] + if !ok { + t.Fatalf("http.server module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "http.server", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("http.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("http.server expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("http.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/midi-input_test.go b/internal/module/test/midi-input_test.go index 96b4dc0..8f6620e 100644 --- a/internal/module/test/midi-input_test.go +++ b/internal/module/test/midi-input_test.go @@ -33,3 +33,55 @@ func TestMIDIInputFromRegistry(t *testing.T) { t.Fatalf("midi.input module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadMIDIInput(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{}, + errorString: "midi.input requires a port parameter", + }, + { + name: "non-string port", + params: map[string]any{"port": 123}, + errorString: "midi.input port must be a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["midi.input"] + if !ok { + t.Fatalf("midi.input module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "midi.input", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("midi.input got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("midi.input expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("midi.input got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/midi-output_test.go b/internal/module/test/midi-output_test.go index 4209d41..9751be2 100644 --- a/internal/module/test/midi-output_test.go +++ b/internal/module/test/midi-output_test.go @@ -33,3 +33,55 @@ func TestMIDIOutputFromRegistry(t *testing.T) { t.Fatalf("midi.output module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadMIDIOutput(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{}, + errorString: "midi.output requires a port parameter", + }, + { + name: "non-string port", + params: map[string]any{"port": 123}, + errorString: "midi.output port must be a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["midi.output"] + if !ok { + t.Fatalf("midi.output module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "midi.output", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("midi.output got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("midi.output expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("midi.output got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/mqtt-client_test.go b/internal/module/test/mqtt-client_test.go index c123be6..21057b1 100644 --- a/internal/module/test/mqtt-client_test.go +++ b/internal/module/test/mqtt-client_test.go @@ -35,3 +35,96 @@ func TestMQTTClientFromRegistry(t *testing.T) { t.Fatalf("mqtt.client module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadMQTTClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no broker param", + params: map[string]any{ + "topic": "test/topic", + "clientId": "test", + }, + errorString: "mqtt.client requires a broker parameter", + }, + { + name: "non-string broker", + params: map[string]any{ + "broker": 123, + "topic": "test/topic", + "clientId": "test", + }, + errorString: "mqtt.client broker must be a string", + }, + { + name: "no topic param", + params: map[string]any{ + "broker": "mqtt://localhost:1883", + "clientId": "test", + }, + errorString: "mqtt.client requires a topic parameter", + }, + { + name: "non-string topic", + params: map[string]any{ + "broker": "mqtt://localhost:1883", + "topic": 123, + "clientId": "test", + }, + errorString: "mqtt.client topic must be a string", + }, + { + name: "no clientId param", + params: map[string]any{ + "broker": "mqtt://localhost:1883", + "topic": "test/topic", + }, + errorString: "mqtt.client requires a clientId parameter", + }, + { + name: "non-string clientId", + params: map[string]any{ + "broker": "mqtt://localhost:1883", + "topic": "test/topic", + "clientId": 123, + }, + errorString: "mqtt.client clientId must be a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["mqtt.client"] + if !ok { + t.Fatalf("mqtt.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "mqtt.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("mqtt.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("mqtt.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("mqtt.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/nats-client_test.go b/internal/module/test/nats-client_test.go index 0ef893c..0c5e09e 100644 --- a/internal/module/test/nats-client_test.go +++ b/internal/module/test/nats-client_test.go @@ -34,3 +34,75 @@ func TestNATSClientFromRegistry(t *testing.T) { t.Fatalf("nats.client module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadNATSClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no url param", + params: map[string]any{ + "subject": "test/subject", + }, + errorString: "nats.client requires a url parameter", + }, + { + name: "non-string url", + params: map[string]any{ + "url": 123, + "subject": "test/subject", + }, + errorString: "nats.client url must be a string", + }, + { + name: "no subject param", + params: map[string]any{ + "url": "nats://127.0.0.1:4222", + }, + errorString: "nats.client requires a subject parameter", + }, + { + name: "non-string subject", + params: map[string]any{ + "url": "nats://127.0.0.1:4222", + "subject": 123, + }, + errorString: "nats.client subject must be a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["nats.client"] + if !ok { + t.Fatalf("nats.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "nats.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("nats.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("nats.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("nats.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/psn-client_test.go b/internal/module/test/psn-client_test.go index 93cffeb..85f9ad3 100644 --- a/internal/module/test/psn-client_test.go +++ b/internal/module/test/psn-client_test.go @@ -30,3 +30,44 @@ func TestPSNClientFromRegistry(t *testing.T) { t.Fatalf("psn.client module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadPSNClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["psn.client"] + if !ok { + t.Fatalf("psn.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "psn.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("psn.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("psn.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("psn.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/serial-client_test.go b/internal/module/test/serial-client_test.go index 0625b4c..5ae312f 100644 --- a/internal/module/test/serial-client_test.go +++ b/internal/module/test/serial-client_test.go @@ -35,3 +35,83 @@ func TestSerialClientFromRegistry(t *testing.T) { t.Fatalf("serial.client module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadSerialClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{ + "framing": "LF", + }, + errorString: "serial.client requires a port parameter", + }, + { + name: "non-string port param", + params: map[string]any{ + "port": 8000, + "framing": "LF", + }, + errorString: "serial.client port must be a string", + }, + { + name: "no framing param", + params: map[string]any{ + "port": "/dev/ttyUSB0", + }, + errorString: "serial.client requires a framing parameter", + }, + { + name: "non-string framing param", + params: map[string]any{ + "port": "/dev/ttyUSB0", + "framing": 1, + }, + errorString: "serial.client framing method must be a string", + }, + { + name: "unkown framing method", + params: map[string]any{ + "port": "/dev/ttyUSB0", + "framing": "asdfasdfasdfasdflkj", + }, + errorString: "serial.client unknown framing method: asdfasdfasdfasdflkj", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["serial.client"] + if !ok { + t.Fatalf("serial.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "serial.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("serial.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("serial.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("serial.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/sip-call-server_test.go b/internal/module/test/sip-call-server_test.go index b988fbc..ffaf6d2 100644 --- a/internal/module/test/sip-call-server_test.go +++ b/internal/module/test/sip-call-server_test.go @@ -30,3 +30,44 @@ func TestSIPCallServerFromRegistry(t *testing.T) { t.Fatalf("sip.call.server module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadSIPCallServer(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["sip.call.server"] + if !ok { + t.Fatalf("sip.call.server module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "sip.call.server", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("sip.call.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("sip.call.server expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("sip.call.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/sip-dtmf-server_test.go b/internal/module/test/sip-dtmf-server_test.go index 274422d..3484cd4 100644 --- a/internal/module/test/sip-dtmf-server_test.go +++ b/internal/module/test/sip-dtmf-server_test.go @@ -33,3 +33,44 @@ func TestSIPDTMFServerFromRegistry(t *testing.T) { t.Fatalf("sip.dtmf.server module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadSIPDTMFServer(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["sip.dtmf.server"] + if !ok { + t.Fatalf("sip.dtmf.server module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "sip.dtmf.server", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("sip.dtmf.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("sip.dtmf.server expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("sip.dtmf.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/tcp-client_test.go b/internal/module/test/tcp-client_test.go index 34d0ff1..da0c65b 100644 --- a/internal/module/test/tcp-client_test.go +++ b/internal/module/test/tcp-client_test.go @@ -35,3 +35,75 @@ func TestTCPClientFromRegistry(t *testing.T) { t.Fatalf("net.tcp.client module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadTCPClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{ + "host": "localhost", + }, + errorString: "net.tcp.client requires a port parameter", + }, + { + name: "non-number port param", + params: map[string]any{ + "host": "localhost", + "port": "8000", + }, + errorString: "net.tcp.client port must be a number", + }, + { + name: "no host param", + params: map[string]any{ + "port": 8000.0, + }, + errorString: "net.tcp.client requires a host parameter", + }, + { + name: "non-string host param", + params: map[string]any{ + "host": 123, + "port": 8000.0, + }, + errorString: "net.tcp.client host must be a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["net.tcp.client"] + if !ok { + t.Fatalf("net.tcp.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "net.tcp.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("net.tcp.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("net.tcp.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("net.tcp.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/tcp-server_test.go b/internal/module/test/tcp-server_test.go index 29c2f89..313a244 100644 --- a/internal/module/test/tcp-server_test.go +++ b/internal/module/test/tcp-server_test.go @@ -34,3 +34,83 @@ func TestTCPServerFromRegistry(t *testing.T) { t.Fatalf("net.tcp.server module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadTCPServer(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{ + "framing": "LF", + }, + errorString: "net.tcp.server requires a port parameter", + }, + { + name: "non-number port param", + params: map[string]any{ + "port": "8000", + "framing": "LF", + }, + errorString: "net.tcp.server port must be a number", + }, + { + name: "no framing param", + params: map[string]any{ + "port": 8000.0, + }, + errorString: "net.tcp.server requires a framing parameter", + }, + { + name: "non-string framing param", + params: map[string]any{ + "port": 8000.0, + "framing": 1, + }, + errorString: "net.tcp.server framing method must be a string", + }, + { + name: "unkown framing method", + params: map[string]any{ + "port": 8000.0, + "framing": "asdfasdfasdfasdflkj", + }, + errorString: "net.tcp.server unknown framing method: asdfasdfasdfasdflkj", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["net.tcp.server"] + if !ok { + t.Fatalf("net.tcp.server module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "net.tcp.server", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("net.tcp.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("net.tcp.server expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("net.tcp.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/time-interval_test.go b/internal/module/test/time-interval_test.go index 4ee37f3..6bda95f 100644 --- a/internal/module/test/time-interval_test.go +++ b/internal/module/test/time-interval_test.go @@ -33,3 +33,57 @@ func TestTimeIntervalFromRegistry(t *testing.T) { t.Fatalf("time.interval module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadTimeInterval(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no duration param", + params: map[string]any{}, + errorString: "time.interval requires a duration parameter", + }, + { + name: "non-number duration param", + params: map[string]any{ + "duration": "8000", + }, + errorString: "time.interval duration must be a number", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["time.interval"] + if !ok { + t.Fatalf("time.interval module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "time.interval", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("time.interval got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("time.interval expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("time.interval got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/time-timer_test.go b/internal/module/test/time-timer_test.go index ada2231..ed6962f 100644 --- a/internal/module/test/time-timer_test.go +++ b/internal/module/test/time-timer_test.go @@ -33,3 +33,57 @@ func TestTimeTimerFromRegistry(t *testing.T) { t.Fatalf("time.timer module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadTimeTimer(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no duration param", + params: map[string]any{}, + errorString: "time.timer requires a duration parameter", + }, + { + name: "non-number duration param", + params: map[string]any{ + "duration": "8000", + }, + errorString: "time.timer duration must be a number", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["time.timer"] + if !ok { + t.Fatalf("time.timer module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "time.timer", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("time.timer got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("time.timer expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("time.timer got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/udp-client_test.go b/internal/module/test/udp-client_test.go index 1ef7160..e81a544 100644 --- a/internal/module/test/udp-client_test.go +++ b/internal/module/test/udp-client_test.go @@ -35,3 +35,75 @@ func TestUDPClientFromRegistry(t *testing.T) { t.Fatalf("net.udp.client module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadUDPClient(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{ + "host": "localhost", + }, + errorString: "net.udp.client requires a port parameter", + }, + { + name: "non-number port param", + params: map[string]any{ + "host": "localhost", + "port": "8000", + }, + errorString: "net.udp.client port must be a number", + }, + { + name: "no host param", + params: map[string]any{ + "port": 8000.0, + }, + errorString: "net.udp.client requires a host parameter", + }, + { + name: "non-string host param", + params: map[string]any{ + "host": 123, + "port": 8000.0, + }, + errorString: "net.udp.client host must be a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["net.udp.client"] + if !ok { + t.Fatalf("net.udp.client module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "net.udp.client", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("net.udp.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("net.udp.client expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("net.udp.client got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/udp-multicast_test.go b/internal/module/test/udp-multicast_test.go index 52f418b..937a018 100644 --- a/internal/module/test/udp-multicast_test.go +++ b/internal/module/test/udp-multicast_test.go @@ -34,3 +34,75 @@ func TestUDPMulticastFromRegistry(t *testing.T) { t.Fatalf("net.udp.multicast module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadUDPMulticast(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{ + "ip": "localhost", + }, + errorString: "net.udp.multicast requires a port parameter", + }, + { + name: "non-number port param", + params: map[string]any{ + "ip": "localhost", + "port": "8000", + }, + errorString: "net.udp.multicast port must be a number", + }, + { + name: "no ip param", + params: map[string]any{ + "port": 8000.0, + }, + errorString: "net.udp.multicast requires an ip parameter", + }, + { + name: "non-string ip param", + params: map[string]any{ + "ip": 123, + "port": 8000.0, + }, + errorString: "net.udp.multicast ip must be a string", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["net.udp.multicast"] + if !ok { + t.Fatalf("net.udp.multicast module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "net.udp.multicast", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("net.udp.multicast got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("net.udp.multicast expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("net.udp.multicast got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/module/test/udp-server_test.go b/internal/module/test/udp-server_test.go index e7ebff7..aa70371 100644 --- a/internal/module/test/udp-server_test.go +++ b/internal/module/test/udp-server_test.go @@ -33,3 +33,57 @@ func TestUDPServerFromRegistry(t *testing.T) { t.Fatalf("net.udp.server module has wrong type: %s", moduleInstance.Type()) } } + +func TestBadUDPServer(t *testing.T) { + tests := []struct { + name string + params map[string]any + errorString string + }{ + { + name: "no port param", + params: map[string]any{}, + errorString: "net.udp.server requires a port parameter", + }, + { + name: "non-number port param", + params: map[string]any{ + "port": "8000", + }, + errorString: "net.udp.server port must be a number", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + + registration, ok := module.ModuleRegistry["net.udp.server"] + if !ok { + t.Fatalf("net.udp.server module not registered") + } + + moduleInstance, err := registration.New(config.ModuleConfig{ + Id: "test", + Type: "net.udp.server", + Params: test.params, + }) + + if err != nil { + if test.errorString != err.Error() { + t.Fatalf("net.udp.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + err = moduleInstance.Start(t.Context()) + + if err == nil { + t.Fatalf("net.udp.server expected to fail") + } + + if err.Error() != test.errorString { + t.Fatalf("net.udp.server got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +}