[service] Fix unit tests

This commit is contained in:
Vladimir Stoilov 2024-12-06 13:28:24 +02:00
parent 22253c4e9e
commit 05a5d5e350
No known key found for this signature in database
GPG key ID: 2F190B67A43A81AF
11 changed files with 32 additions and 33 deletions

View file

@ -15,7 +15,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/hectane/go-acl"
"github.com/safing/portmaster/base/database/iterator" "github.com/safing/portmaster/base/database/iterator"
"github.com/safing/portmaster/base/database/query" "github.com/safing/portmaster/base/database/query"
"github.com/safing/portmaster/base/database/record" "github.com/safing/portmaster/base/database/record"

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"sync" "sync"
@ -13,10 +12,6 @@ import (
"github.com/safing/portmaster/base/utils" "github.com/safing/portmaster/base/utils"
) )
const (
onWindows = runtime.GOOS == "windows"
)
// ResourceRegistry is a registry for managing update resources. // ResourceRegistry is a registry for managing update resources.
type ResourceRegistry struct { type ResourceRegistry struct {
sync.RWMutex sync.RWMutex

View file

@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
DevMode: true, DevMode: true,
Online: true, Online: true,
} }
err = registry.Initialize(utils.NewDirStructure(tmpDir, 0o0777)) err = registry.Initialize(utils.NewDirStructure(tmpDir, utils.PublicWritePermission))
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -21,7 +21,7 @@ func EnsureDirectory(path string, perm FSPermission) error {
// directory exists, check permissions // directory exists, check permissions
if isWindows { if isWindows {
// Ignore windows permission error. For none admin users it will always fail. // Ignore windows permission error. For none admin users it will always fail.
SetDirPermission(path, perm) _ = SetDirPermission(path, perm)
return nil return nil
} else if f.Mode().Perm() != perm.AsUnixDirExecPermission() { } else if f.Mode().Perm() != perm.AsUnixDirExecPermission() {
return SetDirPermission(path, perm) return SetDirPermission(path, perm)
@ -39,10 +39,11 @@ func EnsureDirectory(path string, perm FSPermission) error {
if err != nil { if err != nil {
return fmt.Errorf("could not create dir %s: %w", path, err) return fmt.Errorf("could not create dir %s: %w", path, err)
} }
// Set windows permissions. Linux permission where already set with creation. // Set permissions.
if isWindows { err = SetDirPermission(path, perm)
// Ignore windows permission error. For none admin users it will always fail. // Ignore windows permission error. For none admin users it will always fail.
SetDirPermission(path, perm) if !isWindows {
return err
} }
return nil return nil
} }

View file

@ -32,4 +32,7 @@ func setWindowsFilePermissions(path string, perm FSPermission) {
acl.Apply(path, true, false, acl.GrantName(windows.GENERIC_ALL|windows.STANDARD_RIGHTS_ALL, "Administrators")) acl.Apply(path, true, false, acl.GrantName(windows.GENERIC_ALL|windows.STANDARD_RIGHTS_ALL, "Administrators"))
acl.Apply(path, false, false, acl.GrantName(windows.GENERIC_ALL|windows.STANDARD_RIGHTS_ALL, "Users")) acl.Apply(path, false, false, acl.GrantName(windows.GENERIC_ALL|windows.STANDARD_RIGHTS_ALL, "Users"))
} }
// For completeness
acl.Apply(path, false, false, acl.GrantName(windows.GENERIC_ALL|windows.STANDARD_RIGHTS_ALL, "SYSTEM"))
} }

View file

