Skip to content

Commit

Permalink
all: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Jun 26, 2023
1 parent 8e952fc commit 7d9ba98
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 deletions.
4 changes: 4 additions & 0 deletions internal/filtering/blocked.go
Expand Up @@ -58,6 +58,10 @@ type BlockedServices struct {

// Clone returns a deep copy of blocked services.
func (s *BlockedServices) Clone() (c *BlockedServices) {
if s == nil {
return nil
}

return &BlockedServices{
Schedule: s.Schedule.Clone(),
IDs: slices.Clone(s.IDs),
Expand Down
5 changes: 1 addition & 4 deletions internal/home/client.go
Expand Up @@ -46,10 +46,7 @@ type Client struct {
func (c *Client) ShallowClone() (sh *Client) {
clone := *c

if c.BlockedServices != nil {
clone.BlockedServices = c.BlockedServices.Clone()
}

clone.BlockedServices = c.BlockedServices.Clone()
clone.IDs = stringutil.CloneSlice(c.IDs)
clone.Tags = stringutil.CloneSlice(c.Tags)
clone.Upstreams = stringutil.CloneSlice(c.Upstreams)
Expand Down
100 changes: 88 additions & 12 deletions internal/home/dns_internal_test.go
Expand Up @@ -6,9 +6,87 @@ import (

"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/AdGuardHome/internal/schedule"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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{}

applyAdditionalFiltering(net.IP{1, 2, 3, 4}, tc.id, setts)
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()

Expand All @@ -29,30 +107,30 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
require.NoError(t, err)

Context.clients.idIndex = map[string]*Client{
"client_1": {
"default": {
UseOwnBlockedServices: false,
},
"client_2": {
"no_services": {
BlockedServices: &filtering.BlockedServices{
Schedule: schedule.EmptyWeekly(),
},
UseOwnBlockedServices: true,
},
"client_3": {
"services": {
BlockedServices: &filtering.BlockedServices{
Schedule: schedule.EmptyWeekly(),
IDs: clientBlockedServices,
},
UseOwnBlockedServices: true,
},
"client_4": {
"invalid_services": {
BlockedServices: &filtering.BlockedServices{
Schedule: schedule.EmptyWeekly(),
IDs: invalidBlockedServices,
},
UseOwnBlockedServices: true,
},
"client_5": {
"allow_all": {
BlockedServices: &filtering.BlockedServices{
Schedule: schedule.FullWeekly(),
IDs: clientBlockedServices,
Expand All @@ -63,29 +141,27 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {

testCases := []struct {
name string
ip net.IP
id string
setts *filtering.Settings
wantLen int
}{{
name: "global_settings",
id: "client_1",
id: "default",
wantLen: len(globalBlockedServices),
}, {
name: "custom_settings",
id: "client_2",
id: "no_services",
wantLen: 0,
}, {
name: "custom_settings_block",
id: "client_3",
id: "services",
wantLen: len(clientBlockedServices),
}, {
name: "custom_settings_invalid",
id: "client_4",
id: "invalid_services",
wantLen: 0,
}, {
name: "custom_settings_inactive_schedule",
id: "client_5",
id: "allow_all",
wantLen: 0,
}}

Expand Down
6 changes: 3 additions & 3 deletions internal/home/upgrade.go
Expand Up @@ -1222,11 +1222,11 @@ func upgradeSchema21to22(diskConf yobj) (err error) {
return fmt.Errorf("unexpected type of persistent clients: %T", persistentVal)
}

for _, val := range persistent {
for i, val := range persistent {
var c yobj
c, ok = val.(yobj)
if !ok {
return fmt.Errorf("unexpected type of client: %T", val)
return fmt.Errorf("persistent %d: unexpected type of client: %T", i, val)
}

var blockedVal any
Expand All @@ -1238,7 +1238,7 @@ func upgradeSchema21to22(diskConf yobj) (err error) {
var services yarr
services, ok = blockedVal.(yarr)
if !ok {
return fmt.Errorf("unexpected type of blocked: %T", blockedVal)
return fmt.Errorf("persistent: %d: unexpected type of blocked: %T", i, blockedVal)
}

c[field] = yobj{
Expand Down

0 comments on commit 7d9ba98

Please sign in to comment.