-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil_test.go
328 lines (304 loc) · 9.54 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// oerc, alias oer-collector
// Copyright (C) 2021-2024 emschu[aet]mailbox.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program.
// If not, see <https://www.gnu.org/licenses/>.
package main
import (
"strings"
"sync/atomic"
"testing"
"time"
)
func TestGenerateDateRange(t *testing.T) {
if len(*generateDateRangeInPastAndFuture(0, 0)) != 1 {
t.Error("invalid date range generated")
}
if len(*generateDateRangeInPastAndFuture(1, 0)) != 2 {
t.Error("invalid date range generated")
}
if len(*generateDateRangeInPastAndFuture(0, 1)) != 2 {
t.Error("invalid date range generated")
}
if len(*generateDateRangeInPastAndFuture(1, 1)) != 3 {
t.Error("invalid date range generated")
}
if len(*generateDateRangeInPastAndFuture(10, 10)) != 21 {
t.Error("invalid date range generated")
}
}
func TestGenerateDateRangeBetweenDates(t *testing.T) {
today := time.Now()
dates := generateDateRangeBetweenDates(today, today)
if len(*dates) != 1 {
t.Errorf("one day expected, was: %d", len(*dates))
}
tomorrow := today.Add(24 * time.Hour)
dates2 := generateDateRangeBetweenDates(today, tomorrow)
if len(*dates2) != 2 {
t.Errorf("two days expected, was: %d", len(*dates2))
}
dates3 := generateDateRangeBetweenDates(tomorrow, today)
if len(*dates3) != 2 {
t.Errorf("two days expected, was: %d", len(*dates3))
}
}
func TestTrim(t *testing.T) {
in := "\n\t\t\t\t\t\t\tZDX-Morgenmagazin\t\t\t\t\t\tn\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tModeration: X Y, Z D und F A\t\t\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
trimString := trimAndSanitizeString(in)
if strings.Contains(trimString, "\t") {
t.Error("Invalid tab found!")
}
if strings.Contains(trimString, "\n") {
t.Error("Invalid newline found!")
}
}
func TestTrimAndSanitizeString(t *testing.T) {
// they should all evaluate to "test"
var inputStrings = []string{" test", "test ", "test", " test", "test ", " test "}
for _, v := range inputStrings {
if "test" != trimAndSanitizeString(v) {
t.Error("Invalid trim and sanitizing function")
}
}
var maliciousStrings = []string{"test<script></script>", "test<script>", "<script>alert('hello')</script>test"}
for _, v := range maliciousStrings {
if "test" != trimAndSanitizeString(v) {
t.Errorf("Invalid return value of trim and sanitizing function. input: '%s', output: '%s'", v, trimAndSanitizeString(v))
}
}
if "<b>test</b>" != trimAndSanitizeString("<b>test</b> ") {
t.Errorf("Invalid return value of trim and sanitizing function.")
}
}
func TestGetProxy(t *testing.T) {
// invalid proxy settings -> fallback to http.ProxyFromEnvironment expected
appConf.ProxyURL = ""
if getHTTPProxy() == nil {
t.Errorf("proxy should NOT be nil")
}
appConf.ProxyURL = " "
if getHTTPProxy() == nil {
t.Errorf("proxy should NOT be nil")
}
appConf.ProxyURL = "test"
if getHTTPProxy() == nil {
t.Error("proxy should NOT be nil")
}
// missing port
appConf.ProxyURL = "http://localhost"
if getHTTPProxy() == nil {
t.Error("proxy should NOT be nil")
}
// valid example, happy case
appConf.ProxyURL = "http://localhost:7676"
if getHTTPProxy() == nil {
t.Error("proxy should NOT be nil")
}
// invalid port number
appConf.ProxyURL = "http://localhost:767676"
if getHTTPProxy() == nil {
t.Error("proxy should NOT be nil")
}
}
func TestGetBaseCollector(t *testing.T) {
host := []string{"example.com"}
collector := baseCollector(host)
if collector == nil {
t.Error("Collector should not be nil")
}
if len(collector.AllowedDomains) == 0 || collector.AllowedDomains[0] != host[0] {
t.Error("Allowed collector does not allow specified domain")
}
}
func TestConnectivity(t *testing.T) {
check, err := connectivityCheck()
if err != nil || !check {
t.Error("Basic connection test failed")
}
}
func TestErrorRegistering(t *testing.T) {
resetErr()
errorCount := atomic.LoadUint64(&globalErrorCounter)
if errorCount != 0 {
t.Error("Error counter is expected to be 0!")
}
incrErr()
errorCount = atomic.LoadUint64(&globalErrorCounter)
if errorCount != 1 {
t.Error("Error counter is expected to be 1")
}
resetErr()
checkErr() // should do nothing, because 1 < errorThreshold
errorCount = atomic.LoadUint64(&globalErrorCounter)
if errorCount != 0 {
t.Error("Error counter is expected to be 0!")
}
}
func TestHash(t *testing.T) {
got := buildHash([]string{"3", "2"})
if got == "" {
t.Errorf("empty hash! %s", got)
}
if len(got) != 32 {
t.Errorf("invalid length != 32 of hash")
}
}
func TestAppLog(t *testing.T) {
setupInMemoryDbForTesting()
appLog("Test example")
db := getDb()
var entry LogEntry
db.Model(&LogEntry{}).Last(&entry)
if entry.Message != "Test example" {
t.Fatalf("Simple app log test failed")
}
}
func TestIsRecentlyUpdated(t *testing.T) {
if (&ProgramEntry{LastCheck: nil}).isRecentlyUpdated() || (&ProgramEntry{LastCheck: &time.Time{}}).isRecentlyUpdated() {
t.Fatalf("cannot be updated if last check is nil")
}
const i = 15
appConf.TimeToRefreshInMinutes = i
appConf.ForceUpdate = false
fakeLastCheckTime := time.Now().Add(-i*time.Minute - 1*time.Second)
fakeLastCheckTime2 := time.Now().Add(-i * time.Minute)
fakeLastCheckTime3 := time.Now().Add(-i*time.Minute + 1*time.Second)
if (&ProgramEntry{LastCheck: &fakeLastCheckTime}).isRecentlyUpdated() ||
(&ProgramEntry{LastCheck: &fakeLastCheckTime2}).isRecentlyUpdated() ||
!(&ProgramEntry{LastCheck: &fakeLastCheckTime3}).isRecentlyUpdated() {
t.Fatalf("time range check failed")
}
appConf.ForceUpdate = true
if (&ProgramEntry{LastCheck: &fakeLastCheckTime}).isRecentlyUpdated() ||
(&ProgramEntry{LastCheck: &fakeLastCheckTime2}).isRecentlyUpdated() ||
(&ProgramEntry{LastCheck: &fakeLastCheckTime3}).isRecentlyUpdated() {
t.Fatalf("force-update check failed")
}
}
func TestIsRecentlyFetched(t *testing.T) {
setupInMemoryDbForTesting()
appConf.ForceUpdate = true
if isRecentlyFetched() {
t.Fatalf("Expected recent fetch is forced")
}
appConf.ForceUpdate = false
if isRecentlyFetched() {
t.Fatalf("Expected recent fetch did not take place")
}
setSetting(settingKeyLastFetch, time.Now().Format(time.RFC3339))
if !isRecentlyFetched() {
t.Fatalf("Expected recent fetch took place")
}
}
func TestClearOldRecommendations(t *testing.T) {
setupInMemoryDbForTesting()
db := getDb()
oldRec := time.Now().Add(-1 * time.Minute)
newRec := time.Now().Add(20 * time.Minute)
db.Create(&Recommendation{ProgramEntryID: 123, ChannelID: 4, StartDateTime: &oldRec})
db.Create(&Recommendation{ProgramEntryID: 123, ChannelID: 4, StartDateTime: &newRec})
var counter int64
db.Model(&Recommendation{}).Count(&counter)
if counter != 2 {
t.Fatalf("Test logic fail")
}
ClearOldRecommendations()
db.Model(&Recommendation{}).Count(&counter)
if counter != 1 {
t.Fatalf("One entry should be deleted")
}
ClearRecommendations()
db.Model(&Recommendation{}).Count(&counter)
if counter != 0 {
t.Fatalf("There should be zero entries after cleanup")
}
}
func TestConsiderTagExists(t *testing.T) {
setupInMemoryDbForTesting()
verboseGlobal = true
var pe ProgramEntry
if pe.Tags != "" {
t.Fatalf("There should be no tags in a new program entry!")
}
emptyStr := ""
testTag := "test"
pe.considerTagExists(&emptyStr)
if pe.Tags != "" {
t.Fatalf("Empty string is not a tag")
}
pe.considerTagExists(&testTag)
if pe.Tags != "test" {
t.Fatalf("There should be a new tag 'test'")
}
pe.considerTagExists(&testTag)
if pe.Tags != "test" {
t.Fatalf("There should be a new tag 'test'")
}
testTag2 := "test2"
pe.considerTagExists(&testTag2)
if pe.Tags != "test;test2" {
t.Fatalf("There should be a new tag 'test2'")
}
}
func TestChunkStringSlice(t *testing.T) {
slice1 := []string{}
if len(chunkStringSlice(slice1, 0)) != 0 {
t.Fatalf("Invalid handling of empty slice of chunk size 0")
}
if len(chunkStringSlice(slice1, 1)) != 0 {
t.Fatalf("Invalid handling of empty slice of chunk size 1")
}
slice2 := []string{"", ""}
if len(chunkStringSlice(slice2, 0)) != 0 {
t.Fatalf("Invalid handling of slice(2) of chunk size 0")
}
if len(chunkStringSlice(slice2, 1)) != 2 {
t.Fatalf("Invalid handling of slice with two elements")
}
if len(chunkStringSlice(slice2, 2)) != 1 {
t.Fatalf("Invalid handling of slice with two elements")
}
slice3 := []string{"", "", ""}
if len(chunkStringSlice(slice3, 0)) != 0 {
t.Fatalf("Invalid handling of slice(3) of chunk size 0")
}
if len(chunkStringSlice(slice3, 1)) != 3 {
t.Fatalf("Invalid handling of slice with three elements")
}
if len(chunkStringSlice(slice3, 2)) != 2 {
t.Fatalf("Invalid handling of slice with three elements")
}
}
func TestParseDate(t *testing.T) {
setupInMemoryDbForTesting()
location, _ := time.LoadLocation(defaultAppConfig().TimeZone)
date, fail := parseDate("2022-02-22T10:10:11Z", location)
if fail || date.IsZero() {
t.Fatalf("invalid date")
}
invalidDates := []string{
"",
"1",
"2022-02-22T10:10:11",
"2022-02-22 10:10:11Z",
"2022-02-22 10:10:11",
}
for _, invalidDate := range invalidDates {
date, fail2 := parseDate(invalidDate, location)
if !fail2 || !date.IsZero() {
t.Fatalf("invalid date parsing result")
}
}
}