v2 tests fix (#1956)

* v2 tests fix
* test: added tests for sqlite
This commit is contained in:
Alexandr Stelnykovych
2025-08-07 02:04:38 +03:00
committed by GitHub
parent 5609594b2a
commit 58f4058633
15 changed files with 347 additions and 447 deletions

View File

@@ -18,6 +18,7 @@ import (
_ "github.com/safing/portmaster/base/database/storage/bbolt" _ "github.com/safing/portmaster/base/database/storage/bbolt"
_ "github.com/safing/portmaster/base/database/storage/fstree" _ "github.com/safing/portmaster/base/database/storage/fstree"
_ "github.com/safing/portmaster/base/database/storage/hashmap" _ "github.com/safing/portmaster/base/database/storage/hashmap"
_ "github.com/safing/portmaster/base/database/storage/sqlite"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@@ -166,7 +167,7 @@ func testDatabase(t *testing.T, storageType string, shadowDelete bool) { //nolin
} }
// run maintenance // run maintenance
err = dbController.MaintainRecordStates(context.TODO(), now.Add(-60*time.Second)) err = dbController.MaintainRecordStates(t.Context(), now.Add(-60*time.Second))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -181,12 +182,22 @@ func testDatabase(t *testing.T, storageType string, shadowDelete bool) { //nolin
if !errors.Is(err, storage.ErrNotFound) { if !errors.Is(err, storage.ErrNotFound) {
t.Errorf("A should be deleted and purged, err=%s", err) t.Errorf("A should be deleted and purged, err=%s", err)
} }
B1, err := dbController.storage.Get("B")
if err != nil { if !shadowDelete {
t.Fatalf("should exist: %s, original meta: %+v", err, B.Meta()) // previous call MaintainRecordStates() must purge all expired and deleted records
} _, err := dbController.storage.Get("B")
if B1.Meta().Deleted == 0 { if !errors.Is(err, storage.ErrNotFound) {
t.Errorf("B should be deleted") t.Errorf("B should be deleted and purged, err=%s", err)
}
} else {
B1, err := dbController.storage.Get("B")
if err != nil {
t.Fatalf("should exist: %s, original meta: %+v", err, B.Meta())
}
if B1.Meta().Deleted == 0 {
t.Errorf("B should be deleted")
}
} }
// delete last entry // delete last entry
@@ -197,7 +208,9 @@ func testDatabase(t *testing.T, storageType string, shadowDelete bool) { //nolin
} }
// run maintenance // run maintenance
err = dbController.MaintainRecordStates(context.TODO(), now) // Since previous call MaintainRecordStates() saved actual timestamp for deleted records,
// use 'now + 1sec' just to guarantee that time is bigger)
err = dbController.MaintainRecordStates(t.Context(), time.Now().Add(time.Second).UTC())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -251,6 +264,7 @@ func TestDatabaseSystem(t *testing.T) { //nolint:tparallel
}() }()
for _, shadowDelete := range []bool{false, true} { for _, shadowDelete := range []bool{false, true} {
testDatabase(t, "sqlite", shadowDelete)
testDatabase(t, "bbolt", shadowDelete) testDatabase(t, "bbolt", shadowDelete)
testDatabase(t, "hashmap", shadowDelete) testDatabase(t, "hashmap", shadowDelete)
testDatabase(t, "fstree", shadowDelete) testDatabase(t, "fstree", shadowDelete)

View File

@@ -28,12 +28,12 @@ func ExampleDirStructure() {
return return
} }
ds := NewDirStructure(basePath, PublicReadPermission) ds := NewDirStructure(basePath, PublicReadExecPermission)
secret := ds.ChildDir("secret", AdminOnlyPermission) secret := ds.ChildDir("secret", AdminOnlyExecPermission)
repo := ds.ChildDir("repo", PublicWritePermission) repo := ds.ChildDir("repo", PublicWriteExecPermission)
_ = repo.ChildDir("a", AdminOnlyPermission) _ = repo.ChildDir("a", AdminOnlyExecPermission)
b := repo.ChildDir("b", PublicReadPermission) b := repo.ChildDir("b", PublicReadExecPermission)
c := b.ChildDir("c", PublicWritePermission) c := b.ChildDir("c", PublicWriteExecPermission)
err = ds.Ensure() err = ds.Ensure()
if err != nil { if err != nil {

View File

@@ -3,67 +3,67 @@ package geoip
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/safing/portmaster/base/api" "github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config" "github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule" "github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications" "github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/configure"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates" "github.com/safing/portmaster/service/updates"
) )
type testInstance struct { type testInstance struct {
db *dbmodule.DBModule db *dbmodule.DBModule
api *api.API api *api.API
config *config.Config config *config.Config
updates *updates.Updater intelUpdates *updates.Updater
} }
var _ instance = &testInstance{} var _ instance = &testInstance{}
func (stub *testInstance) IntelUpdates() *updates.Updater { func (stub *testInstance) IntelUpdates() *updates.Updater { return stub.intelUpdates }
return stub.updates func (stub *testInstance) Config() *config.Config { return stub.config }
} func (stub *testInstance) Notifications() *notifications.Notifications { return nil }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) BinaryUpdates() *updates.Updater { return nil }
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
func (stub *testInstance) API() *api.API { var _dataDir string
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080") var err error
ds, err := config.InitializeUnitTestDataroot("test-geoip")
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err) return fmt.Errorf("failed to create temporary data directory: %w", err)
} }
defer func() { _ = os.RemoveAll(ds) }() defer func() { _ = os.RemoveAll(_dataDir) }()
installDir, err := os.MkdirTemp("", "geoip_installdir")
if err != nil { // Initialize the Intel update configuration
return fmt.Errorf("failed to create tmp install dir: %w", err) intelUpdateConfig := updates.Config{
} Name: configure.DefaultIntelIndexName,
defer func() { _ = os.RemoveAll(installDir) }() Directory: filepath.Join(_dataDir, "test_intel"),
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0") DownloadDirectory: filepath.Join(_dataDir, "test_download_intel"),
if err != nil { PurgeDirectory: filepath.Join(_dataDir, "test_upgrade_obsolete_intel"),
return fmt.Errorf("failed to generate mock installation: %w", err) IndexURLs: configure.DefaultIntelIndexURLs,
IndexFile: "index.json",
AutoCheck: true,
AutoDownload: true,
AutoApply: true,
} }
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
// Initialize the instance with the necessary components
stub := &testInstance{} stub := &testInstance{}
stub.db, err = dbmodule.New(stub) stub.db, err = dbmodule.New(stub)
if err != nil { if err != nil {
@@ -77,10 +77,7 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to create api: %w", err) return fmt.Errorf("failed to create api: %w", err)
} }
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{ stub.intelUpdates, err = updates.New(stub, "Intel Updater", intelUpdateConfig)
Directory: installDir,
IndexFile: "index.json",
})
if err != nil { if err != nil {
return fmt.Errorf("failed to create updates: %w", err) return fmt.Errorf("failed to create updates: %w", err)
} }
@@ -88,7 +85,6 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize module: %w", err) return fmt.Errorf("failed to initialize module: %w", err)
} }
err = stub.db.Start() err = stub.db.Start()
if err != nil { if err != nil {
return fmt.Errorf("Failed to start database: %w", err) return fmt.Errorf("Failed to start database: %w", err)
@@ -101,7 +97,7 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("Failed to start api: %w", err) return fmt.Errorf("Failed to start api: %w", err)
} }
err = stub.updates.Start() err = stub.intelUpdates.Start()
if err != nil { if err != nil {
return fmt.Errorf("Failed to start updates: %w", err) return fmt.Errorf("Failed to start updates: %w", err)
} }

