2019-06-10 01:33:19 -07:00
|
|
|
package home
|
2019-02-28 10:41:18 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-02-09 06:51:44 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-28 10:41:18 -07:00
|
|
|
)
|
|
|
|
|
2021-03-23 02:32:07 -07:00
|
|
|
// TODO(a.garipov): Cover all migrations, use a testdata/ dir.
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
func TestUpgradeSchema1to2(t *testing.T) {
|
|
|
|
diskConf := testDiskConf(1)
|
2019-03-01 01:27:15 -07:00
|
|
|
|
2021-03-24 07:17:44 -07:00
|
|
|
err := upgradeSchema1to2(diskConf)
|
2021-04-15 09:00:31 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
require.Equal(t, diskConf["schema_version"], 2)
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
_, ok := diskConf["coredns"]
|
|
|
|
require.False(t, ok)
|
|
|
|
|
2021-03-23 02:32:07 -07:00
|
|
|
newDNSConf, ok := diskConf["dns"]
|
2021-02-09 06:51:44 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-03-23 02:32:07 -07:00
|
|
|
oldDNSConf := testDNSConf(1)
|
2021-02-09 06:51:44 -07:00
|
|
|
assert.Equal(t, oldDNSConf, newDNSConf)
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2019-03-01 01:27:15 -07:00
|
|
|
oldExcludedEntries := []string{"coredns", "schema_version"}
|
|
|
|
newExcludedEntries := []string{"dns", "schema_version"}
|
2021-02-09 06:51:44 -07:00
|
|
|
oldDiskConf := testDiskConf(1)
|
|
|
|
assertEqualExcept(t, oldDiskConf, diskConf, oldExcludedEntries, newExcludedEntries)
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
func TestUpgradeSchema2to3(t *testing.T) {
|
|
|
|
diskConf := testDiskConf(2)
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-03-24 07:17:44 -07:00
|
|
|
err := upgradeSchema2to3(diskConf)
|
2021-04-15 09:00:31 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
require.Equal(t, diskConf["schema_version"], 3)
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
dnsMap, ok := diskConf["dns"]
|
|
|
|
require.True(t, ok)
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-03-23 02:32:07 -07:00
|
|
|
newDNSConf, ok := dnsMap.(yobj)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
bootstrapDNS := newDNSConf["bootstrap_dns"]
|
2019-02-28 10:41:18 -07:00
|
|
|
switch v := bootstrapDNS.(type) {
|
|
|
|
case []string:
|
2021-02-09 06:51:44 -07:00
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "8.8.8.8:53", v[0])
|
2019-02-28 10:41:18 -07:00
|
|
|
default:
|
2021-02-09 06:51:44 -07:00
|
|
|
t.Fatalf("wrong type for bootsrap dns: %T", v)
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
|
|
|
|
2019-03-01 01:27:15 -07:00
|
|
|
excludedEntries := []string{"bootstrap_dns"}
|
2021-03-23 02:32:07 -07:00
|
|
|
oldDNSConf := testDNSConf(2)
|
2021-02-09 06:51:44 -07:00
|
|
|
assertEqualExcept(t, oldDNSConf, newDNSConf, excludedEntries, excludedEntries)
|
2019-03-01 01:27:15 -07:00
|
|
|
|
|
|
|
excludedEntries = []string{"dns", "schema_version"}
|
2021-02-09 06:51:44 -07:00
|
|
|
oldDiskConf := testDiskConf(2)
|
|
|
|
assertEqualExcept(t, oldDiskConf, diskConf, excludedEntries, excludedEntries)
|
2019-03-01 01:27:15 -07:00
|
|
|
}
|
|
|
|
|
2021-03-23 02:32:07 -07:00
|
|
|
func TestUpgradeSchema7to8(t *testing.T) {
|
|
|
|
const host = "1.2.3.4"
|
|
|
|
oldConf := yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"bind_host": host,
|
|
|
|
},
|
|
|
|
"schema_version": 7,
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
|
|
|
|
2021-03-24 07:17:44 -07:00
|
|
|
err := upgradeSchema7to8(oldConf)
|
2021-04-15 09:00:31 -07:00
|
|
|
require.NoError(t, err)
|
2021-03-23 02:32:07 -07:00
|
|
|
|
|
|
|
require.Equal(t, oldConf["schema_version"], 8)
|
|
|
|
|
|
|
|
dnsVal, ok := oldConf["dns"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
newDNSConf, ok := dnsVal.(yobj)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
newBindHosts, ok := newDNSConf["bind_hosts"].(yarr)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Len(t, newBindHosts, 1)
|
|
|
|
assert.Equal(t, host, newBindHosts[0])
|
2019-03-01 01:27:15 -07:00
|
|
|
}
|
2019-02-28 10:41:18 -07:00
|
|
|
|
2021-04-15 09:00:31 -07:00
|
|
|
func TestUpgradeSchema8to9(t *testing.T) {
|
|
|
|
const tld = "foo"
|
|
|
|
|
2021-04-20 08:56:15 -07:00
|
|
|
t.Run("with_autohost_tld", func(t *testing.T) {
|
|
|
|
oldConf := yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"autohost_tld": tld,
|
|
|
|
},
|
|
|
|
"schema_version": 8,
|
|
|
|
}
|
2021-04-15 09:00:31 -07:00
|
|
|
|
2021-04-20 08:56:15 -07:00
|
|
|
err := upgradeSchema8to9(oldConf)
|
|
|
|
require.NoError(t, err)
|
2021-04-15 09:00:31 -07:00
|
|
|
|
2021-04-20 08:56:15 -07:00
|
|
|
require.Equal(t, oldConf["schema_version"], 9)
|
2021-04-15 09:00:31 -07:00
|
|
|
|
2021-04-20 08:56:15 -07:00
|
|
|
dnsVal, ok := oldConf["dns"]
|
|
|
|
require.True(t, ok)
|
2021-04-15 09:00:31 -07:00
|
|
|
|
2021-04-20 08:56:15 -07:00
|
|
|
newDNSConf, ok := dnsVal.(yobj)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
localDomainName, ok := newDNSConf["local_domain_name"].(string)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.Equal(t, tld, localDomainName)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("without_autohost_tld", func(t *testing.T) {
|
|
|
|
oldConf := yobj{
|
|
|
|
"dns": yobj{},
|
|
|
|
"schema_version": 8,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := upgradeSchema8to9(oldConf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, oldConf["schema_version"], 9)
|
|
|
|
|
|
|
|
dnsVal, ok := oldConf["dns"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
newDNSConf, ok := dnsVal.(yobj)
|
|
|
|
require.True(t, ok)
|
2021-04-15 09:00:31 -07:00
|
|
|
|
2021-04-20 08:56:15 -07:00
|
|
|
// Should be nil in order to be set to the default value by the
|
|
|
|
// following config rewrite.
|
|
|
|
_, ok = newDNSConf["local_domain_name"]
|
|
|
|
require.False(t, ok)
|
|
|
|
})
|
2021-04-15 09:00:31 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
// assertEqualExcept removes entries from configs and compares them.
|
2021-03-23 02:32:07 -07:00
|
|
|
func assertEqualExcept(t *testing.T, oldConf, newConf yobj, oldKeys, newKeys []string) {
|
2021-02-09 06:51:44 -07:00
|
|
|
t.Helper()
|
2019-03-01 01:27:15 -07:00
|
|
|
|
2021-02-09 06:51:44 -07:00
|
|
|
for _, k := range oldKeys {
|
|
|
|
delete(oldConf, k)
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
2021-02-09 06:51:44 -07:00
|
|
|
for _, k := range newKeys {
|
|
|
|
delete(newConf, k)
|
2019-03-01 01:27:15 -07:00
|
|
|
}
|
2021-02-09 06:51:44 -07:00
|
|
|
|
|
|
|
assert.Equal(t, oldConf, newConf)
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
|
|
|
|
2021-03-23 02:32:07 -07:00
|
|
|
func testDiskConf(schemaVersion int) (diskConf yobj) {
|
|
|
|
filters := []filter{{
|
|
|
|
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,
|
|
|
|
}}
|
|
|
|
diskConf = yobj{
|
2021-02-09 06:51:44 -07:00
|
|
|
"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 10:41:18 -07:00
|
|
|
if schemaVersion > 1 {
|
2021-02-09 06:51:44 -07:00
|
|
|
diskConf["dns"] = dnsConf
|
2019-02-28 10:41:18 -07:00
|
|
|
} else {
|
2021-02-09 06:51:44 -07:00
|
|
|
diskConf["coredns"] = dnsConf
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
2021-02-09 06:51:44 -07:00
|
|
|
|
|
|
|
return diskConf
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
|
|
|
|
2021-02-09 06:51:44 -07: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.
|
2021-03-23 02:32:07 -07:00
|
|
|
func testDNSConf(schemaVersion int) (dnsConf yobj) {
|
|
|
|
dnsConf = yobj{
|
2021-02-09 06:51:44 -07:00
|
|
|
"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 10:41:18 -07:00
|
|
|
if schemaVersion > 2 {
|
2021-02-09 06:51:44 -07:00
|
|
|
dnsConf["bootstrap_dns"] = []string{"8.8.8.8:53"}
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
2021-02-09 06:51:44 -07:00
|
|
|
|
|
|
|
return dnsConf
|
2019-02-28 10:41:18 -07:00
|
|
|
}
|
2021-04-21 02:49:41 -07:00
|
|
|
|
|
|
|
func TestAddQUICPort(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
ups string
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
name: "simple_ip",
|
|
|
|
ups: "8.8.8.8",
|
|
|
|
want: "8.8.8.8",
|
|
|
|
}, {
|
|
|
|
name: "url_ipv4",
|
|
|
|
ups: "quic://8.8.8.8",
|
|
|
|
want: "quic://8.8.8.8:784",
|
|
|
|
}, {
|
|
|
|
name: "url_ipv4_with_port",
|
|
|
|
ups: "quic://8.8.8.8:25565",
|
|
|
|
want: "quic://8.8.8.8:25565",
|
|
|
|
}, {
|
|
|
|
name: "url_ipv6",
|
|
|
|
ups: "quic://[::1]",
|
|
|
|
want: "quic://[::1]:784",
|
|
|
|
}, {
|
|
|
|
name: "url_ipv6_invalid",
|
|
|
|
ups: "quic://::1",
|
|
|
|
want: "quic://::1",
|
|
|
|
}, {
|
|
|
|
name: "url_ipv6_with_port",
|
|
|
|
ups: "quic://[::1]:25565",
|
|
|
|
want: "quic://[::1]:25565",
|
|
|
|
}, {
|
|
|
|
name: "url_hostname",
|
|
|
|
ups: "quic://example.com",
|
|
|
|
want: "quic://example.com:784",
|
|
|
|
}, {
|
|
|
|
name: "url_hostname_with_port",
|
|
|
|
ups: "quic://example.com:25565",
|
|
|
|
want: "quic://example.com:25565",
|
|
|
|
}, {
|
|
|
|
name: "url_hostname_with_endpoint",
|
|
|
|
ups: "quic://example.com/some-endpoint",
|
|
|
|
want: "quic://example.com:784/some-endpoint",
|
|
|
|
}, {
|
|
|
|
name: "url_hostname_with_port_endpoint",
|
|
|
|
ups: "quic://example.com:25565/some-endpoint",
|
|
|
|
want: "quic://example.com:25565/some-endpoint",
|
|
|
|
}, {
|
|
|
|
name: "non-quic_proto",
|
|
|
|
ups: "tls://example.com",
|
|
|
|
want: "tls://example.com",
|
|
|
|
}, {
|
|
|
|
name: "comment",
|
|
|
|
ups: "# comment",
|
|
|
|
want: "# comment",
|
|
|
|
}, {
|
|
|
|
name: "blank",
|
|
|
|
ups: "",
|
|
|
|
want: "",
|
|
|
|
}, {
|
|
|
|
name: "with_domain_ip",
|
|
|
|
ups: "[/example.domain/]8.8.8.8",
|
|
|
|
want: "[/example.domain/]8.8.8.8",
|
|
|
|
}, {
|
|
|
|
name: "with_domain_url",
|
|
|
|
ups: "[/example.domain/]quic://example.com",
|
|
|
|
want: "[/example.domain/]quic://example.com:784",
|
|
|
|
}, {
|
|
|
|
name: "invalid_domain",
|
|
|
|
ups: "[/exmaple.domain]quic://example.com",
|
|
|
|
want: "[/exmaple.domain]quic://example.com",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
withPort := addQUICPort(tc.ups, 784)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, withPort)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpgradeSchema9to10(t *testing.T) {
|
|
|
|
const ultimateAns = 42
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
ups any
|
|
|
|
want any
|
|
|
|
wantErr string
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
ups: yarr{"quic://8.8.8.8"},
|
|
|
|
want: yarr{"quic://8.8.8.8:784"},
|
|
|
|
wantErr: "",
|
|
|
|
name: "success",
|
|
|
|
}, {
|
|
|
|
ups: ultimateAns,
|
|
|
|
want: nil,
|
|
|
|
wantErr: "unexpected type of dns.upstream_dns: int",
|
|
|
|
name: "bad_yarr_type",
|
|
|
|
}, {
|
|
|
|
ups: yarr{ultimateAns},
|
|
|
|
want: nil,
|
|
|
|
wantErr: "unexpected type of upstream field: int",
|
|
|
|
name: "bad_upstream_type",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
conf := yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"upstream_dns": tc.ups,
|
|
|
|
},
|
|
|
|
"schema_version": 9,
|
|
|
|
}
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := upgradeSchema9to10(conf)
|
|
|
|
|
|
|
|
if tc.wantErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, tc.wantErr, err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, conf["schema_version"], 10)
|
|
|
|
|
|
|
|
dnsVal, ok := conf["dns"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
newDNSConf, ok := dnsVal.(yobj)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
fixedUps, ok := newDNSConf["upstream_dns"].(yarr)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, fixedUps)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no_dns", func(t *testing.T) {
|
|
|
|
err := upgradeSchema9to10(yobj{})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad_dns", func(t *testing.T) {
|
|
|
|
err := upgradeSchema9to10(yobj{
|
|
|
|
"dns": ultimateAns,
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, "unexpected type of dns: int", err.Error())
|
|
|
|
})
|
|
|
|
}
|