Skip to content

Commit f020963

Browse files
authored
fix(pkg/board/remote): adb kill all port use wrong command (#147)
* fix(pkg/board/remote): adb kill all port use wrong command * add adb forward test * embed the script in then dockerfile * Revert "embed the script in then dockerfile" This reverts commit b7481d3.
1 parent 70b5caa commit f020963

File tree

4 files changed

+67
-78
lines changed

4 files changed

+67
-78
lines changed

adbd.Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM debian:trixie
22

33
RUN apt-get update \
4-
&& apt-get install -y --no-install-recommends adbd file ssh sudo \
4+
&& apt-get install -y --no-install-recommends adbd file ssh sudo netcat-traditional \
55
&& apt-get clean \
66
&& rm -rf /var/lib/apt/lists/*
77

@@ -10,7 +10,9 @@ RUN useradd -m --create-home --shell /bin/bash --user-group --groups sudo arduin
1010
mkdir /home/arduino/ArduinoApps && \
1111
chown -R arduino:arduino /home/arduino/ArduinoApps
1212

13+
ADD scripts/pong-server.sh /usr/local/bin/pong-server.sh
14+
1315
WORKDIR /home/arduino
1416
EXPOSE 22
1517

16-
CMD ["/bin/sh", "-c", "/usr/sbin/sshd -D & su arduino -c adbd"]
18+
CMD ["/bin/sh", "-c", "/usr/sbin/sshd -D & su arduino -c adbd & pong-server.sh"]

pkg/board/remote/adb/adb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (a *ADBConnection) Forward(ctx context.Context, localPort int, remotePort i
132132
}
133133

134134
func (a *ADBConnection) ForwardKillAll(ctx context.Context) error {
135-
cmd, err := paths.NewProcess(nil, a.adbPath, "-s", a.host, "killforward-all")
135+
cmd, err := paths.NewProcess(nil, a.adbPath, "-s", a.host, "forward", "--remove-all")
136136
if err != nil {
137137
return err
138138
}

pkg/board/remote/remote_test.go

Lines changed: 55 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@
1616
package remote_test
1717

1818
import (
19-
"context"
2019
"fmt"
20+
"net"
2121

2222
"io"
23-
"os/exec"
24-
"strconv"
2523
"strings"
2624
"testing"
2725

2826
"github.com/stretchr/testify/assert"
2927
"github.com/stretchr/testify/require"
3028

31-
"github.com/arduino/arduino-app-cli/cmd/feedback"
3229
"github.com/arduino/arduino-app-cli/internal/testtools"
3330
"github.com/arduino/arduino-app-cli/pkg/board/remote"
3431
"github.com/arduino/arduino-app-cli/pkg/board/remote/adb"
@@ -123,7 +120,7 @@ func TestRemoteFS(t *testing.T) {
123120
}
124121
}
125122

126-
func TestSSHShell(t *testing.T) {
123+
func TestRemoteShell(t *testing.T) {
127124
name, adbPort, sshPort := testtools.StartAdbDContainer(t)
128125
t.Cleanup(func() { testtools.StopAdbDContainer(t, name) })
129126

@@ -187,83 +184,66 @@ func TestSSHShell(t *testing.T) {
187184

188185
}
189186

190-
func TestSSHForwarder(t *testing.T) {
191-
name, _, sshPort := testtools.StartAdbDContainer(t)
187+
func TestRemoteForwarder(t *testing.T) {
188+
name, adbPort, sshPort := testtools.StartAdbDContainer(t)
192189
t.Cleanup(func() { testtools.StopAdbDContainer(t, name) })
193190

194-
conn, err := ssh.FromHost("arduino", "arduino", fmt.Sprintf("%s:%s", "localhost", sshPort))
195-
require.NoError(t, err)
196-
197-
t.Run("Forward ADB", func(t *testing.T) {
198-
ctx, cancel := context.WithCancel(t.Context())
199-
defer cancel()
200-
201-
forwardPort, err := ports.GetAvailable()
202-
require.NoError(t, err)
203-
204-
err = conn.Forward(ctx, forwardPort, 5555)
205-
if err != nil {
206-
t.Errorf("Forward failed: %v", err)
207-
}
208-
if forwardPort <= 0 || forwardPort > 65535 {
209-
t.Fatalf("invalid port: %d", forwardPort)
210-
}
211-
adb_forwarded_endpoint := fmt.Sprintf("localhost:%s", strconv.Itoa(forwardPort))
191+
const pongServerPort = 9999
192+
193+
remotes := []struct {
194+
name string
195+
conn remote.Forwarder
196+
forwardPort int
197+
}{
198+
{
199+
name: "adb",
200+
conn: func() remote.Forwarder {
201+
conn, err := adb.FromHost("localhost:"+adbPort, "")
202+
require.NoError(t, err)
203+
return conn
204+
}(),
205+
forwardPort: func() int {
206+
port, err := ports.GetAvailable()
207+
require.NoError(t, err)
208+
return port
209+
}(),
210+
},
211+
{
212+
name: "ssh",
213+
conn: func() remote.Forwarder {
214+
conn, err := ssh.FromHost("arduino", "arduino", "127.0.0.1:"+sshPort)
215+
require.NoError(t, err)
216+
return conn
217+
}(),
218+
},
219+
220+
// We are skipping the local forwarder test, which is just an no op in this case.
221+
}
212222

213-
out, err := exec.Command("adb", "connect", adb_forwarded_endpoint).CombinedOutput()
214-
require.NoError(t, err, "adb connect output: %q", out)
223+
for _, remote := range remotes {
224+
t.Run(remote.name, func(t *testing.T) {
225+
forwardPort, err := ports.GetAvailable()
226+
require.NoError(t, err)
215227

216-
cmd := exec.Command("adb", "-s", adb_forwarded_endpoint, "shell", "echo", "Hello, World!")
217-
out, err = cmd.CombinedOutput()
218-
require.NoError(t, err, "command output: %q", out)
219-
feedback.Printf("Command output:\n%s\n", string(out))
220-
require.NotNil(t, string(out))
221-
})
222-
}
228+
err = remote.conn.Forward(t.Context(), forwardPort, pongServerPort)
229+
assert.NoError(t, err)
223230

224-
func TestSSHKillForwarder(t *testing.T) {
225-
name, _, sshPort := testtools.StartAdbDContainer(t)
226-
t.Cleanup(func() { testtools.StopAdbDContainer(t, name) })
231+
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", forwardPort))
232+
require.NoError(t, err)
227233

228-
conn, err := ssh.FromHost("arduino", "arduino", fmt.Sprintf("%s:%s", "localhost", sshPort))
229-
require.NoError(t, err)
234+
buf := [128]byte{}
235+
n, err := conn.Read(buf[:])
236+
require.NoError(t, err)
237+
require.Equal(t, "pong", string(buf[:n]))
230238

231-
t.Run("KillAllForwards", func(t *testing.T) {
232-
ctx, cancel := context.WithCancel(t.Context())
233-
defer cancel()
239+
err = conn.Close()
240+
require.NoError(t, err)
234241

235-
forwardPort, err := ports.GetAvailable()
236-
require.NoError(t, err)
242+
err = remote.conn.ForwardKillAll(t.Context())
243+
assert.NoError(t, err)
237244

238-
err = conn.Forward(ctx, forwardPort, 5555)
239-
if err != nil {
240-
t.Errorf("Forward failed: %v", err)
241-
}
242-
if forwardPort <= 0 || forwardPort > 65535 {
243-
t.Fatalf("invalid port: %d", forwardPort)
244-
}
245-
adb_forwarded_endpoint := fmt.Sprintf("localhost:%s", strconv.Itoa(forwardPort))
246-
247-
out, err := exec.Command("adb", "connect", adb_forwarded_endpoint).CombinedOutput()
248-
require.NoError(t, err, "adb connect output: %q", out)
249-
250-
cmd := exec.Command("adb", "-s", adb_forwarded_endpoint, "shell", "echo", "Hello, World!")
251-
out, err = cmd.CombinedOutput()
252-
require.NoError(t, err, "command output: %q", out)
253-
feedback.Printf("Command output:\n%s\n", string(out))
254-
require.NotNil(t, string(out))
255-
256-
err = conn.ForwardKillAll(t.Context())
257-
require.NoError(t, err)
258-
out, err = exec.Command("adb", "disconnect", adb_forwarded_endpoint).CombinedOutput()
259-
require.NoError(t, err, "adb disconnect output: %q", out)
260-
261-
out, err = exec.Command("adb", "connect", adb_forwarded_endpoint).CombinedOutput()
262-
require.NoError(t, err, "adb connect output: %q", out)
263-
264-
cmd = exec.Command("adb", "-s", adb_forwarded_endpoint, "shell", "echo", "Hello, World!")
265-
out, err = cmd.CombinedOutput()
266-
require.Error(t, err, "command output: %q", out)
267-
feedback.Printf("Command output:\n%s\n", string(out))
268-
})
245+
_, err = net.Dial("tcp", fmt.Sprintf("localhost:%d", forwardPort))
246+
require.Error(t, err)
247+
})
248+
}
269249
}

scripts/pong-server.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
PORT=9999
4+
5+
while true; do
6+
echo -n "pong" | nc -l -p $PORT
7+
done

0 commit comments

Comments
 (0)