forked from fclairamb/ftpserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
187 lines (153 loc) · 3.75 KB
/
server_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
package ftpserver
import (
"errors"
"fmt"
"net"
"os"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
lognoop "github.com/fclairamb/go-log/noop"
)
func TestMain(m *testing.M) {
// we change the timezone to be able to test that MLSD/MLST commands write UTC timestamps
loc, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Printf("unable to set timezone: %v\n", err)
os.Exit(1)
}
time.Local = loc
os.Exit(m.Run())
}
var errListenerAccept = errors.New("error accepting a connection")
type fakeNetError struct {
error
count int
}
func (e *fakeNetError) Timeout() bool {
return false
}
func (e *fakeNetError) Temporary() bool {
e.count++
return e.count < 10
}
func (e *fakeNetError) Error() string {
return e.error.Error()
}
type fakeListener struct {
server net.Conn
client net.Conn
err error
}
func (l *fakeListener) Accept() (net.Conn, error) {
return l.client, l.err
}
func (l *fakeListener) Close() error {
errClient := l.client.Close()
errServer := l.server.Close()
if errServer != nil {
return errServer
}
return errClient
}
func (l *fakeListener) Addr() net.Addr {
return l.server.LocalAddr()
}
func newFakeListener(err error) net.Listener {
server, client := net.Pipe()
return &fakeListener{
server: server,
client: client,
err: err,
}
}
func TestListenerAcceptErrors(t *testing.T) {
errNetFake := &fakeNetError{error: errListenerAccept}
server := FtpServer{
listener: newFakeListener(errNetFake),
Logger: lognoop.NewNoOpLogger(),
}
err := server.Serve()
require.EqualError(t, err, errListenerAccept.Error())
}
func TestPortCommandFormatOK(t *testing.T) {
net, err := parsePORTAddr("127,0,0,1,239,163")
require.NoError(t, err, "Problem parsing")
require.Equal(t, "127.0.0.1", net.IP.String(), "Problem parsing IP")
require.Equal(t, 239<<8+163, net.Port, "Problem parsing port")
}
func TestPortCommandFormatInvalid(t *testing.T) {
badFormats := []string{
"127,0,0,1,239,",
"127,0,0,1,1,1,1",
}
for _, f := range badFormats {
_, err := parsePORTAddr(f)
require.Error(t, err, "This should have failed")
}
}
func TestQuoteDoubling(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
{"1", args{" white space"}, " white space"},
{"1", args{` one" quote`}, ` one"" quote`},
{"1", args{` two"" quote`}, ` two"""" quote`},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.want, quoteDoubling(tt.args.s))
})
}
}
func TestServerSettings(t *testing.T) {
server := FtpServer{
Logger: lognoop.NewNoOpLogger(),
driver: &TestServerDriver{
Settings: &Settings{
PublicHost: "127.0.0",
},
},
}
err := server.loadSettings()
_, ok := err.(*ipValidationError)
require.True(t, ok)
server.driver = &TestServerDriver{
Settings: &Settings{
PublicHost: "::1",
},
}
err = server.loadSettings()
_, ok = err.(*ipValidationError)
require.True(t, ok)
server.driver = &TestServerDriver{
Settings: &Settings{
PublicHost: "::ffff:192.168.1.1",
},
}
err = server.loadSettings()
require.NoError(t, err)
require.Equal(t, "192.168.1.1", server.settings.PublicHost)
}
func TestTemporaryError(t *testing.T) {
a := assert.New(t)
// Test the temporaryError function
a.False(temporaryError(nil))
a.False(temporaryError(&fakeNetError{error: errListenerAccept}))
a.False(temporaryError(&net.OpError{
Err: &fakeNetError{error: errListenerAccept},
}))
for _, serr := range []syscall.Errno{syscall.ECONNABORTED, syscall.ECONNRESET} {
a.True(temporaryError(&net.OpError{Err: &os.SyscallError{Err: serr}}))
}
a.False(temporaryError(&net.OpError{Err: &os.SyscallError{Err: syscall.EAGAIN}}))
}