Skip to content

Commit

Permalink
[service] Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Dec 6, 2024
1 parent 22253c4 commit 05a5d5e
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 33 deletions.
1 change: 0 additions & 1 deletion base/database/storage/fstree/fstree.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"strings"
"time"

"github.com/hectane/go-acl"
"github.com/safing/portmaster/base/database/iterator"
"github.com/safing/portmaster/base/database/query"
"github.com/safing/portmaster/base/database/record"
Expand Down
5 changes: 0 additions & 5 deletions base/updater/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/base/utils"
)

const (
onWindows = runtime.GOOS == "windows"
)

// ResourceRegistry is a registry for managing update resources.
type ResourceRegistry struct {
sync.RWMutex
Expand Down
2 changes: 1 addition & 1 deletion base/updater/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
DevMode: true,
Online: true,
}
err = registry.Initialize(utils.NewDirStructure(tmpDir, 0o0777))
err = registry.Initialize(utils.NewDirStructure(tmpDir, utils.PublicWritePermission))
if err != nil {
panic(err)
}
Expand Down
11 changes: 6 additions & 5 deletions base/utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func EnsureDirectory(path string, perm FSPermission) error {
// directory exists, check permissions
if isWindows {
// Ignore windows permission error. For none admin users it will always fail.
SetDirPermission(path, perm)
_ = SetDirPermission(path, perm)
return nil
} else if f.Mode().Perm() != perm.AsUnixDirExecPermission() {
return SetDirPermission(path, perm)
Expand All @@ -39,10 +39,11 @@ func EnsureDirectory(path string, perm FSPermission) error {
if err != nil {
return fmt.Errorf("could not create dir %s: %w", path, err)
}
// Set windows permissions. Linux permission where already set with creation.
if isWindows {
// Ignore windows permission error. For none admin users it will always fail.
SetDirPermission(path, perm)
// Set permissions.
err = SetDirPermission(path, perm)
// Ignore windows permission error. For none admin users it will always fail.
if !isWindows {
return err
}
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions base/utils/permissions_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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"))
}
6 changes: 3 additions & 3 deletions base/utils/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
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 {
switch perm {
case AdminOnlyPermission:
Expand All @@ -30,13 +30,13 @@ func (perm FSPermission) AsUnixDirExecPermission() fs.FileMode {
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 {
switch perm {
case AdminOnlyPermission:
return 0o600
case PublicReadPermission:
return 0o655
return 0o644
case PublicWritePermission:
return 0o666
}
Expand Down
26 changes: 13 additions & 13 deletions base/utils/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func ExampleDirStructure() {
// output:
// / [755]
// /repo [777]
// /repo/b [707]
// /repo/b/c [750]
// /repo/b/d [707]
// /repo/b/d/e [707]
// /repo/b/d/f [707]
// /repo/b/d/f/g [707]
// /repo/b/d/f/g/h [707]
// /repo/b [755]
// /repo/b/c [777]
// /repo/b/d [755]
// /repo/b/d/e [755]
// /repo/b/d/f [755]
// /repo/b/d/f/g [755]
// /repo/b/d/f/g/h [755]
// /secret [700]

basePath, err := os.MkdirTemp("", "")
Expand All @@ -28,12 +28,12 @@ func ExampleDirStructure() {
return
}

ds := NewDirStructure(basePath, 0o0755)
secret := ds.ChildDir("secret", 0o0700)
repo := ds.ChildDir("repo", 0o0777)
_ = repo.ChildDir("a", 0o0700)
b := repo.ChildDir("b", 0o0707)
c := b.ChildDir("c", 0o0750)
ds := NewDirStructure(basePath, PublicReadPermission)
secret := ds.ChildDir("secret", AdminOnlyPermission)
repo := ds.ChildDir("repo", PublicWritePermission)
_ = repo.ChildDir("a", AdminOnlyPermission)
b := repo.ChildDir("b", PublicReadPermission)
c := b.ChildDir("c", PublicWritePermission)

err = ds.Ensure()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmds/notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ func configureRegistry(mustLoadIndex bool) error {
// Remove left over quotes.
dataDir = strings.Trim(dataDir, `\"`)
// Initialize data root.
err := dataroot.Initialize(dataDir, 0o0755)
err := dataroot.Initialize(dataDir, utils.PublicReadPermission)
if err != nil {
return fmt.Errorf("failed to initialize data root: %w", err)
}
dataRoot = dataroot.Root()

// Initialize registry.
err = registry.Initialize(dataRoot.ChildDir("updates", 0o0755))
err = registry.Initialize(dataRoot.ChildDir("updates", utils.PublicReadPermission))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmds/updatemgr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var rootCmd = &cobra.Command{
}

registry = &updater.ResourceRegistry{}
err = registry.Initialize(utils.NewDirStructure(absDistPath, 0o0755))
err = registry.Initialize(utils.NewDirStructure(absDistPath, utils.PublicReadPermission))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmds/updatemgr/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"

"github.com/safing/portmaster/base/updater"
"github.com/safing/portmaster/base/utils"
)

var (
Expand Down Expand Up @@ -63,7 +64,7 @@ func release(cmd *cobra.Command, args []string) error {
fmt.Println("aborted...")
return nil
}
symlinksDir := registry.StorageDir().ChildDir("latest", 0o755)
symlinksDir := registry.StorageDir().ChildDir("latest", utils.PublicReadPermission)
err = registry.CreateSymlinks(symlinksDir)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion service/updates/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func upgradeBinary(fileToUpgrade string, file *updater.File) error {

// check permissions
if onWindows {
utils.SetExecPermission(fileToUpgrade, utils.PublicReadPermission)
_ = utils.SetExecPermission(fileToUpgrade, utils.PublicReadPermission)
} else {
perm := utils.PublicReadPermission
info, err := os.Stat(fileToUpgrade)
Expand Down

0 comments on commit 05a5d5e

Please sign in to comment.