//go:build linux

// Copyright 2026 The Agent Substrate 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 imagecache

import (
	"archive/tar"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"golang.org/x/sys/unix"

	"go.ate.dev/substrate/internal/roottest"
)

// FinalizeLayer materializes 0:0 whiteout devices and opaque
// xattrs (trusted.*, CAP_SYS_ADMIN); only root has those in a plain test
// environment. Runs in privileged CI / root shells, skips elsewhere.
func TestFinalizeLayer_MaterializesWhiteouts(t *testing.T) {
	roottest.Require(t, "CAP_SYS_ADMIN for trusted.* xattrs")
	dir := t.TempDir()
	writeLayer(t, dir,
		map[string]string{"kept.txt": "kept"},
		&whiteoutSet{
			Whiteouts: []string{"removed.txt", "sub/dir/removed-deep.txt"},
			Opaques:   []string{"opaque-dir"},
		})

	if err := FinalizeLayer(dir); err != nil {
		t.Fatalf("FinalizeLayer: %v", err)
	}

	for _, p := range []string{"removed.txt", "sub/dir/removed-deep.txt"} {
		fi, err := os.Lstat(filepath.Join(dir, layerFSDirName, p))
		if err != nil {
			t.Fatalf("whiteout %q not created: %v", p, err)
		}
		if fi.Mode()&os.ModeCharDevice == 0 {
			t.Errorf("whiteout %q mode = %v, want char device", p, fi.Mode())
		}
		var st unix.Stat_t
		if err := unix.Stat(filepath.Join(dir, layerFSDirName, p), &st); err == nil && st.Rdev != 0 {
			t.Errorf("whiteout %q rdev = %d, want 0:0", p, st.Rdev)
		}
	}

	var val [8]byte
	n, err := unix.Getxattr(filepath.Join(dir, layerFSDirName, "opaque-dir"), "trusted.overlay.opaque", val[:])
	if err != nil || string(val[:n]) != "y" {
		t.Errorf("opaque xattr = %q (err=%v), want \"y\"", val[:n], err)
	}

	if _, err := os.Stat(filepath.Join(dir, layerFinalizedMarkerName)); err != nil {
		t.Errorf("finalized marker missing: %v", err)
	}

	// Idempotent: second call is a marker-hit no-op.
	if err := FinalizeLayer(dir); err != nil {
		t.Errorf("FinalizeLayer (second call): %v", err)
	}
}

// Escape rejection needs no privileges: a crafted whiteouts.json must fail
// validation before any mknod/setxattr is attempted.
func TestFinalizeLayer_RejectsEscapingPaths(t *testing.T) {
	t.Run("whiteout escape", func(t *testing.T) {
		dir := t.TempDir()
		writeLayer(t, dir, nil, &whiteoutSet{Whiteouts: []string{"../escape"}})
		if err := FinalizeLayer(dir); err == nil {
			t.Errorf("FinalizeLayer accepted an escaping whiteout path")
		}
	})
	t.Run("opaque escape", func(t *testing.T) {
		dir := t.TempDir()
		writeLayer(t, dir, nil, &whiteoutSet{Opaques: []string{"a/../../escape"}})
		if err := FinalizeLayer(dir); err == nil {
			t.Errorf("FinalizeLayer accepted an escaping opaque path")
		}
	})
}

// A layer with no whiteouts.json finalizes to just the marker; needs no
// privileges.
func TestFinalizeLayer_NoWhiteouts(t *testing.T) {
	dir := t.TempDir()
	writeLayer(t, dir, map[string]string{"f": "x"}, nil)
	if err := FinalizeLayer(dir); err != nil {
		t.Fatalf("FinalizeLayer: %v", err)
	}
	if _, err := os.Stat(filepath.Join(dir, layerFinalizedMarkerName)); err != nil {
		t.Errorf("finalized marker missing: %v", err)
	}
}

// A bundle without an overlay spec must be left untouched.
func TestSetupBundleRootfs_NoSpecIsNoop(t *testing.T) {
	bundle := t.TempDir()
	if err := SetupBundleRootfs(bundle); err != nil {
		t.Fatalf("SetupBundleRootfs: %v", err)
	}
	if entries, _ := os.ReadDir(bundle); len(entries) != 0 {
		t.Errorf("no-spec bundle was modified: %v", entries)
	}
}

