Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
179 lines (164 loc) · 5.38 KB
/
Copy pathserver_test.go
File metadata and controls
179 lines (164 loc) · 5.38 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
package server
import (
"context"
"errors"
"fmt"
"net"
"net/textproto"
"testing"
"time"
)
// TestAuthUserUnknownUserDoesNotPanic reproduces the historical operator-precedence bug where a
// map miss (ok == false, user == nil) fell through to a nil pointer dereference. Against the old
// AuthUser this test panics instead of failing cleanly.
func TestAuthUserUnknownUserDoesNotPanic(t *testing.T) {
d := &driver{
ctx: context.Background(),
users: map[string]*user{
"anonymous": {password: "*", basePath: "/"},
},
}
if _, err := d.AuthUser(nil, "nosuchuser", "whatever"); err == nil {
t.Fatal("expected an error for an unknown user, got nil")
}
}
func TestAuthUserAnonymous(t *testing.T) {
d := &driver{
ctx: context.Background(),
users: map[string]*user{
"anonymous": {password: "*", basePath: "/"},
},
}
if _, err := d.AuthUser(nil, "anonymous", "password-one"); err != nil {
t.Fatalf("expected anonymous login to succeed, got %v", err)
}
if _, err := d.AuthUser(nil, "anonymous", "password-two"); err != nil {
t.Fatalf("expected anonymous login to succeed with a different password, got %v", err)
}
if _, err := d.AuthUser(nil, "somebody-else", "whatever"); err == nil {
t.Fatal("expected login for an unknown user to fail")
}
}
func TestAuthUserValidator(t *testing.T) {
var gotUserName, gotPassword string
errRejected := errors.New("rejected")
d := &driver{
ctx: context.Background(),
basePath: "/",
validate: func(_ context.Context, userName, password string) error {
gotUserName, gotPassword = userName, password
if userName == "bob" && password == "s3cret" {
return nil
}
return errRejected
},
}
cd, err := d.AuthUser(nil, "bob", "s3cret")
if err != nil {
t.Fatalf("expected accepted login, got %v", err)
}
if cd == nil {
t.Fatal("expected a non-nil ClientDriver on success")
}
if gotUserName != "bob" || gotPassword != "s3cret" {
t.Fatalf("validator received (%q, %q), want (%q, %q)", gotUserName, gotPassword, "bob", "s3cret")
}
cd, err = d.AuthUser(nil, "bob", "wrong")
if !errors.Is(err, errRejected) {
t.Fatalf("expected rejection error %v, got %v", errRejected, err)
}
if cd != nil {
t.Fatal("expected a nil ClientDriver on rejection")
}
}
// startAnonymousTestServer starts a real FTP server on an ephemeral loopback port using the
// anonymous Start entry point and returns its address. The server is stopped via t.Cleanup.
func startAnonymousTestServer(t *testing.T, basePath string) string {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
portCh := make(chan uint16, 1)
errCh := make(chan error, 1)
go func() {
errCh <- Start(ctx, "127.0.0.1", basePath, portCh)
}()
return waitForTestServer(t, cancel, portCh, errCh)
}
// startValidatorTestServer is like startAnonymousTestServer, but authenticates using validate.
func startValidatorTestServer(t *testing.T, basePath string, validate PasswordValidator) string {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
portCh := make(chan uint16, 1)
errCh := make(chan error, 1)
go func() {
errCh <- StartWithValidator(ctx, "127.0.0.1", basePath, portCh, validate)
}()
return waitForTestServer(t, cancel, portCh, errCh)
}
func waitForTestServer(t *testing.T, cancel context.CancelFunc, portCh <-chan uint16, errCh <-chan error) string {
t.Helper()
port, ok := <-portCh
if !ok {
cancel()
t.Fatalf("server failed to start: %v", <-errCh)
}
t.Cleanup(func() {
cancel()
if err := <-errCh; err != nil {
t.Errorf("server returned an error after being stopped: %v", err)
}
})
return fmt.Sprintf("127.0.0.1:%d", port)
}
// ftpLogin dials addr and performs a USER/PASS login round-trip, returning the code of the PASS
// response (230 on success, 530 on rejection).
func ftpLogin(t *testing.T, addr, userName, password string) int {
t.Helper()
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil {
t.Fatalf("dial %s: %v", addr, err)
}
defer conn.Close()
tp := textproto.NewConn(conn)
if _, _, err := tp.ReadResponse(0); err != nil {
t.Fatalf("read greeting: %v", err)
}
if err := tp.PrintfLine("USER %s", userName); err != nil {
t.Fatalf("send USER: %v", err)
}
if _, _, err := tp.ReadResponse(0); err != nil {
t.Fatalf("read USER response: %v", err)
}
if err := tp.PrintfLine("PASS %s", password); err != nil {
t.Fatalf("send PASS: %v", err)
}
code, _, err := tp.ReadResponse(0)
if err != nil {
t.Fatalf("read PASS response: %v", err)
}
_ = tp.PrintfLine("QUIT")
return code
}
func TestEndToEndAnonymous(t *testing.T) {
addr := startAnonymousTestServer(t, t.TempDir())
if code := ftpLogin(t, addr, "anonymous", "does-not-matter"); code != 230 {
t.Fatalf("anonymous login: got code %d, want 230", code)
}
if code := ftpLogin(t, addr, "someone-else", "does-not-matter"); code != 530 {
t.Fatalf("unknown user login: got code %d, want 530", code)
}
}
func TestEndToEndValidator(t *testing.T) {
validate := func(_ context.Context, userName, password string) error {
if userName == "carol" && password == "swordfish" {
return nil
}
return errors.New("invalid credentials")
}
addr := startValidatorTestServer(t, t.TempDir(), validate)
if code := ftpLogin(t, addr, "carol", "swordfish"); code != 230 {
t.Fatalf("validator login: got code %d, want 230", code)
}
if code := ftpLogin(t, addr, "carol", "wrong-password"); code != 530 {
t.Fatalf("validator rejection: got code %d, want 530", code)
}
}