Skip to content

Commit

Permalink
Merge pull request #33 from arduino/fix_hello_handling
Browse files Browse the repository at this point in the history
Fix `HELLO` command handler for empty command input
  • Loading branch information
cmaglie authored Feb 22, 2023
2 parents 69bcf9a + 71b0ce2 commit 64ac0f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
6 changes: 5 additions & 1 deletion discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
32 changes: 24 additions & 8 deletions discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
}

0 comments on commit 64ac0f7

Please sign in to comment.