summaryrefslogtreecommitdiff
path: root/internal/plan/hostile_test.go
blob: 4e30b41e02037674ffddfe551d144e6ae6c8def2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: GPL-3.0-or-later

package plan

import (
	"fmt"
	"testing"
	"time"

	"git.labunix.xyz/krino/internal/config"
)

// TestBuildRefusesDotNames: a rename whose placeholders produce "", "." or
// ".." names a directory, not a file; the step is skipped with a reason
// instead of renaming a file onto its own folder or the one above.
func TestBuildRefusesDotNames(t *testing.T) {
	for _, capture := range []string{"", ".", ".."} {
		in := []Input{{
			File:  file("/r", "x.pdf"),
			Rules: []RuleMatch{{Name: "a", Captures: []string{"x.pdf", capture}, Actions: []config.Action{act(config.Rename, "{1}")}}},
		}}
		c := Build("/r", in, time.Now(), NoDisk{}, NewClaims())[0]
		if want := fmt.Sprintf("rename produced the name %q", capture); c.Steps[0].Skip != want {
			t.Errorf("capture %q: Skip = %q, want %q", capture, c.Steps[0].Skip, want)
		}
	}
}

// TestBuildRefusesDotDotFromPlaceholder: a file name can capture "..",
// though never "/", so a destination whose placeholders add a ".." segment
// would leave the directory the rule names: that step is skipped. A ".."
// the rule wrote itself is the rule's own business, and "..x" is a name,
// not a segment.
func TestBuildRefusesDotDotFromPlaceholder(t *testing.T) {
	step := func(dest string) Step {
		in := []Input{{
			File:  file("/r", "x.pdf"),
			Rules: []RuleMatch{{Name: "a", Captures: []string{"x.pdf", ".."}, Actions: []config.Action{act(config.Move, dest)}}},
		}}
		return Build("/r", in, time.Now(), NoDisk{}, NewClaims())[0].Steps[0]
	}
	if s := step("/w/{1}/in"); s.Skip != `destination "/w/../in" leaves /w through a placeholder` {
		t.Errorf("placeholder ..: Skip = %q, Dst = %q", s.Skip, s.Dst)
	}
	if s := step("/w/../in"); s.Skip != "" || s.Dst != "/in/x.pdf" {
		t.Errorf("written ..: Skip = %q, Dst = %q", s.Skip, s.Dst)
	}
	if s := step("/w/{1}x"); s.Skip != "" || s.Dst != "/w/..x/x.pdf" {
		t.Errorf("..x: Skip = %q, Dst = %q", s.Skip, s.Dst)
	}
	// "{{" is a literal brace, not a placeholder: the text names /w/{x}, and
	// a capture may not climb out of it (triage 28b).
	if s := step("/w/{{x}}/{1}/in"); s.Skip != `destination "/w/{x}/../in" leaves /w/{x} through a placeholder` {
		t.Errorf("literal braces: Skip = %q, Dst = %q", s.Skip, s.Dst)
	}
}

// TestBuildKeepsPlaceholdersInsideTheirDirectory: a capture of "~", an
// empty capture, or an extension-less {ext} at the start of a destination
// used to make it $HOME or absolute (review M1). The resolved destination
// must stay under the directory the rule's text names before its first
// placeholder; a destination with no placeholder is the rule's own
// business.
func TestBuildKeepsPlaceholdersInsideTheirDirectory(t *testing.T) {
	t.Setenv("HOME", "/home/x")
	step := func(name, dest string, caps ...string) Step {
		in := []Input{{File: file("/r", name), Rules: []RuleMatch{{Name: "a", Captures: caps, Actions: []config.Action{act(config.Move, dest)}}}}}
		return Build("/r", in, time.Now(), NoDisk{}, NewClaims())[0].Steps[0]
	}
	for _, tc := range []struct {
		name, dest string
		caps       []string
		skip       bool
		dst        string
	}{
		{"~_i.pdf", "{1}/Filed", []string{"~_", "~"}, true, ""},
		{"_i.pdf", "{1}/Filed", []string{"_", ""}, true, ""},
		{"README", "{ext}/tmp", nil, true, ""},
		{"a.pdf", "Out/{1}", []string{"a", "~"}, false, "/r/Out/~/a.pdf"},
		{"a.pdf", "~/docs/{1}", []string{"a", "x"}, false, "/home/x/docs/x/a.pdf"},
		{"a.pdf", "~/docs/{1}", []string{"a", ".."}, true, ""},
		{"a.pdf", "Work/Ac{1}", []string{"a", "me"}, false, "/r/Work/Acme/a.pdf"},
		{"a.pdf", "/w/{1}/in", []string{"a", ".."}, true, ""},
		{"a.pdf", "/w/../in", nil, false, "/in/a.pdf"},
		{"a.pdf", "{mtime:%Y}", nil, false, "/r/2026/a.pdf"},
	} {
		s := step(tc.name, tc.dest, tc.caps...)
		if tc.skip != (s.Skip != "") || (!tc.skip && s.Dst != tc.dst) {
			t.Errorf("%s -> %s %q: Skip %q Dst %q; want skip=%v dst=%q", tc.name, tc.dest, tc.caps, s.Skip, s.Dst, tc.skip, tc.dst)
		}
	}
}