-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathconfig_file_test.go
More file actions
91 lines (76 loc) · 2.48 KB
/
Copy pathconfig_file_test.go
File metadata and controls
91 lines (76 loc) · 2.48 KB
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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestSetBlockListFromFileContinuesPastUnchangedFile(t *testing.T) {
oldConfig := *config
oldBlockListFileLastMod := blockListFileLastMod
defer func() {
tmpConf := oldConfig
config = &tmpConf
blockListFileLastMod = oldBlockListFileLastMod
EraseSyncMap(&blockListCompiled)
}()
EraseSyncMap(&blockListCompiled)
blockListFileLastMod = make(map[string]int64)
dir := t.TempDir()
firstFile := filepath.Join(dir, "first.txt")
secondFile := filepath.Join(dir, "second.txt")
if err := os.WriteFile(firstFile, []byte("OldAgent\n"), 0644); err != nil {
t.Fatalf("failed to write first file: %v", err)
}
if err := os.WriteFile(secondFile, []byte("NewAgent\n"), 0644); err != nil {
t.Fatalf("failed to write second file: %v", err)
}
firstStat, err := os.Stat(firstFile)
if err != nil {
t.Fatalf("failed to stat first file: %v", err)
}
blockListFileLastMod[firstFile] = firstStat.ModTime().Unix()
tmpConf := oldConfig
config = &tmpConf
config.BlockListFile = []string{firstFile, secondFile}
if !SetBlockListFromFile() {
t.Fatal("SetBlockListFromFile should succeed")
}
if _, exists := blockListCompiled.Load("NewAgent"); !exists {
t.Fatal("second changed file should still be processed")
}
}
func TestSetIPBlockListFromFileContinuesPastUnchangedFile(t *testing.T) {
oldConfig := *config
oldIPBlockListFileLastMod := ipBlockListFileLastMod
defer func() {
tmpConf := oldConfig
config = &tmpConf
ipBlockListFileLastMod = oldIPBlockListFileLastMod
EraseSyncMap(&ipBlockListCompiled)
}()
EraseSyncMap(&ipBlockListCompiled)
ipBlockListFileLastMod = make(map[string]int64)
dir := t.TempDir()
firstFile := filepath.Join(dir, "first.txt")
secondFile := filepath.Join(dir, "second.txt")
if err := os.WriteFile(firstFile, []byte("1.1.1.1\n"), 0644); err != nil {
t.Fatalf("failed to write first file: %v", err)
}
if err := os.WriteFile(secondFile, []byte("2.2.2.2\n"), 0644); err != nil {
t.Fatalf("failed to write second file: %v", err)
}
firstStat, err := os.Stat(firstFile)
if err != nil {
t.Fatalf("failed to stat first file: %v", err)
}
ipBlockListFileLastMod[firstFile] = firstStat.ModTime().Unix()
tmpConf := oldConfig
config = &tmpConf
config.IPBlockListFile = []string{firstFile, secondFile}
if !SetIPBlockListFromFile() {
t.Fatal("SetIPBlockListFromFile should succeed")
}
if _, exists := ipBlockListCompiled.Load("2.2.2.2"); !exists {
t.Fatal("second changed IP file should still be processed")
}
}