-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm_test.go
160 lines (140 loc) · 3.51 KB
/
fsm_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
package telnetlib
import (
"bytes"
"io"
"log"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type mockConn struct {
r io.Reader
w io.Writer
}
func (c *mockConn) Read(b []byte) (int, error) {
return c.r.Read(b)
}
func (c *mockConn) Write(b []byte) (int, error) {
return c.w.Write(b)
}
func (c *mockConn) Close() error {
return nil
}
func (c *mockConn) LocalAddr() net.Addr {
return nil
}
func (c *mockConn) RemoteAddr() net.Addr {
return nil
}
func (c *mockConn) SetDeadline(t time.Time) error {
return nil
}
func (c *mockConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *mockConn) SetWriteDeadline(t time.Time) error {
return nil
}
func newMockConn(r io.Reader, w io.Writer) net.Conn {
return &mockConn{
r: r,
w: w,
}
}
type cmd struct {
cmdBuf []byte
called bool
}
func (d *cmd) mockCmdHandler(w io.Writer, b []byte, tc *TelnetConn) {
d.called = true
d.cmdBuf = b
}
type opt struct {
cmd byte
optn byte
called bool
}
func (o *opt) optCallback(cmd, option byte) {
o.cmd = cmd
o.optn = option
o.called = true
}
type testSample struct {
inputSeq []byte
expState []state
expOpt []*opt
expCmd []*cmd
}
var samples = []testSample{
testSample{
inputSeq: []byte{10, 20, 5, 12, 34, 125, 98},
expState: []state{0, 0, 0, 0, 0, 0, 0},
expOpt: []*opt{nil, nil, nil, nil, nil, nil, nil},
expCmd: []*cmd{nil, nil, nil, nil, nil, nil, nil},
},
testSample{
inputSeq: []byte{Iac, Do, Echo, 10, 20, Iac, Will, Sga},
expState: []state{cmdState, optionNegotiationState, dataState, dataState, dataState, cmdState, optionNegotiationState, dataState},
expOpt: []*opt{nil, nil, &opt{Do, Echo, true}, nil, nil, nil, nil, &opt{Will, Sga, true}},
expCmd: []*cmd{nil, nil, nil, nil, nil, nil, nil, nil},
},
testSample{
inputSeq: []byte{10, 20, Iac, Ayt, 5, Iac, Ao},
expState: []state{dataState, dataState, cmdState, dataState, dataState, cmdState, dataState},
expOpt: []*opt{nil, nil, nil, nil, nil, nil, nil},
expCmd: []*cmd{nil, nil, nil, &cmd{[]byte{Ayt}, true}, nil, nil, &cmd{[]byte{Ao}, true}},
},
testSample{
inputSeq: []byte{10, Iac, Sb, 5, 12, Iac, Se},
expState: []state{dataState, cmdState, subnegState, subnegState, subnegState, subnegEndState, dataState},
expOpt: []*opt{nil, nil, nil, nil, nil, nil, nil},
expCmd: []*cmd{nil, nil, nil, nil, nil, nil, &cmd{[]byte{Sb, 5, 12, Se}, true}},
},
}
func TestFSM(t *testing.T) {
for count, s := range samples {
log.Printf("test sample %d", count)
b := make([]byte, 512)
outBuf := bytes.NewBuffer(b)
inBuf := bytes.NewBuffer(s.inputSeq)
cmdPtr := &cmd{
called: false,
}
optPtr := &opt{
called: false,
}
dummyConn := newMockConn(inBuf, outBuf)
fsm := newTelnetFSM()
opts := connOpts{
conn: dummyConn,
fsm: fsm,
cmdHandler: cmdPtr.mockCmdHandler,
dataHandler: defaultDataHandlerFunc,
optCallback: optPtr.optCallback,
}
tc := newTelnetConn(opts)
go tc.dataHandlerWrapper(tc.handlerWriter, tc.dataRW)
assert.Equal(t, fsm.curState, dataState)
for i, ch := range s.inputSeq {
ns := fsm.nextState(ch)
fsm.curState = ns
assert.Equal(t, s.expState[i], ns)
if optPtr.called {
assert.Equal(t, s.expOpt[i].cmd, optPtr.cmd)
assert.Equal(t, s.expOpt[i].optn, optPtr.optn)
} else {
assert.Nil(t, s.expOpt[i])
}
if cmdPtr.called {
exp := s.expCmd[i].cmdBuf
actual := cmdPtr.cmdBuf
assert.Equal(t, exp, actual)
} else {
assert.Nil(t, s.expCmd[i])
}
optPtr.called = false
cmdPtr.called = false
}
}
}