-
Notifications
You must be signed in to change notification settings - Fork 8
/
handlers_test.go
184 lines (155 loc) · 4.37 KB
/
handlers_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
// Copyright (C) 2017 Kale Blankenship. All rights reserved.
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details
package tftp // import "pack.ag/tftp"
import (
"bytes"
"io/ioutil"
"net"
"path/filepath"
"reflect"
"testing"
)
type readRequestMock struct {
addr *net.UDPAddr
name string
writer bytes.Buffer
errCode ErrorCode
errMsg string
size *int64
tmode TransferMode
}
func (r *readRequestMock) Addr() *net.UDPAddr { return r.addr }
func (r *readRequestMock) Name() string { return r.name }
func (r *readRequestMock) Write(p []byte) (int, error) { return r.writer.Write(p) }
func (r *readRequestMock) WriteSize(i int64) { r.size = &i }
func (r *readRequestMock) WriteError(c ErrorCode, m string) {
r.errCode = c
r.errMsg = m
}
func (r *readRequestMock) TransferMode() TransferMode { return r.tmode }
func TestFileServer_ServeTFTP(t *testing.T) {
text := getTestData(t, "text")
cases := []struct {
name string
reqName string
expectedData []byte
expectedSize *int64
expectedErrorCode ErrorCode
expectedErrorMsg string
}{
{
name: "file exists",
reqName: "text",
expectedData: text,
expectedSize: ptrInt64(int64(len(text))),
},
{
name: "file does not exist",
reqName: "other",
expectedErrorCode: ErrCodeFileNotFound,
expectedErrorMsg: `File "other" does not exist`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
fs := FileServer("testdata")
req := readRequestMock{name: c.reqName}
fs.ServeTFTP(&req)
// Data
if !reflect.DeepEqual(c.expectedData, req.writer.Bytes()) {
t.Errorf("expected data to be %s, but it was %s", c.expectedData, req.writer.String())
}
// Size
if !reflect.DeepEqual(c.expectedSize, req.size) {
if c.expectedSize == nil || req.size == nil {
t.Errorf("expected size to be %v, but it was %v", c.expectedSize, req.size)
} else {
t.Errorf("expected size to be %v, but it was %v", *c.expectedSize, *req.size)
}
}
// Error Code
if c.expectedErrorCode != req.errCode {
t.Errorf("expected error code to be %s, but it was %s", c.expectedErrorCode, req.errCode)
}
// Error Message
if c.expectedErrorMsg != req.errMsg {
t.Errorf("expected error msg to be %q, but it was %q", c.expectedErrorMsg, req.errMsg)
}
})
}
}
type writeRequestMock struct {
addr *net.UDPAddr
name string
reader bytes.Buffer
errCode ErrorCode
errMsg string
size *int64
tmode TransferMode
}
func (r *writeRequestMock) Addr() *net.UDPAddr { return r.addr }
func (r *writeRequestMock) Name() string { return r.name }
func (r *writeRequestMock) Read(p []byte) (int, error) { return r.reader.Read(p) }
func (r *writeRequestMock) Size() (int64, error) {
if r.size != nil {
return *r.size, nil
}
return 0, ErrSizeNotReceived
}
func (r *writeRequestMock) WriteError(c ErrorCode, m string) {
r.errCode = c
r.errMsg = m
}
func (r *writeRequestMock) TransferMode() TransferMode { return r.tmode }
func TestFileServer_ReceiveTFTP(t *testing.T) {
text := getTestData(t, "text")
cases := []struct {
name string
reqName string
data []byte
expectedFilename string
expectedData []byte
expectedErrorCode ErrorCode
expectedErrorMsg string
}{
{
name: "success",
reqName: "text",
data: text,
expectedData: text,
},
{
name: "fail",
reqName: "",
expectedData: []byte{},
expectedErrorCode: ErrCodeAccessViolation,
expectedErrorMsg: `Cannot create file "."`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
fs := FileServer(dir)
req := writeRequestMock{name: c.reqName}
req.reader.Write(c.data)
fs.ReceiveTFTP(&req)
// Data
data, _ := ioutil.ReadFile(filepath.Join(dir, c.reqName))
if !reflect.DeepEqual(c.expectedData, data) {
t.Errorf("expected data to be %s, but it was %s", c.expectedData, data)
}
// Error Code
if c.expectedErrorCode != req.errCode {
t.Errorf("expected error code to be %s, but it was %s", c.expectedErrorCode, req.errCode)
}
// Error Message
if c.expectedErrorMsg != req.errMsg {
t.Errorf("expected error msg to be %q, but it was %q", c.expectedErrorMsg, req.errMsg)
}
})
}
}