aboutsummaryrefslogtreecommitdiff
path: root/blockd_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-05-22 21:29:35 +0700
committerShulhan <ms@kilabit.info>2022-05-22 21:51:47 +0700
commit0b92dbb8463dee31c005c59f67c2bf754a9eae12 (patch)
treea95162fc3aaa88ce772f9b7c8b79747c225a0ba8 /blockd_test.go
parent89494ecacdd03426da17def52e35a1a66c9630eb (diff)
downloadrescached-0b92dbb8463dee31c005c59f67c2bf754a9eae12.tar.xz
all: export and rename the hostsBlock type to Blockd
In the Environment, we have a field HostsBlocks that reference an unexported type hostsFile. In order for any package to be able to use this field, we need to export this type. While at it, we rename the type from hostsBlock to Blockd to make it simple and consistent with name.
Diffstat (limited to 'blockd_test.go')
-rw-r--r--blockd_test.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/blockd_test.go b/blockd_test.go
new file mode 100644
index 0000000..f1d6abf
--- /dev/null
+++ b/blockd_test.go
@@ -0,0 +1,104 @@
+// SPDX-FileCopyrightText: 2020 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package rescached
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/shuLhan/share/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)
+ }
+}