fix(windows): protect lifecycle and backup state

This commit is contained in:
2026-08-18 14:05:52 +02:00
parent b6396e66fa
commit ada3f9fc7f
7 changed files with 122 additions and 21 deletions
+36 -4
View File
@@ -7,15 +7,37 @@ import (
"testing"
"github.com/aritmolab/thothii/tools/tht/internal/config"
"github.com/aritmolab/thothii/tools/tht/internal/safeio"
)
func TestLifecycleLockIsExclusivePerInstallationAndReusableAfterRelease(t *testing.T) {
installation := config.Installation{ProjectDirectory: t.TempDir(), Path: filepath.Join(t.TempDir(), "thothii-installation.yaml")}
root := lifecycleTestRoot(t)
installation := config.Installation{ProjectDirectory: root, Path: filepath.Join(root, "thothii-installation.yaml")}
if err := os.MkdirAll(installation.ControlDirectory(), 0o755); err != nil {
t.Fatal(err)
}
first, err := Acquire(installation)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = first.Release() })
for name, path := range map[string]string{
"lifecycle parent": filepath.Dir(installation.ControlDirectory()),
"lifecycle directory": installation.ControlDirectory(),
"owner file": first.Path(),
} {
t.Run(name, func(t *testing.T) {
var err error
if name == "owner file" {
err = safeio.ValidatePrivateRegular(path)
} else {
err = safeio.ValidatePrivateDirectory(path)
}
if err != nil {
t.Fatalf("%s protection = %v", name, err)
}
})
}
if _, err := Acquire(installation); !errors.Is(err, ErrLocked) {
t.Fatalf("second Acquire() error = %v, want ErrLocked", err)
@@ -33,7 +55,8 @@ func TestLifecycleLockIsExclusivePerInstallationAndReusableAfterRelease(t *testi
}
func TestLifecycleLockReleaseDoesNotRemoveAnotherOwnersFile(t *testing.T) {
installation := config.Installation{ProjectDirectory: t.TempDir(), Path: filepath.Join(t.TempDir(), "thothii-installation.yaml")}
root := lifecycleTestRoot(t)
installation := config.Installation{ProjectDirectory: root, Path: filepath.Join(root, "thothii-installation.yaml")}
lock, err := Acquire(installation)
if err != nil {
t.Fatal(err)
@@ -50,7 +73,7 @@ func TestLifecycleLockReleaseDoesNotRemoveAnotherOwnersFile(t *testing.T) {
}
func TestTransactionCapabilityIsInstallationBoundAndExpiresOnRelease(t *testing.T) {
root := t.TempDir()
root := lifecycleTestRoot(t)
installation := config.Installation{ProjectDirectory: root, Path: filepath.Join(root, "thothii-installation.yaml")}
transaction, err := AcquireTransaction(installation)
if err != nil {
@@ -60,7 +83,7 @@ func TestTransactionCapabilityIsInstallationBoundAndExpiresOnRelease(t *testing.
t.Fatalf("Verify() active capability error = %v", err)
}
otherRoot := t.TempDir()
otherRoot := lifecycleTestRoot(t)
other := config.Installation{ProjectDirectory: otherRoot, Path: filepath.Join(otherRoot, "thothii-installation.yaml")}
if err := transaction.Verify(other); !errors.Is(err, ErrTransactionInstallation) {
t.Fatalf("Verify() for another installation error = %v, want ErrTransactionInstallation", err)
@@ -72,3 +95,12 @@ func TestTransactionCapabilityIsInstallationBoundAndExpiresOnRelease(t *testing.
t.Fatalf("Verify() after Release() error = %v, want ErrTransactionInactive", err)
}
}
func lifecycleTestRoot(t *testing.T) string {
t.Helper()
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
return root
}