Debian Codegraph @

mtreefs_test.go

raw
1 package desync
2
3 import (
4 "bytes"
5 "os"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func TestMtreeFilename(t *testing.T) {
15 cases := []struct {
16 in string
17 want string
18 }{
19 {"abc", "abc"},
20 {"name with space", `name\040with\040space`},
21 {`a\b`, `a\134b`},
22 {"a#b", `a\043b`},
23 {"a\tb", `a\011b`},
24 {"a\nb", `a\012b`},
25 {"a\x7fb", `a\177b`},
26 {"é", `\303\251`}, // UTF-8 bytes, each > 126
27 }
28 for _, c := range cases {
29 assert.Equal(t, c.want, mtreeFilename(c.in), "mtreeFilename(%q)", c.in)
30 }
31 }
32
33 // TestMtreeFSNoKeywordInjection verifies that an entry name containing a space
34 // cannot inject mtree(5) keywords. mtree lines are space-delimited
35 // keyword=value pairs after the filename, so an unescaped space in
36 // "usr ignore" would make mtree(8) read "ignore" as a value-less keyword that
37 // suppresses verification of the whole subtree.
38 func TestMtreeFSNoKeywordInjection(t *testing.T) {
39 var buf bytes.Buffer
40 fs, err := NewMtreeFS(&buf)
41 require.NoError(t, err)
42
43 require.NoError(t, fs.CreateDir(NodeDirectory{
44 Name: "usr ignore",
45 Mode: os.ModeDir | 0755,
46 MTime: time.Unix(0, 0),
47 }))
48
49 // Grab the line describing the directory (skip the "#mtree v1.0" header).
50 var line string
51 for _, l := range strings.Split(buf.String(), "\n") {
52 if strings.HasPrefix(l, "#") || l == "" {
53 continue
54 }
55 line = l
56 break
57 }
58 require.NotEmpty(t, line)
59
60 fields := strings.Split(line, " ")
61 assert.Equal(t, `usr\040ignore`, fields[0], "filename must be a single escaped field")
62 for _, f := range fields[1:] {
63 assert.NotEqual(t, "ignore", f, "injected keyword must not appear as its own field")
64 }
65 }