Repository navigation
Expand file tree
/
Copy pathshell_control_test.go
More file actions
76 lines (60 loc) · 1.96 KB
/
Copy pathshell_control_test.go
File metadata and controls
76 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sshlib
import (
"bytes"
"errors"
"io"
"net"
"testing"
"golang.org/x/crypto/ssh"
)
func TestCopyControlOutputReturnsUnexpectedEOF(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
go func() {
defer server.Close()
}()
var stdout bytes.Buffer
var stderr bytes.Buffer
err := (&Connect{}).copyControlOutput(client, &stdout, &stderr, nil)
if !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("copyControlOutput() error = %v, want %v", err, io.ErrUnexpectedEOF)
}
}
func TestCopyControlOutputReadsExitFrame(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
go func() {
defer server.Close()
if err := writeStreamFrame(server, streamFrameStdout, []byte("shared")); err != nil {
t.Errorf("write stdout frame: %v", err)
return
}
if err := writeStreamFrame(server, streamFrameExit, encodeExitStatus(0)); err != nil {
t.Errorf("write exit frame: %v", err)
}
}()
var stdout bytes.Buffer
var stderr bytes.Buffer
if err := (&Connect{}).copyControlOutput(client, &stdout, &stderr, nil); err != nil {
t.Fatalf("copyControlOutput() error = %v", err)
}
if got := stdout.String(); got != "shared" {
t.Fatalf("stdout = %q, want %q", got, "shared")
}
}
func TestShouldSuppressControlStreamErrorForInteractiveRequests(t *testing.T) {
exitErr := &ssh.ExitError{}
exitMissingErr := &ssh.ExitMissingError{}
if !shouldSuppressControlStreamError(controlRequest{Type: controlRequestShell}, exitErr) {
t.Fatal("shell exit error should be suppressed")
}
if !shouldSuppressControlStreamError(controlRequest{Type: controlRequestCmdShell}, exitErr) {
t.Fatal("cmdshell exit error should be suppressed")
}
if shouldSuppressControlStreamError(controlRequest{Type: controlRequestCommand}, exitErr) {
t.Fatal("command exit error should not be suppressed")
}
if !shouldSuppressControlStreamError(controlRequest{Type: controlRequestCmdShell}, exitMissingErr) {
t.Fatal("cmdshell exit missing error should be suppressed")
}
}