2019-06-10 08:33:19 +00:00
|
|
|
package home
|
2019-02-28 17:41:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2021-02-09 13:51:44 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-28 17:41:18 +00:00
|
|
|
)
|
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
// any is a convenient alias for interface{}.
|
|
|
|
type any = interface{}
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
// object is a convenient alias for map[string]interface{}.
|
|
|
|
type object = map[string]any
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
func TestUpgradeSchema1to2(t *testing.T) {
|
|
|
|
diskConf := testDiskConf(1)
|
2019-03-01 08:27:15 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
err := upgradeSchema1to2(&diskConf)
|
|
|
|
require.Nil(t, err)
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
require.Equal(t, diskConf["schema_version"], 2)
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
_, ok := diskConf["coredns"]
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
dnsMap, ok := diskConf["dns"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
oldDNSConf := convertToObject(t, testDNSConf(1))
|
|
|
|
newDNSConf := convertToObject(t, dnsMap)
|
|
|
|
assert.Equal(t, oldDNSConf, newDNSConf)
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2019-03-01 08:27:15 +00:00
|
|
|
oldExcludedEntries := []string{"coredns", "schema_version"}
|
|
|
|
newExcludedEntries := []string{"dns", "schema_version"}
|
2021-02-09 13:51:44 +00:00
|
|
|
oldDiskConf := testDiskConf(1)
|
|
|
|
assertEqualExcept(t, oldDiskConf, diskConf, oldExcludedEntries, newExcludedEntries)
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
func TestUpgradeSchema2to3(t *testing.T) {
|
|
|
|
diskConf := testDiskConf(2)
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
err := upgradeSchema2to3(&diskConf)
|
|
|
|
require.Nil(t, err)
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
require.Equal(t, diskConf["schema_version"], 3)
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
dnsMap, ok := diskConf["dns"]
|
|
|
|
require.True(t, ok)
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
newDNSConf := convertToObject(t, dnsMap)
|
|
|
|
bootstrapDNS := newDNSConf["bootstrap_dns"]
|
2019-02-28 17:41:18 +00:00
|
|
|
switch v := bootstrapDNS.(type) {
|
|
|
|
case []string:
|
2021-02-09 13:51:44 +00:00
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "8.8.8.8:53", v[0])
|
2019-02-28 17:41:18 +00:00
|
|
|
default:
|
2021-02-09 13:51:44 +00:00
|
|
|
t.Fatalf("wrong type for bootsrap dns: %T", v)
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 08:27:15 +00:00
|
|
|
excludedEntries := []string{"bootstrap_dns"}
|
2021-02-09 13:51:44 +00:00
|
|
|
oldDNSConf := convertToObject(t, testDNSConf(2))
|
|
|
|
assertEqualExcept(t, oldDNSConf, newDNSConf, excludedEntries, excludedEntries)
|
2019-03-01 08:27:15 +00:00
|
|
|
|
|
|
|
excludedEntries = []string{"dns", "schema_version"}
|
2021-02-09 13:51:44 +00:00
|
|
|
oldDiskConf := testDiskConf(2)
|
|
|
|
assertEqualExcept(t, oldDiskConf, diskConf, excludedEntries, excludedEntries)
|
2019-03-01 08:27:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
func convertToObject(t *testing.T, oldConf any) (newConf object) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
switch v := oldConf.(type) {
|
|
|
|
case map[any]any:
|
|
|
|
newConf = make(object, len(v))
|
2019-03-01 08:27:15 +00:00
|
|
|
for key, value := range v {
|
2021-02-09 13:51:44 +00:00
|
|
|
newConf[fmt.Sprint(key)] = value
|
2019-03-01 08:27:15 +00:00
|
|
|
}
|
2021-02-09 13:51:44 +00:00
|
|
|
case object:
|
|
|
|
newConf = make(object, len(v))
|
2019-03-01 08:27:15 +00:00
|
|
|
for key, value := range v {
|
2021-02-09 13:51:44 +00:00
|
|
|
newConf[key] = value
|
2019-03-01 08:27:15 +00:00
|
|
|
}
|
|
|
|
default:
|
2021-02-09 13:51:44 +00:00
|
|
|
t.Fatalf("dns configuration is not a map, got %T", oldConf)
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
return newConf
|
2019-03-01 08:27:15 +00:00
|
|
|
}
|
2019-02-28 17:41:18 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
// assertEqualExcept removes entries from configs and compares them.
|
|
|
|
func assertEqualExcept(t *testing.T, oldConf, newConf object, oldKeys, newKeys []string) {
|
|
|
|
t.Helper()
|
2019-03-01 08:27:15 +00:00
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
for _, k := range oldKeys {
|
|
|
|
delete(oldConf, k)
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
2021-02-09 13:51:44 +00:00
|
|
|
for _, k := range newKeys {
|
|
|
|
delete(newConf, k)
|
2019-03-01 08:27:15 +00:00
|
|
|
}
|
2021-02-09 13:51:44 +00:00
|
|
|
|
|
|
|
assert.Equal(t, oldConf, newConf)
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
func testDiskConf(schemaVersion int) (diskConf object) {
|
|
|
|
filters := []filter{
|
2019-03-01 08:27:15 +00:00
|
|
|
{
|
|
|
|
URL: "https://filters.adtidy.org/android/filters/111_optimized.txt",
|
|
|
|
Name: "Latvian filter",
|
|
|
|
RulesCount: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "https://easylist.to/easylistgermany/easylistgermany.txt",
|
|
|
|
Name: "Germany filter",
|
|
|
|
RulesCount: 200,
|
|
|
|
},
|
|
|
|
}
|
2021-02-09 13:51:44 +00:00
|
|
|
diskConf = object{
|
|
|
|
"language": "en",
|
|
|
|
"filters": filters,
|
|
|
|
"user_rules": []string{},
|
|
|
|
"schema_version": schemaVersion,
|
|
|
|
"bind_host": "0.0.0.0",
|
|
|
|
"bind_port": 80,
|
|
|
|
"auth_name": "name",
|
|
|
|
"auth_pass": "pass",
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsConf := testDNSConf(schemaVersion)
|
2019-02-28 17:41:18 +00:00
|
|
|
if schemaVersion > 1 {
|
2021-02-09 13:51:44 +00:00
|
|
|
diskConf["dns"] = dnsConf
|
2019-02-28 17:41:18 +00:00
|
|
|
} else {
|
2021-02-09 13:51:44 +00:00
|
|
|
diskConf["coredns"] = dnsConf
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
2021-02-09 13:51:44 +00:00
|
|
|
|
|
|
|
return diskConf
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:51:44 +00:00
|
|
|
// testDNSConf creates a DNS config for test the way gopkg.in/yaml.v2 would
|
|
|
|
// unmarshal it. In YAML, keys aren't guaranteed to always only be strings.
|
|
|
|
func testDNSConf(schemaVersion int) (dnsConf map[any]any) {
|
|
|
|
dnsConf = map[any]any{
|
|
|
|
"port": 53,
|
|
|
|
"blocked_response_ttl": 10,
|
|
|
|
"querylog_enabled": true,
|
|
|
|
"ratelimit": 20,
|
|
|
|
"bootstrap_dns": "8.8.8.8:53",
|
|
|
|
"parental_sensitivity": 13,
|
|
|
|
"ratelimit_whitelist": []string{},
|
|
|
|
"upstream_dns": []string{"tls://1.1.1.1", "tls://1.0.0.1", "8.8.8.8"},
|
|
|
|
"filtering_enabled": true,
|
|
|
|
"refuse_any": true,
|
|
|
|
"parental_enabled": true,
|
|
|
|
"bind_host": "0.0.0.0",
|
|
|
|
"protection_enabled": true,
|
|
|
|
"safesearch_enabled": true,
|
|
|
|
"safebrowsing_enabled": true,
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:41:18 +00:00
|
|
|
if schemaVersion > 2 {
|
2021-02-09 13:51:44 +00:00
|
|
|
dnsConf["bootstrap_dns"] = []string{"8.8.8.8:53"}
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|
2021-02-09 13:51:44 +00:00
|
|
|
|
|
|
|
return dnsConf
|
2019-02-28 17:41:18 +00:00
|
|
|
}
|