package platform

import (
	"os"
	"path/filepath"
	"testing"
)

func TestValidateRestoreTarget(t *testing.T) {
	root := t.TempDir()
	target := filepath.Join(root, "task")
	if got, err := ValidateRestoreTarget(target, root, false, false); err != nil || got != target {
		t.Fatalf("staging target rejected: %q %v", got, err)
	}
	if _, err := ValidateRestoreTarget("/etc", root, true, true); err == nil {
		t.Fatal("protected target accepted")
	}
	if _, err := ValidateRestoreTarget("/mnt/user/data", root, true, false); err == nil {
		t.Fatal("unconfirmed in-place restore accepted")
	}
}

func TestValidateRestoreTargetRejectsSymlink(t *testing.T) {
	root := t.TempDir()
	real := filepath.Join(root, "real")
	link := filepath.Join(root, "link")
	if err := os.Mkdir(real, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(real, link); err != nil {
		t.Fatal(err)
	}
	if _, err := ValidateRestoreTarget(filepath.Join(link, "task"), root, false, false); err == nil {
		t.Fatal("symlink traversal accepted")
	}
}