// A zero-layer spec composes without any mount: empty rootfs plus ExtraDirs.
// Needs no privileges (the stale-unmount attempt's failure is ignored).
func TestSetupBundleRootfs_ZeroLayers(t *testing.T) {
	bundle := t.TempDir()
	if err := WriteSpec(bundle, &OverlaySpec{Layers: nil, ExtraDirs: []string{"/run/ate"}}); err != nil {
		t.Fatalf("WriteSpec: %v", err)
	}
	if err := SetupBundleRootfs(bundle); err != nil {
		t.Fatalf("SetupBundleRootfs: %v", err)
	}
	fi, err := os.Stat(filepath.Join(bundle, "rootfs", "run", "ate"))
	if err != nil || !fi.IsDir() {
		t.Errorf("ExtraDir not created in rootfs: fi=%v err=%v", fi, err)
	}
	for _, d := range []string{"upper", "work"} {
		if fi, err := os.Stat(filepath.Join(bundle, d)); err != nil || !fi.IsDir() {
			t.Errorf("bundle dir %q missing: %v", d, err)
		}
	}
}

// Implicit-parent metadata repair through a real overlay: the base declares
// a 0700 dir, the top layer created it implicitly (0755 in its tree), and
// after compose the merged view must show 0700 — copied up into the
// bundle's upper, with the shared layer trees untouched. Needs root.
func TestSetupBundleRootfs_ImplicitDirMetadataRepair(t *testing.T) {
	roottest.Require(t, "mount/unmount")
	base := t.TempDir()
	writeLayer(t, base, map[string]string{"secret/keep.txt": "k"}, nil)
	if err := os.Chmod(filepath.Join(base, layerFSDirName, "secret"), 0o700); err != nil {
		t.Fatal(err)
	}
	top := t.TempDir()
	writeLayer(t, top, map[string]string{"secret/new.txt": "n"}, &whiteoutSet{ImplicitDirs: []string{"secret"}})

	bundle := t.TempDir()
	if err := WriteSpec(bundle, &OverlaySpec{Layers: []string{base, top}}); err != nil {
		t.Fatalf("WriteSpec: %v", err)
	}
	if err := SetupBundleRootfs(bundle); err != nil {
		t.Fatalf("SetupBundleRootfs: %v", err)
	}
	t.Cleanup(func() { _ = UnmountAllUnder(bundle) })

	fi, err := os.Lstat(filepath.Join(bundle, "rootfs", "secret"))
	if err != nil {
		t.Fatalf("stat merged dir: %v", err)
	}
	if fi.Mode().Perm() != 0o700 {
		t.Errorf("merged secret mode = %v, want 0700 from the declaring base layer", fi.Mode().Perm())
	}
	// The repair must land in the bundle upper, not the shared pool.
	if fi, err := os.Lstat(filepath.Join(top, layerFSDirName, "secret")); err != nil || fi.Mode().Perm() != 0o755 {
		t.Errorf("shared top layer tree was modified: %v %v", fi, err)
	}
	if _, err := os.Lstat(filepath.Join(bundle, "upper", "secret")); err != nil {
		t.Errorf("repair did not copy up into the bundle upper: %v", err)
	}
}

