-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshell_transfer_test.go
More file actions
348 lines (315 loc) · 8.73 KB
/
Copy pathshell_transfer_test.go
File metadata and controls
348 lines (315 loc) · 8.73 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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")
}
}
func TestContainsNotFoundChinese(t *testing.T) {
// Chinese locale "未找到命令" should be detected.
if !containsNotFound([]byte("-bash: rz: 未找到命令\r\n")) {
t.Error("expected '未找到命令' to be detected as not-found")
}
}
func TestContainsNotFoundNoSuchFile(t *testing.T) {
// "No such file or directory" should be detected (covers sh fallback).
if !containsNotFound([]byte("sh: rz: No such file or directory\r\n")) {
t.Error("expected 'No such file or directory' to be detected")
}
}
func TestExtractCommandFromNotFoundShFormat(t *testing.T) {
// sh format: "sh: rz: not found"
cmd, args := extractCommandFromNotFound([]byte("sh: rz: not found\r\n"))
if cmd != "rz" {
t.Errorf("cmd = %q, want rz", cmd)
}
if len(args) != 0 {
t.Errorf("args = %v, want empty", args)
}
}
func TestExtractCommandFromNotFoundNotInBuffer(t *testing.T) {
// "not found" present but no rz/sz command.
cmd, _ := extractCommandFromNotFound([]byte("-bash: ls: command not found\r\n"))
if cmd != "" {
t.Errorf("cmd = %q, want empty (ls is not rz/sz)", cmd)
}
}
func TestReadLineFromStdin(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"simple", "hello\n", "hello"},
{"carriage return", "world\r\n", "world"},
{"empty", "\n", ""},
{"no newline", "noline", "noline"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := bytes.NewBufferString(tt.input)
got := readLineFromStdin(r)
if got != tt.want {
t.Errorf("readLineFromStdin(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
func TestOutputWriterHandledFlagResets(t *testing.T) {
// After handling rz, the Handled flag should reset to false, allowing a
// subsequent rz/sz to be handled again.
monitor := &rzszMonitor{}
var outBuf bytes.Buffer
writer := &outputWriter{monitor: monitor, out: &outBuf}
callCount := 0
monitor.onRZ = func(path string) { callCount++ }
// First rz.
writer.Write([]byte("rz\r\n"))
writer.Write([]byte("-bash: rz: command not found\r\n"))
if callCount != 1 {
t.Fatalf("expected 1 call after first rz, got %d", callCount)
}
// Second rz should also trigger (Handled was reset).
writer.Write([]byte("rz\r\n"))
writer.Write([]byte("-bash: rz: command not found\r\n"))
if callCount != 2 {
t.Fatalf("expected 2 calls after second rz, got %d", callCount)
}
}
func TestOutputWriterLargeOutputDoesNotPanic(t *testing.T) {
// Writing a large chunk (> 4096 rolling buffer) should not panic.
monitor := &rzszMonitor{}
var outBuf bytes.Buffer
writer := &outputWriter{monitor: monitor, out: &outBuf}
large := make([]byte, 8192)
for i := range large {
large[i] = 'x'
}
writer.Write(large)
if outBuf.Len() != 8192 {
t.Errorf("output len = %d, want 8192", outBuf.Len())
}
}