-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshell_transfer_test.go
More file actions
252 lines (230 loc) · 6 KB
/
Copy pathshell_transfer_test.go
File metadata and controls
252 lines (230 loc) · 6 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package sshpass
import (
"bytes"
"testing"
)
func TestParseEchoedCommand(t *testing.T) {
tests := []struct {
name string
buf string
pending string
wantCmd string
wantArgs []string
}{
{
name: "sz with prompt",
buf: "\r\n[root@host ~]# sz CLAUDE.md\r\n",
pending: "sz",
wantCmd: "sz",
wantArgs: []string{"CLAUDE.md"},
},
{
name: "rz with prompt",
buf: "\r\n[root@host ~]# rz\r\n",
pending: "rz",
wantCmd: "rz",
wantArgs: []string{},
},
{
name: "sz with local path",
buf: "[root@host ~]# sz /remote/file /local/file\r\n",
pending: "sz",
wantCmd: "sz",
wantArgs: []string{"/remote/file", "/local/file"},
},
{
name: "rz with arg",
buf: "[root@host ~]# rz /path/to/file\r\n",
pending: "rz",
wantCmd: "rz",
wantArgs: []string{"/path/to/file"},
},
{
name: "no command found",
buf: "some random output\r\n",
pending: "sz",
wantCmd: "",
wantArgs: nil,
},
{
name: "tab completed sz",
buf: "sz CLAUDE.md\r\n",
pending: "sz",
wantCmd: "sz",
wantArgs: []string{"CLAUDE.md"},
},
{
name: "rzbackup not matched",
buf: "[root@host ~]# rzbackup\r\n",
pending: "rz",
wantCmd: "",
wantArgs: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd, args := parseEchoedCommand([]byte(tt.buf), tt.pending)
if cmd != tt.wantCmd {
t.Errorf("cmd = %q, want %q", cmd, tt.wantCmd)
}
if len(args) != len(tt.wantArgs) {
t.Errorf("args = %v, want %v", args, tt.wantArgs)
return
}
for i, a := range args {
if a != tt.wantArgs[i] {
t.Errorf("args[%d] = %q, want %q", i, a, tt.wantArgs[i])
}
}
})
}
}
func TestExtractCommandFromNotFound(t *testing.T) {
tests := []struct {
name string
buf string
wantCmd string
wantArgs []string
}{
{
name: "sz command bash",
buf: "[root@host ~]# sz CLAUDE.md\r\n-bash: sz: command not found\r\n",
wantCmd: "sz",
wantArgs: []string{"CLAUDE.md"},
},
{
name: "rz command bash",
buf: "[root@host ~]# rz\r\n-bash: rz: command not found\r\n",
wantCmd: "rz",
wantArgs: []string{},
},
{
name: "rz no args",
buf: "rz\r\n-bash: rz: command not found\r\n",
wantCmd: "rz",
wantArgs: []string{},
},
{
name: "sz with tab completion",
buf: "[root@host ~]# sz CLAUDE.md\r\n-bash: sz: command not found\r\n",
wantCmd: "sz",
wantArgs: []string{"CLAUDE.md"},
},
{
name: "ls not found - should not trigger",
buf: "[root@host ~]# nonexistent_cmd\r\n-bash: nonexistent_cmd: command not found\r\n",
wantCmd: "",
wantArgs: nil,
},
{
name: "zsh format",
buf: "[host]# sz /path/file\r\nzsh: command not found: sz\r\n",
wantCmd: "sz",
wantArgs: []string{"/path/file"},
},
{
name: "old sz in buffer should not false-positive",
buf: "[root@host ~]# ls\r\nfile_sz.txt\r\n[root@host ~]# rz\r\n-bash: rz: command not found\r\n",
wantCmd: "rz",
wantArgs: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd, args := extractCommandFromNotFound([]byte(tt.buf))
if cmd != tt.wantCmd {
t.Errorf("cmd = %q, want %q", cmd, tt.wantCmd)
}
if len(args) != len(tt.wantArgs) {
t.Errorf("args = %v, want %v", args, tt.wantArgs)
return
}
for i, a := range args {
if a != tt.wantArgs[i] {
t.Errorf("args[%d] = %q, want %q", i, a, tt.wantArgs[i])
}
}
})
}
}
func TestContainsNotFound(t *testing.T) {
tests := []struct {
input string
want bool
}{
{"-bash: rz: command not found\r\n", true},
{"zsh: command not found: rz\r\n", true},
{"sh: rz: not found\r\n", true},
{"total 0\r\n", false},
{"drwxr-xr-x 2 root root 4096 Jun 23 10:00 .\r\n", false},
}
for _, tt := range tests {
if got := containsNotFound([]byte(tt.input)); got != tt.want {
t.Errorf("containsNotFound(%q) = %v, want %v", tt.input, got, tt.want)
}
}
}
func TestOutputWriterTriggersSZ(t *testing.T) {
monitor := &rzszMonitor{}
var outBuf bytes.Buffer
writer := &outputWriter{monitor: monitor, out: &outBuf}
var szRemote string
var szCalled bool
monitor.onSZ = func(remote, local string) {
szCalled = true
szRemote = remote
}
// Simulate remote echoing the command
writer.Write([]byte("sz CLAUDE.md\r\n"))
// Simulate "command not found" error
writer.Write([]byte("-bash: sz: command not found\r\n"))
if !szCalled {
t.Fatal("onSZ not called")
}
if szRemote != "CLAUDE.md" {
t.Errorf("onSZ remote = %q, want %q", szRemote, "CLAUDE.md")
}
// Verify output was passed through
if !bytes.Contains(outBuf.Bytes(), []byte("sz CLAUDE.md")) {
t.Errorf("output missing echoed command")
}
if !bytes.Contains(outBuf.Bytes(), []byte("command not found")) {
t.Errorf("output missing error message")
}
}
func TestOutputWriterTriggersRZ(t *testing.T) {
monitor := &rzszMonitor{}
var outBuf bytes.Buffer
writer := &outputWriter{monitor: monitor, out: &outBuf}
var rzCalled bool
monitor.onRZ = func(path string) {
rzCalled = true
}
writer.Write([]byte("rz\r\n"))
writer.Write([]byte("-bash: rz: command not found\r\n"))
if !rzCalled {
t.Fatal("onRZ not called")
}
}
func TestOutputWriterNoTriggerForOtherCommands(t *testing.T) {
monitor := &rzszMonitor{}
var outBuf bytes.Buffer
writer := &outputWriter{monitor: monitor, out: &outBuf}
var called bool
monitor.onRZ = func(path string) { called = true }
monitor.onSZ = func(remote, local string) { called = true }
writer.Write([]byte("ls\r\n"))
writer.Write([]byte("-bash: ls: command not found\r\n"))
if called {
t.Error("handler should not be called for ls")
}
}
func TestOutputWriterPassthrough(t *testing.T) {
monitor := &rzszMonitor{}
var outBuf bytes.Buffer
writer := &outputWriter{monitor: monitor, out: &outBuf}
writer.Write([]byte("ls -la\r\ntotal 0\r\n"))
if outBuf.String() != "ls -la\r\ntotal 0\r\n" {
t.Errorf("Output = %q, want %q", outBuf.String(), "ls -la\r\ntotal 0\r\n")
}
}