-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_test.go
136 lines (125 loc) · 3.06 KB
/
client_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
package ftp
import (
/*"fmt"*/
"testing"
)
// TODO: Mock out network access. Tests currently require an FTP server
// running on localhost.
func TestDial(t *testing.T) {
_, err := Dial("")
if err == nil {
t.Error("Dial() should return an error on blank 'host'!")
}
_, err = Dial("localhost:21")
if err != nil {
t.Error(err)
}
}
func TestLogin(t *testing.T) {
conn, err := Dial("localhost:21")
if err != nil {
t.Fatal(err)
}
err = conn.Login("anonymous", "anonymous@")
if err != nil {
t.Error(err)
}
err = conn.Login("", "anonymous@")
if err == nil {
t.Error("Login() should return an error on blank 'user'!")
}
err = conn.Login("anonymous", "")
if err == nil {
t.Error("Login() should return an error on blank 'password'!")
}
}
func TestLogout(t *testing.T) {
conn, err := Dial("localhost:21")
if err != nil {
t.Fatal(err)
}
err = conn.Login("anonymous", "anonymous@")
if err != nil {
t.Error(err)
}
err = conn.Logout()
if err != nil {
t.Error(err)
}
}
func TestCmd(t *testing.T) {
conn, err := Dial("localhost:ftp")
if err != nil {
t.Fatal(err)
}
_, _, err = conn.Cmd("USER", "anonymous")
if err != nil {
t.Error(err)
}
}
func TestExtractDataPort(t *testing.T) {
test_string := "227 Entering Passive Mode (127,0,0,1,205,238)."
port, err := extractDataPort(test_string)
if err != nil {
t.Error(err)
}
if port != 52718 {
t.Error("Failed port calculation! Expected 52718, got", port)
}
}
func TestCheckResponseCode(t *testing.T) {
// Success
err := checkResponseCode(2, 230)
if err != nil {
t.Error("Should return nil if response code matches! ExpectCode: 2 ActualCode: 230")
}
err = checkResponseCode(23, 230)
if err != nil {
t.Error("Should return nil if response code matches! ExpectCode: 23 ActualCode: 230")
}
err = checkResponseCode(230, 230)
if err != nil {
t.Error("Should return nil if response code matches! ExpectCode: 230 ActualCode: 230")
}
// Failure
err = checkResponseCode(2, 500)
if err == nil {
t.Error("Should return error if response code doesn't match (ExpectCode: 2 ActualCode: 500)!")
}
err = checkResponseCode(23, 500)
if err == nil {
t.Error("Should return error if response code doesn't match (ExpectCode: 23 ActualCode: 500)!")
}
err = checkResponseCode(230, 500)
if err == nil {
t.Error("Should return error if response code doesn't match (ExpectCode: 230 ActualCode: 500)!")
}
}
func TestDownload(t *testing.T) {
conn, err := Dial("localhost:21")
if err != nil {
t.Fatal(err)
}
err = conn.Login("anonymous", "anonymous@")
if err != nil {
t.Error(err)
}
err = conn.DownloadFile("/test_file.txt", "./download_test.txt", BINARY)
if err != nil {
t.Error(err)
}
}
func TestUpload(t *testing.T) {
conn, err := Dial("localhost:21")
if err != nil {
t.Fatal(err)
}
err = conn.Login("anonymous", "anonymous@")
if err != nil {
t.Error(err)
}
err = conn.UploadFile("upload_file.txt", "/up/uploaded.txt", BINARY)
if err != nil {
t.Error(err)
}
}