aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/initplace_test.go
blob: 02c37336af3839bf72ffd5a0968508c0f7ab91ba (plain)
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
package container

import (
	"os"
	"testing"
)

func TestTmpfileOp(t *testing.T) {
	const sampleDataString = `chronos:x:65534:65534:Hakurei:/var/empty:/bin/zsh`
	var (
		samplePath = MustAbs("/etc/passwd")
		sampleData = []byte(sampleDataString)
	)

	checkOpBehaviour(t, []opBehaviourTestCase{
		{"createTemp", &Params{ParentPerm: 0700}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, nil, nil, []kexpect{
			{"createTemp", expectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), errUnique},
		}, wrapErrSelf(errUnique)},

		{"Write", &Params{ParentPerm: 0700}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, nil, nil, []kexpect{
			{"createTemp", expectArgs{"/", "tmp.*"}, writeErrOsFile{errUnique}, nil},
		}, wrapErrSuffix(errUnique, "cannot write to intermediate file:")},

		{"Close", &Params{ParentPerm: 0700}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, nil, nil, []kexpect{
			{"createTemp", expectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, errUnique), nil},
		}, wrapErrSuffix(errUnique, "cannot close intermediate file:")},

		{"ensureFile", &Params{ParentPerm: 0700}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, nil, nil, []kexpect{
			{"createTemp", expectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
			{"ensureFile", expectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, errUnique},
		}, errUnique},

		{"bindMount", &Params{ParentPerm: 0700}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, nil, nil, []kexpect{
			{"createTemp", expectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
			{"ensureFile", expectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, nil},
			{"bindMount", expectArgs{"tmp.32768", "/sysroot/etc/passwd", uintptr(0x5), false}, nil, errUnique},
		}, errUnique},

		{"remove", &Params{ParentPerm: 0700}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, nil, nil, []kexpect{
			{"createTemp", expectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
			{"ensureFile", expectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, nil},
			{"bindMount", expectArgs{"tmp.32768", "/sysroot/etc/passwd", uintptr(0x5), false}, nil, nil},
			{"remove", expectArgs{"tmp.32768"}, nil, errUnique},
		}, wrapErrSelf(errUnique)},

		{"success", &Params{ParentPerm: 0700}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, nil, nil, []kexpect{
			{"createTemp", expectArgs{"/", "tmp.*"}, newCheckedFile(t, "tmp.32768", sampleDataString, nil), nil},
			{"ensureFile", expectArgs{"/sysroot/etc/passwd", os.FileMode(0444), os.FileMode(0700)}, nil, nil},
			{"bindMount", expectArgs{"tmp.32768", "/sysroot/etc/passwd", uintptr(0x5), false}, nil, nil},
			{"remove", expectArgs{"tmp.32768"}, nil, nil},
		}, nil},
	})

	checkOpsValid(t, []opValidTestCase{
		{"nil", (*TmpfileOp)(nil), false},
		{"zero", new(TmpfileOp), false},
		{"valid", &TmpfileOp{Path: samplePath}, true},
	})

	checkOpsBuilder(t, []opsBuilderTestCase{
		{"noref", new(Ops).Place(samplePath, sampleData), Ops{
			&TmpfileOp{
				Path: samplePath,
				Data: sampleData,
			},
		}},

		{"ref", new(Ops).PlaceP(samplePath, new(*[]byte)), Ops{
			&TmpfileOp{
				Path: samplePath,
				Data: []byte{},
			},
		}},
	})

	checkOpIs(t, []opIsTestCase{
		{"zero", new(TmpfileOp), new(TmpfileOp), false},

		{"differs path", &TmpfileOp{
			Path: MustAbs("/etc/group"),
			Data: sampleData,
		}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, false},

		{"differs data", &TmpfileOp{
			Path: samplePath,
			Data: append(sampleData, 0),
		}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, false},

		{"equals", &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, true},
	})

	checkOpMeta(t, []opMetaTestCase{
		{"passwd", &TmpfileOp{
			Path: samplePath,
			Data: sampleData,
		}, "placing", `tmpfile "/etc/passwd" (49 bytes)`},
	})
}