Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

21.8 KB
Raw Blame
// Copyright 2026 The OpenSandbox Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package isolation

import (
	"errors"
	"io/fs"
	"os"
	"path/filepath"
	"testing"
)

func TestNewUpperManager(t *testing.T) {
	dir := t.TempDir()
	root := filepath.Join(dir, "isolation")

	mgr, err := NewUpperManager(root, 8<<30)
	if err != nil {
		t.Fatal(err)
	}
	if mgr.Root() != root {
		t.Errorf("Root() = %q, want %q", mgr.Root(), root)
	}
	if mgr.MaxBytes() != 8<<30 {
		t.Errorf("MaxBytes() = %d, want %d", mgr.MaxBytes(), 8<<30)
	}

	if _, err := os.Stat(root); os.IsNotExist(err) {
		t.Error("root dir not created")
	}
}

func TestNewUpperManager_EmptyRoot(t *testing.T) {
	_, err := NewUpperManager("", 0)
	if err == nil {
		t.Error("expected error for empty root")
	}
}

func TestNewUpperManager_ReclaimsRecordedSessions(t *testing.T) {
	root := filepath.Join(t.TempDir(), "isolation")
	owner, err := NewUpperManager(root, 8<<30)
	if err != nil {
		t.Fatal(err)
	}

	staleID, staleUpper, staleWork, err := owner.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(staleUpper, "residue.bin"), []byte("stale"), 0o644); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(staleWork, "scratch.bin"), []byte("scratch"), 0o644); err != nil {
		t.Fatal(err)
	}

	multiID, multiPairs, err := owner.AllocateN(2)
	if err != nil {
		t.Fatal(err)
	}
	for _, pair := range multiPairs {
		if err := os.WriteFile(filepath.Join(pair.UpperDir, "residue.bin"), []byte("stale"), 0o644); err != nil {
			t.Fatal(err)
		}
	}

	// Unrecorded children, including allocator-shaped trees, are not proof of
	// execd ownership and must survive startup.
	if err := os.WriteFile(filepath.Join(root, "junk.txt"), []byte("junk"), 0o644); err != nil {
		t.Fatal(err)
	}
	sharedPayload := filepath.Join(root, "shared-data", "upper", "important.bin")
	if err := os.MkdirAll(filepath.Dir(sharedPayload), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(sharedPayload, []byte("unrelated"), 0o600); err != nil {
		t.Fatal(err)
	}
	unrecordedID := "00112233445566778899aabbccddeeff"
	unrecordedPayload := filepath.Join(root, unrecordedID, "upper", "important.bin")
	if err := os.MkdirAll(filepath.Dir(unrecordedPayload), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(filepath.Join(root, unrecordedID, "work"), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(unrecordedPayload, []byte("also unrelated"), 0o600); err != nil {
		t.Fatal(err)
	}

	mgr, err := NewUpperManager(root, 8<<30)
	if err != nil {
		t.Fatal(err)
	}

	for _, id := range []string{staleID, multiID} {
		parent := filepath.Join(root, id)
		if _, err := os.Stat(parent); !os.IsNotExist(err) {
			t.Errorf("recorded session dir %s should be reclaimed at startup", parent)
		}
		if _, err := os.Stat(filepath.Join(root, cleanupRegistryName, id)); !os.IsNotExist(err) {
			t.Errorf("cleanup record %s should be retired at startup", id)
		}
	}

	for _, p := range []string{
		filepath.Join(root, "junk.txt"),
		sharedPayload,
		unrecordedPayload,
	} {
		if _, err := os.Stat(p); err != nil {
			t.Errorf("unrecorded child %s must not be touched: %v", p, err)
		}
	}

	if usage, err := mgr.Usage(); err != nil || usage != 0 {
		t.Errorf("Usage() = (%d, %v), want (0, nil) after reclamation", usage, err)
	}

	mgr.mu.Lock()
	tracked := len(mgr.entries)
	mgr.mu.Unlock()
	if tracked != 0 {
		t.Errorf("tracked entries after reclamation = %d, want 0", tracked)
	}
}

func TestUpperManager_RecordedReclaimFailureRetainedForGC(t *testing.T) {
	if os.Geteuid() == 0 {
		t.Skip("requires an unprivileged UID for the real permission failure")
	}

	root := filepath.Join(t.TempDir(), "isolation")
	owner, err := NewUpperManager(root, 8<<30)
	if err != nil {
		t.Fatal(err)
	}

	id, pairs, err := owner.AllocateN(3)
	if err != nil {
		t.Fatal(err)
	}
	for _, pair := range pairs {
		if err := os.WriteFile(filepath.Join(pair.UpperDir, "residue.bin"), []byte("stale"), 0o644); err != nil {
			t.Fatal(err)
		}
	}
	for _, pair := range pairs {
		if err := os.Chmod(pair.UpperDir, 0o500); err != nil {
			t.Fatal(err)
		}
	}
	t.Cleanup(func() {
		for _, pair := range pairs {
			_ = os.Chmod(pair.UpperDir, 0o700)
		}
	})

	// A mount or permission error can block reclamation at startup. The
	// durable record must keep such residue released and tracked for GC.
	mgr, err := NewUpperManager(root, 8<<30)
	if err != nil {
		t.Fatal(err)
	}

	mgr.mu.Lock()
	entry := mgr.entries[id]
	mgr.mu.Unlock()
	if entry == nil {
		t.Fatal("failed reclamation must remain tracked for a GC retry")
	}
	if entry.InUse {
		t.Fatal("stale residue must be registered as released")
	}
	if len(entry.Pairs) != 3 {
		t.Fatalf("AllocateN-layout residue pairs = %d, want 3", len(entry.Pairs))
	}
	if _, err := os.Stat(filepath.Join(root, cleanupRegistryName, id)); err != nil {
		t.Fatalf("failed reclamation lost its cleanup record: %v", err)
	}
	usage, err := mgr.Usage()
	if err != nil {
		t.Fatal(err)
	}
	if usage < 15 {
		t.Errorf("Usage() = %d, want all residue bytes counted (>= 15)", usage)
	}

	for _, pair := range pairs {
		if err := os.Chmod(pair.UpperDir, 0o700); err != nil {
			t.Fatal(err)
		}
	}
	freed, err := mgr.CollectWithErrors()
	if err != nil {
		t.Fatal(err)
	}
	if len(freed) != 1 || freed[0] != id {
		t.Fatalf("CollectWithErrors freed %v, want [%s]", freed, id)
	}
	if _, err := os.Stat(filepath.Join(root, id)); !os.IsNotExist(err) {
		t.Error("retried reclamation should remove the residue")
	}
}

func TestUpperManager_UsageSkipsMissingReleasedUpper(t *testing.T) {
	mgr := newTestUpperManager(t)

	id, upper, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	// Simulate a partially removed residue entry: the released upper dir is
	// gone from disk but still registered.
	mgr.Release(id)
	if err := os.RemoveAll(filepath.Dir(upper)); err != nil {
		t.Fatal(err)
	}

	usage, err := mgr.Usage()
	if err != nil {
		t.Fatalf("Usage() error = %v, want nil for missing upper dir", err)
	}
	if usage != 0 {
		t.Errorf("Usage() = %d, want 0 for missing upper dir", usage)
	}

	mgr.mu.Lock()
	_, tracked := mgr.entries[id]
	mgr.mu.Unlock()
	if !tracked {
		t.Fatal("entry should stay tracked for a GC retry")
	}
}

func TestUpperManager_UsageRejectsMissingInUseUpper(t *testing.T) {
	mgr := newTestUpperManager(t)

	_, upper, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	if err := os.RemoveAll(upper); err != nil {
		t.Fatal(err)
	}

	if _, err := mgr.Usage(); !errors.Is(err, os.ErrNotExist) {
		t.Fatalf("Usage() error = %v, want missing in-use upper error", err)
	}
}

func TestDirSizeWalkErrorOnlySkipsMissingDescendants(t *testing.T) {
	root := filepath.Join(t.TempDir(), "upper")
	descendant := filepath.Join(root, "transient")
	if err := dirSizeWalkError(root, descendant, fs.ErrNotExist); err != nil {
		t.Fatalf("missing descendant error = %v, want nil", err)
	}
	if err := dirSizeWalkError(root, root, fs.ErrNotExist); !errors.Is(err, fs.ErrNotExist) {
		t.Fatalf("missing root error = %v, want fs.ErrNotExist", err)
	}
	if err := dirSizeWalkError(root, descendant, fs.ErrPermission); !errors.Is(err, fs.ErrPermission) {
		t.Fatalf("permission error = %v, want fs.ErrPermission", err)
	}
}

func TestDirSizeStillCountsFilesAndRejectsMissingRoot(t *testing.T) {
	root := filepath.Join(t.TempDir(), "upper")
	if err := os.Mkdir(root, 0o700); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(root, "payload"), []byte("payload"), 0o600); err != nil {
		t.Fatal(err)
	}
	if size, err := dirSize(root); err != nil || size != int64(len("payload")) {
		t.Fatalf("dirSize() = (%d, %v), want (7, nil)", size, err)
	}
	if err := os.RemoveAll(root); err != nil {
		t.Fatal(err)
	}
	if _, err := dirSize(root); !errors.Is(err, fs.ErrNotExist) {
		t.Fatalf("missing root error = %v, want fs.ErrNotExist", err)
	}
}

func TestUpperManager_AllocateRejectsInvalidUsageWithLimit(t *testing.T) {
	root := filepath.Join(t.TempDir(), "isolation")
	mgr, err := NewUpperManager(root, 100)
	if err != nil {
		t.Fatal(err)
	}

	_, healthyUpper, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	_, missingUpper, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(healthyUpper, "full.bin"), make([]byte, 100), 0o600); err != nil {
		t.Fatal(err)
	}
	if err := os.RemoveAll(missingUpper); err != nil {
		t.Fatal(err)
	}

	// Unknown usage must not let new allocations bypass an exhausted quota.
	if _, _, err := mgr.AllocateN(1); !errors.Is(err, os.ErrNotExist) {
		t.Fatalf("AllocateN() error = %v, want missing upper directory error", err)
	}
	// Once measurement recovers, the intact upper still consumes the budget.
	if err := os.MkdirAll(missingUpper, 0o755); err != nil {
		t.Fatal(err)
	}
	if _, _, err := mgr.AllocateN(1); !errors.Is(err, ErrUpperLimitExceeded) {
		t.Fatalf("AllocateN() error = %v, want ErrUpperLimitExceeded", err)
	}
}