// The merged rootfs root is the container's "/"; at 0700 a non-root container
// cannot search it and can exec nothing.
func TestSetupBundleRootfs_RootIsSearchableByNonRoot(t *testing.T) {
	assertSearchable := func(t *testing.T, rootfs string) {
		t.Helper()
		fi, err := os.Stat(rootfs)
		if err != nil {
			t.Fatalf("stat merged rootfs root: %v", err)
		}
		if perm := fi.Mode().Perm(); perm&0o005 != 0o005 {
			t.Errorf("merged rootfs root mode = %04o, want world read+search", perm)
		}
	}

	t.Run("zero layers", func(t *testing.T) {
		bundle := t.TempDir()
		if err := WriteSpec(bundle, &OverlaySpec{Layers: nil}); err != nil {
			t.Fatalf("WriteSpec: %v", err)
		}
		if err := SetupBundleRootfs(bundle); err != nil {
			t.Fatalf("SetupBundleRootfs: %v", err)
		}
		assertSearchable(t, filepath.Join(bundle, "rootfs"))
	})

	t.Run("overlay", func(t *testing.T) {
		roottest.Require(t, "mount/unmount")
		layer := t.TempDir()
		writeLayer(t, layer, map[string]string{"bin/app": "x"}, nil)

		bundle := t.TempDir()
		if err := WriteSpec(bundle, &OverlaySpec{Layers: []string{layer}}); err != nil {
			t.Fatalf("WriteSpec: %v", err)
		}
		if err := SetupBundleRootfs(bundle); err != nil {
			t.Fatalf("SetupBundleRootfs: %v", err)
		}
		t.Cleanup(func() { _ = UnmountAllUnder(bundle) })
		assertSearchable(t, filepath.Join(bundle, "rootfs"))
	})
}

// Full overlay mount + UnmountAllUnder round trip; needs CAP_SYS_ADMIN.
func TestSetupBundleRootfs_MountAndUnmount(t *testing.T) {
	roottest.Require(t, "mount/unmount")
	layer := t.TempDir()
	writeLayer(t, layer, map[string]string{"from-layer.txt": "hello"}, nil)

	bundle := t.TempDir()
	if err := WriteSpec(bundle, &OverlaySpec{Layers: []string{layer}, ExtraDirs: []string{"/run/ate"}}); err != nil {
		t.Fatalf("WriteSpec: %v", err)
	}
	if err := SetupBundleRootfs(bundle); err != nil {
		t.Fatalf("SetupBundleRootfs: %v", err)
	}
	t.Cleanup(func() { _ = UnmountAllUnder(bundle) })

	if got, err := os.ReadFile(filepath.Join(bundle, "rootfs", "from-layer.txt")); err != nil || string(got) != "hello" {
		t.Errorf("layer content not visible through overlay: %q (%v)", got, err)
	}
	if fi, err := os.Stat(filepath.Join(bundle, "rootfs", "run", "ate")); err != nil || !fi.IsDir() {
		t.Errorf("ExtraDir missing in overlay: %v", err)
	}
	// A write through the mount lands in the bundle's upper, not the layer.
	if err := os.WriteFile(filepath.Join(bundle, "rootfs", "written.txt"), []byte("w"), 0o644); err != nil {
		t.Fatalf("write through overlay: %v", err)
	}
	if _, err := os.Stat(filepath.Join(bundle, "upper", "written.txt")); err != nil {
		t.Errorf("write did not land in upper: %v", err)
	}
	if _, err := os.Stat(filepath.Join(layer, layerFSDirName, "written.txt")); err == nil {
		t.Errorf("write leaked into the shared layer")
	}

	if err := UnmountAllUnder(bundle); err != nil {
		t.Fatalf("UnmountAllUnder: %v", err)
	}
	if _, err := os.Stat(filepath.Join(bundle, "rootfs", "from-layer.txt")); err == nil {
		t.Errorf("rootfs still shows layer content after unmount")
	}
}