View File

@@ -21,7 +21,8 @@ func TestLocationLookup(t *testing.T) {
worker.triggerUpdate() worker.triggerUpdate()
select { select {
case <-waiter: case <-waiter:
case <-time.After(15 * time.Second): case <-time.After(50 * time.Second):
t.Error("timeout waiting for geoip database to be initialized (updated)")
} }
ip1 := net.ParseIP("81.2.69.142") ip1 := net.ParseIP("81.2.69.142")

View File

@@ -3,67 +3,67 @@ package netenv
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/safing/portmaster/base/api" "github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config" "github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule" "github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications" "github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/configure"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates" "github.com/safing/portmaster/service/updates"
) )
type testInstance struct { type testInstance struct {
db *dbmodule.DBModule db *dbmodule.DBModule
api *api.API api *api.API
config *config.Config config *config.Config
updates *updates.Updater intelUpdates *updates.Updater
} }
var _ instance = &testInstance{} var _ instance = &testInstance{}
func (stub *testInstance) IntelUpdates() *updates.Updater { func (stub *testInstance) IntelUpdates() *updates.Updater { return stub.intelUpdates }
return stub.updates func (stub *testInstance) API() *api.API { return stub.api }
} func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) Notifications() *notifications.Notifications { return nil }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
func (stub *testInstance) API() *api.API { var _dataDir string
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080") var err error
ds, err := config.InitializeUnitTestDataroot("test-netenv")
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err) return fmt.Errorf("failed to create temporary data directory: %w", err)
} }
defer func() { _ = os.RemoveAll(ds) }() defer func() { _ = os.RemoveAll(_dataDir) }()
installDir, err := os.MkdirTemp("", "netenv_installdir")
if err != nil { // Initialize the Intel update configuration
return fmt.Errorf("failed to create tmp install dir: %w", err) intelUpdateConfig := updates.Config{
} Name: configure.DefaultIntelIndexName,
defer func() { _ = os.RemoveAll(installDir) }() Directory: filepath.Join(_dataDir, "test_intel"),
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0") DownloadDirectory: filepath.Join(_dataDir, "test_download_intel"),
if err != nil { PurgeDirectory: filepath.Join(_dataDir, "test_upgrade_obsolete_intel"),
return fmt.Errorf("failed to generate mock installation: %w", err) IndexURLs: configure.DefaultIntelIndexURLs,
IndexFile: "index.json",
AutoCheck: true,
AutoDownload: true,
AutoApply: true,
} }
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080")
// Initialize the instance with the necessary components
stub := &testInstance{} stub := &testInstance{}
stub.db, err = dbmodule.New(stub) stub.db, err = dbmodule.New(stub)
if err != nil { if err != nil {
@@ -77,10 +77,7 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to create api: %w", err) return fmt.Errorf("failed to create api: %w", err)
} }
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{ stub.intelUpdates, err = updates.New(stub, "Intel Updater", intelUpdateConfig)
Directory: installDir,
IndexFile: "index.json",
})
if err != nil { if err != nil {
return fmt.Errorf("failed to create updates: %w", err) return fmt.Errorf("failed to create updates: %w", err)
} }
@@ -96,7 +93,7 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("Failed to start api: %w", err) return fmt.Errorf("Failed to start api: %w", err)
} }
err = stub.updates.Start() err = stub.intelUpdates.Start()
if err != nil { if err != nil {
return fmt.Errorf("Failed to start updates: %w", err) return fmt.Errorf("Failed to start updates: %w", err)
} }

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"path/filepath"
"runtime" "runtime"
"testing" "testing"
@@ -14,63 +15,62 @@ import (
"github.com/safing/portmaster/base/config" "github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule" "github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications" "github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/configure"
"github.com/safing/portmaster/service/intel" "github.com/safing/portmaster/service/intel"
"github.com/safing/portmaster/service/intel/geoip" "github.com/safing/portmaster/service/intel/geoip"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates" "github.com/safing/portmaster/service/updates"
) )
type testInstance struct { type testInstance struct {
db *dbmodule.DBModule db *dbmodule.DBModule
api *api.API api *api.API
config *config.Config config *config.Config
updates *updates.Updater intelUpdates *updates.Updater
geoip *geoip.GeoIP geoip *geoip.GeoIP
} }
func (stub *testInstance) IntelUpdates() *updates.Updater { func (stub *testInstance) IntelUpdates() *updates.Updater { return stub.intelUpdates }
return stub.updates func (stub *testInstance) API() *api.API { return stub.api }
} func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) Notifications() *notifications.Notifications { return nil }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) BinaryUpdates() *updates.Updater { return nil }
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
func (stub *testInstance) API() *api.API { var _dataDir string
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
var err error
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to create temporary data directory: %w", err)
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the Intel update configuration
intelUpdateConfig := updates.Config{
Name: configure.DefaultIntelIndexName,
Directory: filepath.Join(_dataDir, "test_intel"),
DownloadDirectory: filepath.Join(_dataDir, "test_download_intel"),
PurgeDirectory: filepath.Join(_dataDir, "test_upgrade_obsolete_intel"),
IndexURLs: configure.DefaultIntelIndexURLs,
IndexFile: "index.json",
AutoCheck: true,
AutoDownload: true,
AutoApply: true,
}
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080") api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-endpoints")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "endpoints_installdir")
if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
}
// Initialize the instance with the necessary components
stub := &testInstance{} stub := &testInstance{}
stub.db, err = dbmodule.New(stub) stub.db, err = dbmodule.New(stub)
if err != nil { if err != nil {
@@ -84,10 +84,7 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to create api: %w", err) return fmt.Errorf("failed to create api: %w", err)
} }
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{ stub.intelUpdates, err = updates.New(stub, "Intel Updater", intelUpdateConfig)
Directory: installDir,
IndexFile: "index.json",
})
if err != nil { if err != nil {
return fmt.Errorf("failed to create updates: %w", err) return fmt.Errorf("failed to create updates: %w", err)
} }
@@ -108,7 +105,7 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("Failed to start api: %w", err) return fmt.Errorf("Failed to start api: %w", err)
} }
err = stub.updates.Start() err = stub.intelUpdates.Start()
if err != nil { if err != nil {
return fmt.Errorf("Failed to start updates: %w", err) return fmt.Errorf("Failed to start updates: %w", err)
} }