func TestUpperManager_Allocate(t *testing.T) {
	mgr := newTestUpperManager(t)

	id1, upper1, work1, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	if id1 == "" {
		t.Error("empty session ID")
	}
	if upper1 == "" || work1 == "" {
		t.Error("empty directories")
	}

	for _, p := range []string{upper1, work1} {
		if _, err := os.Stat(p); os.IsNotExist(err) {
			t.Errorf("directory %s not created", p)
		}
	}

	mgr.mu.Lock()
	e := mgr.entries[id1]
	mgr.mu.Unlock()
	if e == nil {
		t.Fatal("entry not tracked")
	}
	if !e.InUse {
		t.Error("entry should be InUse after allocation")
	}
}

func TestUpperManager_AllocateN(t *testing.T) {
	mgr := newTestUpperManager(t)

	id, pairs, err := mgr.AllocateN(3)
	if err != nil {
		t.Fatal(err)
	}
	if id == "" {
		t.Error("empty session ID")
	}
	if len(pairs) != 3 {
		t.Fatalf("len(pairs) = %d, want 3", len(pairs))
	}

	// Pair 0 keeps the legacy layout (<id>/upper, <id>/work) so startup
	// reclamation still recognizes the session directory.
	wantNames := [][2]string{
		{"upper", "work"},
		{"upper-1", "work-1"},
		{"upper-2", "work-2"},
	}
	for i, p := range pairs {
		if filepath.Base(p.UpperDir) != wantNames[i][0] {
			t.Errorf("pairs[%d].UpperDir = %s, want %s", i, p.UpperDir, wantNames[i][0])
		}
		if filepath.Base(p.WorkDir) != wantNames[i][1] {
			t.Errorf("pairs[%d].WorkDir = %s, want %s", i, p.WorkDir, wantNames[i][1])
		}
		if filepath.Dir(p.UpperDir) != filepath.Dir(p.WorkDir) {
			t.Errorf("pairs[%d] dirs must share the session dir", i)
		}
		for _, dir := range []string{p.UpperDir, p.WorkDir} {
			if _, err := os.Stat(dir); os.IsNotExist(err) {
				t.Errorf("directory %s not created", dir)
			}
		}
	}

	// All pairs share one session directory...
	if filepath.Dir(pairs[0].UpperDir) != filepath.Dir(pairs[2].UpperDir) {
		t.Error("pairs must live under one session directory")
	}

	// ...so a single Remove clears every pair.
	if err := mgr.Remove(id); err != nil {
		t.Fatal(err)
	}
	if _, err := os.Stat(filepath.Dir(pairs[0].UpperDir)); !os.IsNotExist(err) {
		t.Error("session dir should be removed with all pairs")
	}
}