// Regression test for the mount(2) single-page option-string cap: a lowerdir
// chain whose joined paths exceed one page (~34 digest-derived layers) used
// to fail with a bare EINVAL. The fsconfig lowerdir+ path has no aggregate
// limit. Needs CAP_SYS_ADMIN.
func TestSetupBundleRootfs_ManyLayers(t *testing.T) {
	roottest.Require(t, "mount/unmount")
	// Overlayfs caps a chain at 500 lower layers, and production-length
	// paths (~114 bytes) cannot outgrow a 64 KiB page within that cap. So
	// each digest-named layer dir is padded until its path fills the 255
	// bytes one fsconfig(2) string value carries, and the layer count
	// follows the page size: 17 layers on 4 KiB pages, 257 on 64 KiB pages.
	pool := filepath.Join(t.TempDir(), "sha256")
	const lowerLen = 255
	pad := lowerLen - len(filepath.Join(pool, strings.Repeat("0", 64), layerFSDirName))
	if pad < 0 {
		t.Fatalf("temp dir %q is too long to pad a lowerdir path to %d bytes", pool, lowerLen)
	}
	pageSize := os.Getpagesize()
	n := pageSize/(lowerLen+1) + 1
	layers := make([]string, n)
	joined := 0
	for i := range layers {
		layers[i] = filepath.Join(pool, fmt.Sprintf("%064d", i)+strings.Repeat("p", pad))
		writeLayer(t, layers[i], map[string]string{fmt.Sprintf("from-layer-%02d.txt", i): "x"}, nil)
		joined += len(layers[i]) + len("/fs") + 1
	}
	if joined <= pageSize {
		t.Fatalf("test layers join to %d bytes, not exceeding the %d-byte page this test guards against", joined, pageSize)
	}

	bundle := t.TempDir()
	if err := WriteSpec(bundle, &OverlaySpec{Layers: layers}); err != nil {
		t.Fatalf("WriteSpec: %v", err)
	}
	if err := SetupBundleRootfs(bundle); err != nil {
		t.Fatalf("SetupBundleRootfs with %d layers: %v", n, err)
	}
	t.Cleanup(func() { _ = UnmountAllUnder(bundle) })

	// Bottom-most and top-most layers are both visible in the merged view.
	for _, i := range []int{0, n - 1} {
		p := filepath.Join(bundle, "rootfs", fmt.Sprintf("from-layer-%02d.txt", i))
		if _, err := os.Stat(p); err != nil {
			t.Errorf("layer %d content missing from merged rootfs: %v", i, err)
		}
	}

	if err := UnmountAllUnder(bundle); err != nil {
		t.Fatalf("UnmountAllUnder: %v", err)
	}
}

// Image volumes reach identical content through both arms: one layer binds,
// several overlay.
func TestSetupBundleRootfs_ImageVolumes(t *testing.T) {
	roottest.Require(t, "mount/unmount")

	for _, tc := range []struct {
		name   string
		layers int
	}{
		{"one layer binds", 1},
		{"three layers overlay", 3},
	} {
		t.Run(tc.name, func(t *testing.T) {
			var layers []string
			for i := range tc.layers {
				dir := t.TempDir()
				writeLayer(t, dir, map[string]string{
					fmt.Sprintf("layer%d.txt", i): "content",
					"shadowed.txt":                fmt.Sprintf("from-layer-%d", i),
				}, nil)
				layers = append(layers, dir)
			}
			// A shared layer with a submount now and one mounted after setup. Only
			// the bind arm could carry either along.
			fsDir := filepath.Join(layers[tc.layers-1], layerFSDirName)
			for _, d := range []string{"sub", "later"} {
				if err := os.Mkdir(filepath.Join(fsDir, d), 0o755); err != nil {
					t.Fatal(err)
				}
			}
			if err := unix.Mount(fsDir, fsDir, "", unix.MS_BIND, ""); err != nil {
				t.Fatalf("self-binding %q: %v", fsDir, err)
			}
			t.Cleanup(func() { _ = unix.Unmount(fsDir, unix.MNT_DETACH) })
			if err := unix.Mount("", fsDir, "", unix.MS_SHARED, ""); err != nil {
				t.Fatalf("making %q shared: %v", fsDir, err)
			}
			if err := unix.Mount("tmpfs", filepath.Join(fsDir, "sub"), "tmpfs", 0, ""); err != nil {
				t.Fatalf("mounting nested tmpfs: %v", err)
			}

			bundle := t.TempDir()
			if err := WriteSpec(bundle, &OverlaySpec{
				Layers:       []string{layers[0]},
				ImageVolumes: []ImageVolumeOverlay{{Name: "agent", Layers: layers}},
			}); err != nil {
				t.Fatalf("WriteSpec: %v", err)
			}
			if err := SetupBundleRootfs(bundle); err != nil {
				t.Fatalf("SetupBundleRootfs: %v", err)
			}
			t.Cleanup(func() { _ = UnmountAllUnder(bundle) })
			if err := unix.Mount("tmpfs", filepath.Join(fsDir, "later"), "tmpfs", 0, ""); err != nil {
				t.Fatalf("mounting tmpfs after setup: %v", err)
			}

			mnt := filepath.Join(bundle, "volumes", "agent")
			for i := range tc.layers {
				if _, err := os.Stat(filepath.Join(mnt, fmt.Sprintf("layer%d.txt", i))); err != nil {
					t.Errorf("layer %d not visible in the volume: %v", i, err)
				}
			}
			// Later layers win, same as the rootfs overlay.
			want := fmt.Sprintf("from-layer-%d", tc.layers-1)
			if got, err := os.ReadFile(filepath.Join(mnt, "shadowed.txt")); err != nil || string(got) != want {
				t.Errorf("shadowed.txt = %q (%v), want %q", got, err, want)
			}
			// No upper on either arm, so there is nowhere for a write to go.
			for _, name := range []string{"nope.txt", "sub/nope.txt", "later/nope.txt"} {
				if err := os.WriteFile(filepath.Join(mnt, name), []byte("x"), 0o644); err == nil {
					t.Errorf("write of %s succeeded through a read-only image volume", name)
				} else if !errors.Is(err, unix.EROFS) {
					t.Errorf("write of %s failed with %v, want EROFS", name, err)
				}
			}
			// The shared pool must never see the attempt.
			if _, err := os.Stat(filepath.Join(layers[tc.layers-1], layerFSDirName, "nope.txt")); err == nil {
				t.Error("write leaked into the shared layer pool")
			}

			if err := UnmountAllUnder(bundle); err != nil {
				t.Fatalf("UnmountAllUnder: %v", err)
			}
			if _, err := os.Stat(filepath.Join(mnt, "layer0.txt")); err == nil {
				t.Error("volume still shows content after unmount")
			}
		})
	}
}

