Debian Codegraph @

common/hexec/exec_test.go

raw
1 // Copyright 2026 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hexec
15
16 import (
17 "os"
18 "path/filepath"
19 "runtime"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/gohugoio/hugo/common/loggers"
24 "github.com/gohugoio/hugo/config/security"
25 )
26
27 func TestNodePermissionArgs(t *testing.T) {
28 c := qt.New(t)
29
30 // Use t.TempDir() so paths are absolute on any OS (avoids Windows volume assumptions).
31 base := t.TempDir()
32 site := filepath.Join(base, "site")
33 tmp := filepath.Join(base, "tmp")
34 cacheDir := filepath.Join(base, "home", "user", ".cache", "hugo_cache", "modules")
35
36 c.Run("Default config tailwindcss", func(c *qt.C) {
37 e := &Exec{
38 sc: security.DefaultConfig,
39 workingDir: site,
40 }
41 args := e.nodePermissionArgs("tailwindcss", "")
42 c.Assert(args, qt.DeepEquals, []string{
43 "--permission",
44 "--allow-fs-read=" + site,
45 "--allow-addons",
46 "--allow-worker",
47 "--allow-child-process",
48 "--disable-warning=SecurityWarning",
49 })
50 })
51
52 c.Run("Default config postcss", func(c *qt.C) {
53 e := &Exec{
54 sc: security.DefaultConfig,
55 workingDir: site,
56 }
57 args := e.nodePermissionArgs("postcss", "")
58 c.Assert(args, qt.DeepEquals, []string{
59 "--permission",
60 "--allow-fs-read=" + site,
61 })
62 })
63
64 c.Run("Multiple paths", func(c *qt.C) {
65 cfg := security.DefaultConfig
66 cfg.Node.Permissions.AllowRead = []string{".", tmp}
67 cfg.Node.Permissions.AllowWrite = []string{"."}
68 e := &Exec{
69 sc: cfg,
70 workingDir: site,
71 }
72 args := e.nodePermissionArgs("tailwindcss", "")
73 c.Assert(args, qt.DeepEquals, []string{
74 "--permission",
75 "--allow-fs-read=" + site,
76 "--allow-fs-read=" + tmp,
77 "--allow-fs-write=" + site,
78 "--allow-addons",
79 "--allow-worker",
80 "--allow-child-process",
81 "--disable-warning=SecurityWarning",
82 })
83 })
84
85 c.Run("Wildcard", func(c *qt.C) {
86 cfg := security.DefaultConfig
87 cfg.Node.Permissions.AllowRead = []string{"*"}
88 cfg.Node.Permissions.AllowWrite = []string{"*"}
89 e := &Exec{
90 sc: cfg,
91 workingDir: site,
92 }
93 args := e.nodePermissionArgs("tailwindcss", "")
94 c.Assert(args, qt.DeepEquals, []string{
95 "--permission",
96 "--allow-fs-read=*",
97 "--allow-fs-write=*",
98 "--allow-addons",
99 "--allow-worker",
100 "--allow-child-process",
101 "--disable-warning=SecurityWarning",
102 })
103 })
104
105 c.Run("Disabled", func(c *qt.C) {
106 cfg := security.DefaultConfig
107 cfg.Node.Permissions.Disable = true
108 e := &Exec{
109 sc: cfg,
110 workingDir: site,
111 }
112 args := e.nodePermissionArgs("tailwindcss", "")
113 c.Assert(args, qt.IsNil)
114 })
115
116 c.Run("No fs flags", func(c *qt.C) {
117 cfg := security.DefaultConfig
118 cfg.Node.Permissions.AllowRead = nil
119 cfg.Node.Permissions.AllowAddons = nil
120 cfg.Node.Permissions.AllowWorker = nil
121 cfg.Node.Permissions.AllowChildProcess = nil
122 e := &Exec{
123 sc: cfg,
124 workingDir: site,
125 }
126 args := e.nodePermissionArgs("postcss", "")
127 c.Assert(args, qt.DeepEquals, []string{"--permission"})
128 })
129
130 c.Run("Read only", func(c *qt.C) {
131 cfg := security.DefaultConfig
132 cfg.Node.Permissions.AllowRead = []string{"."}
133 cfg.Node.Permissions.AllowWrite = nil
134 e := &Exec{
135 sc: cfg,
136 workingDir: site,
137 }
138 args := e.nodePermissionArgs("postcss", "")
139 c.Assert(args, qt.DeepEquals, []string{
140 "--permission",
141 "--allow-fs-read=" + site,
142 })
143 })
144
145 c.Run("With additional read paths", func(c *qt.C) {
146 e := &Exec{
147 sc: security.DefaultConfig,
148 workingDir: site,
149 nodeReadPaths: []string{cacheDir},
150 }
151 args := e.nodePermissionArgs("postcss", "")
152 c.Assert(args, qt.DeepEquals, []string{
153 "--permission",
154 "--allow-fs-read=" + site,
155 "--allow-fs-read=" + cacheDir,
156 })
157 })
158
159 c.Run("Global install script path", func(c *qt.C) {
160 e := &Exec{
161 sc: security.DefaultConfig,
162 workingDir: site,
163 }
164 globalNM := filepath.Join(base, "nvm", "lib", "node_modules")
165 script := filepath.Join(globalNM, "postcss-cli", "bin", "postcss")
166 args := e.nodePermissionArgs("postcss", script)
167 c.Assert(args, qt.DeepEquals, []string{
168 "--permission",
169 "--allow-fs-read=" + site,
170 "--allow-fs-read=" + globalNM,
171 })
172 })
173 }
174
175 func TestNodeScriptReadPath(t *testing.T) {
176 c := qt.New(t)
177
178 base := t.TempDir()
179 nm := filepath.Join(base, "node_modules")
180 globalNM := filepath.Join(base, "nvm", "lib", "node_modules")
181
182 c.Assert(nodeScriptReadPath(""), qt.Equals, "")
183 c.Assert(nodeScriptReadPath(filepath.Join(nm, "postcss-cli", "index.js")), qt.Equals, nm)
184 c.Assert(nodeScriptReadPath(filepath.Join(nm, "@babel", "cli", "bin", "babel.js")), qt.Equals, nm)
185 c.Assert(nodeScriptReadPath(filepath.Join(globalNM, "postcss-cli", "bin", "postcss")), qt.Equals, globalNM)
186
187 loose := filepath.Join(base, "tools", "script.js")
188 c.Assert(nodeScriptReadPath(loose), qt.Equals, filepath.Dir(loose))
189 }
190
191 func TestResolveNodeBin(t *testing.T) {
192 c := qt.New(t)
193
194 // Create a fake node_modules structure.
195 dir := t.TempDir()
196 nodeModules := filepath.Join(dir, "node_modules")
197 binDir := filepath.Join(nodeModules, ".bin")
198
199 // Create target JS files.
200 postcssJS := filepath.Join(nodeModules, "postcss-cli", "index.js")
201 babelJS := filepath.Join(nodeModules, "@babel", "cli", "bin", "babel.js")
202 mkdirAndWrite(t, postcssJS, "#!/usr/bin/env node\nconsole.log('postcss');\n")
203 mkdirAndWrite(t, babelJS, "#!/usr/bin/env node\nconsole.log('babel');\n")
204 os.MkdirAll(binDir, 0o755)
205
206 c.Run("Symlink to JS file", func(c *qt.C) {
207 if runtime.GOOS == "windows" {
208 c.Skip("Symlinks may require elevated privileges on Windows")
209 }
210 link := filepath.Join(binDir, "postcss-link")
211 os.Remove(link)
212 c.Assert(os.Symlink(postcssJS, link), qt.IsNil)
213
214 resolved := resolveNodeBin(link)
215 t.Logf("Symlink: link=%q, resolved=%q", link, resolved)
216 c.Assert(resolved, qt.Not(qt.Equals), "")
217 c.Assert(sameFile(t, resolved, postcssJS), qt.IsTrue)
218 })
219
220 c.Run("Symlink to scoped package", func(c *qt.C) {
221 if runtime.GOOS == "windows" {
222 c.Skip("Symlinks may require elevated privileges on Windows")
223 }
224 link := filepath.Join(binDir, "babel-link")
225 os.Remove(link)
226 c.Assert(os.Symlink(babelJS, link), qt.IsNil)
227
228 resolved := resolveNodeBin(link)
229 t.Logf("Scoped symlink: link=%q, resolved=%q", link, resolved)
230 c.Assert(resolved, qt.Not(qt.Equals), "")
231 c.Assert(sameFile(t, resolved, babelJS), qt.IsTrue)
232 })
233
234 c.Run("Shell wrapper", func(c *qt.C) {
235 wrapper := filepath.Join(binDir, "postcss-sh")
236 content := "#!/bin/sh\n" +
237 `basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")` + "\n" +
238 `exec node "$basedir/../postcss-cli/index.js" "$@"` + "\n"
239 mkdirAndWrite(t, wrapper, content)
240
241 resolved := resolveNodeBin(wrapper)
242 t.Logf("Shell wrapper: wrapper=%q, resolved=%q", wrapper, resolved)
243 c.Assert(resolved, qt.Not(qt.Equals), "")
244 c.Assert(sameFile(t, resolved, postcssJS), qt.IsTrue)
245 })
246
247 c.Run("Cmd wrapper", func(c *qt.C) {
248 wrapper := filepath.Join(binDir, "postcss.cmd")
249 content := "@ECHO off\r\n" +
250 "SETLOCAL\r\n" +
251 `endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\postcss-cli\index.js" %*` + "\r\n"
252 mkdirAndWrite(t, wrapper, content)
253
254 resolved := resolveNodeBin(wrapper)
255 t.Logf("Cmd wrapper: wrapper=%q, resolved=%q", wrapper, resolved)
256 c.Assert(resolved, qt.Not(qt.Equals), "")
257 c.Assert(sameFile(t, resolved, postcssJS), qt.IsTrue)
258 })
259
260 c.Run("Cmd wrapper scoped package", func(c *qt.C) {
261 wrapper := filepath.Join(binDir, "babel.cmd")
262 content := "@ECHO off\r\n" +
263 `endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\@babel\cli\bin\babel.js" %*` + "\r\n"
264 mkdirAndWrite(t, wrapper, content)
265
266 resolved := resolveNodeBin(wrapper)
267 t.Logf("Cmd wrapper (scoped): wrapper=%q, resolved=%q", wrapper, resolved)
268 c.Assert(resolved, qt.Not(qt.Equals), "")
269 c.Assert(sameFile(t, resolved, babelJS), qt.IsTrue)
270 })
271
272 c.Run("Node script with shebang", func(c *qt.C) {
273 script := filepath.Join(binDir, "node-global")
274 mkdirAndWrite(t, script, "#!/usr/bin/env node\nconsole.log('global');\n")
275
276 resolved := resolveNodeBin(script)
277 t.Logf("Node script: path=%q, resolved=%q", script, resolved)
278 c.Assert(resolved, qt.Equals, script)
279 })
280
281 c.Run("Native binary", func(c *qt.C) {
282 native := filepath.Join(binDir, "native-tool")
283 mkdirAndWrite(t, native, "\x7fELF\x00\x00\x00")
284
285 resolved := resolveNodeBin(native)
286 t.Logf("Native binary: path=%q, resolved=%q", native, resolved)
287 c.Assert(resolved, qt.Equals, "")
288 })
289
290 c.Run("Nonexistent file", func(c *qt.C) {
291 c.Assert(resolveNodeBin("/nonexistent/path"), qt.Equals, "")
292 })
293
294 c.Run("Wrapper with missing target", func(c *qt.C) {
295 wrapper := filepath.Join(binDir, "missing-target")
296 mkdirAndWrite(t, wrapper, "#!/bin/sh\nexec node \"$basedir/../no-such-pkg/index.js\" \"$@\"\n")
297
298 resolved := resolveNodeBin(wrapper)
299 t.Logf("Missing target: wrapper=%q, resolved=%q", wrapper, resolved)
300 c.Assert(resolved, qt.Equals, "")
301 })
302 }
303
304 func TestExtractNodeEntryPointRegex(t *testing.T) {
305 c := qt.New(t)
306
307 cases := []struct {
308 name string
309 content string
310 want string // expected capture group (with original separators)
311 }{
312 {"shell postcss", `"$basedir/../postcss-cli/index.js"`, "../postcss-cli/index.js"},
313 {"shell babel", `"$basedir/../@babel/cli/bin/babel.js"`, "../@babel/cli/bin/babel.js"},
314 {"shell tailwind mjs", `"$basedir/../@tailwindcss/cli/dist/index.mjs"`, "../@tailwindcss/cli/dist/index.mjs"},
315 {"cmd postcss", `"%dp0%\..\postcss-cli\index.js"`, `..\postcss-cli\index.js`},
316 {"cmd babel", `"%dp0%\..\@babel\cli\bin\babel.js"`, `..\@babel\cli\bin\babel.js`},
317 {"cmd postcss no ext", `"%dp0%\..\postcss-cli\bin\postcss"`, `..\postcss-cli\bin\postcss`},
318 {"shell postcss no ext", `"$basedir/../postcss-cli/bin/postcss"`, "../postcss-cli/bin/postcss"},
319 {"cmd postcss global", `"%dp0%\node_modules\postcss-cli\index.js"`, `node_modules\postcss-cli\index.js`},
320 {"cmd babel global", `"%dp0%\node_modules\@babel\cli\bin\babel.js"`, `node_modules\@babel\cli\bin\babel.js`},
321 {"shell postcss global", `"$basedir/node_modules/postcss-cli/index.js"`, "node_modules/postcss-cli/index.js"},
322 }
323
324 for _, tc := range cases {
325 c.Run(tc.name, func(c *qt.C) {
326 m := nodeEntryPointRe.FindStringSubmatch(tc.content)
327 t.Logf("regex match for %q: %v", tc.name, m)
328 c.Assert(m, qt.Not(qt.IsNil))
329 c.Assert(m[1], qt.Equals, tc.want)
330 })
331 }
332
333 c.Run("No match", func(c *qt.C) {
334 for _, s := range []string{"@ECHO off", "#!/bin/bash\necho hello", "\x7fELF"} {
335 c.Assert(nodeEntryPointRe.FindStringSubmatch(s), qt.IsNil)
336 }
337 })
338 }
339
340 // TestResolveNodeBinWindows tests wrapper resolution on all platforms
341 // by simulating Windows-style wrapper files. On Windows CI, this also
342 // tests the native .cmd resolution path.
343 func TestResolveNodeBinWindows(t *testing.T) {
344 c := qt.New(t)
345
346 dir := t.TempDir()
347 nodeModules := filepath.Join(dir, "node_modules")
348 binDir := filepath.Join(nodeModules, ".bin")
349
350 // Create target JS file.
351 targetJS := filepath.Join(nodeModules, "postcss-cli", "index.js")
352 mkdirAndWrite(t, targetJS, "#!/usr/bin/env node\nconsole.log('postcss');\n")
353 os.MkdirAll(binDir, 0o755)
354
355 // Simulate what npm creates on Windows: a .cmd wrapper and a shell script.
356 cmdWrapper := filepath.Join(binDir, "postcss.cmd")
357 cmdContent := "@ECHO off\r\n" +
358 "SETLOCAL\r\n" +
359 "CALL :find_dp0\r\n" +
360 `endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\postcss-cli\index.js" %*` + "\r\n"
361 mkdirAndWrite(t, cmdWrapper, cmdContent)
362
363 shWrapper := filepath.Join(binDir, "postcss")
364 shContent := "#!/bin/sh\n" +
365 `exec node "$basedir/../postcss-cli/index.js" "$@"` + "\n"
366 mkdirAndWrite(t, shWrapper, shContent)
367
368 t.Logf("GOOS=%s", runtime.GOOS)
369 t.Logf("cmd wrapper: %s", cmdWrapper)
370 t.Logf("sh wrapper: %s", shWrapper)
371 t.Logf("target JS: %s", targetJS)
372
373 c.Run("cmd wrapper resolves to JS", func(c *qt.C) {
374 resolved := resolveNodeBin(cmdWrapper)
375 t.Logf("resolveNodeBin(%q) = %q", cmdWrapper, resolved)
376 c.Assert(resolved, qt.Not(qt.Equals), "")
377 c.Assert(sameFile(t, resolved, targetJS), qt.IsTrue)
378 })
379
380 c.Run("sh wrapper resolves to JS", func(c *qt.C) {
381 resolved := resolveNodeBin(shWrapper)
382 t.Logf("resolveNodeBin(%q) = %q", shWrapper, resolved)
383 c.Assert(resolved, qt.Not(qt.Equals), "")
384 c.Assert(sameFile(t, resolved, targetJS), qt.IsTrue)
385 })
386
387 // Simulate postcss-cli 7, whose wrappers point to an extensionless Node
388 // shebang script (bin/postcss) rather than a .js file.
389 targetNoExt := filepath.Join(nodeModules, "postcss-cli", "bin", "postcss")
390 mkdirAndWrite(t, targetNoExt, "#!/usr/bin/env node\nrequire('../');\n")
391
392 cmdNoExt := filepath.Join(binDir, "postcssne.cmd")
393 mkdirAndWrite(t, cmdNoExt, "@ECHO off\r\n"+
394 `"%dp0%\..\postcss-cli\bin\postcss" %*`+"\r\n")
395
396 shNoExt := filepath.Join(binDir, "postcssne")
397 mkdirAndWrite(t, shNoExt, "#!/bin/sh\n"+
398 `exec node "$basedir/../postcss-cli/bin/postcss" "$@"`+"\n")
399
400 c.Run("cmd wrapper resolves to extensionless script", func(c *qt.C) {
401 resolved := resolveNodeBin(cmdNoExt)
402 t.Logf("resolveNodeBin(%q) = %q", cmdNoExt, resolved)
403 c.Assert(resolved, qt.Not(qt.Equals), "")
404 c.Assert(sameFile(t, resolved, targetNoExt), qt.IsTrue)
405 })
406
407 c.Run("sh wrapper resolves to extensionless script", func(c *qt.C) {
408 resolved := resolveNodeBin(shNoExt)
409 t.Logf("resolveNodeBin(%q) = %q", shNoExt, resolved)
410 c.Assert(resolved, qt.Not(qt.Equals), "")
411 c.Assert(sameFile(t, resolved, targetNoExt), qt.IsTrue)
412 })
413
414 // Simulate `npm install -g` on Windows: the wrapper sits at the npm
415 // global prefix and references node_modules as a child (no `..`).
416 globalDir := filepath.Join(dir, "global")
417 globalTarget := filepath.Join(globalDir, "node_modules", "postcss-cli", "index.js")
418 mkdirAndWrite(t, globalTarget, "#!/usr/bin/env node\nconsole.log('postcss');\n")
419
420 globalCmd := filepath.Join(globalDir, "postcss.cmd")
421 mkdirAndWrite(t, globalCmd, "@ECHO off\r\n"+
422 `endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\node_modules\postcss-cli\index.js" %*`+"\r\n")
423
424 globalSh := filepath.Join(globalDir, "postcss")
425 mkdirAndWrite(t, globalSh, "#!/bin/sh\n"+
426 `exec node "$basedir/node_modules/postcss-cli/index.js" "$@"`+"\n")
427
428 c.Run("global cmd wrapper resolves to JS", func(c *qt.C) {
429 resolved := resolveNodeBin(globalCmd)
430 t.Logf("resolveNodeBin(%q) = %q", globalCmd, resolved)
431 c.Assert(resolved, qt.Not(qt.Equals), "")
432 c.Assert(sameFile(t, resolved, globalTarget), qt.IsTrue)
433 })
434
435 c.Run("global sh wrapper resolves to JS", func(c *qt.C) {
436 resolved := resolveNodeBin(globalSh)
437 t.Logf("resolveNodeBin(%q) = %q", globalSh, resolved)
438 c.Assert(resolved, qt.Not(qt.Equals), "")
439 c.Assert(sameFile(t, resolved, globalTarget), qt.IsTrue)
440 })
441
442 globalScopedTarget := filepath.Join(globalDir, "node_modules", "@babel", "cli", "bin", "babel.js")
443 mkdirAndWrite(t, globalScopedTarget, "#!/usr/bin/env node\nconsole.log('babel');\n")
444 globalScopedCmd := filepath.Join(globalDir, "babel.cmd")
445 mkdirAndWrite(t, globalScopedCmd, "@ECHO off\r\n"+
446 `endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\node_modules\@babel\cli\bin\babel.js" %*`+"\r\n")
447
448 c.Run("global cmd wrapper scoped package", func(c *qt.C) {
449 resolved := resolveNodeBin(globalScopedCmd)
450 t.Logf("resolveNodeBin(%q) = %q", globalScopedCmd, resolved)
451 c.Assert(resolved, qt.Not(qt.Equals), "")
452 c.Assert(sameFile(t, resolved, globalScopedTarget), qt.IsTrue)
453 })
454 }
455
456 func sameFile(t *testing.T, a, b string) bool {
457 t.Helper()
458 infoA, errA := os.Stat(a)
459 infoB, errB := os.Stat(b)
460 if errA != nil || errB != nil {
461 t.Logf("sameFile: stat errors: a=%v, b=%v", errA, errB)
462 return false
463 }
464 return os.SameFile(infoA, infoB)
465 }
466
467 func mkdirAndWrite(t *testing.T, path, content string) {
468 t.Helper()
469 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
470 t.Fatal(err)
471 }
472 if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
473 t.Fatal(err)
474 }
475 }
476
477 func TestCheckNodeSymlinks(t *testing.T) {
478 if runtime.GOOS == "windows" {
479 t.Skip("symlinks")
480 }
481 c := qt.New(t)
482
483 base := t.TempDir()
484 site := filepath.Join(base, "site")
485 outside := filepath.Join(base, "outside")
486 c.Assert(os.MkdirAll(filepath.Join(site, "assets", "css"), 0o755), qt.IsNil)
487 c.Assert(os.MkdirAll(filepath.Join(site, "node_modules", ".bin"), 0o755), qt.IsNil)
488 c.Assert(os.MkdirAll(outside, 0o755), qt.IsNil)
489 c.Assert(os.WriteFile(filepath.Join(outside, "secret.txt"), []byte("secret"), 0o644), qt.IsNil)
490 c.Assert(os.WriteFile(filepath.Join(site, "node_modules", "tool.js"), nil, 0o644), qt.IsNil)
491 c.Assert(os.Symlink(filepath.Join("..", "tool.js"), filepath.Join(site, "node_modules", ".bin", "tool")), qt.IsNil)
492 c.Assert(os.Symlink(filepath.Join(site, "doesnotexist"), filepath.Join(site, "dangling")), qt.IsNil)
493
494 newExec := func(allowRead ...string) *Exec {
495 sc := security.DefaultConfig
496 sc.Node.Permissions.AllowRead = allowRead
497 return New(sc, site, loggers.NewDefault())
498 }
499
500 c.Run("Relative and dangling symlinks inside", func(c *qt.C) {
501 e := newExec(".")
502 c.Assert(e.checkNodeSymlinks("allowRead", e.nodeReadRoots("")), qt.IsNil)
503 })
504
505 c.Assert(os.Symlink(filepath.Join(outside, "secret.txt"), filepath.Join(site, "assets", "css", "slink.css")), qt.IsNil)
506
507 c.Run("Symlink outside", func(c *qt.C) {
508 e := newExec(".")
509 err := e.checkNodeSymlinks("allowRead", e.nodeReadRoots(""))
510 c.Assert(err, qt.ErrorMatches, `symlink ".*slink.css" resolves to ".*secret.txt" outside .*allowRead`)
511 })
512
513 c.Run("Target added to allowRead", func(c *qt.C) {
514 e := newExec(".", outside)
515 c.Assert(e.checkNodeSymlinks("allowRead", e.nodeReadRoots("")), qt.IsNil)
516 })
517
518 c.Run("Wildcard", func(c *qt.C) {
519 e := newExec("*")
520 c.Assert(e.checkNodeSymlinks("allowRead", e.nodeReadRoots("")), qt.IsNil)
521 })
522
523 c.Run("Empty write set", func(c *qt.C) {
524 e := newExec(".")
525 c.Assert(e.checkNodeSymlinks("allowWrite", e.nodeWriteRoots()), qt.IsNil)
526 })
527 }
528
529 func TestTopLevelDirs(t *testing.T) {
530 c := qt.New(t)
531 fs := func(ss ...string) []string {
532 for i, s := range ss {
533 ss[i] = filepath.FromSlash(s)
534 }
535 return ss
536 }
537 c.Assert(topLevelDirs(fs("/a/b", "/a", "/ab", "/a/c", "/a")), qt.DeepEquals, fs("/a", "/ab"))
538 }