View File

@@ -8,76 +8,50 @@ import (
"github.com/safing/portmaster/base/api" "github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config" "github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule" "github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/core/base" "github.com/safing/portmaster/service/core/base"
"github.com/safing/portmaster/service/mgr" "github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/netenv" "github.com/safing/portmaster/service/netenv"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates" "github.com/safing/portmaster/service/updates"
) )
var domainFeed = make(chan string) var domainFeed = make(chan string)
type testInstance struct { type testInstance struct {
db *dbmodule.DBModule db *dbmodule.DBModule
base *base.Base base *base.Base
api *api.API config *config.Config
config *config.Config netenv *netenv.NetEnv
updates *updates.Updater
netenv *netenv.NetEnv
} }
func (stub *testInstance) IntelUpdates() *updates.Updater {
return stub.updates
}
func (stub *testInstance) API() *api.API {
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) NetEnv() *netenv.NetEnv {
return stub.netenv
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) GetEventSPNConnected() *mgr.EventMgr[struct{}] { func (stub *testInstance) GetEventSPNConnected() *mgr.EventMgr[struct{}] {
return mgr.NewEventMgr[struct{}]("spn connect", nil) return mgr.NewEventMgr[struct{}]("spn connect", nil)
} }
func (stub *testInstance) IntelUpdates() *updates.Updater { return nil }
func (stub *testInstance) Config() *config.Config { return stub.config }
func (stub *testInstance) NetEnv() *netenv.NetEnv { return stub.netenv }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
var err error
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
fmt.Printf("failed to create temporary data directory: %s", err)
os.Exit(0)
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080") api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-resolver")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "resolver_installdir")
if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
}
// Initialize the instance with the necessary components
stub := &testInstance{} stub := &testInstance{}
stub.db, err = dbmodule.New(stub) stub.db, err = dbmodule.New(stub)
if err != nil { if err != nil {
@@ -91,26 +65,14 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to create base: %w", err) return fmt.Errorf("failed to create base: %w", err)
} }
stub.api, err = api.New(stub)
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.netenv, err = netenv.New(stub) stub.netenv, err = netenv.New(stub)
if err != nil { if err != nil {
return fmt.Errorf("failed to create netenv: %w", err) return fmt.Errorf("failed to create netenv: %w", err)
} }
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{
Directory: installDir,
IndexFile: "index.json",
})
if err != nil {
return fmt.Errorf("failed to create updates: %w", err)
}
module, err := New(stub) module, err := New(stub)
if err != nil { if err != nil {
return fmt.Errorf("failed to create module: %w", err) return fmt.Errorf("failed to create module: %w", err)
} }
err = stub.db.Start() err = stub.db.Start()
if err != nil { if err != nil {
return fmt.Errorf("Failed to start database: %w", err) return fmt.Errorf("Failed to start database: %w", err)
@@ -123,14 +85,6 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("Failed to start base: %w", err) return fmt.Errorf("Failed to start base: %w", err)
} }
err = stub.api.Start()
if err != nil {
return fmt.Errorf("Failed to start api: %w", err)
}
err = stub.updates.Start()
if err != nil {
return fmt.Errorf("Failed to start updates: %w", err)
}
err = stub.netenv.Start() err = stub.netenv.Start()
if err != nil { if err != nil {
return fmt.Errorf("Failed to start netenv: %w", err) return fmt.Errorf("Failed to start netenv: %w", err)

View File

@@ -10,22 +10,17 @@ import (
"github.com/safing/portmaster/base/notifications" "github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/mgr" "github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/ui"
) )
type testInstance struct{} type testInstance struct{}
func (i *testInstance) Restart() {} func (i *testInstance) Restart() {}
func (i *testInstance) Shutdown() {} func (i *testInstance) Shutdown() {}
func (i *testInstance) Notifications() *notifications.Notifications { return nil }
func (i *testInstance) Notifications() *notifications.Notifications { func (i *testInstance) Ready() bool { return true }
return nil func (i *testInstance) SetCmdLineOperation(f func() error) {}
} func (i *testInstance) UI() *ui.UI { return nil }
func (i *testInstance) Ready() bool {
return true
}
func (i *testInstance) SetCmdLineOperation(f func() error) {}
func TestPerformUpdate(t *testing.T) { func TestPerformUpdate(t *testing.T) {
t.Parallel() t.Parallel()
@@ -39,11 +34,13 @@ func TestPerformUpdate(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer func() { _ = os.RemoveAll(installedDir) }() defer func() { _ = os.RemoveAll(installedDir) }()
updateDir, err := os.MkdirTemp("", "updates_new_") updateDir, err := os.MkdirTemp("", "updates_new_")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer func() { _ = os.RemoveAll(updateDir) }() defer func() { _ = os.RemoveAll(updateDir) }()
purgeDir, err := os.MkdirTemp("", "updates_purge_") purgeDir, err := os.MkdirTemp("", "updates_purge_")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -109,7 +106,7 @@ func TestPerformUpdate(t *testing.T) {
// GenerateMockFolder generates mock index folder for testing. // GenerateMockFolder generates mock index folder for testing.
func GenerateMockFolder(dir, name, version string, published time.Time) error { func GenerateMockFolder(dir, name, version string, published time.Time) error {
// Make sure dir exists // Make sure dir exists
_ = os.MkdirAll(dir, defaultDirMode) _ = os.MkdirAll(dir, 0o750)
// Create empty files // Create empty files
file, err := os.Create(filepath.Join(dir, "portmaster")) file, err := os.Create(filepath.Join(dir, "portmaster"))
@@ -147,7 +144,7 @@ func GenerateMockFolder(dir, name, version string, published time.Time) error {
fmt.Fprintf(os.Stderr, "failed to marshal index: %s\n", err) fmt.Fprintf(os.Stderr, "failed to marshal index: %s\n", err)
} }
err = os.WriteFile(filepath.Join(dir, "index.json"), indexJSON, defaultFileMode) err = os.WriteFile(filepath.Join(dir, "index.json"), indexJSON, 0o600)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -6,6 +6,8 @@ import (
"testing" "testing"
"github.com/safing/portmaster/base/config" "github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database"
_ "github.com/safing/portmaster/base/database/storage/hashmap"
"github.com/safing/portmaster/service/mgr" "github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/spn/conf" "github.com/safing/portmaster/spn/conf"
) )
@@ -14,44 +16,75 @@ type testInstance struct {
config *config.Config config *config.Config
} }
func (stub *testInstance) Config() *config.Config { func (stub *testInstance) Config() *config.Config { return stub.config }
return stub.config func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup { return nil }
} func (stub *testInstance) Stopping() bool { return false }
func (stub *testInstance) IsShuttingDown() bool { return false }
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
return nil
}
func (stub *testInstance) Stopping() bool {
return false
}
func (stub *testInstance) SetCmdLineOperation(f func() error) {} func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
instance := &testInstance{} exitCode := 1
defer func() {
if exitCode != 0 {
os.Exit(exitCode)
}
}()
var err error var err error
// Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
fmt.Printf("failed to create temporary data directory: %s", err)
return // Exit with error
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the database module
err = database.Initialize(_dataDir)
if err != nil {
fmt.Printf("failed to initialize database module: %s", err)
return // Exit with error
}
_, err = database.Register(&database.Database{
Name: "core",
Description: "Holds core data, such as settings and profiles",
StorageType: "hashmap",
})
if err != nil {
fmt.Printf("failed to register core database: %s", err)
return // Exit with error
}
// Initialize the instance
instance := &testInstance{}
instance.config, err = config.New(instance) instance.config, err = config.New(instance)
if err != nil { if err != nil {
fmt.Printf("failed to create config module: %s", err) fmt.Printf("failed to create config module: %s", err)
os.Exit(0) return // Exit with error
} }
module, err = New(instance) module, err = New(instance)
if err != nil { if err != nil {
fmt.Printf("failed to create access module: %s", err) fmt.Printf("failed to create access module: %s", err)
os.Exit(0) return // Exit with error
} }
err = instance.config.Start() err = instance.config.Start()
if err != nil { if err != nil {
fmt.Printf("failed to start config module: %s", err) fmt.Printf("failed to start config module: %s", err)
os.Exit(0) return // Exit with error
} }
err = module.Start() err = module.Start()
if err != nil { if err != nil {
fmt.Printf("failed to start access module: %s", err) fmt.Printf("failed to start access module: %s", err)
os.Exit(0) return // Exit with error
} }
conf.EnableClient(true) conf.EnableClient(true)
m.Run() m.Run()
exitCode = 0 // success
} }

View File

@@ -22,31 +22,24 @@ type testInstance struct {
base *base.Base base *base.Base
} }
func (stub *testInstance) Config() *config.Config { func (stub *testInstance) Config() *config.Config { return stub.config }
return stub.config func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup { return nil }
} func (stub *testInstance) Stopping() bool { return false }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
return nil
}
func (stub *testInstance) Stopping() bool {
return false
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) SetCmdLineOperation(f func() error) {} func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080") api.SetDefaultAPIListenAddress("0.0.0.0:8080")
// Initialize dataroot var err error
ds, err := config.InitializeUnitTestDataroot("test-cabin") // Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err) return fmt.Errorf("failed to initialize dataroot: %w", err)
} }
defer func() { _ = os.RemoveAll(ds) }() defer func() { _ = os.RemoveAll(_dataDir) }()
// Init // Init
instance := &testInstance{} instance := &testInstance{}