// unpackTestLayer unpacks entries into a layer dir under dir the way the
// store's unpack does: the tree under fs/ plus its whiteouts.json.
func unpackTestLayer(t *testing.T, dir string, entries []tarEntry) {
	t.Helper()
	fsDir := filepath.Join(dir, layerFSDirName)
	if err := os.Mkdir(fsDir, 0o755); err != nil {
		t.Fatalf("mkdir fs: %v", err)
	}
	wh, err := unpackInto(t, fsDir, buildTar(t, entries))
	if err != nil {
		t.Fatalf("unpackLayer: %v", err)
	}
	b, err := json.Marshal(wh)
	if err != nil {
		t.Fatalf("marshal whiteouts: %v", err)
	}
	if err := os.WriteFile(filepath.Join(dir, layerWhiteoutsFileName), b, 0o600); err != nil {
		t.Fatalf("write whiteouts.json: %v", err)
	}
}

// TestSetupBundleRootfs_PresentsLayerOwnership composes a rootfs from layers
// unpacked from tars and checks the merged view shows the owners the tars
// record: the view runsc's gofer and virtiofsd serve to the actor. The top
// layer adds files under the private home directory and the setgid team
// directory without declaring them, so the merged directories' owners and
// modes also have to survive the implicit-dir repair, which copies them from
// the base layer's unpacked tree. It runs as root and under the gVisor
// worker's capabilities, which have no CAP_FSETID.
func TestSetupBundleRootfs_PresentsLayerOwnership(t *testing.T) {
	roottest.Require(t, "chown and mount")
	t.Run("root", testSetupBundleRootfsPresentsLayerOwnership)
	runUnderCaps(t, "gvisor-worker-caps", gvisorWorkerCaps, testSetupBundleRootfsPresentsLayerOwnership)
}