func TestUpperManager_AllocateN_InvalidCount(t *testing.T) {
	mgr := newTestUpperManager(t)
	if _, _, err := mgr.AllocateN(0); err == nil {
		t.Error("expected error for AllocateN(0)")
	}
	if _, _, err := mgr.AllocateN(-1); err == nil {
		t.Error("expected error for AllocateN(-1)")
	}
}

func TestUpperManager_AllocateN_UsageSumsAllPairs(t *testing.T) {
	mgr := newTestUpperManager(t)

	_, pairs, err := mgr.AllocateN(2)
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(pairs[0].UpperDir, "a.bin"), make([]byte, 100), 0o644); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(pairs[1].UpperDir, "b.bin"), make([]byte, 50), 0o644); err != nil {
		t.Fatal(err)
	}

	usage, err := mgr.Usage()
	if err != nil {
		t.Fatal(err)
	}
	if usage < 150 {
		t.Errorf("usage = %d, want at least 150 (both pairs counted)", usage)
	}

	// Limit accounting must see every pair: 150 bytes used of a 100-byte
	// budget already blocks the next allocation.
	root := filepath.Join(t.TempDir(), "isolation")
	limited, err := NewUpperManager(root, 100)
	if err != nil {
		t.Fatal(err)
	}
	_, lp, err := limited.AllocateN(2)
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(lp[0].UpperDir, "big.bin"), make([]byte, 200), 0o644); err != nil {
		t.Fatal(err)
	}
	if _, _, err := limited.AllocateN(1); !errors.Is(err, ErrUpperLimitExceeded) {
		t.Errorf("got %v, want ErrUpperLimitExceeded", err)
	}
}

