mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-17 13:11:42 +03:00
143616ca6e
Merge in DNS/adguard-home from 4299-querylog-stats-api to master Updates #1717. Updates #4299. Squashed commit of the following: commit 5b706b7997a536bc4fd2c532fb89ca5ab3536848 Merge: 48b62b0f306c1983
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 22 13:53:09 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 48b62b0f1882f1ad120c6cdd90cd7dd8cb8a7738 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Wed Mar 22 12:25:04 2023 +0200 client: fix styles, add titles and descrs commit 97e31cff70d05b51bd0e5ea2d20e8e7a251a7e41 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Tue Mar 21 18:38:12 2023 +0200 client: add ignored domains for querylog commit 24d75c4376382205ae6b8f731b1cd23d517772c9 Author: Vladislav Abdulmyanov <v.abdulmyanov@adguard.com> Date: Tue Mar 21 18:21:13 2023 +0200 client: add ignore domains for stats commit eefc3891d01f90af79fdac9ba8eea06d4d54a0bc Merge: 978675ea1daabb97
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 10:53:35 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 978675ea2c07bf248b4c8f26ebdf78cf59a12ef5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Mar 21 10:53:11 2023 +0300 openapi: fix chlog commit 2ed33007aade115d38b0ca582206cc10678b084c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 17:49:07 2023 +0300 home: fix tests commit 6af11520c164553ee9fce8f214ea169672188d7e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 17:40:16 2023 +0300 home: fix typo commit 56acdfde5b1ee8d16b232c1293b91affbe319ad1 Merge: 319da34d48431f8b
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 17:32:58 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 319da34de41ec84310b23bba2ad79c8a3a4c14ff Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 3 17:34:38 2023 +0300 querylog: fix docs commit d5a8f24d5b336e7bdbbca18069f6ede8c96bcc2c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 3 11:42:00 2023 +0300 stats: fix docs commit e0cbfc1c4078180a05835ce7587e9f45484adc81 Merge: 4743c810012e5beb
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 18:45:17 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 4743c81038052b9e0ca29ae5f1565021d36ca1ef Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 18:14:16 2023 +0300 all: imp code; fix time conversion commit 34310cffd7e331d098c535590245387051674fa8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 12:34:11 2023 +0300 chlog: restore order commit cadd864a66655242948f1cb16e6d4945c0235d7e Merge: 2f3e25bebb226434
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 12:26:06 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit 2f3e25bee56d2c6ddcf4aa2fc6a1dc51ed9b06e1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 1 12:25:14 2023 +0300 all: fix fmt commit d54022baa6c8a3d0d3c308a9b6b1a6a9dc6ac7b6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Feb 28 16:16:40 2023 +0300 all: imp code; fix chlog commit df22de91f59a51194c55e7bcbe5bc3fcc60cb8e3 Merge: e1ea4797a772212d
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 27 17:24:09 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit e1ea4797af974c36f06683ffc6eaaae917921a43 Merge: d7db0a5abb80a7c2
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 27 17:23:20 2023 +0300 Merge branch 'master' into 4299-querylog-stats-api commit d7db0a5af1e1f49f6174c1c42e6d9306f2381d16 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Feb 27 17:12:20 2023 +0300 all: imp docs ... and 15 more commits
1049 lines
21 KiB
Go
1049 lines
21 KiB
Go
package home
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TODO(a.garipov): Cover all migrations, use a testdata/ dir.
|
|
|
|
func TestUpgradeSchema1to2(t *testing.T) {
|
|
diskConf := testDiskConf(1)
|
|
|
|
err := upgradeSchema1to2(diskConf)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, diskConf["schema_version"], 2)
|
|
|
|
_, ok := diskConf["coredns"]
|
|
require.False(t, ok)
|
|
|
|
newDNSConf, ok := diskConf["dns"]
|
|
require.True(t, ok)
|
|
|
|
oldDNSConf := testDNSConf(1)
|
|
assert.Equal(t, oldDNSConf, newDNSConf)
|
|
|
|
oldExcludedEntries := []string{"coredns", "schema_version"}
|
|
newExcludedEntries := []string{"dns", "schema_version"}
|
|
oldDiskConf := testDiskConf(1)
|
|
assertEqualExcept(t, oldDiskConf, diskConf, oldExcludedEntries, newExcludedEntries)
|
|
}
|
|
|
|
func TestUpgradeSchema2to3(t *testing.T) {
|
|
diskConf := testDiskConf(2)
|
|
|
|
err := upgradeSchema2to3(diskConf)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, diskConf["schema_version"], 3)
|
|
|
|
dnsMap, ok := diskConf["dns"]
|
|
require.True(t, ok)
|
|
|
|
newDNSConf, ok := dnsMap.(yobj)
|
|
require.True(t, ok)
|
|
|
|
bootstrapDNS := newDNSConf["bootstrap_dns"]
|
|
switch v := bootstrapDNS.(type) {
|
|
case []string:
|
|
require.Len(t, v, 1)
|
|
require.Equal(t, "8.8.8.8:53", v[0])
|
|
default:
|
|
t.Fatalf("wrong type for bootstrap dns: %T", v)
|
|
}
|
|
|
|
excludedEntries := []string{"bootstrap_dns"}
|
|
oldDNSConf := testDNSConf(2)
|
|
assertEqualExcept(t, oldDNSConf, newDNSConf, excludedEntries, excludedEntries)
|
|
|
|
excludedEntries = []string{"dns", "schema_version"}
|
|
oldDiskConf := testDiskConf(2)
|
|
assertEqualExcept(t, oldDiskConf, diskConf, excludedEntries, excludedEntries)
|
|
}
|
|
|
|
func TestUpgradeSchema7to8(t *testing.T) {
|
|
const host = "1.2.3.4"
|
|
oldConf := yobj{
|
|
"dns": yobj{
|
|
"bind_host": host,
|
|
},
|
|
"schema_version": 7,
|
|
}
|
|
|
|
err := upgradeSchema7to8(oldConf)
|
|
require.NoError(t, err)
|
|
|
|
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])
|
|
}
|
|
|
|
func TestUpgradeSchema8to9(t *testing.T) {
|
|
const tld = "foo"
|
|
|
|
t.Run("with_autohost_tld", func(t *testing.T) {
|
|
oldConf := yobj{
|
|
"dns": yobj{
|
|
"autohost_tld": tld,
|
|
},
|
|
"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)
|
|
|
|
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)
|
|
|
|
// 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)
|
|
})
|
|
}
|
|
|
|
// assertEqualExcept removes entries from configs and compares them.
|
|
func assertEqualExcept(t *testing.T, oldConf, newConf yobj, oldKeys, newKeys []string) {
|
|
t.Helper()
|
|
|
|
for _, k := range oldKeys {
|
|
delete(oldConf, k)
|
|
}
|
|
for _, k := range newKeys {
|
|
delete(newConf, k)
|
|
}
|
|
|
|
assert.Equal(t, oldConf, newConf)
|
|
}
|
|
|
|
func testDiskConf(schemaVersion int) (diskConf yobj) {
|
|
filters := []filtering.FilterYAML{{
|
|
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{
|
|
"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)
|
|
if schemaVersion > 1 {
|
|
diskConf["dns"] = dnsConf
|
|
} else {
|
|
diskConf["coredns"] = dnsConf
|
|
}
|
|
|
|
return diskConf
|
|
}
|
|
|
|
// testDNSConf creates a DNS config for test the way gopkg.in/yaml.v3 would
|
|
// unmarshal it. In YAML, keys aren't guaranteed to always only be strings.
|
|
func testDNSConf(schemaVersion int) (dnsConf yobj) {
|
|
dnsConf = yobj{
|
|
"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,
|
|
}
|
|
|
|
if schemaVersion > 2 {
|
|
dnsConf["bootstrap_dns"] = []string{"8.8.8.8:53"}
|
|
}
|
|
|
|
return dnsConf
|
|
}
|
|
|
|
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 != "" {
|
|
testutil.AssertErrorMsg(t, tc.wantErr, err)
|
|
|
|
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,
|
|
})
|
|
|
|
testutil.AssertErrorMsg(t, "unexpected type of dns: int", err)
|
|
})
|
|
}
|
|
|
|
func TestUpgradeSchema10to11(t *testing.T) {
|
|
check := func(t *testing.T, conf yobj) {
|
|
rlimit, _ := conf["rlimit_nofile"].(int)
|
|
|
|
err := upgradeSchema10to11(conf)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, conf["schema_version"], 11)
|
|
|
|
_, ok := conf["rlimit_nofile"]
|
|
assert.False(t, ok)
|
|
|
|
osVal, ok := conf["os"]
|
|
require.True(t, ok)
|
|
|
|
newOSConf, ok := osVal.(yobj)
|
|
require.True(t, ok)
|
|
|
|
_, ok = newOSConf["group"]
|
|
assert.True(t, ok)
|
|
|
|
_, ok = newOSConf["user"]
|
|
assert.True(t, ok)
|
|
|
|
rlimitVal, ok := newOSConf["rlimit_nofile"].(int)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, rlimit, rlimitVal)
|
|
}
|
|
|
|
const rlimit = 42
|
|
t.Run("with_rlimit", func(t *testing.T) {
|
|
conf := yobj{
|
|
"rlimit_nofile": rlimit,
|
|
"schema_version": 10,
|
|
}
|
|
check(t, conf)
|
|
})
|
|
|
|
t.Run("without_rlimit", func(t *testing.T) {
|
|
conf := yobj{
|
|
"schema_version": 10,
|
|
}
|
|
check(t, conf)
|
|
})
|
|
}
|
|
|
|
func TestUpgradeSchema11to12(t *testing.T) {
|
|
testCases := []struct {
|
|
ivl any
|
|
want any
|
|
wantErr string
|
|
name string
|
|
}{{
|
|
ivl: 1,
|
|
want: timeutil.Duration{Duration: timeutil.Day},
|
|
wantErr: "",
|
|
name: "success",
|
|
}, {
|
|
ivl: 0.25,
|
|
want: 0,
|
|
wantErr: "unexpected type of querylog_interval: float64",
|
|
name: "fail",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
conf := yobj{
|
|
"dns": yobj{
|
|
"querylog_interval": tc.ivl,
|
|
},
|
|
"schema_version": 11,
|
|
}
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema11to12(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"], 12)
|
|
|
|
dnsVal, ok := conf["dns"]
|
|
require.True(t, ok)
|
|
|
|
var newDNSConf yobj
|
|
newDNSConf, ok = dnsVal.(yobj)
|
|
require.True(t, ok)
|
|
|
|
var newIvl timeutil.Duration
|
|
newIvl, ok = newDNSConf["querylog_interval"].(timeutil.Duration)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, tc.want, newIvl)
|
|
})
|
|
}
|
|
|
|
t.Run("no_dns", func(t *testing.T) {
|
|
err := upgradeSchema11to12(yobj{})
|
|
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("bad_dns", func(t *testing.T) {
|
|
err := upgradeSchema11to12(yobj{
|
|
"dns": 0,
|
|
})
|
|
|
|
testutil.AssertErrorMsg(t, "unexpected type of dns: int", err)
|
|
})
|
|
|
|
t.Run("no_field", func(t *testing.T) {
|
|
conf := yobj{
|
|
"dns": yobj{},
|
|
}
|
|
|
|
err := upgradeSchema11to12(conf)
|
|
require.NoError(t, err)
|
|
|
|
dns, ok := conf["dns"]
|
|
require.True(t, ok)
|
|
|
|
var dnsVal yobj
|
|
dnsVal, ok = dns.(yobj)
|
|
require.True(t, ok)
|
|
|
|
var ivl any
|
|
ivl, ok = dnsVal["querylog_interval"]
|
|
require.True(t, ok)
|
|
|
|
var ivlVal timeutil.Duration
|
|
ivlVal, ok = ivl.(timeutil.Duration)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, 90*24*time.Hour, ivlVal.Duration)
|
|
})
|
|
}
|
|
|
|
func TestUpgradeSchema12to13(t *testing.T) {
|
|
const newSchemaVer = 13
|
|
|
|
testCases := []struct {
|
|
in yobj
|
|
want yobj
|
|
name string
|
|
}{{
|
|
in: yobj{},
|
|
want: yobj{"schema_version": newSchemaVer},
|
|
name: "no_dns",
|
|
}, {
|
|
in: yobj{"dns": yobj{}},
|
|
want: yobj{
|
|
"dns": yobj{},
|
|
"schema_version": newSchemaVer,
|
|
},
|
|
name: "no_dhcp",
|
|
}, {
|
|
in: yobj{
|
|
"dns": yobj{
|
|
"local_domain_name": "lan",
|
|
},
|
|
"dhcp": yobj{},
|
|
"schema_version": newSchemaVer - 1,
|
|
},
|
|
want: yobj{
|
|
"dns": yobj{},
|
|
"dhcp": yobj{
|
|
"local_domain_name": "lan",
|
|
},
|
|
"schema_version": newSchemaVer,
|
|
},
|
|
name: "good",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema12to13(tc.in)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpgradeSchema13to14(t *testing.T) {
|
|
const newSchemaVer = 14
|
|
|
|
testClient := &clientObject{
|
|
Name: "agh-client",
|
|
IDs: []string{"id1"},
|
|
UseGlobalSettings: true,
|
|
}
|
|
|
|
testCases := []struct {
|
|
in yobj
|
|
want yobj
|
|
name string
|
|
}{{
|
|
in: yobj{},
|
|
want: yobj{
|
|
"schema_version": newSchemaVer,
|
|
// The clients field will be added anyway.
|
|
"clients": yobj{
|
|
"persistent": yarr{},
|
|
"runtime_sources": &clientSourcesConfig{
|
|
WHOIS: true,
|
|
ARP: true,
|
|
RDNS: false,
|
|
DHCP: true,
|
|
HostsFile: true,
|
|
},
|
|
},
|
|
},
|
|
name: "no_clients",
|
|
}, {
|
|
in: yobj{
|
|
"clients": []*clientObject{testClient},
|
|
},
|
|
want: yobj{
|
|
"schema_version": newSchemaVer,
|
|
"clients": yobj{
|
|
"persistent": []*clientObject{testClient},
|
|
"runtime_sources": &clientSourcesConfig{
|
|
WHOIS: true,
|
|
ARP: true,
|
|
RDNS: false,
|
|
DHCP: true,
|
|
HostsFile: true,
|
|
},
|
|
},
|
|
},
|
|
name: "no_dns",
|
|
}, {
|
|
in: yobj{
|
|
"clients": []*clientObject{testClient},
|
|
"dns": yobj{
|
|
"resolve_clients": true,
|
|
},
|
|
},
|
|
want: yobj{
|
|
"schema_version": newSchemaVer,
|
|
"clients": yobj{
|
|
"persistent": []*clientObject{testClient},
|
|
"runtime_sources": &clientSourcesConfig{
|
|
WHOIS: true,
|
|
ARP: true,
|
|
RDNS: true,
|
|
DHCP: true,
|
|
HostsFile: true,
|
|
},
|
|
},
|
|
"dns": yobj{},
|
|
},
|
|
name: "good",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema13to14(tc.in)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpgradeSchema14to15(t *testing.T) {
|
|
const newSchemaVer = 15
|
|
|
|
defaultWantObj := yobj{
|
|
"querylog": map[string]any{
|
|
"enabled": true,
|
|
"file_enabled": true,
|
|
"interval": "2160h",
|
|
"size_memory": 1000,
|
|
"ignored": []any{},
|
|
},
|
|
"dns": map[string]any{},
|
|
"schema_version": newSchemaVer,
|
|
}
|
|
|
|
testCases := []struct {
|
|
in yobj
|
|
want yobj
|
|
name string
|
|
}{{
|
|
in: yobj{
|
|
"dns": map[string]any{
|
|
"querylog_enabled": true,
|
|
"querylog_file_enabled": true,
|
|
"querylog_interval": "2160h",
|
|
"querylog_size_memory": 1000,
|
|
},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "basic",
|
|
}, {
|
|
in: yobj{
|
|
"dns": map[string]any{},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "default_values",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema14to15(tc.in)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpgradeSchema15to16(t *testing.T) {
|
|
const newSchemaVer = 16
|
|
|
|
defaultWantObj := yobj{
|
|
"statistics": map[string]any{
|
|
"enabled": true,
|
|
"interval": 1,
|
|
"ignored": []any{},
|
|
},
|
|
"dns": map[string]any{},
|
|
"schema_version": newSchemaVer,
|
|
}
|
|
|
|
testCases := []struct {
|
|
in yobj
|
|
want yobj
|
|
name string
|
|
}{{
|
|
in: yobj{
|
|
"dns": map[string]any{
|
|
"statistics_interval": 1,
|
|
},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "basic",
|
|
}, {
|
|
in: yobj{
|
|
"dns": map[string]any{},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "default_values",
|
|
}, {
|
|
in: yobj{
|
|
"dns": map[string]any{
|
|
"statistics_interval": 0,
|
|
},
|
|
},
|
|
want: yobj{
|
|
"statistics": map[string]any{
|
|
"enabled": false,
|
|
"interval": 0,
|
|
"ignored": []any{},
|
|
},
|
|
"dns": map[string]any{},
|
|
"schema_version": newSchemaVer,
|
|
},
|
|
name: "stats_disabled",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema15to16(tc.in)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpgradeSchema16to17(t *testing.T) {
|
|
const newSchemaVer = 17
|
|
|
|
defaultWantObj := yobj{
|
|
"dns": map[string]any{
|
|
"edns_client_subnet": map[string]any{
|
|
"enabled": false,
|
|
"use_custom": false,
|
|
"custom_ip": "",
|
|
},
|
|
},
|
|
"schema_version": newSchemaVer,
|
|
}
|
|
|
|
testCases := []struct {
|
|
in yobj
|
|
want yobj
|
|
name string
|
|
}{{
|
|
in: yobj{
|
|
"dns": map[string]any{
|
|
"edns_client_subnet": false,
|
|
},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "basic",
|
|
}, {
|
|
in: yobj{
|
|
"dns": map[string]any{},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "default_values",
|
|
}, {
|
|
in: yobj{
|
|
"dns": map[string]any{
|
|
"edns_client_subnet": true,
|
|
},
|
|
},
|
|
want: yobj{
|
|
"dns": map[string]any{
|
|
"edns_client_subnet": map[string]any{
|
|
"enabled": true,
|
|
"use_custom": false,
|
|
"custom_ip": "",
|
|
},
|
|
},
|
|
"schema_version": newSchemaVer,
|
|
},
|
|
name: "is_true",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema16to17(tc.in)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpgradeSchema17to18(t *testing.T) {
|
|
const newSchemaVer = 18
|
|
|
|
defaultWantObj := yobj{
|
|
"dns": yobj{
|
|
"safe_search": yobj{
|
|
"enabled": true,
|
|
"bing": true,
|
|
"duckduckgo": true,
|
|
"google": true,
|
|
"pixabay": true,
|
|
"yandex": true,
|
|
"youtube": true,
|
|
},
|
|
},
|
|
"schema_version": newSchemaVer,
|
|
}
|
|
|
|
testCases := []struct {
|
|
in yobj
|
|
want yobj
|
|
name string
|
|
}{{
|
|
in: yobj{"dns": yobj{}},
|
|
want: defaultWantObj,
|
|
name: "default_values",
|
|
}, {
|
|
in: yobj{"dns": yobj{"safesearch_enabled": true}},
|
|
want: defaultWantObj,
|
|
name: "enabled",
|
|
}, {
|
|
in: yobj{"dns": yobj{"safesearch_enabled": false}},
|
|
want: yobj{
|
|
"dns": yobj{
|
|
"safe_search": map[string]any{
|
|
"enabled": false,
|
|
"bing": true,
|
|
"duckduckgo": true,
|
|
"google": true,
|
|
"pixabay": true,
|
|
"yandex": true,
|
|
"youtube": true,
|
|
},
|
|
},
|
|
"schema_version": newSchemaVer,
|
|
},
|
|
name: "disabled",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema17to18(tc.in)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpgradeSchema18to19(t *testing.T) {
|
|
const newSchemaVer = 19
|
|
|
|
defaultWantObj := yobj{
|
|
"clients": yobj{
|
|
"persistent": []yobj{{
|
|
"name": "localhost",
|
|
"safe_search": yobj{
|
|
"enabled": true,
|
|
"bing": true,
|
|
"duckduckgo": true,
|
|
"google": true,
|
|
"pixabay": true,
|
|
"yandex": true,
|
|
"youtube": true,
|
|
},
|
|
}},
|
|
},
|
|
"schema_version": newSchemaVer,
|
|
}
|
|
|
|
testCases := []struct {
|
|
in yobj
|
|
want yobj
|
|
name string
|
|
}{{
|
|
in: yobj{
|
|
"clients": yobj{},
|
|
},
|
|
want: yobj{
|
|
"clients": yobj{},
|
|
"schema_version": newSchemaVer,
|
|
},
|
|
name: "no_clients",
|
|
}, {
|
|
in: yobj{
|
|
"clients": yobj{
|
|
"persistent": []yobj{{"name": "localhost"}},
|
|
},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "default_values",
|
|
}, {
|
|
in: yobj{
|
|
"clients": yobj{
|
|
"persistent": []yobj{{"name": "localhost", "safesearch_enabled": true}},
|
|
},
|
|
},
|
|
want: defaultWantObj,
|
|
name: "enabled",
|
|
}, {
|
|
in: yobj{
|
|
"clients": yobj{
|
|
"persistent": []yobj{{"name": "localhost", "safesearch_enabled": false}},
|
|
},
|
|
},
|
|
want: yobj{
|
|
"clients": yobj{"persistent": []yobj{{
|
|
"name": "localhost",
|
|
"safe_search": yobj{
|
|
"enabled": false,
|
|
"bing": true,
|
|
"duckduckgo": true,
|
|
"google": true,
|
|
"pixabay": true,
|
|
"yandex": true,
|
|
"youtube": true,
|
|
},
|
|
}}},
|
|
"schema_version": newSchemaVer,
|
|
},
|
|
name: "disabled",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema18to19(tc.in)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUpgradeSchema19to20(t *testing.T) {
|
|
testCases := []struct {
|
|
ivl any
|
|
want any
|
|
wantErr string
|
|
name string
|
|
}{{
|
|
ivl: 1,
|
|
want: timeutil.Duration{Duration: timeutil.Day},
|
|
wantErr: "",
|
|
name: "success",
|
|
}, {
|
|
ivl: 0.25,
|
|
want: 0,
|
|
wantErr: "unexpected type of interval: float64",
|
|
name: "fail",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
conf := yobj{
|
|
"statistics": yobj{
|
|
"interval": tc.ivl,
|
|
},
|
|
"schema_version": 19,
|
|
}
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := upgradeSchema19to20(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"], 20)
|
|
|
|
statsVal, ok := conf["statistics"]
|
|
require.True(t, ok)
|
|
|
|
var stats yobj
|
|
stats, ok = statsVal.(yobj)
|
|
require.True(t, ok)
|
|
|
|
var newIvl timeutil.Duration
|
|
newIvl, ok = stats["interval"].(timeutil.Duration)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, tc.want, newIvl)
|
|
})
|
|
}
|
|
|
|
t.Run("no_stats", func(t *testing.T) {
|
|
err := upgradeSchema19to20(yobj{})
|
|
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("bad_stats", func(t *testing.T) {
|
|
err := upgradeSchema19to20(yobj{
|
|
"statistics": 0,
|
|
})
|
|
|
|
testutil.AssertErrorMsg(t, "unexpected type of stats: int", err)
|
|
})
|
|
|
|
t.Run("no_field", func(t *testing.T) {
|
|
conf := yobj{
|
|
"statistics": yobj{},
|
|
}
|
|
|
|
err := upgradeSchema19to20(conf)
|
|
require.NoError(t, err)
|
|
|
|
statsVal, ok := conf["statistics"]
|
|
require.True(t, ok)
|
|
|
|
var stats yobj
|
|
stats, ok = statsVal.(yobj)
|
|
require.True(t, ok)
|
|
|
|
var ivl any
|
|
ivl, ok = stats["interval"]
|
|
require.True(t, ok)
|
|
|
|
var ivlVal timeutil.Duration
|
|
ivlVal, ok = ivl.(timeutil.Duration)
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, 24*time.Hour, ivlVal.Duration)
|
|
})
|
|
}
|