aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/outcome/spaccount_test.go
blob: fb20d8df4ee778bfd5c2eca7d80ca5615315b665 (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
package outcome

import (
	"os"
	"syscall"
	"testing"

	"hakurei.app/container"
	"hakurei.app/hst"
	"hakurei.app/internal/stub"
)

func TestSpAccountOp(t *testing.T) {
	t.Parallel()
	config := hst.Template()

	checkOpBehaviour(t, []opBehaviourTestCase{
		{"invalid state", func(bool, bool) outcomeOp { return spAccountOp{} }, func() *hst.Config {
			c := hst.Template()
			c.Container.Shell = nil
			return c
		}, nil, []stub.Call{
			// this op performs basic validation and does not make calls during toSystem
		}, nil, nil, syscall.ENOTRECOVERABLE, nil, nil, nil, nil, nil},

		{"invalid user name", func(bool, bool) outcomeOp { return spAccountOp{} }, func() *hst.Config {
			c := hst.Template()
			c.Container.Username = "9"
			return c
		}, nil, []stub.Call{
			// this op performs basic validation and does not make calls during toSystem
		}, nil, nil, &hst.AppError{
			Step: "finalise",
			Err:  os.ErrInvalid,
			Msg:  `invalid user name "9"`,
		}, nil, nil, nil, nil, nil},

		{"success fallback username", func(bool, bool) outcomeOp { return spAccountOp{} }, func() *hst.Config {
			c := hst.Template()
			c.Container.Username = ""
			c.Container.Flags = hst.FMapRealUID
			return c
		}, nil, []stub.Call{
			// this op performs basic validation and does not make calls during toSystem
		}, newI(), nil, nil, insertsOps(nil), []stub.Call{
			// this op configures the container state and does not make calls during toContainer
		}, &container.Params{
			Dir: config.Container.Home,
			Ops: new(container.Ops).
				Place(m("/etc/passwd"), []byte("chronos:x:1000:100:Hakurei:/data/data/org.chromium.Chromium:/run/current-system/sw/bin/zsh\n")).
				Place(m("/etc/group"), []byte("hakurei:x:100:\n")),
		}, paramsWantEnv(config, map[string]string{
			"HOME":  config.Container.Home.String(),
			"USER":  config.Container.Username,
			"SHELL": config.Container.Shell.String(),
		}, nil), nil},

		{"success", func(bool, bool) outcomeOp { return spAccountOp{} }, hst.Template, nil, []stub.Call{
			// this op performs basic validation and does not make calls during toSystem
		}, newI(), nil, nil, insertsOps(nil), []stub.Call{
			// this op configures the container state and does not make calls during toContainer
		}, &container.Params{
			Dir: config.Container.Home,
			Ops: new(container.Ops),
		}, paramsWantEnv(config, map[string]string{
			"HOME":  config.Container.Home.String(),
			"USER":  config.Container.Username,
			"SHELL": config.Container.Shell.String(),
		}, nil), nil},
	})
}