func TestUpperManager_AllocateUnique(t *testing.T) {
	mgr := newTestUpperManager(t)

	id1, _, _, _ := mgr.Allocate()
	id2, _, _, _ := mgr.Allocate()
	if id1 == id2 {
		t.Error("session IDs should be unique")
	}
}

func TestUpperManager_Release(t *testing.T) {
	mgr := newTestUpperManager(t)
	id, upper, work, _ := mgr.Allocate()

	mgr.Release(id)

	mgr.mu.Lock()
	e := mgr.entries[id]
	mgr.mu.Unlock()
	if e.InUse {
		t.Error("entry should not be InUse after release")
	}

	// Directories should still exist (GC removes them).
	for _, p := range []string{upper, work} {
		if _, err := os.Stat(p); os.IsNotExist(err) {
			t.Errorf("directory %s removed prematurely", p)
		}
	}
}

func TestUpperManager_Remove(t *testing.T) {
	mgr := newTestUpperManager(t)
	id, upper, _, _ := mgr.Allocate()

	if err := mgr.Remove(id); err != nil {
		t.Fatal(err)
	}

	upperParent := filepath.Dir(upper)
	if _, err := os.Stat(upperParent); !os.IsNotExist(err) {
		t.Error("upper parent should be removed")
	}

	mgr.mu.Lock()
	_, ok := mgr.entries[id]
	mgr.mu.Unlock()
	if ok {
		t.Error("entry should be removed from map")
	}
}

func TestUpperManager_RemoveMissing(t *testing.T) {
	mgr := newTestUpperManager(t)
	if err := mgr.Remove("nonexistent"); err == nil {
		t.Error("expected error for nonexistent session")
	}
}

func TestUpperManager_RemoveFailureRemainsTrackedForGC(t *testing.T) {
	mgr := newTestUpperManager(t)
	id, upper, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	removeErr := errors.New("transient remove failure")
	mgr.removeAll = func(string) error { return removeErr }

	if err := mgr.Remove(id); !errors.Is(err, removeErr) {
		t.Fatalf("Remove error = %v, want %v", err, removeErr)
	}
	mgr.mu.Lock()
	entry := mgr.entries[id]
	mgr.mu.Unlock()
	if entry == nil {
		t.Fatal("failed removal must remain tracked")
	}
	if entry.InUse {
		t.Fatal("failed removal must be released for a later GC retry")
	}
	if _, err := os.Stat(filepath.Dir(upper)); err != nil {
		t.Fatalf("failed removal unexpectedly removed upper: %v", err)
	}

	mgr.removeAll = os.RemoveAll
	freed, err := mgr.CollectWithErrors()
	if err != nil {
		t.Fatal(err)
	}
	if len(freed) != 1 || freed[0] != id {
		t.Fatalf("CollectWithErrors freed %v, want [%s]", freed, id)
	}
}

