This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
expect_test.go
1429 lines (1321 loc) · 36.5 KB
/
expect_test.go
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package expect
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
stdlog "log"
"google.golang.org/grpc/codes"
log "github.com/golang/glog"
term "github.com/google/goterm/term"
"golang.org/x/crypto/ssh"
)
var (
// eReg identifies the expect commands in the shell files.
eReg = regexp.MustCompile(`^# e: (.*)$`)
// sReg identifies the send commands in the shell files.
sReg = regexp.MustCompile(`^# s: (.*)$`)
// sReg identifies the sw/case commands in the shell files.
cReg = regexp.MustCompile(`^# c: '(.*)' (.*)$`)
)
const expTestData = "./testdata/"
var privateKey = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEAnUKOpnWUyxCGy9lyTLK3Crd5BXGG718wIgHzpCvKbopVbYu4
xa9Fk1cjPHp2F/pPwXmIKJuTVpkm/VXcFfABH6cMeszaXqVBhqm6AA0E7y0K0oYZ
GUMMm3sBLPV3ydUHECI2NnEXOCLGysKM6Ht2ZuGxCKdXpquRE1HdLUUIJep31gSO
J4dyQRk12VYHrpTjz1Tzv9prf76vJqYmr6+axeH7I3/9KGnPe1vD0z8NhOwqQONz
DMSjpbSYFiVyRbDVgSi9xq+BeFrASZuwkoHut3tzmIvQLcGIR+LoOeN9mDpXbseQ
y84PXkduF8udrBCIBETekmK7kqi6hwLRCn9/twIDAQABAoIBAQCTeEOvQ5oJpvDR
HpN56ymNCiqZ+TERLhFEAtKIRGxrppufw6O89bToC5HGeAxgReIey6nscqADWFFg
xfBCPjO/i/Y+/fVVReEht+3teEgFRhbc/tVwhBjBgOLEV1hC09rwvTRbb0fX43zJ
zRE4Pfb1WXWbaNngOQkttdoURqTyb+n8zgwx0AUsueSrYxTk1UTF+Jet7g23jRjd
YCCx9qhHez5yif+1LZGIqJD0OKGHr9q+bbOZpy5dqjamuf9ulBvnZkKzcuHf0m/W
Vhf9YI8kOQhPXfztTnZrN5Jg64gGuvJ0sEZucp5hOR4hYkLagOCaUuNIeHG7SsYU
hChWCDphAoGBAM7nr4t714etJnuRE39FG+rylV5K3T2osr2Hwp7wSZfXHZUcnk2N
KSDA9tzeFYX7QxUlc7qNwsLC2WkK97x1WbrNdO8Zn5lmBHfyIS7jxEGQY9htWrgr
sjAaUg/JfHMu/lxNAigXAaGU2VzsTySgB3eWbfaAd/sImaxnVPBajOARAoGBAMKT
NykNYl3zg1pIXGQlu3a0pTv2gRcBnnW7bUAM6b6tDdQZ+5cbu43MfAwhsGsMZ/HL
gKQRJI952olrPa4dEiirxUKqfVVPLnDcUSu6uhvJFpzN2YVdMyPNWc3V9lfIztbu
UvCvupnmeViG9qRoJgbMLIBLBN1oS5MKOL55oqtHAoGBAMX21XZe4qRVHlniQEZo
aELPIe1bMf3Z2FMRfzw1aiSW1R4jiK9o3a4SEuDWuL893jxwXh9jnbJdXklsDgbK
PTVHeZd/672I58Of7vH/SXr13SJp1wAaBt6RgGzMen92uja0E9kp0gy475RCIaNI
XnykeMf+uU1+OBLFt3ZVHS8RAoGBAJr/BpvPK6LHzsTmi6LDY/gFovKHRQH8qiwC
595z6ueXl0J0iDQxRVCJqe9IDu7XbR3yDEGl3kfku69oHDRMuCBp5LNceIaykr4Y
4xhAoOxtXXP/jt1sBsboWDddz+TR8+LG6o8MjUr3i4Z3zJXe2RvlHTX9jJyK7ljt
dZJV9r0VAoGBAI+yBh2oa5D66XztQ2pfKm/B03RYARR0iKvho6Ass1nM1YSvhflt
CkGNYNP5Mwr/VpfTppl0JTl9++gtoAmDgVm19JUYiABhYNmbTq62STJb+LkEL+xK
Jf5UkUJOE8Rf+A1vmI1igjVffSIRLTJC6zOX0JCZMIFKZhyTZsPOuFcm
-----END RSA PRIVATE KEY-----`)
// SSHServer represents one local SSH server.
type SSHServer struct {
batch []Batcher
cfg *ssh.ServerConfig
l net.Listener
sync.Mutex
opts []Option
term *term.Termios
}
// New returns a new SSHServer configured for username/password.
func NewSSHServer(user, pass string, b []Batcher, t *term.Termios, opts ...Option) *SSHServer {
srv := &ssh.ServerConfig{
PasswordCallback: func(c ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
if c.User() == user && string(password) == pass {
return nil, nil
}
return nil, errors.New("password rejected for:" + c.User())
},
}
k, _ := ssh.ParsePrivateKey(privateKey)
srv.AddHostKey(k)
if t == nil {
t = &term.Termios{
Wz: term.Winsize{
WsCol: 132,
WsRow: 43,
},
}
}
return &SSHServer{cfg: srv, batch: b, term: t, opts: opts}
}
// Batcher replaces the batcher.
func (s *SSHServer) Batcher(b []Batcher) {
s.Lock()
s.batch = b
s.Unlock()
}
// Termios replaces the termios.
func (s *SSHServer) Termios(t *term.Termios) {
if t == nil {
t = &term.Termios{
Wz: term.Winsize{
WsCol: 132,
WsRow: 43,
},
}
}
s.term = t
}
// Serve spins up the SSH server and returns the port used.
func (s *SSHServer) Serve() (uint16, error) {
l, err := net.Listen("tcp", "")
if err != nil {
return 0, err
}
s.l = l
go func() {
for {
c, err := l.Accept()
if err != nil {
log.Errorf("Accept failed: %v", err)
return
}
go s.runBatch(c)
}
}()
_, port, err := net.SplitHostPort(l.Addr().String())
if err != nil {
return 0, err
}
p, err := strconv.Atoi(port)
if err != nil {
return 0, err
}
return uint16(p), nil
}
// Close closes the SSH server write pipe.
func (s *SSHServer) Close() error {
return s.l.Close()
}
const testTimeout = 20 * time.Second
// RFC 4254 Section 6.2.
type ptyRequestMsg struct {
Term string
Columns uint32
Rows uint32
Width uint32
Height uint32
Modelist string
}
func (s *SSHServer) runBatch(conn net.Conn) {
defer conn.Close()
_, chs, rq, err := ssh.NewServerConn(conn, s.cfg)
if err != nil {
log.Errorf("ssh.NewServerConn failed: %v", err)
return
}
go ssh.DiscardRequests(rq)
for ch := range chs {
switch ch.ChannelType() {
case "session":
sch, in, err := ch.Accept()
if err != nil {
log.Errorf("ch.Accept failed: %v", err)
return
}
for sess := range in {
switch sess.Type {
case "dummy":
if err := sess.Reply(true, nil); err != nil {
log.Errorf("sess.Reply(%t,nil) failed: %v", true, err)
}
case "pty-req":
ptyReq := ptyRequestMsg{}
if err := ssh.Unmarshal(sess.Payload, &ptyReq); err != nil {
if err := sess.Reply(false, nil); err != nil {
log.Errorf("sess.Reply(%t,nil) failed: %v", false, err)
}
log.Errorf("ssh.Unmarshal of PTYRequest failed: %v", err)
continue
}
if ptyReq.Columns != uint32(s.term.Wz.WsCol) || ptyReq.Rows != uint32(s.term.Wz.WsRow) {
log.Errorf("PTY cols/rows: %d/%d want: %d/%d", ptyReq.Columns, ptyReq.Rows, s.term.Wz.WsCol, s.term.Wz.WsRow)
if err := sess.Reply(false, nil); err != nil {
log.Errorf("sess.Reply(%t,nil) failed: %v", false, err)
}
continue
}
if err := sess.Reply(true, nil); err != nil {
log.Errorf("sess.Reply(%t,nil) failed: %v", true, err)
}
case "shell":
log.Infof("Shell request coming in")
resCh := make(chan error)
defer close(resCh)
if err := sess.Reply(true, nil); err != nil {
log.Errorf("sess.Reply(%t,nil) failed: %v", true, err)
}
rIn, wIn := io.Pipe()
rOut, wOut := io.Pipe()
go io.Copy(sch, rIn)
go io.Copy(wOut, sch)
go func() {
exp, _, err := SpawnGeneric(&GenOptions{
In: wIn,
Out: rOut,
Wait: func() error {
return <-resCh
},
Close: func() error { return wIn.Close() },
Check: func() bool { return true },
}, testTimeout*2, s.opts...)
s.Lock()
out, err := exp.ExpectBatch(s.batch, testTimeout*2)
if err != nil {
log.Errorf("exp.ExpectBatch(%v) failed: %v, res: %v", s.batch, err, out)
}
s.Unlock()
}()
default:
sess.Reply(false, []byte(fmt.Sprint("session type not supported")))
}
}
default:
ch.Reject(ssh.UnknownChannelType, "channel type not supported")
}
}
}
// Tc is an example of implementing custom tag functions.
type Tc uint32
func NewTc() (t Tc) {
return
}
// Count works like ContinueLog with a counter.
func (t Tc) Count(msg string, s *Status) func() (Tag, *Status) {
return func() (Tag, *Status) {
t++
log.Infof("%d: %s", t, msg)
return ContinueTag, s
}
}
// NextLog adds loggin and counting to the Next tag.
func (t Tc) NextLog(msg string) func() (Tag, *Status) {
return func() (Tag, *Status) {
t++
log.Infof("Next %d: %s", t, msg)
return NextTag, NewStatus(codes.Unimplemented, "Should not matter")
}
}
func TestBatcher(t *testing.T) {
tests := []struct {
name string
clt, srv []Batcher
fail bool
}{
{
name: "Config mode",
clt: []Batcher{
&BExp{`router1>`},
&BSnd{"conf t\n"},
&BExp{`\(configure\) router1>`},
},
srv: []Batcher{
&BSnd{`router1> `},
&BExp{"conf t\n"},
&BSnd{`(configure) router1> `},
}}, {
name: "Login caser",
clt: []Batcher{
&BCas{[]Caser{
&Case{R: regexp.MustCompile(`Login: `), S: "TestUser\n", T: LogContinue("at login prompt", NewStatus(codes.PermissionDenied, "wrong username")), Rt: 1},
&Case{R: regexp.MustCompile(`Password: `), S: "TestPass\n", T: LogContinue("at password prompt", NewStatus(codes.PermissionDenied, "wrong pass")), Rt: 1},
&Case{R: regexp.MustCompile(`Permission denied`), T: Fail(NewStatus(codes.PermissionDenied, "login failed"))},
&Case{R: regexp.MustCompile(`router 1>`), T: OK()},
}},
},
srv: []Batcher{
&BSnd{"Login: "},
&BCas{[]Caser{
&Case{R: regexp.MustCompile("TestUser\n"), S: `Password: `, T: Continue(NewStatus(codes.PermissionDenied, "permission denied")), Rt: 3},
&Case{R: regexp.MustCompile("TestPass\n"), S: `router 1> `, T: OK()},
},
},
}}, {
name: "100 Hello World",
clt: []Batcher{
&BSnd{`Hello `},
&BCas{[]Caser{
&Case{R: regexp.MustCompile("Done"), T: OK()},
&Case{R: regexp.MustCompile("World"), S: "Hello ", T: NewTc().Count("Hello", NewStatus(codes.OutOfRange, "too many worlds")), Rt: 100},
}},
},
srv: []Batcher{
&BCas{[]Caser{
&Case{R: regexp.MustCompile("Hello"), S: "World\n", T: NewTc().Count("World", NewStatus(codes.OK, "too many hellos")), Rt: 99},
}},
&BSnd{"Done"},
},
}, {
name: "100 Hello World using Next tag",
clt: []Batcher{
&BSnd{`Hello `},
&BCas{[]Caser{
&Case{R: regexp.MustCompile("Done"), T: OK()},
&Case{R: regexp.MustCompile("World"), S: "Hello ", T: NewTc().Count("Hello", NewStatus(codes.OutOfRange, "too many worlds")), Rt: 100},
}},
},
srv: []Batcher{
&BCas{[]Caser{
&Case{R: regexp.MustCompile("Hello"), S: "World\n", T: NewTc().NextLog("World"), Rt: 99},
&Case{R: regexp.MustCompile("Hello"), S: "Done\n", T: LogContinue("Done sent", NewStatus(codes.OK, "Done sent"))},
}},
},
},
}
srv := NewSSHServer("test", "test", nil, nil, CheckDuration(40*time.Millisecond))
port, err := srv.Serve()
if err != nil {
t.Fatalf("srv.Serve failed: %v", err)
}
defer srv.Close()
for _, tst := range tests {
srv.Batcher(tst.srv)
clt, err := ssh.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(int(port))),
&ssh.ClientConfig{
User: "test",
Auth: []ssh.AuthMethod{ssh.Password("test")},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
t.Errorf("%s: ssh.Dial failed: %v", tst.name, err)
continue
}
e, _, err := SpawnSSH(clt, testTimeout*2)
if err != nil {
t.Errorf("%s: SpawnSSH failed: %v", tst.name, err)
}
res, err := e.ExpectBatch(tst.clt, testTimeout*2)
if got, want := err != nil, tst.fail; got != want {
t.Errorf("%s: e.ExpectBatch(%v,_) = %v want: %v, res: %q", tst.name, tst.clt, err, want, res)
}
if err := clt.Close(); err != nil {
t.Errorf("%s: clt.Close failed: %v", tst.name, err)
}
}
}
var (
cliMap = map[string]string{
"show system uptime": `Current time: 1998-10-13 19:45:47 UTC
Time Source: NTP CLOCK
System booted: 1998-10-12 20:51:41 UTC (22:54:06 ago)
Protocols started: 1998-10-13 19:33:45 UTC (00:12:02 ago)
Last configured: 1998-10-13 19:33:45 UTC (00:12:02 ago) by abc
12:45PM up 22:54, 2 users, load averages: 0.07, 0.02, 0.01
testuser@testrouter#`,
"show version": `Cisco IOS Software, 3600 Software (C3660-I-M), Version 12.3(4)T
TAC Support: http://www.cisco.com/tac
Copyright (c) 1986-2003 by Cisco Systems, Inc.
Compiled Thu 18-Sep-03 15:37 by ccai
ROM: System Bootstrap, Version 12.0(6r)T, RELEASE SOFTWARE (fc1)
ROM:
C3660-1 uptime is 1 week, 3 days, 6 hours, 41 minutes
System returned to ROM by power-on
System image file is "slot0:tftpboot/c3660-i-mz.123-4.T"
Cisco 3660 (R527x) processor (revision 1.0) with 57344K/8192K bytes of memory.
Processor board ID JAB055180FF
R527x CPU at 225Mhz, Implementation 40, Rev 10.0, 2048KB L2 Cache
3660 Chassis type: ENTERPRISE
2 FastEthernet interfaces
4 Serial interfaces
DRAM configuration is 64 bits wide with parity disabled.
125K bytes of NVRAM.
16384K bytes of processor board System flash (Read/Write)
Flash card inserted. Reading filesystem...done.
20480K bytes of processor board PCMCIA Slot0 flash (Read/Write)
Configuration register is 0x2102
testrouter#`,
"show system users": `7:30PM up 4 days, 2:26, 2 users, load averages: 0.07, 0.02, 0.01
USER TTY FROM LOGIN@ IDLE WHAT
root d0 - Fri05PM 4days -csh (csh)
blue p0 level5.company.net 7:30PM - cli
testuser@testrouter#`,
}
)
func fakeCli(tMap map[string]string, in io.Reader, out io.Writer) {
scn := bufio.NewScanner(in)
for scn.Scan() {
tst, ok := tMap[scn.Text()]
if !ok {
out.Write([]byte(fmt.Sprintf("command: %q not found", scn.Text())))
continue
}
_, err := out.Write([]byte(tst))
if err != nil {
log.Warningf("Write of %q failed: %v", tst, err)
return
}
}
}
// ExampleDebugCheck toggles the DebugCheck option.
func ExampleDebugCheck() {
rIn, wIn := io.Pipe()
rOut, wOut := io.Pipe()
rLog, wLog := io.Pipe()
waitCh := make(chan error)
defer rIn.Close()
defer wOut.Close()
defer wLog.Close()
go fakeCli(cliMap, rIn, wOut)
exp, r, err := SpawnGeneric(&GenOptions{
In: wIn,
Out: rOut,
Wait: func() error { return <-waitCh },
Close: func() error { return wIn.Close() },
Check: func() bool {
return true
}}, -1)
if err != nil {
log.Errorf("SpawnGeneric failed: %v", err)
return
}
re := regexp.MustCompile("testrouter#")
interact := func() {
for cmd := range cliMap {
if err := exp.Send(cmd + "\n"); err != nil {
log.Errorf("exp.Send(%q) failed: %v\n", cmd+"\n", err)
return
}
out, _, err := exp.Expect(re, -1)
if err != nil {
log.Errorf("exp.Expect(%v) failed: %v out: %v", re, err, out)
return
}
}
}
go func() {
var last string
scn := bufio.NewScanner(rLog)
for scn.Scan() {
ws := strings.Split(scn.Text(), " ")
if ws[0] == last {
continue
}
last = ws[0]
fmt.Println(ws[0])
}
}()
fmt.Println("First round")
interact()
fmt.Println("Second round - Debugging enabled")
prev := exp.Options(DebugCheck(stdlog.New(wLog, "DebugExample ", 0)))
interact()
exp.Options(prev)
fmt.Println("Last round - Previous Check put back")
interact()
waitCh <- nil
exp.Close()
wOut.Close()
<-r
// Output:
// First round
// Second round - Debugging enabled
// DebugExample
// Last round - Previous Check put back
}
// ExampleChangeCheck changes the check function runtime for an Expect session.
func ExampleChangeCheck() {
rIn, wIn := io.Pipe()
rOut, wOut := io.Pipe()
waitCh := make(chan error)
outCh := make(chan string)
defer close(outCh)
go fakeCli(cliMap, rIn, wOut)
go func() {
var last string
for s := range outCh {
if s == last {
continue
}
fmt.Println(s)
last = s
}
}()
exp, r, err := SpawnGeneric(&GenOptions{
In: wIn,
Out: rOut,
Wait: func() error { return <-waitCh },
Close: func() error { return wIn.Close() },
Check: func() bool {
outCh <- "Original check"
return true
}}, -1)
if err != nil {
fmt.Printf("SpawnGeneric failed: %v\n", err)
return
}
re := regexp.MustCompile("testrouter#")
interact := func() {
for cmd := range cliMap {
if err := exp.Send(cmd + "\n"); err != nil {
fmt.Printf("exp.Send(%q) failed: %v\n", cmd+"\n", err)
return
}
out, _, err := exp.Expect(re, -1)
if err != nil {
fmt.Printf("exp.Expect(%v) failed: %v out: %v", re, err, out)
return
}
}
}
interact()
prev := exp.Options(ChangeCheck(func() bool {
outCh <- "Replaced check"
return true
}))
interact()
exp.Options(prev)
interact()
waitCh <- nil
exp.Close()
wOut.Close()
<-r
// Output:
// Original check
// Replaced check
// Original check
}
// ExampleVerbose changes the Verbose and VerboseWriter options.
func ExampleVerbose() {
rIn, wIn := io.Pipe()
rOut, wOut := io.Pipe()
waitCh := make(chan error)
outCh := make(chan string)
defer close(outCh)
go fakeCli(cliMap, rIn, wOut)
go func() {
var last string
for s := range outCh {
if s == last {
continue
}
fmt.Println(s)
last = s
}
}()
exp, r, err := SpawnGeneric(&GenOptions{
In: wIn,
Out: rOut,
Wait: func() error { return <-waitCh },
Close: func() error { return wIn.Close() },
Check: func() bool {
return true
}}, -1, Verbose(true), VerboseWriter(os.Stdout))
if err != nil {
fmt.Printf("SpawnGeneric failed: %v\n", err)
return
}
re := regexp.MustCompile("testrouter#")
var interactCmdSorted []string
for k := range cliMap {
interactCmdSorted = append(interactCmdSorted, k)
}
sort.Strings(interactCmdSorted)
interact := func() {
for _, cmd := range interactCmdSorted {
if err := exp.Send(cmd + "\n"); err != nil {
fmt.Printf("exp.Send(%q) failed: %v\n", cmd+"\n", err)
return
}
out, _, err := exp.Expect(re, -1)
if err != nil {
fmt.Printf("exp.Expect(%v) failed: %v out: %v", re, err, out)
return
}
}
}
interact()
waitCh <- nil
exp.Close()
wOut.Close()
<-r
// Output:
// [34mSent:[39m "show system uptime\n"
// [32mMatch for RE:[39m "testrouter#" found: ["testrouter#"] Buffer: Current time: 1998-10-13 19:45:47 UTC
// Time Source: NTP CLOCK
// System booted: 1998-10-12 20:51:41 UTC (22:54:06 ago)
// Protocols started: 1998-10-13 19:33:45 UTC (00:12:02 ago)
// Last configured: 1998-10-13 19:33:45 UTC (00:12:02 ago) by abc
// 12:45PM up 22:54, 2 users, load averages: 0.07, 0.02, 0.01
//
// testuser@testrouter#
// [34mSent:[39m "show system users\n"
// [32mMatch for RE:[39m "testrouter#" found: ["testrouter#"] Buffer: 7:30PM up 4 days, 2:26, 2 users, load averages: 0.07, 0.02, 0.01
// USER TTY FROM LOGIN@ IDLE WHAT
// root d0 - Fri05PM 4days -csh (csh)
// blue p0 level5.company.net 7:30PM - cli
//
// testuser@testrouter#
// [34mSent:[39m "show version\n"
// [32mMatch for RE:[39m "testrouter#" found: ["testrouter#"] Buffer: Cisco IOS Software, 3600 Software (C3660-I-M), Version 12.3(4)T
//
// TAC Support: http://www.cisco.com/tac
// Copyright (c) 1986-2003 by Cisco Systems, Inc.
// Compiled Thu 18-Sep-03 15:37 by ccai
//
// ROM: System Bootstrap, Version 12.0(6r)T, RELEASE SOFTWARE (fc1)
// ROM:
//
// C3660-1 uptime is 1 week, 3 days, 6 hours, 41 minutes
// System returned to ROM by power-on
// System image file is "slot0:tftpboot/c3660-i-mz.123-4.T"
//
// Cisco 3660 (R527x) processor (revision 1.0) with 57344K/8192K bytes of memory.
// Processor board ID JAB055180FF
// R527x CPU at 225Mhz, Implementation 40, Rev 10.0, 2048KB L2 Cache
//
// 3660 Chassis type: ENTERPRISE
// 2 FastEthernet interfaces
// 4 Serial interfaces
// DRAM configuration is 64 bits wide with parity disabled.
// 125K bytes of NVRAM.
// 16384K bytes of processor board System flash (Read/Write)
//
// Flash card inserted. Reading filesystem...done.
// 20480K bytes of processor board PCMCIA Slot0 flash (Read/Write)
//
// Configuration register is 0x2102
//
// testrouter#
}
// TestTee tests the Tee option can write to a file.
func TestTee(t *testing.T) {
// Create a temporary file to tee output.
f, err := ioutil.TempFile("", "goexpect-tee")
if err != nil {
t.Fatalf("Could not create temporary file: %v", err)
}
fileName := f.Name()
defer os.Remove(fileName)
// Send abcdef to cat 4096 times.
input := "abcdef\n"
e, _, err := Spawn("cat", 400*time.Millisecond, Tee(f), CheckDuration(1*time.Millisecond))
for i := 0; i < 4096; i++ {
e.Send(input)
re := regexp.MustCompile(input)
if _, _, err = e.Expect(re, 400*time.Millisecond); err != nil {
t.Errorf("Expect(%q) failed: %v", input, err)
}
}
e.Close()
// Check the tee'd output.
got, err := ioutil.ReadFile(fileName)
if err != nil {
t.Fatalf("Could not read temporary file: %v", err)
}
want := strings.Repeat(input, 4096)
if string(got) != want {
t.Errorf("tee output mismatch, got: %q want: %q", got, want)
}
}
// TestTee_SpawnFake tests the Tee option can operate on SpawnFake.
func TestTee_SpawnFake(t *testing.T) {
// Create a temporary file to tee output.
f, err := ioutil.TempFile("", "goexpect-tee")
if err != nil {
t.Fatalf("Could not create temporary file: %v", err)
}
fileName := f.Name()
defer os.Remove(fileName)
msg := `
Pretty please don't hack my chassis
router1> `
srv := []Batcher{
&BSnd{msg},
}
re := regexp.MustCompile("router1>")
timeout := 2 * time.Second
exp, endch, err := SpawnFake(srv, timeout, Tee(f))
if err != nil {
t.Fatalf("SpawnFake failed: %v", err)
}
out, _, err := exp.Expect(re, timeout)
if err != nil {
t.Fatalf("Expect(%q,%v), err: %v, out: %q", re.String(), timeout, err, out)
}
exp.Close()
// wait for end
<-endch
// Check the tee'd output.
got, err := ioutil.ReadFile(fileName)
if err != nil {
t.Fatalf("Could not read temporary file: %v", err)
}
if string(got) != msg {
t.Errorf("tee output mismatch, got: %q want: %q", got, msg)
}
}
// TestSpawnGeneric tests out the generic spawn function.
func TestSpawnGeneric(t *testing.T) {
fr, fw := io.Pipe()
tests := []struct {
name string
opt *GenOptions
check func() bool
cli map[string]string
re *regexp.Regexp
fail bool
}{{
name: "Clean test",
check: func() bool { return true },
cli: cliMap,
re: regexp.MustCompile("testrouter#"),
fail: false,
}, {
name: "Fail check",
check: func() bool {
return false
},
cli: cliMap,
re: regexp.MustCompile("testrouter#"),
fail: true,
}, {
name: "In nil",
opt: &GenOptions{},
fail: true,
}, {
name: "Out nil",
opt: &GenOptions{
In: fw,
},
fail: true,
}, {
name: "Wait nil",
opt: &GenOptions{
In: fw,
Out: fr,
},
fail: true,
}}
for _, tst := range tests {
t.Logf("Running test: %v", tst.name)
waitCh := make(chan error)
rIn, wIn := io.Pipe()
rOut, wOut := io.Pipe()
if tst.opt == nil {
tst.opt = &GenOptions{
In: wIn,
Out: rOut,
Wait: func() error { return <-waitCh },
Close: func() error { return wIn.Close() },
Check: tst.check}
}
go fakeCli(tst.cli, rIn, wOut)
exp, r, err := SpawnGeneric(tst.opt, -1)
if err != nil {
if !tst.fail {
t.Errorf("test: %v , SpawnGeneric failed: %v", tst.name, err)
}
continue
}
gotFail := false
for cmd := range tst.cli {
err := exp.Send(cmd + "\n")
if err != nil {
if tst.fail {
gotFail = true
break
}
t.Errorf("Send(%q) failed: %v", cmd, err)
break
}
out, _, err := exp.Expect(tst.re, -1)
if err != nil {
if tst.fail {
gotFail = true
break
}
t.Errorf("Expect(%q) failed: %v, out: %q", tst.re, err, out)
break
}
}
if gotFail != tst.fail {
t.Errorf("test: %v , failed status mismatch, got: %v want: %v", tst.name, gotFail, tst.fail)
}
waitCh <- nil
exp.Close()
wOut.Close()
<-r
}
}
// TestSendTimeout tests that Send command can fail on timeout.
func TestSendTimeout(t *testing.T) {
t.Log("Running test: TestSendTimeout")
rIn, wIn := io.Pipe()
rOut, wOut := io.Pipe()
waitCh := make(chan error)
outCh := make(chan string)
defer close(outCh)
go fakeCli(cliMap, rIn, wOut)
exp, r, err := SpawnGeneric(
&GenOptions{
In: wIn,
Out: rOut,
Wait: func() error { return <-waitCh },
Close: func() error { return nil },
Check: func() bool { return true },
}, -1, SendTimeout(time.Second))
if err != nil {
t.Fatalf("SpawnGeneric(_, %d , SendTimeout(%v)) failed: %v", -1, time.Second, err)
}
if err := wIn.Close(); err != nil {
t.Fatalf("wIn.Close() failed: %v", err)
}
if err := exp.Send("test" + "\n"); err != nil {
t.Fatalf("Send(%q) command failed: %v", "test"+"\n", err)
}
if err := exp.Send("test" + "\n"); err == nil {
t.Errorf("Send(%q) = %t want: %t, err: %v", "test"+"\n", (err != nil), true, err)
}
waitCh <- nil
exp.Close()
wOut.Close()
<-r
}
// TestSpawnSSHPTY tests the SSHPTY spawner.
func TestSpawnSSHPTY(t *testing.T) {
tests := []struct {
name string
fail bool
srv []Batcher
clt []Batcher
sshNil bool
srvTerm *term.Termios
cltTerm term.Termios
}{{
name: "sshClient broken",
fail: true,
sshNil: true,
}, {
name: "Empty Termios",
clt: []Batcher{
&BSnd{"Hello"},
&BExp{"World"},
},
srv: []Batcher{
&BExp{"Hello"},
&BSnd{"World"},
},
}, {
name: "Termios mismatch",
fail: true,
srvTerm: &term.Termios{
Wz: term.Winsize{
WsCol: 120,
WsRow: 40,
}},
cltTerm: term.Termios{
Wz: term.Winsize{
WsCol: 240,
WsRow: 22,
},
},
}}
srv := NewSSHServer("test", "test", nil, nil)
port, err := srv.Serve()
if err != nil {
t.Fatalf("srv.Serve failed: %v", err)
}
defer func() {
if err := srv.Close(); err != nil {
t.Errorf("srv.Close failed: %v", err)
}
}()
for _, tst := range tests {
srv.Batcher(tst.srv)
srv.Termios(tst.srvTerm)
var sshClt *ssh.Client
if !tst.sshNil {
clt, err := ssh.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(int(port))),
&ssh.ClientConfig{
User: "test",
Auth: []ssh.AuthMethod{ssh.Password("test")},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
t.Errorf("%s: net.Dial(%q) failed: %v", tst.name, net.JoinHostPort("localhost", strconv.Itoa(int(port))), err)
continue
}
sshClt = clt
}
e, _, err := SpawnSSHPTY(sshClt, testTimeout*2, tst.cltTerm)
if got, want := err != nil, tst.fail; got != want {
t.Errorf("%s: SpawnSSH = %t want: %t, err: %v", tst.name, got, want, err)
continue
}
if err != nil {
continue
}
res, err := e.ExpectBatch(tst.clt, testTimeout*2)
if err != nil {
t.Errorf("%s: e.ExpectBatch failed: %v, out: %v", tst.name, err, res)
continue
}
if err := sshClt.Close(); err != nil {
t.Errorf("%s: clt.Close failed: %v", tst.name, err)
}
}
}
// TestOptions tests manipulating options.
func TestOptions(t *testing.T) {
tests := []struct {
name string
check func() bool
opts []Option
re *regexp.Regexp
fail bool