From 37e046acc44138b0275733e03eadff1827b40c45 Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Thu, 22 Jun 2023 15:58:09 +0300 Subject: [PATCH] Pull request 1886: fix-blocked-services-client-schedule Merge in DNS/adguard-home from fix-blocked-services-client-schedule to master Updates #951. Squashed commit of the following: commit 1c890953ff4b7862db10f99f4851797050ad9d8e Merge: ac0b722d1 f40ef76c7 Author: Stanislav Chzhen Date: Thu Jun 22 15:54:03 2023 +0300 Merge branch 'master' into fix-blocked-services-client-schedule commit ac0b722d1deef664ab464e0bff387542e027bd3f Author: Stanislav Chzhen Date: Thu Jun 22 14:32:03 2023 +0300 home: fix typo commit 64eb519c4b77cff1071059f1f41acfd0e3ea9513 Author: Stanislav Chzhen Date: Thu Jun 22 14:23:07 2023 +0300 home: add test case commit c7b60f3ea89e41f8b84bf6f2f5e075e9b3dd45ec Author: Stanislav Chzhen Date: Thu Jun 22 12:00:24 2023 +0300 home: imp code commit 7806c920bb0c4b4c44c3fed7e920f795904630b2 Author: Stanislav Chzhen Date: Wed Jun 21 20:49:47 2023 +0300 home: add tests commit d37c9fde882965f005ddec86ad0502585d2eea95 Author: Stanislav Chzhen Date: Wed Jun 21 18:49:43 2023 +0300 home: fix client blocked services --- internal/home/dns.go | 4 +- internal/home/dns_internal_test.go | 80 ++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 internal/home/dns_internal_test.go diff --git a/internal/home/dns.go b/internal/home/dns.go index ebbcb16e..344f0be0 100644 --- a/internal/home/dns.go +++ b/internal/home/dns.go @@ -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) } diff --git a/internal/home/dns_internal_test.go b/internal/home/dns_internal_test.go new file mode 100644 index 00000000..9450cdf6 --- /dev/null +++ b/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) + }) + } +}