aboutsummaryrefslogtreecommitdiffhomepage
path: root/system/tmpfiles_test.go
blob: aa7a7e96955147b7106ab58065bbf91f1057b0d3 (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
132
133
134
135
136
137
138
139
140
141
142
package system

import (
	"bytes"
	"errors"
	"os"
	"strings"
	"syscall"
	"testing"

	"hakurei.app/container/stub"
)

type errorReader struct{}

func (errorReader) Read([]byte) (int, error) { return 0, stub.UniqueError(0xdeadbeef) }

func TestTmpfileOp(t *testing.T) {
	// 255 bytes
	const paSample = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

	checkOpBehaviour(t, []opBehaviourTestCase{
		{"payload", 0xdead, 0xff, &tmpfileOp{
			nil, "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, nil, errors.New("invalid payload"), nil, nil},

		{"stat", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, nil, stub.UniqueError(1)),
		}, &OpError{Op: "tmpfile", Err: stub.UniqueError(1)}, nil, nil},

		{"stat EISDIR", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, stubFi{1 << 8, true}, nil),
		}, &OpError{Op: "tmpfile", Err: &os.PathError{Op: "stat", Path: "/home/ophestra/xdg/config/pulse/cookie", Err: syscall.EISDIR}}, nil, nil},

		{"stat ENOMEM", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, stubFi{1<<8 + 1, false}, nil),
		}, &OpError{Op: "tmpfile", Err: &os.PathError{Op: "stat", Path: "/home/ophestra/xdg/config/pulse/cookie", Err: syscall.ENOMEM}}, nil, nil},

		{"open", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, stubFi{1 << 8, false}, nil),
			call("open", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, nil, stub.UniqueError(0)),
		}, &OpError{Op: "tmpfile", Err: stub.UniqueError(0)}, nil, nil},

		{"reader", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, stubFi{1 << 8, false}, nil),
			call("open", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, &readerOsFile{true, errorReader{}}, nil),
		}, &OpError{Op: "tmpfile", Err: stub.UniqueError(0xdeadbeef)}, nil, nil},

		{"closed", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, stubFi{1 << 8, false}, nil),
			call("open", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, &readerOsFile{true, strings.NewReader(paSample + "=")}, nil),
		}, &OpError{Op: "tmpfile", Err: os.ErrClosed}, nil, nil},

		{"success full", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, stubFi{1 << 8, false}, nil),
			call("open", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, &readerOsFile{false, strings.NewReader(paSample + "=")}, nil),
		}, nil, nil, nil},

		{"success", 0xdead, 0xff, &tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}, []stub.Call{
			call("verbose", stub.ExpectArgs{[]any{"copying", &tmpfileOp{new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }()}}}, nil, nil),
			call("stat", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, stubFi{1 << 8, false}, nil),
			call("open", stub.ExpectArgs{"/home/ophestra/xdg/config/pulse/cookie"}, &readerOsFile{false, strings.NewReader(paSample)}, nil),
			call("verbosef", stub.ExpectArgs{"copied %d bytes from %q", []any{int64(1<<8 - 1), "/home/ophestra/xdg/config/pulse/cookie"}}, nil, nil),
		}, nil, nil, nil},
	})

	checkOpsBuilder(t, "CopyFile", []opsBuilderTestCase{
		{"pulse", 0xcafebabe, func(_ *testing.T, sys *I) {
			sys.CopyFile(new([]byte), m("/home/ophestra/xdg/config/pulse/cookie"), 1<<8, 1<<8)
		}, []Op{&tmpfileOp{
			new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 1 << 8,
			func() *bytes.Buffer { buf := new(bytes.Buffer); buf.Grow(1 << 8); return buf }(),
		}}, stub.Expect{}},
	})

	checkOpIs(t, []opIsTestCase{
		{"nil", (*tmpfileOp)(nil), (*tmpfileOp)(nil), false},
		{"zero", new(tmpfileOp), new(tmpfileOp), true},

		{"n differs", &tmpfileOp{
			src: "/home/ophestra/xdg/config/pulse/cookie",
			n:   1 << 7,
		}, &tmpfileOp{
			src: "/home/ophestra/xdg/config/pulse/cookie",
			n:   1 << 8,
		}, false},

		{"src differs", &tmpfileOp{
			src: "/home/ophestra/xdg/config/pulse",
			n:   1 << 8,
		}, &tmpfileOp{
			src: "/home/ophestra/xdg/config/pulse/cookie",
			n:   1 << 8,
		}, false},

		{"equals", &tmpfileOp{
			src: "/home/ophestra/xdg/config/pulse/cookie",
			n:   1 << 8,
		}, &tmpfileOp{
			src: "/home/ophestra/xdg/config/pulse/cookie",
			n:   1 << 8,
		}, true},
	})

	checkOpMeta(t, []opMetaTestCase{
		{"pulse", &tmpfileOp{nil, "/home/ophestra/xdg/config/pulse/cookie", 1 << 8, nil},
			Process, "/home/ophestra/xdg/config/pulse/cookie",
			`up to 256 bytes from "/home/ophestra/xdg/config/pulse/cookie"`},
	})
}