-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockd_test.go
104 lines (92 loc) · 2.32 KB
/
blockd_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
// SPDX-FileCopyrightText: 2020 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package rescached
import (
"os"
"path/filepath"
"testing"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
func TestBlockd_init(t *testing.T) {
type testCase struct {
desc string
hb Blockd
exp Blockd
}
var (
testDirBase = t.TempDir()
pathDirBlock = filepath.Join(testDirBase, dirBlock)
fileEnabled = "fileEnabled"
fileDisabled = "fileDisabled"
fileNotExist = "fileNotExist"
hostsFileEnabled = filepath.Join(pathDirBlock, fileEnabled)
hostsFileDisabled = filepath.Join(pathDirBlock, "."+fileDisabled)
fiEnabled os.FileInfo
fiDisabled os.FileInfo
cases []testCase
c testCase
err error
)
err = os.MkdirAll(pathDirBlock, 0700)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(hostsFileEnabled, []byte("127.0.0.2 localhost"), 0600)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(hostsFileDisabled, []byte("127.0.0.2 localhost"), 0600)
if err != nil {
t.Fatal(err)
}
fiEnabled, err = os.Stat(hostsFileEnabled)
if err != nil {
t.Fatal(err)
}
fiDisabled, err = os.Stat(hostsFileDisabled)
if err != nil {
t.Fatal(err)
}
cases = []testCase{{
desc: "With hosts block file enabled",
hb: Blockd{
Name: fileEnabled,
},
exp: Blockd{
Name: fileEnabled,
lastUpdated: fiEnabled.ModTime(),
LastUpdated: fiEnabled.ModTime().Format(lastUpdatedFormat),
file: hostsFileEnabled,
fileDisabled: filepath.Join(pathDirBlock, "."+fileEnabled),
IsEnabled: true,
isFileExist: true,
},
}, {
desc: "With hosts block file disabled",
hb: Blockd{
Name: fileDisabled,
},
exp: Blockd{
Name: fileDisabled,
lastUpdated: fiDisabled.ModTime(),
LastUpdated: fiDisabled.ModTime().Format(lastUpdatedFormat),
file: filepath.Join(pathDirBlock, fileDisabled),
fileDisabled: hostsFileDisabled,
isFileExist: true,
},
}, {
desc: "With hosts block file not exist",
hb: Blockd{
Name: fileNotExist,
},
exp: Blockd{
Name: fileNotExist,
file: filepath.Join(pathDirBlock, fileNotExist),
fileDisabled: filepath.Join(pathDirBlock, "."+fileNotExist),
},
}}
for _, c = range cases {
c.hb.init(pathDirBlock)
test.Assert(t, c.desc, c.exp, c.hb)
}
}