func TestUpperManager_CollectWithErrorsRetainsFailureForRetry(t *testing.T) {
	mgr := newTestUpperManager(t)
	id, _, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	mgr.Release(id)

	removeErr := errors.New("transient collect failure")
	mgr.removeAll = func(string) error { return removeErr }
	freed, err := mgr.CollectWithErrors()
	if len(freed) != 0 {
		t.Fatalf("CollectWithErrors freed %v after removal failure", freed)
	}
	if !errors.Is(err, removeErr) {
		t.Fatalf("CollectWithErrors error = %v, want %v", err, removeErr)
	}
	mgr.mu.Lock()
	_, tracked := mgr.entries[id]
	mgr.mu.Unlock()
	if !tracked {
		t.Fatal("failed collection removed the entry from tracking")
	}

	mgr.removeAll = os.RemoveAll
	freed, err = mgr.CollectWithErrors()
	if err != nil {
		t.Fatal(err)
	}
	if len(freed) != 1 || freed[0] != id {
		t.Fatalf("retry freed %v, want [%s]", freed, id)
	}
}

func TestUpperManager_Collect(t *testing.T) {
	mgr := newTestUpperManager(t)

	id1, upper1, _, _ := mgr.Allocate()
	_, upper2, _, _ := mgr.Allocate()

	mgr.Release(id1)
	// id2 stays InUse.

	freed := mgr.Collect()
	if len(freed) != 1 {
		t.Fatalf("Collect freed %d entries, want 1", len(freed))
	}
	if freed[0] != id1 {
		t.Errorf("freed id = %q, want %q", freed[0], id1)
	}

	upperParent1 := filepath.Dir(upper1)
	if _, err := os.Stat(upperParent1); !os.IsNotExist(err) {
		t.Error("freed upper should be removed")
	}

	if _, err := os.Stat(upper2); os.IsNotExist(err) {
		t.Error("in-use upper should not be removed")
	}
}

func TestUpperManager_Usage(t *testing.T) {
	mgr := newTestUpperManager(t)

	usage, err := mgr.Usage()
	if err != nil {
		t.Fatal(err)
	}
	if usage != 0 {
		t.Errorf("empty usage = %d, want 0", usage)
	}

	_, upper, _, _ := mgr.Allocate()
	if err := os.WriteFile(filepath.Join(upper, "test.txt"), []byte("hello"), 0o644); err != nil {
		t.Fatal(err)
	}

	usage, err = mgr.Usage()
	if err != nil {
		t.Fatal(err)
	}
	if usage < 5 {
		t.Errorf("usage = %d, want at least 5", usage)
	}
}

func TestDirSize(t *testing.T) {
	dir := t.TempDir()

	if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("hello"), 0o644); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(dir, "sub", "b.txt"), []byte("world"), 0o644); err != nil {
		t.Fatal(err)
	}

	size, err := dirSize(dir)
	if err != nil {
		t.Fatal(err)
	}
	if size < 10 { // 5 + 5
		t.Errorf("dirSize = %d, want at least 10", size)
	}
}

func TestNewSessionID(t *testing.T) {
	id := newSessionID()
	if len(id) != 32 {
		t.Errorf("session ID length = %d, want 32", len(id))
	}
}

func TestUpperManager_AllocateLimitExceeded(t *testing.T) {
	root := filepath.Join(t.TempDir(), "isolation")
	mgr, err := NewUpperManager(root, 100)
	if err != nil {
		t.Fatal(err)
	}

	// Allocate and write enough data to exceed the 100-byte limit.
	_, upper, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(upper, "big.bin"), make([]byte, 200), 0o644); err != nil {
		t.Fatal(err)
	}

	_, _, _, err = mgr.Allocate()
	if err == nil {
		t.Fatal("expected error when upper limit exceeded")
	}
	if !errors.Is(err, ErrUpperLimitExceeded) {
		t.Errorf("got %v, want ErrUpperLimitExceeded", err)
	}
}

func TestUpperManager_AllocateNoLimitWhenZero(t *testing.T) {
	root := filepath.Join(t.TempDir(), "isolation")
	mgr, err := NewUpperManager(root, 0)
	if err != nil {
		t.Fatal(err)
	}

	// maxBytes=0 means no limit. Allocation should always succeed.
	_, upper, _, err := mgr.Allocate()
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(upper, "big.bin"), make([]byte, 1000), 0o644); err != nil {
		t.Fatal(err)
	}

	_, _, _, err = mgr.Allocate()
	if err != nil {
		t.Errorf("unexpected error with maxBytes=0: %v", err)
	}
}

