diff options
| author | Shulhan <ms@kilabit.info> | 2022-06-09 23:44:02 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-06-09 23:44:02 +0700 |
| commit | 5c354863c9c6fcc080f2407e6f7adea41fa4371d (patch) | |
| tree | b892e13340335436e6a785a9267e080f937b10f8 | |
| parent | 03ae9f6bfde5262b44d2318cc1162c20d867a3f6 (diff) | |
| download | rescached-5c354863c9c6fcc080f2407e6f7adea41fa4371d.tar.xz | |
all: add integration tests for HTTP API related to block.d
The test is conducted by running the server and call the HTTP API using
the Client methods.
| l--------- | _test/etc/rescached/block.d | 1 | ||||
| -rw-r--r-- | _test/etc/rescached/block.d/.a.block | 1 | ||||
| -rw-r--r-- | _test/etc/rescached/block.d/.b.block | 1 | ||||
| -rw-r--r-- | _test/etc/rescached/block.d/.c.block | 1 | ||||
| -rw-r--r-- | _test/etc/rescached/rescached.cfg | 18 | ||||
| -rw-r--r-- | client_test.go | 149 | ||||
| -rw-r--r-- | environment_test.go | 20 | ||||
| -rw-r--r-- | rescached_test.go | 96 | ||||
| -rw-r--r-- | testdata/rescached.cfg.test.out | 20 |
9 files changed, 277 insertions, 30 deletions
diff --git a/_test/etc/rescached/block.d b/_test/etc/rescached/block.d deleted file mode 120000 index 1ad6a53..0000000 --- a/_test/etc/rescached/block.d +++ /dev/null @@ -1 +0,0 @@ -../../../_sys/etc/rescached/block.d/
\ No newline at end of file diff --git a/_test/etc/rescached/block.d/.a.block b/_test/etc/rescached/block.d/.a.block new file mode 100644 index 0000000..38e950e --- /dev/null +++ b/_test/etc/rescached/block.d/.a.block @@ -0,0 +1 @@ +127.0.0.1 a.block diff --git a/_test/etc/rescached/block.d/.b.block b/_test/etc/rescached/block.d/.b.block new file mode 100644 index 0000000..311fa7e --- /dev/null +++ b/_test/etc/rescached/block.d/.b.block @@ -0,0 +1 @@ +127.0.0.1 b.block diff --git a/_test/etc/rescached/block.d/.c.block b/_test/etc/rescached/block.d/.c.block new file mode 100644 index 0000000..0dd6a45 --- /dev/null +++ b/_test/etc/rescached/block.d/.c.block @@ -0,0 +1 @@ +127.0.0.1 c.block diff --git a/_test/etc/rescached/rescached.cfg b/_test/etc/rescached/rescached.cfg index 5db7c9f..fd11806 100644 --- a/_test/etc/rescached/rescached.cfg +++ b/_test/etc/rescached/rescached.cfg @@ -12,17 +12,17 @@ file.resolvconf= debug=1 wui.listen = 127.0.0.1:5381 -[block.d "pgl.yoyo.org"] -name = pgl.yoyo.org -url = http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext +[block.d "a.block"] +name = a.block +url = http://127.0.0.1:11180/hosts/a -[block.d "winhelp2002.mvps.org"] -name = winhelp2002.mvps.org -url = http://winhelp2002.mvps.org/hosts.txt +[block.d "b.block"] +name = b.block +url = http://127.0.0.1:11180/hosts/b -[block.d "someonewhocares.org"] -name = someonewhocares.org -url = http://someonewhocares.org/hosts/hosts +[block.d "c.block"] +name = c.block +url = http://127.0.0.1:11180/hosts/c [dns "server"] parent=udp://10.8.0.1 diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..8d1d3d9 --- /dev/null +++ b/client_test.go @@ -0,0 +1,149 @@ +// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-or-later + +package rescached + +import ( + "os" + "testing" + "time" + + "github.com/shuLhan/share/lib/test" +) + +func TestClient_BlockdEnable(t *testing.T) { + type testCase struct { + desc string + name string + expError string + expBlockd Blockd + } + + var ( + cases []testCase + c testCase + gotBlockd *Blockd + err error + ) + + cases = []testCase{{ + desc: "With invalid block.d name", + name: "xxx", + expError: "BlockdEnable: 400 hosts block.d name not found: xxx", + }, { + desc: "With valid block.d name", + name: "a.block", + expBlockd: Blockd{ + Name: "a.block", + URL: "http://127.0.0.1:11180/hosts/a", + IsEnabled: true, + }, + }} + + for _, c = range cases { + gotBlockd, err = resc.BlockdEnable(c.name) + if err != nil { + test.Assert(t, "error", c.expError, err.Error()) + continue + } + + gotBlockd.LastUpdated = "" + + test.Assert(t, c.desc, c.expBlockd, *gotBlockd) + } +} + +func TestClient_BlockdDisable(t *testing.T) { + type testCase struct { + desc string + name string + expError string + expBlockd Blockd + } + + var ( + cases []testCase + c testCase + gotBlockd *Blockd + err error + ) + + cases = []testCase{{ + desc: "With invalid block.d name", + name: "xxx", + expError: "BlockdDisable: 400 hosts block.d name not found: xxx", + }, { + desc: "With valid block.d name", + name: "a.block", + expBlockd: Blockd{ + Name: "a.block", + URL: "http://127.0.0.1:11180/hosts/a", + IsEnabled: false, + }, + }} + + for _, c = range cases { + gotBlockd, err = resc.BlockdDisable(c.name) + if err != nil { + test.Assert(t, "error", c.expError, err.Error()) + continue + } + + gotBlockd.LastUpdated = "" + + test.Assert(t, c.desc, c.expBlockd, *gotBlockd) + } +} + +func TestClient_BlockdFetch(t *testing.T) { + var ( + affectedBlockd = testEnv.HostBlockd["a.block"] + + gotBlockd *Blockd + expBlockd *Blockd + expString string + gotBytes []byte + err error + ) + + // Revert the content of a.block. + t.Cleanup(func() { + err = os.WriteFile(affectedBlockd.fileDisabled, []byte("127.0.0.1 a.block\n"), 0644) + }) + + expString = "BlockdFetch: 400 httpApiBlockdFetch: unknown hosts block.d name: xxx" + + gotBlockd, err = resc.BlockdFetch("xxx") + if err != nil { + test.Assert(t, "error", expString, err.Error()) + } else { + test.Assert(t, "BlockdFetch", expBlockd, gotBlockd) + } + + expBlockd = &Blockd{ + Name: "a.block", + URL: "http://127.0.0.1:11180/hosts/a", + IsEnabled: false, + } + + // Make the block.d last updated less than 7 days ago. + testEnv.HostBlockd["a.block"].lastUpdated = time.Now().Add(-1 * 10 * 24 * time.Hour) + + gotBlockd, err = resc.BlockdFetch("a.block") + if err != nil { + t.Fatal(err) + } + + expBlockd.LastUpdated = gotBlockd.LastUpdated + + test.Assert(t, "BlockdFetch", expBlockd, gotBlockd) + + gotBytes, err = os.ReadFile(affectedBlockd.fileDisabled) + if err != nil { + t.Fatal(err) + } + + expString = "127.0.0.2 a.block\n" + + test.Assert(t, "BlockdFetch", expString, string(gotBytes)) +} diff --git a/environment_test.go b/environment_test.go index 99d0950..467bd5b 100644 --- a/environment_test.go +++ b/environment_test.go @@ -74,17 +74,17 @@ func TestLoadEnvironment(t *testing.T) { WUIListen: "127.0.0.1:5381", HostBlockd: map[string]*Blockd{ - "pgl.yoyo.org": &Blockd{ - Name: "pgl.yoyo.org", - URL: `http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext`, + "a.block": &Blockd{ + Name: "a.block", + URL: "http://127.0.0.1:11180/hosts/a", }, - "winhelp2002.mvps.org": &Blockd{ - Name: "winhelp2002.mvps.org", - URL: `http://winhelp2002.mvps.org/hosts.txt`, + "b.block": &Blockd{ + Name: "b.block", + URL: "http://127.0.0.1:11180/hosts/b", }, - "someonewhocares.org": &Blockd{ - Name: "someonewhocares.org", - URL: `http://someonewhocares.org/hosts/hosts`, + "c.block": &Blockd{ + Name: "c.block", + URL: "http://127.0.0.1:11180/hosts/c", }, }, HttpdOptions: &libhttp.ServerOptions{ @@ -130,5 +130,5 @@ func TestLoadEnvironment(t *testing.T) { t.Fatal(err) } - test.Assert(t, "Write", expBuffer, gotBuffer.Bytes()) + test.Assert(t, "Write", string(expBuffer), gotBuffer.String()) } diff --git a/rescached_test.go b/rescached_test.go new file mode 100644 index 0000000..ebf480f --- /dev/null +++ b/rescached_test.go @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-or-later + +package rescached + +import ( + "log" + "os" + "testing" + + libhttp "github.com/shuLhan/share/lib/http" +) + +const ( + blockdServerAddress = "127.0.0.1:11180" +) + +var ( + testEnv *Environment + testServer *Server + resc *Client +) + +func TestMain(m *testing.M) { + var ( + err error + testStatus int + ) + + go mockBlockdServer() + + testEnv, err = LoadEnvironment("_test", "/etc/rescached/rescached.cfg") + if err != nil { + log.Fatal(err) + } + + testServer, err = New(testEnv) + if err != nil { + log.Fatal(err) + } + + err = testServer.Start() + if err != nil { + log.Fatal(err) + } + + defer testServer.Stop() + + resc = NewClient("http://"+testEnv.WUIListen, false) + + testStatus = m.Run() + + os.Exit(testStatus) +} + +func mockBlockdServer() { + var ( + serverOpts = libhttp.ServerOptions{ + Address: blockdServerAddress, + } + epHostsA = libhttp.Endpoint{ + Path: "/hosts/a", + Method: libhttp.RequestMethodGet, + RequestType: libhttp.RequestTypeNone, + ResponseType: libhttp.ResponseTypePlain, + Call: func(_ *libhttp.EndpointRequest) ([]byte, error) { + return []byte("127.0.0.2 a.block\n"), nil + }, + } + + mockServer *libhttp.Server + err error + ) + + mockServer, err = libhttp.NewServer(&serverOpts) + if err != nil { + log.Fatal(err) + } + + err = mockServer.RegisterEndpoint(&epHostsA) + if err != nil { + log.Fatal(err) + } + + defer func() { + err = mockServer.Stop(0) + if err != nil { + log.Fatal(err) + } + }() + + err = mockServer.Start() + if err != nil { + log.Fatal(err) + } +} diff --git a/testdata/rescached.cfg.test.out b/testdata/rescached.cfg.test.out index e55d29c..8f36010 100644 --- a/testdata/rescached.cfg.test.out +++ b/testdata/rescached.cfg.test.out @@ -3,22 +3,22 @@ file.resolvconf = wui.listen = 127.0.0.1:5381 debug = 1 -[block.d "pgl.yoyo.org"] -name = pgl.yoyo.org -url = http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext +[block.d "a.block"] +name = a.block +url = http://127.0.0.1:11180/hosts/a -[block.d "someonewhocares.org"] -name = someonewhocares.org -url = http://someonewhocares.org/hosts/hosts +[block.d "b.block"] +name = b.block +url = http://127.0.0.1:11180/hosts/b + +[block.d "c.block"] +name = c.block +url = http://127.0.0.1:11180/hosts/c [block.d "test"] name = test url = http://someurl -[block.d "winhelp2002.mvps.org"] -name = winhelp2002.mvps.org -url = http://winhelp2002.mvps.org/hosts.txt - [dns "server"] listen = 127.0.0.1:5350 tls.certificate = |