func testSetupBundleRootfsPresentsLayerOwnership(t *testing.T) {
	base := t.TempDir()
	unpackTestLayer(t, base, ownershipEntries)
	top := t.TempDir()
	unpackTestLayer(t, top, []tarEntry{
		{name: "home/agent/.profile", typeflag: tar.TypeReg, mode: 0o600, body: "p", uid: 1000, gid: 1000},
		{name: "srv/team/notes", typeflag: tar.TypeReg, mode: 0o664, body: "n", uid: 1000, gid: 100},
	})

	bundle := t.TempDir()
	if err := WriteSpec(bundle, &OverlaySpec{Layers: []string{base, top}}); err != nil {
		t.Fatalf("WriteSpec: %v", err)
	}
	if err := SetupBundleRootfs(bundle); err != nil {
		t.Fatalf("SetupBundleRootfs: %v", err)
	}
	t.Cleanup(func() { _ = UnmountAllUnder(bundle) })

	rootfs := filepath.Join(bundle, "rootfs")
	assertOwner(t, filepath.Join(rootfs, "home/agent"), 1000, 1000, os.ModeDir|0o700)
	assertOwner(t, filepath.Join(rootfs, "home/agent/.bashrc"), 1000, 1000, 0o644)
	assertOwner(t, filepath.Join(rootfs, "home/agent/.profile"), 1000, 1000, 0o600)
	assertOwner(t, filepath.Join(rootfs, "home/agent/rc"), 1002, 1002, os.ModeSymlink|0o777)
	assertOwner(t, filepath.Join(rootfs, "srv/app.conf"), 1000, 2000, 0o640)
	assertOwner(t, filepath.Join(rootfs, "srv/team"), 1000, 100, os.ModeDir|os.ModeSetgid|0o775)
	assertOwner(t, filepath.Join(rootfs, "opt/app"), 1001, 1001, os.ModeDir|0o555)
	assertOwner(t, filepath.Join(rootfs, "home"), 0, 0, os.ModeDir|0o755)
}

// TestSetupBundleRootfs_RepairsWhiteoutParentUnderSetgidDir checks the repair
// of an implicit directory that FinalizeLayer creates for a whiteout. The top
// layer declares srv/shared setgid with group 1000 and whites out a file in
// srv/shared/docs without declaring docs, so docs starts as 0:1000 with
// setgid inherited. The repair must still give it the base layer's 02770, and
// in the gVisor worker root is not in group 1000 and has no CAP_FSETID.
func TestSetupBundleRootfs_RepairsWhiteoutParentUnderSetgidDir(t *testing.T) {
	roottest.Require(t, "chown, mknod and mount")
	t.Run("root", testSetupBundleRootfsRepairsWhiteoutParentUnderSetgidDir)
	runUnderCaps(t, "gvisor-worker-caps", gvisorWorkerCaps, testSetupBundleRootfsRepairsWhiteoutParentUnderSetgidDir)
}

func testSetupBundleRootfsRepairsWhiteoutParentUnderSetgidDir(t *testing.T) {
	base := t.TempDir()
	unpackTestLayer(t, base, []tarEntry{
		{name: "srv/shared/docs/", typeflag: tar.TypeDir, mode: 0o2770, uid: 1000, gid: 1000},
		{name: "srv/shared/docs/old", typeflag: tar.TypeReg, mode: 0o660, body: "o", uid: 1000, gid: 1000},
	})
	top := t.TempDir()
	unpackTestLayer(t, top, []tarEntry{
		{name: "srv/shared/", typeflag: tar.TypeDir, mode: 0o2775, gid: 1000},
		{name: "srv/shared/docs/.wh.old", typeflag: tar.TypeReg},
	})

	bundle := t.TempDir()
	if err := WriteSpec(bundle, &OverlaySpec{Layers: []string{base, top}}); err != nil {
		t.Fatalf("WriteSpec: %v", err)
	}
	if err := SetupBundleRootfs(bundle); err != nil {
		t.Fatalf("SetupBundleRootfs: %v", err)
	}
	t.Cleanup(func() { _ = UnmountAllUnder(bundle) })

	// FinalizeLayer created docs in the top layer under the setgid parent.
	assertOwner(t, filepath.Join(top, layerFSDirName, "srv/shared/docs"), 0, 1000, 0)
	rootfs := filepath.Join(bundle, "rootfs")
	assertOwner(t, filepath.Join(rootfs, "srv/shared/docs"), 1000, 1000, os.ModeDir|os.ModeSetgid|0o770)
	if _, err := os.Lstat(filepath.Join(rootfs, "srv/shared/docs/old")); !errors.Is(err, os.ErrNotExist) {
		t.Errorf("whited-out srv/shared/docs/old: Lstat error %v, want ErrNotExist", err)
	}
}