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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
package processor
import (
"os"
"path/filepath"
"runtime"
"testing"
"codeberg.org/snonux/snonux/internal/config"
)
func TestIsSimpleImageRef(t *testing.T) {
t.Parallel()
cases := []struct {
ref string
want bool
}{
{"img.png", true},
{"a.png", true},
{"sub/img.png", false},
{"a/b/c.png", false},
{"../img.png", false},
{"img/../other.png", false},
{"/etc/passwd", false},
}
for _, c := range cases {
t.Run(c.ref, func(t *testing.T) {
t.Parallel()
got := isSimpleImageRef(c.ref)
if got != c.want {
t.Fatalf("isSimpleImageRef(%q) = %v; want %v", c.ref, got, c.want)
}
})
}
}
func TestFindLocalImages(t *testing.T) {
t.Parallel()
t.Run("remote skipped", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
got := findLocalImages(` `, dir)
if len(got) != 0 {
t.Fatalf("expected no locals, got %v", got)
}
})
t.Run("missing file ignored", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
got := findLocalImages(``, dir)
if len(got) != 0 {
t.Fatalf("expected no locals, got %v", got)
}
})
t.Run("picks existing basename", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "shot.png"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
got := findLocalImages(``, dir)
if len(got) != 1 || got[0] != "shot.png" {
t.Fatalf("got %v; want [shot.png]", got)
}
})
t.Run("subdir ref ignored even if file exists", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "sub", "photo.png"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
got := findLocalImages(``, dir)
if len(got) != 0 {
t.Fatalf("expected no locals for subdir ref, got %v", got)
}
})
t.Run("parent traversal ref ignored even if file exists", func(t *testing.T) {
t.Parallel()
// Create a layout where ../photo.png from dir would resolve to a real file.
base := t.TempDir()
dir := filepath.Join(base, "inbox")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(base, "photo.png"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
got := findLocalImages(``, dir)
if len(got) != 0 {
t.Fatalf("expected no locals for traversal ref, got %v", got)
}
})
tests := []struct {
name string
md string
files []string
want []string
wantLen int
}{
{
name: "multiple locals order",
md: ` `,
files: []string{"a.png", "b.png"},
wantLen: 2,
},
{
name: "alt with spaces",
md: ``,
files: []string{"z.gif"},
want: []string{"z.gif"},
wantLen: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
for _, f := range tt.files {
if err := os.WriteFile(filepath.Join(dir, f), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
}
got := findLocalImages(tt.md, dir)
if tt.want != nil {
if len(got) != len(tt.want) {
t.Fatalf("got %v; want %v", got, tt.want)
}
for i := range tt.want {
if got[i] != tt.want[i] {
t.Fatalf("got %v; want %v", got, tt.want)
}
}
return
}
if len(got) != tt.wantLen {
t.Fatalf("len(got)=%d; want %d (%v)", len(got), tt.wantLen, got)
}
})
}
}
func TestRun_UnreadableMarkdownPreScanFails(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("chmod does not reliably deny read for owned files on Windows")
}
base := t.TempDir()
inputDir := filepath.Join(base, "inbox")
outputDir := filepath.Join(base, "out")
if err := os.MkdirAll(inputDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(outputDir, 0o755); err != nil {
t.Fatal(err)
}
mdPath := filepath.Join(inputDir, "note.md")
if err := os.WriteFile(mdPath, []byte("# x\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chmod(mdPath, 0); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(mdPath, 0o644) })
cfg := &config.Config{InputDir: inputDir, OutputDir: outputDir}
_, err := Run(cfg)
if err == nil {
t.Fatal("Run: expected error when markdown pre-scan cannot read a .md file")
}
}
|