From e17909b1428fda294cab112cfe7bc85ebc9f54ae Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 22 Feb 2023 10:21:56 +0100 Subject: [PATCH 1/2] Added test for #32 --- discovery_test.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/discovery_test.go b/discovery_test.go index 9dbfe86..dbd6bc0 100644 --- a/discovery_test.go +++ b/discovery_test.go @@ -41,13 +41,29 @@ func TestDisc(t *testing.T) { require.NoError(t, discovery.Start()) - n, err := stdin.Write([]byte("quit\n")) - require.NoError(t, err) - require.Greater(t, n, 0) - output := [1024]byte{} - n, err = stdout.Read(output[:]) - require.Greater(t, n, 0) - require.NoError(t, err) + { + // Check that discovery is able to handle an "hello" without parameters gracefully + // https://github.com/arduino/pluggable-discovery-protocol-handler/issues/32 + inN, err := stdin.Write([]byte("hello\n")) + require.NoError(t, err) + require.Greater(t, inN, 0) + + output := [1024]byte{} + outN, err := stdout.Read(output[:]) + require.Greater(t, outN, 0) + require.NoError(t, err) + require.Equal(t, "{\n \"eventType\": \"hello\",\n \"message\": \"Invalid HELLO command\",\n \"error\": true\n}\n", string(output[:outN])) + } + + { + inN, err := stdin.Write([]byte("quit\n")) + require.NoError(t, err) + require.Greater(t, inN, 0) - require.Equal(t, "{\n \"eventType\": \"quit\",\n \"message\": \"OK\"\n}\n", string(output[:n])) + output := [1024]byte{} + outN, err := stdout.Read(output[:]) + require.Greater(t, outN, 0) + require.NoError(t, err) + require.Equal(t, "{\n \"eventType\": \"quit\",\n \"message\": \"OK\"\n}\n", string(output[:outN])) + } } From 71b0ce2e26dcb23e1683419a498e0edc4c82fedf Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 22 Feb 2023 10:22:20 +0100 Subject: [PATCH 2/2] Fixed 'hello' command handler to gracefully handle empty command Fix #32 --- discovery.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/discovery.go b/discovery.go index db9265b..54f064c 100644 --- a/discovery.go +++ b/discovery.go @@ -132,7 +132,11 @@ func (d *Server) Run(in io.Reader, out io.Writer) error { switch cmd { case "HELLO": - d.hello(fullCmd[6:]) + if len(fullCmd) < 7 { + d.hello("") + } else { + d.hello(fullCmd[6:]) + } case "START": d.start() case "LIST":