forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
277 lines (250 loc) · 6.35 KB
/
util_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"bufio"
"bytes"
"io"
"testing"
)
func TestReadLine(t *testing.T) {
testData := []struct {
text string
lines []string
}{
{"one\ntwo", []string{"one", "two"}},
{"three\r\nfour\n", []string{"three", "four"}},
{"five\r\nsix\r\n", []string{"five", "six"}},
{"seven", []string{"seven"}},
{"eight\n", []string{"eight"}},
{"\r\n", []string{""}},
{"\n", []string{""}},
}
for _, td := range testData {
raw := bytes.NewBufferString(td.text)
rd := bufio.NewReader(raw)
for i, line := range td.lines {
l, err := ReadLine(rd)
if err != nil {
t.Fatalf("%d read error %v got: %s\ntext: %s\n", i+1, err, l, td.text)
}
if line != l {
t.Fatalf("%d read got: %s (%d) should be: %s (%d)\n", i+1, l, len(l), line, len(line))
}
}
_, err := ReadLine(rd)
if err != io.EOF {
t.Error("ReadLine past end should return EOF")
}
}
}
func TestASCIIToUpper(t *testing.T) {
testData := []struct {
raw []byte
upper []byte
}{
{[]byte("foobar"), []byte("FOOBAR")},
{[]byte("fOoBAr"), []byte("FOOBAR")},
{[]byte("..fOoBAr\n"), []byte("..FOOBAR\n")},
}
for _, td := range testData {
up := ASCIIToUpper(td.raw)
if !bytes.Equal(up, td.upper) {
t.Errorf("raw: %s, upper: %s\n", string(up), string(td.upper))
}
}
}
func TestASCIIToLower(t *testing.T) {
testData := []struct {
raw []byte
upper []byte
}{
{[]byte("FOOBAR"), []byte("foobar")},
{[]byte("fOoBAr"), []byte("foobar")},
{[]byte("..fOoBAr\n"), []byte("..foobar\n")},
}
for _, td := range testData {
low := ASCIIToLower(td.raw)
if !bytes.Equal(low, td.upper) {
t.Errorf("raw: %s, upper: %s\n", string(low), string(td.upper))
}
}
}
func TestIsDigit(t *testing.T) {
for i := 0; i < 10; i++ {
digit := '0' + byte(i)
letter := 'a' + byte(i)
if IsDigit(digit) != true {
t.Errorf("%c should return true", digit)
}
if IsDigit(letter) == true {
t.Errorf("%c should return false", letter)
}
}
}
func TestIsSpace(t *testing.T) {
testData := []struct {
c byte
is bool
}{
{'a', false},
{'B', false},
{' ', true},
{'\r', true},
{'\n', true},
}
for _, td := range testData {
if IsSpace(td.c) != td.is {
t.Errorf("%v isspace wrong", rune(td.c))
}
}
}
func TestTrimSpace(t *testing.T) {
testData := []struct {
old string
trimed string
}{
{"hello", "hello"},
{" hello", "hello"},
{" hello\r\n ", "hello"},
{" hello \t ", "hello"},
{"", ""},
{"\r\n", ""},
}
for _, td := range testData {
trimed := string(TrimSpace([]byte(td.old)))
if trimed != td.trimed {
t.Errorf("%s trimmed to %s, wrong", td.old, trimed)
}
}
}
func TestCopyN(t *testing.T) {
testStr := "hello world"
src := bytes.NewBufferString(testStr)
dst := new(bytes.Buffer)
buf := make([]byte, 5)
copyN(src, dst, nil, len(testStr), buf, nil, nil)
if dst.String() != "hello world" {
t.Error("copy without pre and end failed, got:", dst.String())
}
src.Reset()
dst.Reset()
src.WriteString(testStr)
copyN(src, dst, nil, len(testStr), buf, []byte("by cyf "), nil)
if dst.String() != "by cyf hello world" {
t.Error("copy with pre no end failed, got:", dst.String())
}
src.Reset()
dst.Reset()
src.WriteString(testStr)
copyN(src, dst, nil, len(testStr), buf, []byte("by cyf "), []byte(" welcome"))
if dst.String() != "by cyf hello world welcome" {
t.Error("copy with both pre and end failed, got:", dst.String())
}
src.Reset()
dst.Reset()
src.WriteString(testStr)
copyN(src, dst, nil, len(testStr), buf, []byte("pre longer then buffer "), []byte(" welcome"))
if dst.String() != "pre longer then buffer hello world welcome" {
t.Error("copy with long pre failed, got:", dst.String())
}
src.Reset()
dst.Reset()
testStr = "34"
src.WriteString(testStr)
copyN(src, dst, nil, len(testStr), buf, []byte("12"), []byte(" welcome"))
if dst.String() != "1234 welcome" {
t.Error("copy len(pre)+size<bufLen failed, got:", dst.String())
}
src.Reset()
dst.Reset()
testStr = "2"
src.WriteString(testStr)
copyN(src, dst, nil, len(testStr), buf, []byte("1"), []byte("34"))
if dst.String() != "1234" {
t.Error("copy len(pre)+size+len(end)<bufLen failed, got:", dst.String())
}
}
func TestIsFileExists(t *testing.T) {
exists, err := isFileExists("testdata")
if err == nil {
t.Error("should return error is path is directory")
}
if exists {
t.Error("directory should return false")
}
exists, err = isFileExists("testdata/none")
if exists {
t.Error("BOOM! You've found a non-existing file!")
}
if err != nil {
t.Error("Not existing file should just return false, on error")
}
exists, err = isFileExists("testdata/file")
if !exists {
t.Error("testdata/file exists, but returns false")
}
if err != nil {
t.Error("Why error for existing file?")
}
}
func TestNewNbitIPv4Mask(t *testing.T) {
mask := []byte(NewNbitIPv4Mask(32))
for i := 0; i < 4; i++ {
if mask[i] != 0xff {
t.Error("NewNbitIPv4Mask with 32 error")
}
}
mask = []byte(NewNbitIPv4Mask(5))
if mask[0] != 0xf8 || mask[1] != 0 || mask[2] != 0 {
t.Error("NewNbitIPv4Mask with 5 error:", mask)
}
mask = []byte(NewNbitIPv4Mask(9))
if mask[0] != 0xff || mask[1] != 0x80 || mask[2] != 0 {
t.Error("NewNbitIPv4Mask with 9 error:", mask)
}
mask = []byte(NewNbitIPv4Mask(23))
if mask[0] != 0xff || mask[1] != 0xff || mask[2] != 0xfe || mask[3] != 0 {
t.Error("NewNbitIPv4Mask with 23 error:", mask)
}
mask = []byte(NewNbitIPv4Mask(28))
if mask[0] != 0xff || mask[1] != 0xff || mask[2] != 0xff || mask[3] != 0xf0 {
t.Error("NewNbitIPv4Mask with 28 error:", mask)
}
}
func TestHost2Domain(t *testing.T) {
var testData = []struct {
host string
domain string
}{
{"www.google.com", "google.com"},
{"google.com", "google.com"},
{"com.cn", "com.cn"},
{"sina.com.cn", "sina.com.cn"},
{"www.bbc.co.uk", "bbc.co.uk"},
{"apple.com.cn", "apple.com.cn"},
{"simplehost", ""},
{"192.168.1.1", ""},
}
for _, td := range testData {
dm := host2Domain(td.host)
if dm != td.domain {
t.Errorf("%s got domain %v should be %v", td.host, dm, td.domain)
}
}
}
func TestHostIsIP(t *testing.T) {
if !hostIsIP("192.168.1.1") {
t.Error("192.168.1.1 is ip")
}
if hostIsIP("256.3.5.3") {
t.Error("256.3.5.3 is not ip")
}
if hostIsIP("192.168.1.1.1") {
t.Error("192.168.1.1.1 is not ip")
}
if hostIsIP("www.google.com") {
t.Error("www.google.com is not ip")
}
if hostIsIP("foo.www.google.com") {
t.Error("foo.www.google.com is not ip")
}
}