-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
306 lines (276 loc) · 8.24 KB
/
helper_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package helper_test
import (
"bytes"
"embed"
"os"
"path/filepath"
"strings"
"testing"
"time"
"unicode/utf8"
"github.com/Defacto2/helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/encoding/charmap"
)
//go:embed testdata
var testdataFS embed.FS
func TestDetermineEncoding_Unicode(t *testing.T) {
t.Parallel()
sr := strings.NewReader("Hello world 👾!!!")
e := helper.Determine(sr)
assert.Equal(t, charmap.CodePage437, e, "wanted CP-437 but got %s", e)
}
func TestDetermineEncoding(t *testing.T) {
t.Parallel()
e := helper.Determine(nil)
assert.Nil(t, e)
sr := strings.NewReader("Hello world!")
e = helper.Determine(sr)
assert.Equal(t, charmap.ISO8859_1, e)
p := []byte("")
p = append(p, 0x1b)
p = append(p, []byte("[31mHelloWorld")...)
br := bytes.NewReader(p)
e = helper.Determine(br)
assert.Equal(t, charmap.ISO8859_1, e, "wanted ISO-8859-1 but got %s", e)
sr = strings.NewReader("\nHello world!\n")
e = helper.Determine(sr)
assert.Equal(t, charmap.ISO8859_1, e, "wanted ISO-8859-1 but got %s", e)
p = []byte("")
p = append(p, 0xb2)
p = append(p, []byte(" Hello world! ")...)
p = append(p, 0xb2)
br = bytes.NewReader(p)
e = helper.Determine(br)
assert.Equal(t, charmap.ISO8859_1, e, "wanted ISO-8859-1 but got %s", e)
p = []byte("")
p = append(p, 0x0D, 0x0E) // CP437 ♪ ♫
p = append(p, []byte(" aah bah cah")...)
br = bytes.NewReader(p)
e = helper.Determine(br)
assert.Equal(t, charmap.CodePage437, e, "wanted CP-437 but got %s", e)
const house = 0x7f
p = []byte("")
p = append(p, house)
p = append(p, []byte(" a DOS house glyph ")...)
br = bytes.NewReader(p)
e = helper.Determine(br)
assert.Equal(t, charmap.CodePage437, e, "wanted CP-437 but got %s", e)
const line = 0xc4
p = []byte("")
p = append(p, line, line, line, line, line, line)
p = append(p, []byte(" a DOS line glyph ")...)
br = bytes.NewReader(p)
e = helper.Determine(br)
assert.Equal(t, charmap.CodePage437, e, "wanted CP-437 but got %s", e)
}
func TestCookieStore(t *testing.T) {
t.Parallel()
b, err := helper.CookieStore("")
require.NoError(t, err)
l := utf8.RuneCount(b)
assert.Equal(t, 32, l)
const key = "my-secret-key"
b, err = helper.CookieStore(key)
require.NoError(t, err)
assert.Len(t, b, len(key))
}
func TestLocalIPs(t *testing.T) {
t.Parallel()
ips, err := helper.LocalIPs()
require.NoError(t, err)
assert.NotEmpty(t, ips)
// we can't test the actual IP addresses as they will be different on each machine.
}
func TestLocalHosts(t *testing.T) {
t.Parallel()
hosts, err := helper.LocalHosts()
require.NoError(t, err)
assert.NotEmpty(t, hosts)
// we can't test the actual host names as they will be different on each machine.
}
func TestIntegrity(t *testing.T) {
t.Parallel()
x, err := helper.Integrity("", embed.FS{})
require.Error(t, err)
assert.Empty(t, x)
x, err = helper.Integrity("nosuchfile", testdataFS)
require.Error(t, err)
assert.Empty(t, x)
x, err = helper.Integrity("testdata/TEST.DOC", testdataFS)
require.NoError(t, err)
assert.Equal(t, "sha384-5X6isqmILTavQSao9DigKt3O8fX1Hd6hrGJ7pUROFPYWmkKRnFuWwTnjO3h9QkWP", x)
}
func TestIntegrityFile(t *testing.T) {
t.Parallel()
x, err := helper.IntegrityFile("")
require.Error(t, err)
assert.Empty(t, x)
x, err = helper.IntegrityFile("nosuchfile")
require.Error(t, err)
assert.Empty(t, x)
x, err = helper.IntegrityFile("testdata/TEST.DOC")
require.NoError(t, err)
assert.Equal(t, "sha384-5X6isqmILTavQSao9DigKt3O8fX1Hd6hrGJ7pUROFPYWmkKRnFuWwTnjO3h9QkWP", x)
}
func TestIntegrityBytes(t *testing.T) {
t.Parallel()
x := helper.IntegrityBytes(nil)
assert.Equal(t, "sha384-OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb", x)
x = helper.IntegrityBytes([]byte("hello world"))
assert.Equal(t, "sha384-/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9", x)
}
func TestLatency(t *testing.T) {
t.Parallel()
result := helper.Latency()
now := time.Now()
assert.Less(t, *result, now)
}
func TestTimeDistance(t *testing.T) {
t.Parallel()
now := time.Now()
s := helper.TimeDistance(now, now, false)
assert.Equal(t, "less than a minute", s)
s = helper.TimeDistance(now, now.Add(time.Minute+time.Second), false)
assert.Equal(t, "1 minute", s)
s = helper.TimeDistance(now, now.Add(time.Second*2), true)
assert.Equal(t, "less than 5 seconds", s)
s = helper.TimeDistance(now, now.Add(time.Second*9), true)
assert.Equal(t, "less than 10 seconds", s)
s = helper.TimeDistance(now, now.Add(time.Second*19), true)
assert.Equal(t, "less than 20 seconds", s)
s = helper.TimeDistance(now, now.Add(time.Second*35), true)
assert.Equal(t, "half a minute", s)
s = helper.TimeDistance(now, now.Add(time.Second*60), true)
assert.Equal(t, "1 minute", s)
s = helper.TimeDistance(now, now.Add(time.Hour), true)
assert.Equal(t, "about 1 hour", s)
s = helper.TimeDistance(now, now.Add(time.Hour*24), true)
assert.Equal(t, "1 day", s)
s = helper.TimeDistance(now, now.Add(time.Hour*24*2), true)
assert.Equal(t, "2 days", s)
s = helper.TimeDistance(now, now.Add(time.Hour*24*30), true)
assert.Equal(t, "about 1 month", s)
s = helper.TimeDistance(now, now.Add(time.Hour*24*365), true)
assert.Equal(t, "about 1 year", s)
s = helper.TimeDistance(now, now.Add(time.Hour*24*500), true)
assert.Equal(t, "over 1 year", s)
s = helper.TimeDistance(now, now.Add(time.Hour*24*700), true)
assert.Equal(t, "almost 2 years", s)
s = helper.TimeDistance(now, now.Add(time.Hour*24*365*10), true)
assert.Equal(t, "10 years", s)
}
func TestAdd1(t *testing.T) {
t.Parallel()
tests := []struct {
a any
expect int64
assertion assert.ComparisonAssertionFunc
}{
{0, 1, assert.Equal},
{"xyz", 0, assert.Equal},
{123, 124, assert.Equal},
{1234567890, 1234567891, assert.Equal},
{1234567890123456789, 1234567890123456790, assert.Equal},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.expect, helper.Add1(tt.a))
})
}
}
func TestFileMatch(t *testing.T) {
t.Parallel()
_, err := helper.FileMatch("", "")
require.Error(t, err)
v, err := helper.FileMatch("helper.go", "helper.go")
require.NoError(t, err)
assert.True(t, v)
v, err = helper.FileMatch("helper_test.go", "helper.go")
require.NoError(t, err)
assert.False(t, v)
}
func TestFinds(t *testing.T) {
t.Parallel()
s := []string{"abc", "def", "ghi"}
type args struct {
name string
names []string
}
tests := []struct {
args args
expect bool
assertion assert.ComparisonAssertionFunc
}{
{args{"", nil}, false, assert.Equal},
{args{"", []string{}}, false, assert.Equal},
{args{"xyz", s}, false, assert.Equal},
{args{"def", s}, true, assert.Equal},
}
for _, tt := range tests {
t.Run(tt.args.name, func(t *testing.T) {
t.Parallel()
tt.assertion(t, tt.expect, helper.Finds(tt.args.name, tt.args.names...))
})
}
}
func TestIsFile(t *testing.T) {
t.Parallel()
self := filepath.Join(".", "helper_test.go")
tests := []struct {
name string
expect bool
assertion assert.ComparisonAssertionFunc
}{
{self, true, assert.Equal},
{"^&%#$%@#", false, assert.Equal},
{"testdata/", false, assert.Equal},
{"testdata/TEST.DOC", true, assert.Equal},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tt.assertion(t, tt.expect, helper.File(tt.name))
})
}
}
func TestIsStat(t *testing.T) {
t.Parallel()
self := filepath.Join(".", "helper_test.go")
tests := []struct {
name string
expect bool
assertion assert.ComparisonAssertionFunc
}{
{self, true, assert.Equal},
{"^&%#$%@#", false, assert.Equal},
{"testdata/", true, assert.Equal},
{"testdata/TEST.DOC", true, assert.Equal},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tt.assertion(t, tt.expect, helper.Stat(tt.name))
})
}
}
func TestBools(t *testing.T) {
t.Parallel()
assert.False(t, helper.Day(-1))
assert.False(t, helper.Day(32))
assert.True(t, helper.Day(1))
assert.False(t, helper.Year(-1))
assert.True(t, helper.Year(1970))
assert.False(t, helper.Year(time.Now().Year()+1))
}
func TestDetermineFile(t *testing.T) {
t.Parallel()
// This is a CP-437 file that can also be read as ISO-8859-1.
r, err := os.Open("testdata/PKZ80A1.TXT")
require.NoError(t, err)
defer r.Close()
e := helper.Determine(r)
assert.Equal(t, charmap.ISO8859_1, e)
}