Skip to content

Commit

Permalink
filtering: imp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Aug 11, 2023
1 parent 39e9d54 commit f1c1edd
Showing 1 changed file with 90 additions and 37 deletions.
127 changes: 90 additions & 37 deletions internal/filtering/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package filtering

import (
"io/fs"
"net"
"net/http"
"net/url"
Expand All @@ -16,6 +15,9 @@ import (
"github.com/stretchr/testify/require"
)

// testTimeout is the common timeout for tests.
const testTimeout = 5 * time.Second

// serveHTTPLocally starts a new HTTP server, that handles its index with h. It
// also gracefully closes the listener when the test under t finishes.
func serveHTTPLocally(t *testing.T, h http.Handler) (urlStr string) {
Expand Down Expand Up @@ -50,57 +52,70 @@ func serveFiltersLocally(t *testing.T, fltContent []byte) (urlStr string) {
}))
}

func TestFilters(t *testing.T) {
const content = `||example.org^$third-party
# Inline comment example
||example.com^$third-party
0.0.0.0 example.com
`

fltContent := []byte(content)
// updateAndAssert loads filter content from its URL and then asserts rules
// count.
func updateAndAssert(
t *testing.T,
dnsFilter *DNSFilter,
f *FilterYAML,
want require.BoolAssertionFunc,
wantRulesCount int,
) {
t.Helper()

addr := serveFiltersLocally(t, fltContent)
ok, err := dnsFilter.update(f)
require.NoError(t, err)
want(t, ok)

tempDir := t.TempDir()
assert.Equal(t, wantRulesCount, f.RulesCount)

filters, err := New(&Config{
DataDir: tempDir,
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
}, nil)
dir, err := os.ReadDir(filepath.Join(dnsFilter.DataDir, filterDir))
require.NoError(t, err)
require.FileExists(t, f.Path(dnsFilter.DataDir))

f := &FilterYAML{
URL: addr,
}
assert.Len(t, dir, 1)

updateAndAssert := func(t *testing.T, want require.BoolAssertionFunc, wantRulesCount int) {
var ok bool
ok, err = filters.update(f)
require.NoError(t, err)
want(t, ok)
err = dnsFilter.load(f)
require.NoError(t, err)
}

assert.Equal(t, wantRulesCount, f.RulesCount)
// newDNSFilter returns initialized dns filter instance.
func newDNSFilter(t *testing.T) (d *DNSFilter) {
t.Helper()

var dir []fs.DirEntry
dir, err = os.ReadDir(filepath.Join(tempDir, filterDir))
require.NoError(t, err)
dnsFilter, err := New(&Config{
DataDir: t.TempDir(),
HTTPClient: &http.Client{
Timeout: testTimeout,
},
}, nil)
require.NoError(t, err)

assert.Len(t, dir, 1)
return dnsFilter
}

require.FileExists(t, f.Path(tempDir))
func TestDNSFilter_Update(t *testing.T) {
const content = `||example.org^$third-party
# Inline comment example
||example.com^$third-party
0.0.0.0 example.com
`

err = filters.load(f)
require.NoError(t, err)
fltContent := []byte(content)
addr := serveFiltersLocally(t, fltContent)
f := &FilterYAML{
URL: addr,
Name: "test-filter",
}

dnsFilter := newDNSFilter(t)

t.Run("download", func(t *testing.T) {
updateAndAssert(t, require.True, 3)
updateAndAssert(t, dnsFilter, f, require.True, 3)
})

t.Run("refresh_idle", func(t *testing.T) {
updateAndAssert(t, require.False, 3)
updateAndAssert(t, dnsFilter, f, require.False, 3)
})

t.Run("refresh_actually", func(t *testing.T) {
Expand All @@ -110,13 +125,51 @@ func TestFilters(t *testing.T) {
f.URL = serveFiltersLocally(t, anotherContent)
t.Cleanup(func() { f.URL = oldURL })

updateAndAssert(t, require.True, 1)
updateAndAssert(t, dnsFilter, f, require.True, 1)
})

t.Run("load_unload", func(t *testing.T) {
err = filters.load(f)
err := dnsFilter.load(f)
require.NoError(t, err)

f.unload()
})
}

func TestFilterYAML_EnsureName(t *testing.T) {
dnsFilter := newDNSFilter(t)

t.Run("title_custom", func(t *testing.T) {
content := []byte("! Title: src-title\n||example.com^")

f := &FilterYAML{
URL: serveFiltersLocally(t, content),
Name: "user-custom",
}

updateAndAssert(t, dnsFilter, f, require.True, 1)
assert.Equal(t, "user-custom", f.Name)
})

t.Run("title_from_src", func(t *testing.T) {
content := []byte("! Title: src-title\n||example.com^")

f := &FilterYAML{
URL: serveFiltersLocally(t, content),
}

updateAndAssert(t, dnsFilter, f, require.True, 1)
assert.Equal(t, "src-title", f.Name)
})

t.Run("title_default", func(t *testing.T) {
content := []byte("||example.com^")

f := &FilterYAML{
URL: serveFiltersLocally(t, content),
}

updateAndAssert(t, dnsFilter, f, require.True, 1)
assert.Equal(t, "List 0", f.Name)
})
}

0 comments on commit f1c1edd

Please sign in to comment.