Skip to content

Commit 64ac0f7

Browse files
authored
Merge pull request #33 from arduino/fix_hello_handling
Fix `HELLO` command handler for empty command input
2 parents 69bcf9a + 71b0ce2 commit 64ac0f7

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

discovery.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ func (d *Server) Run(in io.Reader, out io.Writer) error {
132132

133133
switch cmd {
134134
case "HELLO":
135-
d.hello(fullCmd[6:])
135+
if len(fullCmd) < 7 {
136+
d.hello("")
137+
} else {
138+
d.hello(fullCmd[6:])
139+
}
136140
case "START":
137141
d.start()
138142
case "LIST":

discovery_test.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,29 @@ func TestDisc(t *testing.T) {
4141

4242
require.NoError(t, discovery.Start())
4343

44-
n, err := stdin.Write([]byte("quit\n"))
45-
require.NoError(t, err)
46-
require.Greater(t, n, 0)
47-
output := [1024]byte{}
48-
n, err = stdout.Read(output[:])
49-
require.Greater(t, n, 0)
50-
require.NoError(t, err)
44+
{
45+
// Check that discovery is able to handle an "hello" without parameters gracefully
46+
// https://github.com/arduino/pluggable-discovery-protocol-handler/issues/32
47+
inN, err := stdin.Write([]byte("hello\n"))
48+
require.NoError(t, err)
49+
require.Greater(t, inN, 0)
50+
51+
output := [1024]byte{}
52+
outN, err := stdout.Read(output[:])
53+
require.Greater(t, outN, 0)
54+
require.NoError(t, err)
55+
require.Equal(t, "{\n \"eventType\": \"hello\",\n \"message\": \"Invalid HELLO command\",\n \"error\": true\n}\n", string(output[:outN]))
56+
}
57+
58+
{
59+
inN, err := stdin.Write([]byte("quit\n"))
60+
require.NoError(t, err)
61+
require.Greater(t, inN, 0)
5162

52-
require.Equal(t, "{\n \"eventType\": \"quit\",\n \"message\": \"OK\"\n}\n", string(output[:n]))
63+
output := [1024]byte{}
64+
outN, err := stdout.Read(output[:])
65+
require.Greater(t, outN, 0)
66+
require.NoError(t, err)
67+
require.Equal(t, "{\n \"eventType\": \"quit\",\n \"message\": \"OK\"\n}\n", string(output[:outN]))
68+
}
5369
}

0 commit comments

Comments
 (0)