all: use storage

This commit is contained in:
Stanislav Chzhen 2024-06-19 20:10:55 +03:00
parent 2c7efa4609
commit eae49f91bc
7 changed files with 107 additions and 259 deletions

View File

@ -2,10 +2,12 @@ package client
import ( import (
"fmt" "fmt"
"net"
"net/netip" "net/netip"
"sync" "sync"
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
) )
// Storage contains information about persistent and runtime clients. // Storage contains information about persistent and runtime clients.
@ -15,9 +17,6 @@ type Storage struct {
// index contains information about persistent clients. // index contains information about persistent clients.
index *Index index *Index
// runtimeIndex contains information about runtime clients.
runtimeIndex map[netip.Addr]*Runtime
} }
// NewStorage returns initialized client storage. // NewStorage returns initialized client storage.
@ -25,7 +24,6 @@ func NewStorage() (s *Storage) {
return &Storage{ return &Storage{
mu: &sync.Mutex{}, mu: &sync.Mutex{},
index: NewIndex(), index: NewIndex(),
runtimeIndex: map[netip.Addr]*Runtime{},
} }
} }
@ -51,9 +49,19 @@ func (s *Storage) Add(p *Persistent) (err error) {
s.index.Add(p) s.index.Add(p)
log.Debug("client storage: added %q: IDs: %q [%d]", p.Name, p.IDs(), s.index.Size())
return nil return nil
} }
// FindByName finds persistent client by name.
func (s *Storage) FindByName(name string) (c *Persistent, found bool) {
s.mu.Lock()
defer s.mu.Unlock()
return s.index.FindByName(name)
}
// Find finds persistent client by string representation of the client ID, IP // Find finds persistent client by string representation of the client ID, IP
// address, or MAC. And returns it shallow copy. // address, or MAC. And returns it shallow copy.
func (s *Storage) Find(id string) (p *Persistent, ok bool) { func (s *Storage) Find(id string) (p *Persistent, ok bool) {
@ -92,6 +100,14 @@ func (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
return nil, false return nil, false
} }
// FindByMAC finds persistent client by MAC.
func (s *Storage) FindByMAC(mac net.HardwareAddr) (c *Persistent, found bool) {
s.mu.Lock()
defer s.mu.Unlock()
return s.index.FindByMAC(mac)
}
// RemoveByName removes persistent client information. ok is false if no such // RemoveByName removes persistent client information. ok is false if no such
// client exists by that name. // client exists by that name.
func (s *Storage) RemoveByName(name string) (ok bool) { func (s *Storage) RemoveByName(name string) (ok bool) {
@ -103,13 +119,18 @@ func (s *Storage) RemoveByName(name string) (ok bool) {
return false return false
} }
if err := p.CloseUpstreams(); err != nil {
log.Error("client storage: removing client %q: %s", p.Name, err)
}
s.index.Delete(p) s.index.Delete(p)
return true return true
} }
// Update finds the stored persistent client by its name and updates its // Update finds the stored persistent client by its name and updates its
// information from n. // information from n. n must be valid persistent client. See
// [Persistent.Validate].
func (s *Storage) Update(name string, n *Persistent) (err error) { func (s *Storage) Update(name string, n *Persistent) (err error) {
defer func() { err = errors.Annotate(err, "updating client: %w") }() defer func() { err = errors.Annotate(err, "updating client: %w") }()
@ -147,6 +168,14 @@ func (s *Storage) RangeByName(f func(c *Persistent) (cont bool)) {
s.index.RangeByName(f) s.index.RangeByName(f)
} }
// Size returns the number of persistent clients.
func (s *Storage) Size() (n int) {
s.mu.Lock()
defer s.mu.Unlock()
return s.index.Size()
}
// CloseUpstreams closes upstream configurations of persistent clients. // CloseUpstreams closes upstream configurations of persistent clients.
func (s *Storage) CloseUpstreams() (err error) { func (s *Storage) CloseUpstreams() (err error) {
s.mu.Lock() s.mu.Lock()
@ -154,50 +183,3 @@ func (s *Storage) CloseUpstreams() (err error) {
return s.index.CloseUpstreams() return s.index.CloseUpstreams()
} }
// ClientRuntime returns the saved runtime client by ip. If no such client
// exists, returns nil.
func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
return s.runtimeIndex[ip]
}
// AddRuntime saves the runtime client information in the storage. IP address
// of a client must be unique. rc must not be nil.
func (s *Storage) AddRuntime(rc *Runtime) {
ip := rc.Addr()
s.runtimeIndex[ip] = rc
}
// SizeRuntime returns the number of the runtime clients.
func (s *Storage) SizeRuntime() (n int) {
return len(s.runtimeIndex)
}
// RangeRuntime calls f for each runtime client in an undefined order.
func (s *Storage) RangeRuntime(f func(rc *Runtime) (cont bool)) {
for _, rc := range s.runtimeIndex {
if !f(rc) {
return
}
}
}
// DeleteRuntime removes the runtime client by ip.
func (s *Storage) DeleteRuntime(ip netip.Addr) {
delete(s.runtimeIndex, ip)
}
// DeleteBySource removes all runtime clients that have information only from
// the specified source and returns the number of removed clients.
func (s *Storage) DeleteBySource(src Source) (n int) {
for ip, rc := range s.runtimeIndex {
rc.unset(src)
if rc.isEmpty() {
delete(s.runtimeIndex, ip)
n++
}
}
return n
}

View File

@ -366,7 +366,6 @@ func TestStorage_Update(t *testing.T) {
cli: &client.Persistent{ cli: &client.Persistent{
Name: "basic", Name: "basic",
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1")}, IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1")},
UID: client.MustNewUID(),
}, },
wantErrMsg: "", wantErrMsg: "",
}, { }, {

View File

@ -44,8 +44,8 @@ type DHCP interface {
// clientsContainer is the storage of all runtime and persistent clients. // clientsContainer is the storage of all runtime and persistent clients.
type clientsContainer struct { type clientsContainer struct {
// clientIndex stores information about persistent clients. // storage stores information about persistent clients.
clientIndex *client.Index storage *client.Storage
// runtimeIndex stores information about runtime clients. // runtimeIndex stores information about runtime clients.
runtimeIndex *client.RuntimeIndex runtimeIndex *client.RuntimeIndex
@ -103,13 +103,13 @@ func (clients *clientsContainer) Init(
filteringConf *filtering.Config, filteringConf *filtering.Config,
) (err error) { ) (err error) {
// TODO(s.chzhen): Refactor it. // TODO(s.chzhen): Refactor it.
if clients.clientIndex != nil { if clients.storage != nil {
return errors.Error("clients container already initialized") return errors.Error("clients container already initialized")
} }
clients.runtimeIndex = client.NewRuntimeIndex() clients.runtimeIndex = client.NewRuntimeIndex()
clients.clientIndex = client.NewIndex() clients.storage = client.NewStorage()
clients.allTags = container.NewMapSet(clientTags...) clients.allTags = container.NewMapSet(clientTags...)
@ -285,17 +285,14 @@ func (clients *clientsContainer) addFromConfig(
return fmt.Errorf("clients: init persistent client at index %d: %w", i, err) return fmt.Errorf("clients: init persistent client at index %d: %w", i, err)
} }
// TODO(s.chzhen): Consider moving to the client index constructor. err = cli.Validate(clients.allTags)
err = clients.clientIndex.ClashesUID(cli)
if err != nil { if err != nil {
return fmt.Errorf("adding client %s at index %d: %w", cli.Name, i, err) return fmt.Errorf("validating client %s at index %d: %w", cli.Name, i, err)
} }
err = clients.add(cli) err = clients.storage.Add(cli)
if err != nil { if err != nil {
// TODO(s.chzhen): Return an error instead of logging if more return fmt.Errorf("adding client %s at index %d: %w", cli.Name, i, err)
// stringent requirements are implemented.
log.Error("clients: adding client %s at index %d: %s", cli.Name, i, err)
} }
} }
@ -308,8 +305,8 @@ func (clients *clientsContainer) forConfig() (objs []*clientObject) {
clients.lock.Lock() clients.lock.Lock()
defer clients.lock.Unlock() defer clients.lock.Unlock()
objs = make([]*clientObject, 0, clients.clientIndex.Size()) objs = []*clientObject{}
clients.clientIndex.RangeByName(func(cli *client.Persistent) (cont bool) { clients.storage.RangeByName(func(cli *client.Persistent) (cont bool) {
objs = append(objs, &clientObject{ objs = append(objs, &clientObject{
Name: cli.Name, Name: cli.Name,
@ -336,7 +333,7 @@ func (clients *clientsContainer) forConfig() (objs []*clientObject) {
return true return true
}) })
return objs return slices.Clip(objs)
} }
// arpClientsUpdatePeriod defines how often ARP clients are updated. // arpClientsUpdatePeriod defines how often ARP clients are updated.
@ -412,12 +409,8 @@ func (clients *clientsContainer) clientOrArtificial(
} }
}() }()
cli, ok := clients.find(id) cli, ok := clients.storage.FindLoose(ip, id)
if !ok { if ok {
cli = clients.clientIndex.FindByIPWithoutZone(ip)
}
if cli != nil {
return &querylog.Client{ return &querylog.Client{
Name: cli.Name, Name: cli.Name,
IgnoreQueryLog: cli.IgnoreQueryLog, IgnoreQueryLog: cli.IgnoreQueryLog,
@ -523,7 +516,7 @@ func (clients *clientsContainer) UpstreamConfigByID(
// findLocked searches for a client by its ID. clients.lock is expected to be // findLocked searches for a client by its ID. clients.lock is expected to be
// locked. // locked.
func (clients *clientsContainer) findLocked(id string) (c *client.Persistent, ok bool) { func (clients *clientsContainer) findLocked(id string) (c *client.Persistent, ok bool) {
c, ok = clients.clientIndex.Find(id) c, ok = clients.storage.Find(id)
if ok { if ok {
return c, true return c, true
} }
@ -545,7 +538,7 @@ func (clients *clientsContainer) findDHCP(ip netip.Addr) (c *client.Persistent,
return nil, false return nil, false
} }
return clients.clientIndex.FindByMAC(foundMAC) return clients.storage.FindByMAC(foundMAC)
} }
// runtimeClient returns a runtime client from internal index. Note that it // runtimeClient returns a runtime client from internal index. Note that it
@ -579,114 +572,6 @@ func (clients *clientsContainer) findRuntimeClient(ip netip.Addr) (rc *client.Ru
return rc return rc
} }
// check validates the client. It also sorts the client tags.
func (clients *clientsContainer) check(c *client.Persistent) (err error) {
switch {
case c == nil:
return errors.Error("client is nil")
case c.Name == "":
return errors.Error("invalid name")
case c.IDsLen() == 0:
return errors.Error("id required")
default:
// Go on.
}
for _, t := range c.Tags {
if !clients.allTags.Has(t) {
return fmt.Errorf("invalid tag: %q", t)
}
}
// TODO(s.chzhen): Move to the constructor.
slices.Sort(c.Tags)
_, err = proxy.ParseUpstreamsConfig(c.Upstreams, &upstream.Options{})
if err != nil {
return fmt.Errorf("invalid upstream servers: %w", err)
}
return nil
}
// add adds a persistent client or returns an error.
func (clients *clientsContainer) add(c *client.Persistent) (err error) {
err = clients.check(c)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
clients.lock.Lock()
defer clients.lock.Unlock()
err = clients.clientIndex.Clashes(c)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
clients.addLocked(c)
log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs(), clients.clientIndex.Size())
return nil
}
// addLocked c to the indexes. clients.lock is expected to be locked.
func (clients *clientsContainer) addLocked(c *client.Persistent) {
clients.clientIndex.Add(c)
}
// remove removes a client. ok is false if there is no such client.
func (clients *clientsContainer) remove(name string) (ok bool) {
clients.lock.Lock()
defer clients.lock.Unlock()
c, ok := clients.clientIndex.FindByName(name)
if !ok {
return false
}
clients.removeLocked(c)
return true
}
// removeLocked removes c from the indexes. clients.lock is expected to be
// locked.
func (clients *clientsContainer) removeLocked(c *client.Persistent) {
if err := c.CloseUpstreams(); err != nil {
log.Error("client container: removing client %s: %s", c.Name, err)
}
// Update the ID index.
clients.clientIndex.Delete(c)
}
// update updates a client by its name.
func (clients *clientsContainer) update(prev, c *client.Persistent) (err error) {
err = clients.check(c)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
clients.lock.Lock()
defer clients.lock.Unlock()
err = clients.clientIndex.Clashes(c)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
clients.removeLocked(prev)
clients.addLocked(c)
return nil
}
// setWHOISInfo sets the WHOIS information for a client. clients.lock is // setWHOISInfo sets the WHOIS information for a client. clients.lock is
// expected to be locked. // expected to be locked.
func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *whois.Info) { func (clients *clientsContainer) setWHOISInfo(ip netip.Addr, wi *whois.Info) {
@ -848,5 +733,5 @@ func (clients *clientsContainer) addFromSystemARP() {
// close gracefully closes all the client-specific upstream configurations of // close gracefully closes all the client-specific upstream configurations of
// the persistent clients. // the persistent clients.
func (clients *clientsContainer) close() (err error) { func (clients *clientsContainer) close() (err error) {
return clients.clientIndex.CloseUpstreams() return clients.storage.CloseUpstreams()
} }

View File

@ -72,7 +72,7 @@ func TestClients(t *testing.T) {
IPs: []netip.Addr{cli1IP, cliIPv6}, IPs: []netip.Addr{cli1IP, cliIPv6},
} }
err := clients.add(c) err := clients.storage.Add(c)
require.NoError(t, err) require.NoError(t, err)
c = &client.Persistent{ c = &client.Persistent{
@ -81,7 +81,7 @@ func TestClients(t *testing.T) {
IPs: []netip.Addr{cli2IP}, IPs: []netip.Addr{cli2IP},
} }
err = clients.add(c) err = clients.storage.Add(c)
require.NoError(t, err) require.NoError(t, err)
c, ok := clients.find(cli1) c, ok := clients.find(cli1)
@ -106,31 +106,6 @@ func TestClients(t *testing.T) {
assert.Equal(t, clients.clientSource(cli2IP), client.SourcePersistent) assert.Equal(t, clients.clientSource(cli2IP), client.SourcePersistent)
}) })
t.Run("add_fail_name", func(t *testing.T) {
err := clients.add(&client.Persistent{
Name: "client1",
UID: client.MustNewUID(),
IPs: []netip.Addr{netip.MustParseAddr("1.2.3.5")},
})
require.Error(t, err)
})
t.Run("add_fail_ip", func(t *testing.T) {
err := clients.add(&client.Persistent{
Name: "client3",
UID: client.MustNewUID(),
})
require.Error(t, err)
})
t.Run("update_fail_ip", func(t *testing.T) {
err := clients.update(&client.Persistent{Name: "client1"}, &client.Persistent{
Name: "client1",
UID: client.MustNewUID(),
})
assert.Error(t, err)
})
t.Run("update_success", func(t *testing.T) { t.Run("update_success", func(t *testing.T) {
var ( var (
cliOld = "1.1.1.1" cliOld = "1.1.1.1"
@ -139,11 +114,11 @@ func TestClients(t *testing.T) {
cliNewIP = netip.MustParseAddr(cliNew) cliNewIP = netip.MustParseAddr(cliNew)
) )
prev, ok := clients.clientIndex.FindByName("client1") prev, ok := clients.storage.FindByName("client1")
require.True(t, ok) require.True(t, ok)
require.NotNil(t, prev) require.NotNil(t, prev)
err := clients.update(prev, &client.Persistent{ err := clients.storage.Update("client1", &client.Persistent{
Name: "client1", Name: "client1",
UID: prev.UID, UID: prev.UID,
IPs: []netip.Addr{cliNewIP}, IPs: []netip.Addr{cliNewIP},
@ -155,11 +130,11 @@ func TestClients(t *testing.T) {
assert.Equal(t, clients.clientSource(cliNewIP), client.SourcePersistent) assert.Equal(t, clients.clientSource(cliNewIP), client.SourcePersistent)
prev, ok = clients.clientIndex.FindByName("client1") prev, ok = clients.storage.FindByName("client1")
require.True(t, ok) require.True(t, ok)
require.NotNil(t, prev) require.NotNil(t, prev)
err = clients.update(prev, &client.Persistent{ err = clients.storage.Update("client1", &client.Persistent{
Name: "client1-renamed", Name: "client1-renamed",
UID: prev.UID, UID: prev.UID,
IPs: []netip.Addr{cliNewIP}, IPs: []netip.Addr{cliNewIP},
@ -173,7 +148,7 @@ func TestClients(t *testing.T) {
assert.Equal(t, "client1-renamed", c.Name) assert.Equal(t, "client1-renamed", c.Name)
assert.True(t, c.UseOwnSettings) assert.True(t, c.UseOwnSettings)
nilCli, ok := clients.clientIndex.FindByName("client1") nilCli, ok := clients.storage.FindByName("client1")
require.False(t, ok) require.False(t, ok)
assert.Nil(t, nilCli) assert.Nil(t, nilCli)
@ -184,7 +159,7 @@ func TestClients(t *testing.T) {
}) })
t.Run("del_success", func(t *testing.T) { t.Run("del_success", func(t *testing.T) {
ok := clients.remove("client1-renamed") ok := clients.storage.RemoveByName("client1-renamed")
require.True(t, ok) require.True(t, ok)
_, ok = clients.find("1.1.1.2") _, ok = clients.find("1.1.1.2")
@ -192,7 +167,7 @@ func TestClients(t *testing.T) {
}) })
t.Run("del_fail", func(t *testing.T) { t.Run("del_fail", func(t *testing.T) {
ok := clients.remove("client3") ok := clients.storage.RemoveByName("client3")
assert.False(t, ok) assert.False(t, ok)
}) })
@ -261,7 +236,7 @@ func TestClientsWHOIS(t *testing.T) {
t.Run("can't_set_manually-added", func(t *testing.T) { t.Run("can't_set_manually-added", func(t *testing.T) {
ip := netip.MustParseAddr("1.1.1.2") ip := netip.MustParseAddr("1.1.1.2")
err := clients.add(&client.Persistent{ err := clients.storage.Add(&client.Persistent{
Name: "client1", Name: "client1",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.2")}, IPs: []netip.Addr{netip.MustParseAddr("1.1.1.2")},
@ -272,7 +247,7 @@ func TestClientsWHOIS(t *testing.T) {
rc := clients.runtimeIndex.Client(ip) rc := clients.runtimeIndex.Client(ip)
require.Nil(t, rc) require.Nil(t, rc)
assert.True(t, clients.remove("client1")) assert.True(t, clients.storage.RemoveByName("client1"))
}) })
} }
@ -283,7 +258,7 @@ func TestClientsAddExisting(t *testing.T) {
ip := netip.MustParseAddr("1.1.1.1") ip := netip.MustParseAddr("1.1.1.1")
// Add a client. // Add a client.
err := clients.add(&client.Persistent{ err := clients.storage.Add(&client.Persistent{
Name: "client1", Name: "client1",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{ip, netip.MustParseAddr("1:2:3::4")}, IPs: []netip.Addr{ip, netip.MustParseAddr("1:2:3::4")},
@ -333,7 +308,7 @@ func TestClientsAddExisting(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Add a new client with the same IP as for a client with MAC. // Add a new client with the same IP as for a client with MAC.
err = clients.add(&client.Persistent{ err = clients.storage.Add(&client.Persistent{
Name: "client2", Name: "client2",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{ip}, IPs: []netip.Addr{ip},
@ -341,7 +316,7 @@ func TestClientsAddExisting(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Add a new client with the IP from the first client's IP range. // Add a new client with the IP from the first client's IP range.
err = clients.add(&client.Persistent{ err = clients.storage.Add(&client.Persistent{
Name: "client3", Name: "client3",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{netip.MustParseAddr("2.2.2.2")}, IPs: []netip.Addr{netip.MustParseAddr("2.2.2.2")},
@ -354,7 +329,7 @@ func TestClientsCustomUpstream(t *testing.T) {
clients := newClientsContainer(t) clients := newClientsContainer(t)
// Add client with upstreams. // Add client with upstreams.
err := clients.add(&client.Persistent{ err := clients.storage.Add(&client.Persistent{
Name: "client1", Name: "client1",
UID: client.MustNewUID(), UID: client.MustNewUID(),
IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("1:2:3::4")}, IPs: []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("1:2:3::4")},

View File

@ -96,7 +96,7 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, r *http
clients.lock.Lock() clients.lock.Lock()
defer clients.lock.Unlock() defer clients.lock.Unlock()
clients.clientIndex.Range(func(c *client.Persistent) (cont bool) { clients.storage.RangeByName(func(c *client.Persistent) (cont bool) {
cj := clientToJSON(c) cj := clientToJSON(c)
data.Clients = append(data.Clients, cj) data.Clients = append(data.Clients, cj)
@ -336,7 +336,14 @@ func (clients *clientsContainer) handleAddClient(w http.ResponseWriter, r *http.
return return
} }
err = clients.add(c) err = c.Validate(clients.allTags)
if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
return
}
err = clients.storage.Add(c)
if err != nil { if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
@ -364,7 +371,7 @@ func (clients *clientsContainer) handleDelClient(w http.ResponseWriter, r *http.
return return
} }
if !clients.remove(cj.Name) { if !clients.storage.RemoveByName(cj.Name) {
aghhttp.Error(r, w, http.StatusBadRequest, "Client not found") aghhttp.Error(r, w, http.StatusBadRequest, "Client not found")
return return
@ -399,30 +406,21 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht
return return
} }
var prev *client.Persistent c, err := clients.jsonToClient(dj.Data, nil)
var ok bool
func() {
clients.lock.Lock()
defer clients.lock.Unlock()
prev, ok = clients.clientIndex.FindByName(dj.Name)
}()
if !ok {
aghhttp.Error(r, w, http.StatusBadRequest, "client not found")
return
}
c, err := clients.jsonToClient(dj.Data, prev)
if err != nil { if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
return return
} }
err = clients.update(prev, c) err = c.Validate(clients.allTags)
if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)
return
}
err = clients.storage.Update(dj.Name, c)
if err != nil { if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err) aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)

View File

@ -198,11 +198,11 @@ func TestClientsContainer_HandleDelClient(t *testing.T) {
clients := newClientsContainer(t) clients := newClientsContainer(t)
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1}) clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
err := clients.add(clientOne) err := clients.storage.Add(clientOne)
require.NoError(t, err) require.NoError(t, err)
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2}) clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
err = clients.add(clientTwo) err = clients.storage.Add(clientTwo)
require.NoError(t, err) require.NoError(t, err)
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo}) assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})
@ -260,7 +260,7 @@ func TestClientsContainer_HandleUpdateClient(t *testing.T) {
clients := newClientsContainer(t) clients := newClientsContainer(t)
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1}) clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
err := clients.add(clientOne) err := clients.storage.Add(clientOne)
require.NoError(t, err) require.NoError(t, err)
assertPersistentClients(t, clients, []*client.Persistent{clientOne}) assertPersistentClients(t, clients, []*client.Persistent{clientOne})
@ -342,11 +342,11 @@ func TestClientsContainer_HandleFindClient(t *testing.T) {
} }
clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1}) clientOne := newPersistentClientWithIDs(t, "client1", []string{testClientIP1})
err := clients.add(clientOne) err := clients.storage.Add(clientOne)
require.NoError(t, err) require.NoError(t, err)
clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2}) clientTwo := newPersistentClientWithIDs(t, "client2", []string{testClientIP2})
err = clients.add(clientTwo) err = clients.storage.Add(clientTwo)
require.NoError(t, err) require.NoError(t, err)
assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo}) assertPersistentClients(t, clients, []*client.Persistent{clientOne, clientTwo})

View File

@ -13,17 +13,18 @@ import (
var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4}) var testIPv4 = netip.AddrFrom4([4]byte{1, 2, 3, 4})
// newIDIndex is a helper function that returns a client index filled with // newStorage is a helper function that returns a client index filled with
// persistent clients from the m. It also generates a UID for each client. // persistent clients from the m. It also generates a UID for each client.
func newIDIndex(m []*client.Persistent) (ci *client.Index) { func newStorage(tb testing.TB, m []*client.Persistent) (s *client.Storage) {
ci = client.NewIndex() tb.Helper()
s = client.NewStorage()
for _, c := range m { for _, c := range m {
c.UID = client.MustNewUID() c.UID = client.MustNewUID()
ci.Add(c) require.NoError(tb, s.Add(c))
} }
return ci return s
} }
func TestApplyAdditionalFiltering(t *testing.T) { func TestApplyAdditionalFiltering(t *testing.T) {
@ -36,7 +37,8 @@ func TestApplyAdditionalFiltering(t *testing.T) {
}, nil) }, nil)
require.NoError(t, err) require.NoError(t, err)
Context.clients.clientIndex = newIDIndex([]*client.Persistent{{ Context.clients.storage = newStorage(t, []*client.Persistent{{
Name: "default",
ClientIDs: []string{"default"}, ClientIDs: []string{"default"},
UseOwnSettings: false, UseOwnSettings: false,
SafeSearchConf: filtering.SafeSearchConfig{Enabled: false}, SafeSearchConf: filtering.SafeSearchConfig{Enabled: false},
@ -44,6 +46,7 @@ func TestApplyAdditionalFiltering(t *testing.T) {
SafeBrowsingEnabled: false, SafeBrowsingEnabled: false,
ParentalEnabled: false, ParentalEnabled: false,
}, { }, {
Name: "custom_filtering",
ClientIDs: []string{"custom_filtering"}, ClientIDs: []string{"custom_filtering"},
UseOwnSettings: true, UseOwnSettings: true,
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true}, SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
@ -51,6 +54,7 @@ func TestApplyAdditionalFiltering(t *testing.T) {
SafeBrowsingEnabled: true, SafeBrowsingEnabled: true,
ParentalEnabled: true, ParentalEnabled: true,
}, { }, {
Name: "partial_custom_filtering",
ClientIDs: []string{"partial_custom_filtering"}, ClientIDs: []string{"partial_custom_filtering"},
UseOwnSettings: true, UseOwnSettings: true,
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true}, SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
@ -121,16 +125,19 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
}, nil) }, nil)
require.NoError(t, err) require.NoError(t, err)
Context.clients.clientIndex = newIDIndex([]*client.Persistent{{ Context.clients.storage = newStorage(t, []*client.Persistent{{
Name: "default",
ClientIDs: []string{"default"}, ClientIDs: []string{"default"},
UseOwnBlockedServices: false, UseOwnBlockedServices: false,
}, { }, {
Name: "no_services",
ClientIDs: []string{"no_services"}, ClientIDs: []string{"no_services"},
BlockedServices: &filtering.BlockedServices{ BlockedServices: &filtering.BlockedServices{
Schedule: schedule.EmptyWeekly(), Schedule: schedule.EmptyWeekly(),
}, },
UseOwnBlockedServices: true, UseOwnBlockedServices: true,
}, { }, {
Name: "services",
ClientIDs: []string{"services"}, ClientIDs: []string{"services"},
BlockedServices: &filtering.BlockedServices{ BlockedServices: &filtering.BlockedServices{
Schedule: schedule.EmptyWeekly(), Schedule: schedule.EmptyWeekly(),
@ -138,6 +145,7 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
}, },
UseOwnBlockedServices: true, UseOwnBlockedServices: true,
}, { }, {
Name: "invalid_services",
ClientIDs: []string{"invalid_services"}, ClientIDs: []string{"invalid_services"},
BlockedServices: &filtering.BlockedServices{ BlockedServices: &filtering.BlockedServices{
Schedule: schedule.EmptyWeekly(), Schedule: schedule.EmptyWeekly(),
@ -145,6 +153,7 @@ func TestApplyAdditionalFiltering_blockedServices(t *testing.T) {
}, },
UseOwnBlockedServices: true, UseOwnBlockedServices: true,
}, { }, {
Name: "allow_all",
ClientIDs: []string{"allow_all"}, ClientIDs: []string{"allow_all"},
BlockedServices: &filtering.BlockedServices{ BlockedServices: &filtering.BlockedServices{
Schedule: schedule.FullWeekly(), Schedule: schedule.FullWeekly(),