2023-07-03 14:10:40 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2023-09-07 17:13:48 +03:00
|
|
|
"net/netip"
|
2023-07-03 14:10:40 +03:00
|
|
|
"testing"
|
|
|
|
|
2024-03-12 17:45:11 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/client"
|
2023-07-03 14:10:40 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/schedule"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4})
|
|
|
|
|
2024-07-03 15:38:37 +03:00
|
|
|
// newStorage is a helper function that returns a client storage filled with
|
|
|
|
// persistent clients. It also generates a UID for each client.
|
|
|
|
func newStorage(tb testing.TB, clients []*client.Persistent) (s *client.Storage) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
s = client.NewStorage(&client.Config{
|
|
|
|
AllowedTags: nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, p := range clients {
|
|
|
|
p.UID = client.MustNewUID()
|
|
|
|
require.NoError(tb, s.Add(p))
|
2024-03-12 17:45:11 +03:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:38:37 +03:00
|
|
|
return s
|
2024-03-12 17:45:11 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 14:10:40 +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)
|
|
|
|
|
2024-07-03 15:38:37 +03:00
|
|
|
Context.clients.storage = newStorage(t, []*client.Persistent{{
|
|
|
|
Name: "default",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"default"},
|
|
|
|
UseOwnSettings: false,
|
|
|
|
SafeSearchConf: filtering.SafeSearchConfig{Enabled: false},
|
|
|
|
FilteringEnabled: false,
|
|
|
|
SafeBrowsingEnabled: false,
|
|
|
|
ParentalEnabled: false,
|
|
|
|
}, {
|
2024-07-03 15:38:37 +03:00
|
|
|
Name: "custom_filtering",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"custom_filtering"},
|
|
|
|
UseOwnSettings: true,
|
|
|
|
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
|
|
|
FilteringEnabled: true,
|
|
|
|
SafeBrowsingEnabled: true,
|
|
|
|
ParentalEnabled: true,
|
|
|
|
}, {
|
2024-07-03 15:38:37 +03:00
|
|
|
Name: "partial_custom_filtering",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"partial_custom_filtering"},
|
|
|
|
UseOwnSettings: true,
|
|
|
|
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
|
|
|
FilteringEnabled: true,
|
|
|
|
SafeBrowsingEnabled: false,
|
|
|
|
ParentalEnabled: false,
|
|
|
|
}})
|
2023-07-03 14:10:40 +03:00
|
|
|
|
|
|
|
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-09-07 17:13:48 +03:00
|
|
|
applyAdditionalFiltering(testIPv4, tc.id, setts)
|
2023-07-03 14:10:40 +03:00
|
|
|
tc.FilteringEnabled(t, setts.FilteringEnabled)
|
|
|
|
tc.SafeSearchEnabled(t, setts.SafeSearchEnabled)
|
|
|
|
tc.SafeBrowsingEnabled(t, setts.SafeBrowsingEnabled)
|
|
|
|
tc.ParentalEnabled(t, setts.ParentalEnabled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-07-03 15:38:37 +03:00
|
|
|
Context.clients.storage = newStorage(t, []*client.Persistent{{
|
|
|
|
Name: "default",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"default"},
|
|
|
|
UseOwnBlockedServices: false,
|
|
|
|
}, {
|
2024-07-03 15:38:37 +03:00
|
|
|
Name: "no_services",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"no_services"},
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
2023-07-03 14:10:40 +03:00
|
|
|
},
|
2024-03-12 17:45:11 +03:00
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
}, {
|
2024-07-03 15:38:37 +03:00
|
|
|
Name: "services",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"services"},
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
IDs: clientBlockedServices,
|
2023-07-03 14:10:40 +03:00
|
|
|
},
|
2024-03-12 17:45:11 +03:00
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
}, {
|
2024-07-03 15:38:37 +03:00
|
|
|
Name: "invalid_services",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"invalid_services"},
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.EmptyWeekly(),
|
|
|
|
IDs: invalidBlockedServices,
|
2023-07-03 14:10:40 +03:00
|
|
|
},
|
2024-03-12 17:45:11 +03:00
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
}, {
|
2024-07-03 15:38:37 +03:00
|
|
|
Name: "allow_all",
|
2024-03-12 17:45:11 +03:00
|
|
|
ClientIDs: []string{"allow_all"},
|
|
|
|
BlockedServices: &filtering.BlockedServices{
|
|
|
|
Schedule: schedule.FullWeekly(),
|
|
|
|
IDs: clientBlockedServices,
|
2023-07-03 14:10:40 +03:00
|
|
|
},
|
2024-03-12 17:45:11 +03:00
|
|
|
UseOwnBlockedServices: true,
|
|
|
|
}})
|
2023-07-03 14:10:40 +03:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
wantLen int
|
|
|
|
}{{
|
|
|
|
name: "global_settings",
|
|
|
|
id: "default",
|
|
|
|
wantLen: len(globalBlockedServices),
|
|
|
|
}, {
|
|
|
|
name: "custom_settings",
|
|
|
|
id: "no_services",
|
|
|
|
wantLen: 0,
|
|
|
|
}, {
|
|
|
|
name: "custom_settings_block",
|
|
|
|
id: "services",
|
|
|
|
wantLen: len(clientBlockedServices),
|
|
|
|
}, {
|
|
|
|
name: "custom_settings_invalid",
|
|
|
|
id: "invalid_services",
|
|
|
|
wantLen: 0,
|
|
|
|
}, {
|
|
|
|
name: "custom_settings_inactive_schedule",
|
|
|
|
id: "allow_all",
|
|
|
|
wantLen: 0,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
setts := &filtering.Settings{}
|
|
|
|
|
2023-09-07 17:13:48 +03:00
|
|
|
applyAdditionalFiltering(testIPv4, tc.id, setts)
|
2023-07-03 14:10:40 +03:00
|
|
|
require.Len(t, setts.ServicesRules, tc.wantLen)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|