2023-06-22 15:58:09 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2023-08-23 16:58:24 +03:00
|
|
|
"net/netip"
|
2023-06-22 15:58:09 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/schedule"
|
2023-06-27 18:03:07 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-22 15:58:09 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4})
|
|
|
|
|
2023-06-27 18:03:07 +03:00
|
|
|
func TestApplyAdditionalFiltering(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
Context.filters, err = filtering.New(&filtering.Config{
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Context.clients.idIndex = map[string]*Client{
|
|
|
|
"default": {
|
|
|
|
UseOwnSettings: false,
|
|
|
|
safeSearchConf: filtering.SafeSearchConfig{Enabled: false},
|
|
|
|
FilteringEnabled: false,
|
|
|
|
SafeBrowsingEnabled: false,
|
|
|
|
ParentalEnabled: false,
|
|
|
|
},
|
|
|
|
"custom_filtering": {
|
|
|
|
UseOwnSettings: true,
|
|
|
|
safeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
|
|
|
FilteringEnabled: true,
|
|
|
|
SafeBrowsingEnabled: true,
|
|
|
|
ParentalEnabled: true,
|
|
|
|
},
|
|
|
|
"partial_custom_filtering": {
|
|
|
|
UseOwnSettings: true,
|
|
|
|
safeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
|
|
|
FilteringEnabled: true,
|
|
|
|
SafeBrowsingEnabled: false,
|
|
|
|
ParentalEnabled: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
FilteringEnabled assert.BoolAssertionFunc
|
|
|
|
SafeSearchEnabled assert.BoolAssertionFunc
|
|
|
|
SafeBrowsingEnabled assert.BoolAssertionFunc
|
|
|
|
ParentalEnabled assert.BoolAssertionFunc
|
|
|
|
}{{
|
|
|
|
name: "global_settings",
|
|
|
|
id: "default",
|
|
|
|
FilteringEnabled: assert.False,
|
|
|
|
SafeSearchEnabled: assert.False,
|
|
|
|
SafeBrowsingEnabled: assert.False,
|
|
|
|
ParentalEnabled: assert.False,
|
|
|
|
}, {
|
|
|
|
name: "custom_settings",
|
|
|
|
id: "custom_filtering",
|
|
|
|
FilteringEnabled: assert.True,
|
|
|
|
SafeSearchEnabled: assert.True,
|
|
|
|
SafeBrowsingEnabled: assert.True,
|
|
|
|
ParentalEnabled: assert.True,
|
|
|
|
}, {
|
|
|
|
name: "partial",
|
|
|
|
id: "partial_custom_filtering",
|
|
|
|
FilteringEnabled: assert.True,
|
|
|
|
SafeSearchEnabled: assert.True,
|
|
|
|
SafeBrowsingEnabled: assert.False,
|
|
|
|
ParentalEnabled: assert.False,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
setts := &filtering.Settings{}
|
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
applyAdditionalFiltering(testIPv4, tc.id, setts)
|
2023-06-27 18:03:07 +03:00
|
|
|
tc.FilteringEnabled(t, setts.FilteringEnabled)
|
|
|
|
tc.SafeSearchEnabled(t, setts.SafeSearchEnabled)
|
|
|
|
tc.SafeBrowsingEnabled(t, setts.SafeBrowsingEnabled)
|
|
|
|
tc.ParentalEnabled(t, setts.ParentalEnabled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:58:09 +03:00
|
|
|
func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
|
|
|
|
filtering.InitModule()
|
|
|
|
|
|
|
|
var (
|
|
|
|
globalBlockedServices = []string{"ok"}
|
|
|
|
clientBlockedServices = []string{"ok", "mail_ru", "vk"}
|
|
|
|
invalidBlockedServices = []string{"invalid"}
|
|
|
|
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
Context.filters, err = filtering.New(&filtering.Config{
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
IDs: globalBlockedServices,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
Context.clients.idIndex = map[string]*Client{
|
2023-06-27 18:03:07 +03:00
|
|
|
"default": {
|
2023-06-22 15:58:09 +03:00
|
|
|
UseOwnBlockedServices: false,
|
|
|
|
},
|
2023-06-27 18:03:07 +03:00
|
|
|
"no_services": {
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
},
|
2023-06-22 15:58:09 +03:00
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
},
|
2023-06-27 18:03:07 +03:00
|
|
|
"services": {
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
IDs: clientBlockedServices,
|
|
|
|
},
|
2023-06-22 15:58:09 +03:00
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
},
|
2023-06-27 18:03:07 +03:00
|
|
|
"invalid_services": {
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
IDs: invalidBlockedServices,
|
|
|
|
},
|
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
},
|
|
|
|
"allow_all": {
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.FullWeekly(),
|
|
|
|
IDs: clientBlockedServices,
|
|
|
|
},
|
2023-06-22 15:58:09 +03:00
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
wantLen int
|
|
|
|
}{{
|
|
|
|
name: "global_settings",
|
2023-06-27 18:03:07 +03:00
|
|
|
id: "default",
|
2023-06-22 15:58:09 +03:00
|
|
|
wantLen: len(globalBlockedServices),
|
|
|
|
}, {
|
|
|
|
name: "custom_settings",
|
2023-06-27 18:03:07 +03:00
|
|
|
id: "no_services",
|
2023-06-22 15:58:09 +03:00
|
|
|
wantLen: 0,
|
|
|
|
}, {
|
|
|
|
name: "custom_settings_block",
|
2023-06-27 18:03:07 +03:00
|
|
|
id: "services",
|
2023-06-22 15:58:09 +03:00
|
|
|
wantLen: len(clientBlockedServices),
|
|
|
|
}, {
|
|
|
|
name: "custom_settings_invalid",
|
2023-06-27 18:03:07 +03:00
|
|
|
id: "invalid_services",
|
|
|
|
wantLen: 0,
|
|
|
|
}, {
|
|
|
|
name: "custom_settings_inactive_schedule",
|
|
|
|
id: "allow_all",
|
2023-06-22 15:58:09 +03:00
|
|
|
wantLen: 0,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
setts := &filtering.Settings{}
|
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
applyAdditionalFiltering(testIPv4, tc.id, setts)
|
2023-06-22 15:58:09 +03:00
|
|
|
require.Len(t, setts.ServicesRules, tc.wantLen)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|