safing-portmaster/base/runtime/registry_test.go
Daniel Hååvi 80664d1a27
Restructure modules (#1572)
* Move portbase into monorepo

* Add new simple module mgr

* [WIP] Switch to new simple module mgr

* Add StateMgr and more worker variants

* [WIP] Switch more modules

* [WIP] Switch more modules

* [WIP] swtich more modules

* [WIP] switch all SPN modules

* [WIP] switch all service modules

* [WIP] Convert all workers to the new module system

* [WIP] add new task system to module manager

* [WIP] Add second take for scheduling workers

* [WIP] Add FIXME for bugs in new scheduler

* [WIP] Add minor improvements to scheduler

* [WIP] Add new worker scheduler

* [WIP] Fix more bug related to new module system

* [WIP] Fix start handing of the new module system

* [WIP] Improve startup process

* [WIP] Fix minor issues

* [WIP] Fix missing subsystem in settings

* [WIP] Initialize managers in constructor

* [WIP] Move module event initialization to constrictors

* [WIP] Fix setting for enabling and disabling the SPN module

* [WIP] Move API registeration into module construction

* [WIP] Update states mgr for all modules

* [WIP] Add CmdLine operation support

* Add state helper methods to module group and instance

* Add notification and module status handling to status package

* Fix starting issues

* Remove pilot widget and update security lock to new status data

* Remove debug logs

* Improve http server shutdown

* Add workaround for cleanly shutting down firewall+netquery

* Improve logging

* Add syncing states with notifications for new module system

* Improve starting, stopping, shutdown; resolve FIXMEs/TODOs

* [WIP] Fix most unit tests

* Review new module system and fix minor issues

* Push shutdown and restart events again via API

* Set sleep mode via interface

* Update example/template module

* [WIP] Fix spn/cabin unit test

* Remove deprecated UI elements

* Make log output more similar for the logging transition phase

* Switch spn hub and observer cmds to new module system

* Fix log sources

* Make worker mgr less error prone

* Fix tests and minor issues

* Fix observation hub

* Improve shutdown and restart handling

* Split up big connection.go source file

* Move varint and dsd packages to structures repo

* Improve expansion test

* Fix linter warnings

* Fix interception module on windows

* Fix linter errors

---------

Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
2024-08-09 18:15:48 +03:00

157 lines
3 KiB
Go

package runtime
import (
"errors"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/safing/portmaster/base/database/query"
"github.com/safing/portmaster/base/database/record"
)
type testRecord struct {
record.Base
sync.Mutex
Value string
}
func makeTestRecord(key, value string) record.Record {
r := &testRecord{Value: value}
r.CreateMeta()
r.SetKey("runtime:" + key)
return r
}
type testProvider struct {
k string
r []record.Record
}
func (tp *testProvider) Get(key string) ([]record.Record, error) {
return tp.r, nil
}
func (tp *testProvider) Set(r record.Record) (record.Record, error) {
return nil, errors.New("not implemented")
}
func getTestRegistry(t *testing.T) *Registry {
t.Helper()
r := NewRegistry()
providers := []testProvider{
{
k: "p1/",
r: []record.Record{
makeTestRecord("p1/f1/v1", "p1.1"),
makeTestRecord("p1/f2/v2", "p1.2"),
makeTestRecord("p1/v3", "p1.3"),
},
},
{
k: "p2/f1",
r: []record.Record{
makeTestRecord("p2/f1/v1", "p2.1"),
makeTestRecord("p2/f1/f2/v2", "p2.2"),
makeTestRecord("p2/f1/v3", "p2.3"),
},
},
}
for idx := range providers {
p := providers[idx]
_, err := r.Register(p.k, &p)
require.NoError(t, err)
}
return r
}
func TestRegistryGet(t *testing.T) {
t.Parallel()
var (
r record.Record
err error
)
reg := getTestRegistry(t)
r, err = reg.Get("p1/f1/v1")
require.NoError(t, err)
require.NotNil(t, r)
assert.Equal(t, "p1.1", r.(*testRecord).Value) //nolint:forcetypeassert
r, err = reg.Get("p1/v3")
require.NoError(t, err)
require.NotNil(t, r)
assert.Equal(t, "p1.3", r.(*testRecord).Value) //nolint:forcetypeassert
r, err = reg.Get("p1/v4")
require.Error(t, err)
assert.Nil(t, r)
r, err = reg.Get("no-provider/foo")
require.Error(t, err)
assert.Nil(t, r)
}
func TestRegistryQuery(t *testing.T) {
t.Parallel()
reg := getTestRegistry(t)
q := query.New("runtime:p")
iter, err := reg.Query(q, true, true)
require.NoError(t, err)
require.NotNil(t, iter)
var records []record.Record //nolint:prealloc
for r := range iter.Next {
records = append(records, r)
}
assert.Len(t, records, 6)
q = query.New("runtime:p1/f")
iter, err = reg.Query(q, true, true)
require.NoError(t, err)
require.NotNil(t, iter)
records = nil
for r := range iter.Next {
records = append(records, r)
}
assert.Len(t, records, 2)
}
func TestRegistryRegister(t *testing.T) {
t.Parallel()
r := NewRegistry()
cases := []struct {
inp string
err bool
}{
{"runtime:foo/bar/bar", false},
{"runtime:foo/bar/bar2", false},
{"runtime:foo/bar", false},
{"runtime:foo/bar", true}, // already used
{"runtime:foo/bar/", true}, // cannot register a prefix if there are providers below
{"runtime:foo/baz/", false},
{"runtime:foo/baz2/", false},
{"runtime:foo/baz3", false},
{"runtime:foo/baz/bar", true},
}
for _, c := range cases {
_, err := r.Register(c.inp, nil)
if c.err {
assert.Error(t, err, c.inp)
} else {
assert.NoError(t, err, c.inp)
}
}
}