File: node_xattr_all_test.go

package info (click to toggle)
restic 0.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,824 kB
  • sloc: sh: 3,704; makefile: 50; python: 34
file content (201 lines) | stat: -rw-r--r-- 5,256 bytes parent folder | download | duplicates (2)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//go:build darwin || freebsd || netbsd || linux || solaris || windows
// +build darwin freebsd netbsd linux solaris windows

package fs

import (
	"bytes"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"

	"github.com/restic/restic/internal/filter"
	"github.com/restic/restic/internal/restic"
	rtest "github.com/restic/restic/internal/test"
)

func setAndVerifyXattr(t *testing.T, file string, attrs []restic.ExtendedAttribute) {
	if runtime.GOOS == "windows" {
		// windows seems to convert the xattr name to upper case
		for i := range attrs {
			attrs[i].Name = strings.ToUpper(attrs[i].Name)
		}
	}

	node := &restic.Node{
		Type:               restic.NodeTypeFile,
		ExtendedAttributes: attrs,
	}
	/* restore all xattrs */
	rtest.OK(t, nodeRestoreExtendedAttributes(node, file, func(_ string) bool { return true }))

	nodeActual := &restic.Node{
		Type: restic.NodeTypeFile,
	}
	rtest.OK(t, nodeFillExtendedAttributes(nodeActual, file, false))

	rtest.Assert(t, nodeActual.Equals(*node), "xattr mismatch got %v expected %v", nodeActual.ExtendedAttributes, node.ExtendedAttributes)
}

func setAndVerifyXattrWithSelectFilter(t *testing.T, file string, testAttr []testXattrToRestore, xattrSelectFilter func(_ string) bool) {
	attrs := make([]restic.ExtendedAttribute, len(testAttr))
	for i := range testAttr {
		// windows seems to convert the xattr name to upper case
		if runtime.GOOS == "windows" {
			testAttr[i].xattr.Name = strings.ToUpper(testAttr[i].xattr.Name)
		}
		attrs[i] = testAttr[i].xattr
	}

	node := &restic.Node{
		Type:               restic.NodeTypeFile,
		ExtendedAttributes: attrs,
	}

	rtest.OK(t, nodeRestoreExtendedAttributes(node, file, xattrSelectFilter))

	nodeActual := &restic.Node{
		Type: restic.NodeTypeFile,
	}
	rtest.OK(t, nodeFillExtendedAttributes(nodeActual, file, false))

	// Check nodeActual to make sure only xattrs we expect are there
	for _, testAttr := range testAttr {
		xattrFound := false
		xattrRestored := false
		for _, restoredAttr := range nodeActual.ExtendedAttributes {
			if restoredAttr.Name == testAttr.xattr.Name {
				xattrFound = true
				xattrRestored = bytes.Equal(restoredAttr.Value, testAttr.xattr.Value)
				break
			}
		}
		if testAttr.shouldRestore {
			rtest.Assert(t, xattrFound, "xattr %s not restored", testAttr.xattr.Name)
			rtest.Assert(t, xattrRestored, "xattr %v value not restored", testAttr.xattr)
		} else {
			rtest.Assert(t, !xattrFound, "xattr %v should not have been restored", testAttr.xattr)
		}
	}
}

type testXattrToRestore struct {
	xattr         restic.ExtendedAttribute
	shouldRestore bool
}

func TestOverwriteXattr(t *testing.T) {
	dir := t.TempDir()
	file := filepath.Join(dir, "file")
	rtest.OK(t, os.WriteFile(file, []byte("hello world"), 0o600))

	setAndVerifyXattr(t, file, []restic.ExtendedAttribute{
		{
			Name:  "user.foo",
			Value: []byte("bar"),
		},
	})

	setAndVerifyXattr(t, file, []restic.ExtendedAttribute{
		{
			Name:  "user.other",
			Value: []byte("some"),
		},
	})
}

func uppercaseOnWindows(patterns []string) []string {
	// windows seems to convert the xattr name to upper case
	if runtime.GOOS == "windows" {
		out := []string{}
		for _, pattern := range patterns {
			out = append(out, strings.ToUpper(pattern))
		}
		return out
	}
	return patterns
}

func TestOverwriteXattrWithSelectFilter(t *testing.T) {
	dir := t.TempDir()
	file := filepath.Join(dir, "file2")
	rtest.OK(t, os.WriteFile(file, []byte("hello world"), 0o600))

	noopWarnf := func(_ string, _ ...interface{}) {}

	// Set a filter as if the user passed in --include-xattr user.*
	xattrSelectFilter1 := func(xattrName string) bool {
		shouldInclude, _ := filter.IncludeByPattern(uppercaseOnWindows([]string{"user.*"}), noopWarnf)(xattrName)
		return shouldInclude
	}

	setAndVerifyXattrWithSelectFilter(t, file, []testXattrToRestore{
		{
			xattr: restic.ExtendedAttribute{
				Name:  "user.foo",
				Value: []byte("bar"),
			},
			shouldRestore: true,
		},
		{
			xattr: restic.ExtendedAttribute{
				Name:  "user.test",
				Value: []byte("testxattr"),
			},
			shouldRestore: true,
		},
		{
			xattr: restic.ExtendedAttribute{
				Name:  "security.other",
				Value: []byte("testing"),
			},
			shouldRestore: false,
		},
	}, xattrSelectFilter1)

	// Set a filter as if the user passed in --include-xattr user.*
	xattrSelectFilter2 := func(xattrName string) bool {
		shouldInclude, _ := filter.IncludeByPattern(uppercaseOnWindows([]string{"user.o*", "user.comm*"}), noopWarnf)(xattrName)
		return shouldInclude
	}

	setAndVerifyXattrWithSelectFilter(t, file, []testXattrToRestore{
		{
			xattr: restic.ExtendedAttribute{
				Name:  "user.other",
				Value: []byte("some"),
			},
			shouldRestore: true,
		},
		{
			xattr: restic.ExtendedAttribute{
				Name:  "security.other",
				Value: []byte("testing"),
			},
			shouldRestore: false,
		},
		{
			xattr: restic.ExtendedAttribute{
				Name:  "user.open",
				Value: []byte("door"),
			},
			shouldRestore: true,
		},
		{
			xattr: restic.ExtendedAttribute{
				Name:  "user.common",
				Value: []byte("testing"),
			},
			shouldRestore: true,
		},
		{
			xattr: restic.ExtendedAttribute{
				Name:  "user.bad",
				Value: []byte("dontincludeme"),
			},
			shouldRestore: false,
		},
	}, xattrSelectFilter2)
}