View File

@@ -41,15 +41,24 @@ func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
func (stub *testInstance) Stopping() bool { func (stub *testInstance) Stopping() bool {
return false return false
} }
func (stub *testInstance) SetCmdLineOperation(f func() error) {} func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) DataDir() string {
return _dataDir
}
var _dataDir string
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
conf.EnablePublicHub(true) // Make hub config available. conf.EnablePublicHub(true) // Make hub config available.
ds, err := config.InitializeUnitTestDataroot("test-crew") var err error
// Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err) return fmt.Errorf("failed to initialize dataroot: %w", err)
} }
defer func() { _ = os.RemoveAll(ds) }() defer func() { _ = os.RemoveAll(_dataDir) }()
instance := &testInstance{} instance := &testInstance{}
// Init // Init

View File

@@ -29,31 +29,25 @@ type testInstance struct {
cabin *cabin.Cabin cabin *cabin.Cabin
} }
func (stub *testInstance) Config() *config.Config { func (stub *testInstance) Config() *config.Config { return stub.config }
return stub.config func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup { return nil }
}
func (stub *testInstance) Metrics() *metrics.Metrics {
return stub.metrics
}
func (stub *testInstance) SPNGroup() *mgr.ExtendedGroup {
return nil
}
func (stub *testInstance) Stopping() bool {
return false
}
func (stub *testInstance) SetCmdLineOperation(f func() error) {} func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) IsShuttingDown() bool { return false }
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
_ = log.Start("info", true, "") _ = log.Start("info", true, "")
ds, err := config.InitializeUnitTestDataroot("test-docks") var err error
// Create a temporary directory for the data
_dataDir, err = os.MkdirTemp("", "")
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err) return fmt.Errorf("failed to initialize dataroot: %w", err)
} }
defer func() { _ = os.RemoveAll(ds) }() defer func() { _ = os.RemoveAll(_dataDir) }()
instance := &testInstance{} instance := &testInstance{}
runningTests = true runningTests = true