func newTestUpperManager(t *testing.T) *UpperManager {
	t.Helper()
	root := filepath.Join(t.TempDir(), "isolation")
	mgr, err := NewUpperManager(root, 8<<30)
	if err != nil {
		t.Fatal(err)
	}
	return mgr
}

func TestAllocateSessionDir(t *testing.T) {
	mgr := newTestUpperManager(t)

	id, err := mgr.AllocateSessionDir()
	if err != nil {
		t.Fatal(err)
	}
	if !validSessionID(id) {
		t.Fatalf("AllocateSessionDir id = %q, want a valid session id", id)
	}

	tmpDir := mgr.TmpDir(id)
	info, err := os.Stat(tmpDir)
	if err != nil || !info.IsDir() {
		t.Fatalf("tmp dir %s missing: %v", tmpDir, err)
	}
	sessionDir := filepath.Dir(tmpDir)
	for _, name := range []string{"upper", "work"} {
		if _, err := os.Stat(filepath.Join(sessionDir, name)); !os.IsNotExist(err) {
			t.Errorf("pair-less session dir should not hold %q: %v", name, err)
		}
	}

	// Remove tears down the whole session subtree and retires the record.
	if err := mgr.Remove(id); err != nil {
		t.Fatal(err)
	}
	if _, err := os.Stat(sessionDir); !os.IsNotExist(err) {
		t.Errorf("session dir survived Remove: %v", err)
	}
}

func TestAllocateSessionDir_ReleasedEntryCollects(t *testing.T) {
	mgr := newTestUpperManager(t)

	id, err := mgr.AllocateSessionDir()
	if err != nil {
		t.Fatal(err)
	}
	mgr.Release(id)

	// A released pair-less entry still collects cleanly.
	freed := mgr.Collect()
	if len(freed) != 1 || freed[0] != id {
		t.Fatalf("Collect freed = %v, want [%s]", freed, id)
	}
}

func TestAllocateN_CreatesTmpDir(t *testing.T) {
	mgr := newTestUpperManager(t)

	id, pairs, err := mgr.AllocateN(2)
	if err != nil {
		t.Fatal(err)
	}
	info, err := os.Stat(mgr.TmpDir(id))
	if err != nil || !info.IsDir() {
		t.Fatalf("AllocateN session missing tmp dir: %v", err)
	}
	for _, pair := range pairs {
		if pair.UpperDir == mgr.TmpDir(id) || pair.WorkDir == mgr.TmpDir(id) {
			t.Fatal("tmp dir must not collide with upper/work pairs")
		}
	}
}

func TestAllocateTmpDirWorldWritableSticky(t *testing.T) {
	mgr := newTestUpperManager(t)

	for _, tc := range []struct {
		name  string
		alloc func() (string, error)
	}{
		{"allocate_session_dir", func() (string, error) { return mgr.AllocateSessionDir() }},
		{"allocate_n", func() (string, error) {
			id, _, err := mgr.AllocateN(1)
			return id, err
		}},
	} {
		t.Run(tc.name, func(t *testing.T) {
			id, err := tc.alloc()
			if err != nil {
				t.Fatal(err)
			}
			info, err := os.Stat(mgr.TmpDir(id))
			if err != nil {
				t.Fatal(err)
			}
			if perm := info.Mode().Perm(); perm != 0o777 {
				t.Fatalf("tmp dir perm = %#o, want 777", perm)
			}
			if info.Mode()&os.ModeSticky == 0 {
				t.Fatal("tmp dir must be sticky")
			}
		})
	}
}

func TestUsageCountsTmpDir(t *testing.T) {
	mgr := newTestUpperManager(t)

	id, err := mgr.AllocateSessionDir()
	if err != nil {
		t.Fatal(err)
	}
	before, err := mgr.Usage()
	if err != nil {
		t.Fatal(err)
	}
	payload := filepath.Join(mgr.TmpDir(id), "payload.bin")
	if err := os.WriteFile(payload, make([]byte, 1024), 0o644); err != nil {
		t.Fatal(err)
	}
	after, err := mgr.Usage()
	if err != nil {
		t.Fatal(err)
	}
	if after-before < 1024 {
		t.Fatalf("usage grew by %d bytes, want >= 1024 (tmp dir must count)", after-before)
	}
}