@ -16,7 +16,7 @@ const (
PublicWritePermission PublicWritePermission
) )
// AsUnixDirPermission return the corresponding unix permission for a directory or executable. // AsUnixDirExecPermission return the corresponding unix permission for a directory or executable.
func (perm FSPermission) AsUnixDirExecPermission() fs.FileMode { func (perm FSPermission) AsUnixDirExecPermission() fs.FileMode {
switch perm { switch perm {
case AdminOnlyPermission: case AdminOnlyPermission:
@ -30,13 +30,13 @@ func (perm FSPermission) AsUnixDirExecPermission() fs.FileMode {
return 0 return 0
} }
// AsUnixDirPermission return the corresponding unix permission for a regular file. // AsUnixFilePermission return the corresponding unix permission for a regular file.
func (perm FSPermission) AsUnixFilePermission() fs.FileMode { func (perm FSPermission) AsUnixFilePermission() fs.FileMode {
switch perm { switch perm {
case AdminOnlyPermission: case AdminOnlyPermission:
return 0o600 return 0o600
case PublicReadPermission: case PublicReadPermission:
return 0o655 return 0o644
case PublicWritePermission: case PublicWritePermission:
return 0o666 return 0o666
} }

View file

@ -13,13 +13,13 @@ func ExampleDirStructure() {
// output: // output:
// / [755] // / [755]
// /repo [777] // /repo [777]
// /repo/b [707] // /repo/b [755]
// /repo/b/c [750] // /repo/b/c [777]
// /repo/b/d [707] // /repo/b/d [755]
// /repo/b/d/e [707] // /repo/b/d/e [755]
// /repo/b/d/f [707] // /repo/b/d/f [755]
// /repo/b/d/f/g [707] // /repo/b/d/f/g [755]
// /repo/b/d/f/g/h [707] // /repo/b/d/f/g/h [755]
// /secret [700] // /secret [700]
basePath, err := os.MkdirTemp("", "") basePath, err := os.MkdirTemp("", "")
@ -28,12 +28,12 @@ func ExampleDirStructure() {
return return
} }
ds := NewDirStructure(basePath, 0o0755) ds := NewDirStructure(basePath, PublicReadPermission)
secret := ds.ChildDir("secret", 0o0700) secret := ds.ChildDir("secret", AdminOnlyPermission)
repo := ds.ChildDir("repo", 0o0777) repo := ds.ChildDir("repo", PublicWritePermission)
_ = repo.ChildDir("a", 0o0700) _ = repo.ChildDir("a", AdminOnlyPermission)
b := repo.ChildDir("b", 0o0707) b := repo.ChildDir("b", PublicReadPermission)
c := b.ChildDir("c", 0o0750) c := b.ChildDir("c", PublicWritePermission)
err = ds.Ensure() err = ds.Ensure()
if err != nil { if err != nil {

View file

@ -225,14 +225,14 @@ func configureRegistry(mustLoadIndex bool) error {
// Remove left over quotes. // Remove left over quotes.
dataDir = strings.Trim(dataDir, `\"`) dataDir = strings.Trim(dataDir, `\"`)
// Initialize data root. // Initialize data root.
err := dataroot.Initialize(dataDir, 0o0755) err := dataroot.Initialize(dataDir, utils.PublicReadPermission)
if err != nil { if err != nil {
return fmt.Errorf("failed to initialize data root: %w", err) return fmt.Errorf("failed to initialize data root: %w", err)
} }
dataRoot = dataroot.Root() dataRoot = dataroot.Root()
// Initialize registry. // Initialize registry.
err = registry.Initialize(dataRoot.ChildDir("updates", 0o0755)) err = registry.Initialize(dataRoot.ChildDir("updates", utils.PublicReadPermission))
if err != nil { if err != nil {
return err return err
} }

View file

@ -31,7 +31,7 @@ var rootCmd = &cobra.Command{
} }
registry = &updater.ResourceRegistry{} registry = &updater.ResourceRegistry{}
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0o0755)) err = registry.Initialize(utils.NewDirStructure(absDistPath, utils.PublicReadPermission))
if err != nil { if err != nil {
return err return err
} }

View file

@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/safing/portmaster/base/updater" "github.com/safing/portmaster/base/updater"
"github.com/safing/portmaster/base/utils"
) )
var ( var (
@ -63,7 +64,7 @@ func release(cmd *cobra.Command, args []string) error {
fmt.Println("aborted...") fmt.Println("aborted...")
return nil return nil
} }
symlinksDir := registry.StorageDir().ChildDir("latest", 0o755) symlinksDir := registry.StorageDir().ChildDir("latest", utils.PublicReadPermission)
err = registry.CreateSymlinks(symlinksDir) err = registry.CreateSymlinks(symlinksDir)
if err != nil { if err != nil {
return err return err

View file

@ -351,7 +351,7 @@ func upgradeBinary(fileToUpgrade string, file *updater.File) error {
// check permissions // check permissions
if onWindows { if onWindows {
utils.SetExecPermission(fileToUpgrade, utils.PublicReadPermission) _ = utils.SetExecPermission(fileToUpgrade, utils.PublicReadPermission)
} else { } else {
perm := utils.PublicReadPermission perm := utils.PublicReadPermission
info, err := os.Stat(fileToUpgrade) info, err := os.Stat(fileToUpgrade)