Skip to content

Commit

Permalink
Pull request 1886: fix-blocked-services-client-schedule
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from fix-blocked-services-client-schedule to master

Updates #951.

Squashed commit of the following:

commit 1c89095
Merge: ac0b722 f40ef76
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Jun 22 15:54:03 2023 +0300

    Merge branch 'master' into fix-blocked-services-client-schedule

commit ac0b722
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Jun 22 14:32:03 2023 +0300

    home: fix typo

commit 64eb519
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Jun 22 14:23:07 2023 +0300

    home: add test case

commit c7b60f3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Jun 22 12:00:24 2023 +0300

    home: imp code

commit 7806c92
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Jun 21 20:49:47 2023 +0300

    home: add tests

commit d37c9fd
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Jun 21 18:49:43 2023 +0300

    home: fix client blocked services
  • Loading branch information
schzhn committed Jun 22, 2023
1 parent f40ef76 commit 37e046a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
4 changes: 1 addition & 3 deletions internal/home/dns.go
Expand Up @@ -473,10 +473,8 @@ func applyAdditionalFiltering(clientIP net.IP, clientID string, setts *filtering

if c.UseOwnBlockedServices {
// TODO(e.burkov): Get rid of this crutch.
setts.ServicesRules = nil
svcs := c.BlockedServices
if svcs == nil {
svcs = []string{}
}
Context.filters.ApplyBlockedServicesList(setts, svcs)
log.Debug("%s: services for client %q set: %s", pref, c.Name, svcs)
}
Expand Down
80 changes: 80 additions & 0 deletions internal/home/dns_internal_test.go
@@ -0,0 +1,80 @@
package home

import (
"net"
"testing"

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

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{
"client_1": {
UseOwnBlockedServices: false,
},
"client_2": {
UseOwnBlockedServices: true,
},
"client_3": {
BlockedServices: clientBlockedServices,
UseOwnBlockedServices: true,
},
"client_4": {
BlockedServices: invalidBlockedServices,
UseOwnBlockedServices: true,
},
}

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

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)
require.Len(t, setts.ServicesRules, tc.wantLen)
})
}
}

0 comments on commit 37e046a

Please sign in to comment.