|
16 | 16 | package remote_test |
17 | 17 |
|
18 | 18 | import ( |
19 | | - "context" |
20 | 19 | "fmt" |
| 20 | + "net" |
21 | 21 |
|
22 | 22 | "io" |
23 | | - "os/exec" |
24 | | - "strconv" |
25 | 23 | "strings" |
26 | 24 | "testing" |
27 | 25 |
|
28 | 26 | "github.com/stretchr/testify/assert" |
29 | 27 | "github.com/stretchr/testify/require" |
30 | 28 |
|
31 | | - "github.com/arduino/arduino-app-cli/cmd/feedback" |
32 | 29 | "github.com/arduino/arduino-app-cli/internal/testtools" |
33 | 30 | "github.com/arduino/arduino-app-cli/pkg/board/remote" |
34 | 31 | "github.com/arduino/arduino-app-cli/pkg/board/remote/adb" |
@@ -123,7 +120,7 @@ func TestRemoteFS(t *testing.T) { |
123 | 120 | } |
124 | 121 | } |
125 | 122 |
|
126 | | -func TestSSHShell(t *testing.T) { |
| 123 | +func TestRemoteShell(t *testing.T) { |
127 | 124 | name, adbPort, sshPort := testtools.StartAdbDContainer(t) |
128 | 125 | t.Cleanup(func() { testtools.StopAdbDContainer(t, name) }) |
129 | 126 |
|
@@ -187,83 +184,66 @@ func TestSSHShell(t *testing.T) { |
187 | 184 |
|
188 | 185 | } |
189 | 186 |
|
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) |
192 | 189 | t.Cleanup(func() { testtools.StopAdbDContainer(t, name) }) |
193 | 190 |
|
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 | + } |
212 | 222 |
|
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) |
215 | 227 |
|
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) |
223 | 230 |
|
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) |
227 | 233 |
|
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])) |
230 | 238 |
|
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) |
234 | 241 |
|
235 | | - forwardPort, err := ports.GetAvailable() |
236 | | - require.NoError(t, err) |
| 242 | + err = remote.conn.ForwardKillAll(t.Context()) |
| 243 | + assert.NoError(t, err) |
237 | 244 |
|
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 | + } |
269 | 249 | } |
0 commit comments