2023-12-15 20:27:47 +03:00
|
|
|
package configmigrate
|
2019-02-28 20:41:18 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-07-01 18:50:28 +03:00
|
|
|
"time"
|
2021-02-09 16:51:44 +03:00
|
|
|
|
2023-12-25 14:16:48 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
|
2022-09-23 13:23:35 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-10-22 11:58:18 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-09-30 21:17:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2021-02-09 16:51:44 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-28 20:41:18 +03:00
|
|
|
)
|
|
|
|
|
2023-08-31 16:34:15 +03:00
|
|
|
// TODO(e.burkov): Cover all migrations, use a testdata/ dir.
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
func TestUpgradeSchema1to2(t *testing.T) {
|
|
|
|
diskConf := testDiskConf(1)
|
2019-03-01 11:27:15 +03:00
|
|
|
|
2023-08-31 16:34:15 +03:00
|
|
|
m := New(&Config{
|
|
|
|
WorkingDir: "",
|
|
|
|
})
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := m.migrateTo2(diskConf)
|
2021-04-15 19:00:31 +03:00
|
|
|
require.NoError(t, err)
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
require.Equal(t, diskConf["schema_version"], 2)
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
_, ok := diskConf["coredns"]
|
|
|
|
require.False(t, ok)
|
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
newDNSConf, ok := diskConf["dns"]
|
2021-02-09 16:51:44 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
oldDNSConf := testDNSConf(1)
|
2021-02-09 16:51:44 +03:00
|
|
|
assert.Equal(t, oldDNSConf, newDNSConf)
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2019-03-01 11:27:15 +03:00
|
|
|
oldExcludedEntries := []string{"coredns", "schema_version"}
|
|
|
|
newExcludedEntries := []string{"dns", "schema_version"}
|
2021-02-09 16:51:44 +03:00
|
|
|
oldDiskConf := testDiskConf(1)
|
|
|
|
assertEqualExcept(t, oldDiskConf, diskConf, oldExcludedEntries, newExcludedEntries)
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
func TestUpgradeSchema2to3(t *testing.T) {
|
|
|
|
diskConf := testDiskConf(2)
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo3(diskConf)
|
2021-04-15 19:00:31 +03:00
|
|
|
require.NoError(t, err)
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
require.Equal(t, diskConf["schema_version"], 3)
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
dnsMap, ok := diskConf["dns"]
|
|
|
|
require.True(t, ok)
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
newDNSConf, ok := dnsMap.(yobj)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
bootstrapDNS := newDNSConf["bootstrap_dns"]
|
2019-02-28 20:41:18 +03:00
|
|
|
switch v := bootstrapDNS.(type) {
|
2023-09-04 20:45:50 +03:00
|
|
|
case yarr:
|
2021-02-09 16:51:44 +03:00
|
|
|
require.Len(t, v, 1)
|
|
|
|
require.Equal(t, "8.8.8.8:53", v[0])
|
2019-02-28 20:41:18 +03:00
|
|
|
default:
|
2022-01-25 19:47:02 +03:00
|
|
|
t.Fatalf("wrong type for bootstrap dns: %T", v)
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 11:27:15 +03:00
|
|
|
excludedEntries := []string{"bootstrap_dns"}
|
2021-03-23 12:32:07 +03:00
|
|
|
oldDNSConf := testDNSConf(2)
|
2021-02-09 16:51:44 +03:00
|
|
|
assertEqualExcept(t, oldDNSConf, newDNSConf, excludedEntries, excludedEntries)
|
2019-03-01 11:27:15 +03:00
|
|
|
|
|
|
|
excludedEntries = []string{"dns", "schema_version"}
|
2021-02-09 16:51:44 +03:00
|
|
|
oldDiskConf := testDiskConf(2)
|
|
|
|
assertEqualExcept(t, oldDiskConf, diskConf, excludedEntries, excludedEntries)
|
2019-03-01 11:27:15 +03:00
|
|
|
}
|
|
|
|
|
2023-05-10 16:30:03 +03:00
|
|
|
func TestUpgradeSchema5to6(t *testing.T) {
|
|
|
|
const newSchemaVer = 6
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
wantErr string
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
in: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{},
|
2023-05-10 16:30:03 +03:00
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{},
|
2023-05-10 16:30:03 +03:00
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErr: "",
|
|
|
|
name: "no_clients",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{"ip": "127.0.0.1"}},
|
2023-05-10 16:30:03 +03:00
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{
|
2023-09-06 13:43:29 +03:00
|
|
|
"ids": yarr{"127.0.0.1"},
|
2023-05-10 16:30:03 +03:00
|
|
|
"ip": "127.0.0.1",
|
|
|
|
}},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErr: "",
|
|
|
|
name: "client_ip",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{"mac": "mac"}},
|
2023-05-10 16:30:03 +03:00
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{
|
2023-09-06 13:43:29 +03:00
|
|
|
"ids": yarr{"mac"},
|
2023-05-10 16:30:03 +03:00
|
|
|
"mac": "mac",
|
|
|
|
}},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErr: "",
|
|
|
|
name: "client_mac",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{"ip": "127.0.0.1", "mac": "mac"}},
|
2023-05-10 16:30:03 +03:00
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{
|
2023-09-06 13:43:29 +03:00
|
|
|
"ids": yarr{"127.0.0.1", "mac"},
|
2023-05-10 16:30:03 +03:00
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"mac": "mac",
|
|
|
|
}},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErr: "",
|
|
|
|
name: "client_ip_mac",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{"ip": 1, "mac": "mac"}},
|
2023-05-10 16:30:03 +03:00
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{"ip": 1, "mac": "mac"}},
|
2023-05-10 16:30:03 +03:00
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErr: `client at index 0: unexpected type of "ip": int`,
|
2023-05-10 16:30:03 +03:00
|
|
|
name: "inv_client_ip",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{"ip": "127.0.0.1", "mac": 1}},
|
2023-05-10 16:30:03 +03:00
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yarr{yobj{"ip": "127.0.0.1", "mac": 1}},
|
2023-05-10 16:30:03 +03:00
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErr: `client at index 0: unexpected type of "mac": int`,
|
2023-05-10 16:30:03 +03:00
|
|
|
name: "inv_client_mac",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo6(tc.in)
|
2023-05-10 16:30:03 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErr, err)
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:32:07 +03: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 20:41:18 +03:00
|
|
|
}
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo8(oldConf)
|
2021-04-15 19:00:31 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-23 12:32:07 +03: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 11:27:15 +03:00
|
|
|
}
|
2019-02-28 20:41:18 +03:00
|
|
|
|
2021-04-15 19:00:31 +03:00
|
|
|
func TestUpgradeSchema8to9(t *testing.T) {
|
|
|
|
const tld = "foo"
|
|
|
|
|
2021-04-20 18:56:15 +03:00
|
|
|
t.Run("with_autohost_tld", func(t *testing.T) {
|
|
|
|
oldConf := yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"autohost_tld": tld,
|
|
|
|
},
|
|
|
|
"schema_version": 8,
|
|
|
|
}
|
2021-04-15 19:00:31 +03:00
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo9(oldConf)
|
2021-04-20 18:56:15 +03:00
|
|
|
require.NoError(t, err)
|
2021-04-15 19:00:31 +03:00
|
|
|
|
2021-04-20 18:56:15 +03:00
|
|
|
require.Equal(t, oldConf["schema_version"], 9)
|
2021-04-15 19:00:31 +03:00
|
|
|
|
2021-04-20 18:56:15 +03:00
|
|
|
dnsVal, ok := oldConf["dns"]
|
|
|
|
require.True(t, ok)
|
2021-04-15 19:00:31 +03:00
|
|
|
|
2021-04-20 18:56:15 +03: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,
|
|
|
|
}
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo9(oldConf)
|
2021-04-20 18:56:15 +03:00
|
|
|
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 19:00:31 +03:00
|
|
|
|
2021-04-20 18:56:15 +03: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 19:00:31 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
// assertEqualExcept removes entries from configs and compares them.
|
2021-03-23 12:32:07 +03:00
|
|
|
func assertEqualExcept(t *testing.T, oldConf, newConf yobj, oldKeys, newKeys []string) {
|
2021-02-09 16:51:44 +03:00
|
|
|
t.Helper()
|
2019-03-01 11:27:15 +03:00
|
|
|
|
2021-02-09 16:51:44 +03:00
|
|
|
for _, k := range oldKeys {
|
|
|
|
delete(oldConf, k)
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
2021-02-09 16:51:44 +03:00
|
|
|
for _, k := range newKeys {
|
|
|
|
delete(newConf, k)
|
2019-03-01 11:27:15 +03:00
|
|
|
}
|
2021-02-09 16:51:44 +03:00
|
|
|
|
|
|
|
assert.Equal(t, oldConf, newConf)
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
func testDiskConf(schemaVersion int) (diskConf yobj) {
|
2022-09-23 13:23:35 +03:00
|
|
|
filters := []filtering.FilterYAML{{
|
2021-03-23 12:32:07 +03: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,
|
|
|
|
}}
|
|
|
|
diskConf = yobj{
|
2021-02-09 16:51:44 +03: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 20:41:18 +03:00
|
|
|
if schemaVersion > 1 {
|
2021-02-09 16:51:44 +03:00
|
|
|
diskConf["dns"] = dnsConf
|
2019-02-28 20:41:18 +03:00
|
|
|
} else {
|
2021-02-09 16:51:44 +03:00
|
|
|
diskConf["coredns"] = dnsConf
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
2021-02-09 16:51:44 +03:00
|
|
|
|
|
|
|
return diskConf
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
|
|
|
|
2022-08-08 15:50:54 +03:00
|
|
|
// testDNSConf creates a DNS config for test the way gopkg.in/yaml.v3 would
|
2021-02-09 16:51:44 +03:00
|
|
|
// unmarshal it. In YAML, keys aren't guaranteed to always only be strings.
|
2021-03-23 12:32:07 +03:00
|
|
|
func testDNSConf(schemaVersion int) (dnsConf yobj) {
|
|
|
|
dnsConf = yobj{
|
2021-02-09 16:51:44 +03: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 20:41:18 +03:00
|
|
|
if schemaVersion > 2 {
|
2021-02-09 16:51:44 +03:00
|
|
|
dnsConf["bootstrap_dns"] = []string{"8.8.8.8:53"}
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
2021-02-09 16:51:44 +03:00
|
|
|
|
|
|
|
return dnsConf
|
2019-02-28 20:41:18 +03:00
|
|
|
}
|
2021-04-21 12:49:41 +03: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,
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErr: `unexpected type of "upstream_dns": int`,
|
2021-04-21 12:49:41 +03:00
|
|
|
name: "bad_yarr_type",
|
|
|
|
}, {
|
|
|
|
ups: yarr{ultimateAns},
|
|
|
|
want: nil,
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErr: `unexpected type of upstream field: int`,
|
2021-04-21 12:49:41 +03:00
|
|
|
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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo10(conf)
|
2021-04-21 12:49:41 +03:00
|
|
|
|
|
|
|
if tc.wantErr != "" {
|
2021-10-22 11:58:18 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErr, err)
|
2021-04-21 12:49:41 +03:00
|
|
|
|
|
|
|
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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo10(yobj{})
|
2021-04-21 12:49:41 +03:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad_dns", func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo10(yobj{
|
2021-04-21 12:49:41 +03:00
|
|
|
"dns": ultimateAns,
|
|
|
|
})
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
testutil.AssertErrorMsg(t, `unexpected type of "dns": int`, err)
|
2021-04-21 12:49:41 +03:00
|
|
|
})
|
|
|
|
}
|
2021-06-04 16:35:34 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema10to11(t *testing.T) {
|
|
|
|
check := func(t *testing.T, conf yobj) {
|
|
|
|
rlimit, _ := conf["rlimit_nofile"].(int)
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo11(conf)
|
2021-06-04 16:35:34 +03:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2021-07-01 18:50:28 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema11to12(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
ivl any
|
|
|
|
want any
|
|
|
|
wantErr string
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
ivl: 1,
|
2021-09-30 21:17:54 +03:00
|
|
|
want: timeutil.Duration{Duration: timeutil.Day},
|
2021-07-01 18:50:28 +03:00
|
|
|
wantErr: "",
|
|
|
|
name: "success",
|
|
|
|
}, {
|
|
|
|
ivl: 0.25,
|
|
|
|
want: 0,
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErr: `unexpected type of "querylog_interval": float64`,
|
2021-07-01 18:50:28 +03:00
|
|
|
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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo12(conf)
|
2021-07-01 18:50:28 +03:00
|
|
|
|
|
|
|
if tc.wantErr != "" {
|
|
|
|
require.Error(t, err)
|
2021-10-22 11:58:18 +03:00
|
|
|
|
2021-07-01 18:50:28 +03:00
|
|
|
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)
|
|
|
|
|
2021-09-30 21:17:54 +03:00
|
|
|
var newIvl timeutil.Duration
|
|
|
|
newIvl, ok = newDNSConf["querylog_interval"].(timeutil.Duration)
|
2021-07-01 18:50:28 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, newIvl)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no_dns", func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo12(yobj{})
|
2021-07-01 18:50:28 +03:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad_dns", func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo12(yobj{
|
2021-07-01 18:50:28 +03:00
|
|
|
"dns": 0,
|
|
|
|
})
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
testutil.AssertErrorMsg(t, `unexpected type of "dns": int`, err)
|
2021-07-01 18:50:28 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no_field", func(t *testing.T) {
|
|
|
|
conf := yobj{
|
|
|
|
"dns": yobj{},
|
|
|
|
}
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo12(conf)
|
2021-07-01 18:50:28 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dns, ok := conf["dns"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
var dnsVal yobj
|
|
|
|
dnsVal, ok = dns.(yobj)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2022-08-03 14:36:18 +03:00
|
|
|
var ivl any
|
2021-07-01 18:50:28 +03:00
|
|
|
ivl, ok = dnsVal["querylog_interval"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2021-09-30 21:17:54 +03:00
|
|
|
var ivlVal timeutil.Duration
|
|
|
|
ivlVal, ok = ivl.(timeutil.Duration)
|
2021-07-01 18:50:28 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.Equal(t, 90*24*time.Hour, ivlVal.Duration)
|
|
|
|
})
|
|
|
|
}
|
2022-01-25 19:47:02 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema12to13(t *testing.T) {
|
2022-04-26 13:04:16 +03:00
|
|
|
const newSchemaVer = 13
|
2022-01-25 19:47:02 +03:00
|
|
|
|
2022-04-26 13:04:16 +03:00
|
|
|
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{
|
2022-01-25 19:47:02 +03:00
|
|
|
"dns": yobj{
|
|
|
|
"local_domain_name": "lan",
|
|
|
|
},
|
|
|
|
"dhcp": yobj{},
|
2022-04-26 13:04:16 +03:00
|
|
|
"schema_version": newSchemaVer - 1,
|
|
|
|
},
|
|
|
|
want: yobj{
|
2022-01-25 19:47:02 +03:00
|
|
|
"dns": yobj{},
|
|
|
|
"dhcp": yobj{
|
|
|
|
"local_domain_name": "lan",
|
|
|
|
},
|
2022-04-26 13:04:16 +03:00
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
name: "good",
|
|
|
|
}}
|
2022-01-25 19:47:02 +03:00
|
|
|
|
2022-04-26 13:04:16 +03:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo13(tc.in)
|
2022-04-26 13:04:16 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-25 19:47:02 +03:00
|
|
|
|
2022-04-26 13:04:16 +03:00
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpgradeSchema13to14(t *testing.T) {
|
|
|
|
const newSchemaVer = 14
|
|
|
|
|
2023-08-31 16:34:15 +03:00
|
|
|
testClient := yobj{
|
|
|
|
"name": "agh-client",
|
|
|
|
"ids": []string{"id1"},
|
|
|
|
"use_global_settings": true,
|
2022-04-26 13:04:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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{},
|
2023-08-31 16:34:15 +03:00
|
|
|
"runtime_sources": yobj{
|
|
|
|
"whois": true,
|
|
|
|
"arp": true,
|
|
|
|
"rdns": false,
|
|
|
|
"dhcp": true,
|
|
|
|
"hosts": true,
|
2022-04-26 13:04:16 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "no_clients",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
2023-08-31 16:34:15 +03:00
|
|
|
"clients": yarr{testClient},
|
2022-04-26 13:04:16 +03:00
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
"clients": yobj{
|
2023-08-31 16:34:15 +03:00
|
|
|
"persistent": yarr{testClient},
|
|
|
|
"runtime_sources": yobj{
|
|
|
|
"whois": true,
|
|
|
|
"arp": true,
|
|
|
|
"rdns": false,
|
|
|
|
"dhcp": true,
|
|
|
|
"hosts": true,
|
2022-04-26 13:04:16 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: "no_dns",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
2023-08-31 16:34:15 +03:00
|
|
|
"clients": yarr{testClient},
|
2022-04-26 13:04:16 +03:00
|
|
|
"dns": yobj{
|
|
|
|
"resolve_clients": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
"clients": yobj{
|
2023-08-31 16:34:15 +03:00
|
|
|
"persistent": yarr{testClient},
|
|
|
|
"runtime_sources": yobj{
|
|
|
|
"whois": true,
|
|
|
|
"arp": true,
|
|
|
|
"rdns": true,
|
|
|
|
"dhcp": true,
|
|
|
|
"hosts": true,
|
2022-04-26 13:04:16 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"dns": yobj{},
|
|
|
|
},
|
|
|
|
name: "good",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo14(tc.in)
|
2022-04-26 13:04:16 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
2022-01-25 19:47:02 +03:00
|
|
|
}
|
2023-02-07 17:50:39 +03:00
|
|
|
|
|
|
|
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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo15(tc.in)
|
2023-02-07 17:50:39 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 18:15:33 +03:00
|
|
|
|
|
|
|
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,
|
2023-03-24 13:17:57 +03:00
|
|
|
"interval": 1,
|
2023-02-13 18:15:33 +03:00
|
|
|
"ignored": []any{},
|
|
|
|
},
|
|
|
|
"dns": map[string]any{},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
name: "stats_disabled",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo16(tc.in)
|
2023-02-13 18:15:33 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-01 16:16:16 +03:00
|
|
|
|
|
|
|
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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo17(tc.in)
|
2023-03-01 16:16:16 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 14:31:07 +03:00
|
|
|
|
|
|
|
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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo18(tc.in)
|
2023-03-15 14:31:07 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpgradeSchema18to19(t *testing.T) {
|
|
|
|
const newSchemaVer = 19
|
|
|
|
|
|
|
|
defaultWantObj := yobj{
|
|
|
|
"clients": yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"persistent": yarr{yobj{
|
2023-03-15 14:31:07 +03:00
|
|
|
"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{
|
2023-09-04 20:45:50 +03:00
|
|
|
"persistent": yarr{yobj{"name": "localhost"}},
|
2023-03-15 14:31:07 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: defaultWantObj,
|
|
|
|
name: "default_values",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
|
|
|
"clients": yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"persistent": yarr{yobj{"name": "localhost", "safesearch_enabled": true}},
|
2023-03-15 14:31:07 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: defaultWantObj,
|
|
|
|
name: "enabled",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
|
|
|
"clients": yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"persistent": yarr{yobj{"name": "localhost", "safesearch_enabled": false}},
|
2023-03-15 14:31:07 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"clients": yobj{"persistent": yarr{yobj{
|
2023-03-15 14:31:07 +03:00
|
|
|
"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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo19(tc.in)
|
2023-03-15 14:31:07 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 13:46:57 +03:00
|
|
|
|
|
|
|
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",
|
2023-03-24 13:17:57 +03:00
|
|
|
}, {
|
|
|
|
ivl: 0,
|
|
|
|
want: timeutil.Duration{Duration: timeutil.Day},
|
|
|
|
wantErr: "",
|
|
|
|
name: "success",
|
2023-03-23 13:46:57 +03:00
|
|
|
}, {
|
|
|
|
ivl: 0.25,
|
|
|
|
want: 0,
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErr: `unexpected type of "interval": float64`,
|
2023-03-23 13:46:57 +03:00
|
|
|
name: "fail",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
conf := yobj{
|
|
|
|
"statistics": yobj{
|
|
|
|
"interval": tc.ivl,
|
|
|
|
},
|
|
|
|
"schema_version": 19,
|
|
|
|
}
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo20(conf)
|
2023-03-23 13:46:57 +03:00
|
|
|
|
|
|
|
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) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo20(yobj{})
|
2023-03-23 13:46:57 +03:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad_stats", func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo20(yobj{
|
2023-03-23 13:46:57 +03:00
|
|
|
"statistics": 0,
|
|
|
|
})
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
testutil.AssertErrorMsg(t, `unexpected type of "statistics": int`, err)
|
2023-03-23 13:46:57 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no_field", func(t *testing.T) {
|
|
|
|
conf := yobj{
|
|
|
|
"statistics": yobj{},
|
|
|
|
}
|
|
|
|
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo20(conf)
|
2023-03-23 13:46:57 +03:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2023-06-14 15:08:57 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema20to21(t *testing.T) {
|
|
|
|
const newSchemaVer = 21
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
name: "nothing",
|
|
|
|
in: yobj{},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "no_clients",
|
|
|
|
in: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"blocked_services": yarr{"ok"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"blocked_services": yobj{
|
|
|
|
"ids": yarr{"ok"},
|
|
|
|
"schedule": yobj{
|
|
|
|
"time_zone": "Local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo21(tc.in)
|
2023-06-14 15:08:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 18:03:07 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema21to22(t *testing.T) {
|
|
|
|
const newSchemaVer = 22
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
in: yobj{
|
|
|
|
"clients": yobj{},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"clients": yobj{},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
name: "nothing",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
|
|
|
"clients": yobj{
|
|
|
|
"persistent": []any{yobj{"name": "localhost", "blocked_services": yarr{}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"clients": yobj{
|
|
|
|
"persistent": []any{yobj{
|
|
|
|
"name": "localhost",
|
|
|
|
"blocked_services": yobj{
|
|
|
|
"ids": yarr{},
|
|
|
|
"schedule": yobj{
|
|
|
|
"time_zone": "Local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
name: "no_services",
|
|
|
|
}, {
|
|
|
|
in: yobj{
|
|
|
|
"clients": yobj{
|
|
|
|
"persistent": []any{yobj{"name": "localhost", "blocked_services": yarr{"ok"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"clients": yobj{
|
|
|
|
"persistent": []any{yobj{
|
|
|
|
"name": "localhost",
|
|
|
|
"blocked_services": yobj{
|
|
|
|
"ids": yarr{"ok"},
|
|
|
|
"schedule": yobj{
|
|
|
|
"time_zone": "Local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
name: "services",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo22(tc.in)
|
2023-06-27 18:03:07 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 15:29:52 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema22to23(t *testing.T) {
|
|
|
|
const newSchemaVer = 23
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
in: yobj{},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "ok",
|
|
|
|
in: yobj{
|
|
|
|
"bind_host": "1.2.3.4",
|
|
|
|
"bind_port": 8081,
|
|
|
|
"web_session_ttl": 720,
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "1.2.3.4:8081",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "v6_address",
|
|
|
|
in: yobj{
|
|
|
|
"bind_host": "2001:db8::1",
|
|
|
|
"bind_port": 8081,
|
|
|
|
"web_session_ttl": 720,
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "[2001:db8::1]:8081",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo23(tc.in)
|
2023-06-29 15:29:52 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-07-06 10:09:04 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema23to24(t *testing.T) {
|
|
|
|
const newSchemaVer = 24
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
in: yobj{},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "ok",
|
|
|
|
in: yobj{
|
|
|
|
"log_file": "/test/path.log",
|
|
|
|
"log_max_backups": 1,
|
|
|
|
"log_max_size": 2,
|
|
|
|
"log_max_age": 3,
|
|
|
|
"log_compress": true,
|
|
|
|
"log_localtime": true,
|
|
|
|
"verbose": true,
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"log": yobj{
|
|
|
|
"file": "/test/path.log",
|
|
|
|
"max_backups": 1,
|
|
|
|
"max_size": 2,
|
|
|
|
"max_age": 3,
|
|
|
|
"compress": true,
|
|
|
|
"local_time": true,
|
|
|
|
"verbose": true,
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "invalid",
|
|
|
|
in: yobj{
|
|
|
|
"log_file": "/test/path.log",
|
|
|
|
"log_max_backups": 1,
|
|
|
|
"log_max_size": 2,
|
|
|
|
"log_max_age": 3,
|
|
|
|
"log_compress": "",
|
|
|
|
"log_localtime": true,
|
|
|
|
"verbose": true,
|
|
|
|
},
|
|
|
|
want: yobj{
|
2023-09-04 20:45:50 +03:00
|
|
|
"log_compress": "",
|
|
|
|
"schema_version": newSchemaVer,
|
2023-07-06 10:09:04 +03:00
|
|
|
},
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErrMsg: `unexpected type of "log_compress": string`,
|
2023-07-06 10:09:04 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo24(tc.in)
|
2023-07-06 10:09:04 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 17:39:33 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema24to25(t *testing.T) {
|
|
|
|
const newSchemaVer = 25
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
in: yobj{},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "ok",
|
|
|
|
in: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "0.0.0.0:3000",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
},
|
|
|
|
"debug_pprof": true,
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "0.0.0.0:3000",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
"pprof": yobj{
|
|
|
|
"enabled": true,
|
|
|
|
"port": 6060,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "ok_disabled",
|
|
|
|
in: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "0.0.0.0:3000",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
},
|
|
|
|
"debug_pprof": false,
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "0.0.0.0:3000",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
"pprof": yobj{
|
|
|
|
"enabled": false,
|
|
|
|
"port": 6060,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "invalid",
|
|
|
|
in: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "0.0.0.0:3000",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
},
|
|
|
|
"debug_pprof": 1,
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"http": yobj{
|
|
|
|
"address": "0.0.0.0:3000",
|
|
|
|
"session_ttl": "720h",
|
|
|
|
},
|
|
|
|
"debug_pprof": 1,
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
2023-09-04 20:45:50 +03:00
|
|
|
wantErrMsg: `unexpected type of "debug_pprof": int`,
|
2023-08-02 17:39:33 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo25(tc.in)
|
2023-08-02 17:39:33 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-08-30 18:26:02 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema25to26(t *testing.T) {
|
|
|
|
const newSchemaVer = 26
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
in: yobj{},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "ok",
|
|
|
|
in: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"filtering_enabled": true,
|
|
|
|
"filters_update_interval": 24,
|
|
|
|
"parental_enabled": false,
|
|
|
|
"safebrowsing_enabled": false,
|
|
|
|
"safebrowsing_cache_size": 1048576,
|
|
|
|
"safesearch_cache_size": 1048576,
|
|
|
|
"parental_cache_size": 1048576,
|
|
|
|
"safe_search": yobj{
|
|
|
|
"enabled": false,
|
|
|
|
"bing": true,
|
|
|
|
"duckduckgo": true,
|
|
|
|
"google": true,
|
|
|
|
"pixabay": true,
|
|
|
|
"yandex": true,
|
|
|
|
"youtube": true,
|
|
|
|
},
|
|
|
|
"rewrites": yarr{},
|
|
|
|
"blocked_services": yobj{
|
|
|
|
"schedule": yobj{
|
|
|
|
"time_zone": "Local",
|
|
|
|
},
|
|
|
|
"ids": yarr{},
|
|
|
|
},
|
|
|
|
"protection_enabled": true,
|
|
|
|
"blocking_mode": "custom_ip",
|
|
|
|
"blocking_ipv4": "1.2.3.4",
|
|
|
|
"blocking_ipv6": "1:2:3::4",
|
|
|
|
"blocked_response_ttl": 10,
|
|
|
|
"protection_disabled_until": nil,
|
|
|
|
"parental_block_host": "p.dns.adguard.com",
|
|
|
|
"safebrowsing_block_host": "s.dns.adguard.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"dns": yobj{},
|
|
|
|
"filtering": yobj{
|
|
|
|
"filtering_enabled": true,
|
|
|
|
"filters_update_interval": 24,
|
|
|
|
"parental_enabled": false,
|
|
|
|
"safebrowsing_enabled": false,
|
|
|
|
"safebrowsing_cache_size": 1048576,
|
|
|
|
"safesearch_cache_size": 1048576,
|
|
|
|
"parental_cache_size": 1048576,
|
|
|
|
"safe_search": yobj{
|
|
|
|
"enabled": false,
|
|
|
|
"bing": true,
|
|
|
|
"duckduckgo": true,
|
|
|
|
"google": true,
|
|
|
|
"pixabay": true,
|
|
|
|
"yandex": true,
|
|
|
|
"youtube": true,
|
|
|
|
},
|
|
|
|
"rewrites": yarr{},
|
|
|
|
"blocked_services": yobj{
|
|
|
|
"schedule": yobj{
|
|
|
|
"time_zone": "Local",
|
|
|
|
},
|
|
|
|
"ids": yarr{},
|
|
|
|
},
|
|
|
|
"protection_enabled": true,
|
|
|
|
"blocking_mode": "custom_ip",
|
|
|
|
"blocking_ipv4": "1.2.3.4",
|
|
|
|
"blocking_ipv6": "1:2:3::4",
|
|
|
|
"blocked_response_ttl": 10,
|
|
|
|
"protection_disabled_until": nil,
|
|
|
|
"parental_block_host": "p.dns.adguard.com",
|
|
|
|
"safebrowsing_block_host": "s.dns.adguard.com",
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-09-04 20:45:50 +03:00
|
|
|
err := migrateTo26(tc.in)
|
2023-08-30 18:26:02 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 15:13:35 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema26to27(t *testing.T) {
|
|
|
|
const newSchemaVer = 27
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
in: yobj{},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "single_dot",
|
|
|
|
in: yobj{
|
|
|
|
"querylog": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
".",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"statistics": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
".",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"querylog": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
"|.^",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"statistics": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
"|.^",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "mixed",
|
|
|
|
in: yobj{
|
|
|
|
"querylog": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
".",
|
|
|
|
"example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"statistics": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
".",
|
|
|
|
"example.org",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"querylog": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
"|.^",
|
|
|
|
"example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"statistics": yobj{
|
|
|
|
"ignored": yarr{
|
|
|
|
"|.^",
|
|
|
|
"example.org",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := migrateTo27(tc.in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 14:16:48 +03:00
|
|
|
|
|
|
|
func TestUpgradeSchema27to28(t *testing.T) {
|
|
|
|
const newSchemaVer = 28
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
in yobj
|
|
|
|
want yobj
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
in: yobj{},
|
|
|
|
want: yobj{
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "load_balance",
|
|
|
|
in: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"all_servers": false,
|
|
|
|
"fastest_addr": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"upstream_mode": dnsforward.UpstreamModeLoadBalance,
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "parallel",
|
|
|
|
in: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"all_servers": true,
|
|
|
|
"fastest_addr": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"upstream_mode": dnsforward.UpstreamModeParallel,
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "parallel_fastest",
|
|
|
|
in: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"all_servers": true,
|
|
|
|
"fastest_addr": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"upstream_mode": dnsforward.UpstreamModeParallel,
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "load_balance",
|
|
|
|
in: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"all_servers": false,
|
|
|
|
"fastest_addr": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: yobj{
|
|
|
|
"dns": yobj{
|
|
|
|
"upstream_mode": dnsforward.UpstreamModeFastestAddr,
|
|
|
|
},
|
|
|
|
"schema_version": newSchemaVer,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := migrateTo28(tc.in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|