View File

@@ -8,91 +8,37 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config"
"github.com/safing/portmaster/base/database/dbmodule" "github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/core/base" "github.com/safing/portmaster/service/core/base"
"github.com/safing/portmaster/service/updates"
) )
type testInstance struct { type testInstance struct {
db *dbmodule.DBModule db *dbmodule.DBModule
api *api.API base *base.Base
config *config.Config
updates *updates.Updater
base *base.Base
} }
func (stub *testInstance) IntelUpdates() *updates.Updater {
return stub.updates
}
func (stub *testInstance) API() *api.API {
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Base() *base.Base {
return stub.base
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {} func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) DataDir() string { return _dataDir }
var _dataDir string
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
api.SetDefaultAPIListenAddress("0.0.0.0:8080") var err error
ds, err := config.InitializeUnitTestDataroot("test-hub")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "hub_installdir") // Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil { if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err) return fmt.Errorf("failed to create temporary data directory: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
} }
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the instance with the necessary components
stub := &testInstance{} stub := &testInstance{}
// Init // Init
stub.db, err = dbmodule.New(stub) stub.db, err = dbmodule.New(stub)
if err != nil { if err != nil {
return fmt.Errorf("failed to create database: %w", err) return fmt.Errorf("failed to create database: %w", err)
} }
stub.api, err = api.New(stub)
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.config, err = config.New(stub)
if err != nil {
return fmt.Errorf("failed to create config: %w", err)
}
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{
Directory: installDir,
IndexFile: "index.json",
})
if err != nil {
return fmt.Errorf("failed to create updates: %w", err)
}
stub.base, err = base.New(stub) stub.base, err = base.New(stub)
if err != nil { if err != nil {
return fmt.Errorf("failed to base updates: %w", err) return fmt.Errorf("failed to base updates: %w", err)
@@ -103,18 +49,6 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to start database: %w", err) return fmt.Errorf("failed to start database: %w", err)
} }
err = stub.api.Start()
if err != nil {
return fmt.Errorf("failed to start api: %w", err)
}
err = stub.config.Start()
if err != nil {
return fmt.Errorf("failed to start config: %w", err)
}
err = stub.updates.Start()
if err != nil {
return fmt.Errorf("failed to start updates: %w", err)
}
err = stub.base.Start() err = stub.base.Start()
if err != nil { if err != nil {
return fmt.Errorf("failed to start base: %w", err) return fmt.Errorf("failed to start base: %w", err)

View File

@@ -3,6 +3,7 @@ package navigator
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/safing/portmaster/base/api" "github.com/safing/portmaster/base/api"
@@ -10,68 +11,59 @@ import (
"github.com/safing/portmaster/base/database/dbmodule" "github.com/safing/portmaster/base/database/dbmodule"
"github.com/safing/portmaster/base/log" "github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/base/notifications" "github.com/safing/portmaster/base/notifications"
"github.com/safing/portmaster/service/core/base" "github.com/safing/portmaster/service/configure"
"github.com/safing/portmaster/service/intel/geoip" "github.com/safing/portmaster/service/intel/geoip"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates" "github.com/safing/portmaster/service/updates"
) )
type testInstance struct { type testInstance struct {
db *dbmodule.DBModule db *dbmodule.DBModule
api *api.API config *config.Config
config *config.Config intelUpdates *updates.Updater
updates *updates.Updater geoip *geoip.GeoIP
base *base.Base
geoip *geoip.GeoIP
} }
func (stub *testInstance) IntelUpdates() *updates.Updater { func (stub *testInstance) IntelUpdates() *updates.Updater { return stub.intelUpdates }
return stub.updates func (stub *testInstance) Config() *config.Config { return stub.config }
} func (stub *testInstance) Notifications() *notifications.Notifications { return nil }
func (stub *testInstance) Ready() bool { return true }
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func (stub *testInstance) BinaryUpdates() *updates.Updater { return nil }
func (stub *testInstance) UI() *ui.UI { return nil }
func (stub *testInstance) DataDir() string { return _dataDir }
func (stub *testInstance) API() *api.API { var _dataDir string
return stub.api
}
func (stub *testInstance) Config() *config.Config {
return stub.config
}
func (stub *testInstance) Base() *base.Base {
return stub.base
}
func (stub *testInstance) Notifications() *notifications.Notifications {
return nil
}
func (stub *testInstance) Ready() bool {
return true
}
func (stub *testInstance) Restart() {}
func (stub *testInstance) Shutdown() {}
func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
var err error
// Create a temporary directory for testing
_dataDir, err = os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("failed to create temporary data directory: %w", err)
}
defer func() { _ = os.RemoveAll(_dataDir) }()
// Initialize the Intel update configuration
intelUpdateConfig := updates.Config{
Name: configure.DefaultIntelIndexName,
Directory: filepath.Join(_dataDir, "test_intel"),
DownloadDirectory: filepath.Join(_dataDir, "test_download_intel"),
PurgeDirectory: filepath.Join(_dataDir, "test_upgrade_obsolete_intel"),
IndexURLs: configure.DefaultIntelIndexURLs,
IndexFile: "index.json",
AutoCheck: true,
AutoDownload: true,
AutoApply: true,
}
// Set the default API listen address
api.SetDefaultAPIListenAddress("0.0.0.0:8080") api.SetDefaultAPIListenAddress("0.0.0.0:8080")
ds, err := config.InitializeUnitTestDataroot("test-navigator")
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
installDir, err := os.MkdirTemp("", "geoip_installdir")
if err != nil {
return fmt.Errorf("failed to create tmp install dir: %w", err)
}
defer func() { _ = os.RemoveAll(installDir) }()
err = updates.GenerateMockFolder(installDir, "Test Intel", "1.0.0")
if err != nil {
return fmt.Errorf("failed to generate mock installation: %w", err)
}
// Initialize the instance with the necessary components
stub := &testInstance{} stub := &testInstance{}
log.SetLogLevel(log.DebugLevel) log.SetLogLevel(log.DebugLevel)
@@ -80,25 +72,14 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to create db: %w", err) return fmt.Errorf("failed to create db: %w", err)
} }
stub.api, err = api.New(stub)
if err != nil {
return fmt.Errorf("failed to create api: %w", err)
}
stub.config, err = config.New(stub) stub.config, err = config.New(stub)
if err != nil { if err != nil {
return fmt.Errorf("failed to create config: %w", err) return fmt.Errorf("failed to create config: %w", err)
} }
stub.updates, err = updates.New(stub, "Test Intel", updates.Config{ stub.intelUpdates, err = updates.New(stub, "Intel Updater", intelUpdateConfig)
Directory: installDir,
IndexFile: "index.json",
})
if err != nil { if err != nil {
return fmt.Errorf("failed to create updates: %w", err) return fmt.Errorf("failed to create updates: %w", err)
} }
stub.base, err = base.New(stub)
if err != nil {
return fmt.Errorf("failed to create base: %w", err)
}
stub.geoip, err = geoip.New(stub) stub.geoip, err = geoip.New(stub)
if err != nil { if err != nil {
return fmt.Errorf("failed to create geoip: %w", err) return fmt.Errorf("failed to create geoip: %w", err)
@@ -112,22 +93,14 @@ func runTest(m *testing.M) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to start db module: %w", err) return fmt.Errorf("failed to start db module: %w", err)
} }
err = stub.api.Start()
if err != nil {
return fmt.Errorf("failed to start api: %w", err)
}
err = stub.config.Start() err = stub.config.Start()
if err != nil { if err != nil {
return fmt.Errorf("failed to start config: %w", err) return fmt.Errorf("failed to start config: %w", err)
} }
err = stub.updates.Start() err = stub.intelUpdates.Start()
if err != nil { if err != nil {
return fmt.Errorf("failed to start updates: %w", err) return fmt.Errorf("failed to start updates: %w", err)
} }
err = stub.base.Start()
if err != nil {
return fmt.Errorf("failed to start base module: %w", err)
}
err = stub.geoip.Start() err = stub.geoip.Start()
if err != nil { if err != nil {
return fmt.Errorf("failed to start geoip module: %w", err) return fmt.Errorf("failed to start geoip module: %w", err)

View File

@@ -22,6 +22,18 @@ type testInstance struct {
rng *rng.Rng rng *rng.Rng
base *base.Base base *base.Base
cabin *cabin.Cabin cabin *cabin.Cabin
dataDir string
}
func (stub *testInstance) DataDir() string {
if len(stub.dataDir) == 0 {
var err error
stub.dataDir, err = os.MkdirTemp("", "")
if err != nil {
panic(fmt.Sprintf("failed to create temp dir: %v", err))
}
}
return stub.dataDir
} }
func (stub *testInstance) Config() *config.Config { func (stub *testInstance) Config() *config.Config {
@@ -42,11 +54,7 @@ func (stub *testInstance) Stopping() bool {
func (stub *testInstance) SetCmdLineOperation(f func() error) {} func (stub *testInstance) SetCmdLineOperation(f func() error) {}
func runTest(m *testing.M) error { func runTest(m *testing.M) error {
ds, err := config.InitializeUnitTestDataroot("test-terminal") var err error
if err != nil {
return fmt.Errorf("failed to initialize dataroot: %w", err)
}
defer func() { _ = os.RemoveAll(ds) }()
conf.EnablePublicHub(true) // Make hub config available. conf.EnablePublicHub(true) // Make hub config available.