aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/app/spaccount_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-10-11 00:54:04 +0900
committerOphestra <cat@gensokyo.uk>2025-10-11 00:54:04 +0900
commit9290748761b9c91c9e379ff2774fefa58bd1b533 (patch)
treeb93add26711da6eec91e5eedc75a4f6b4366533a /internal/app/spaccount_test.go
parent23084888a028aec7e75a73c5912331cc60391297 (diff)
internal/app/spaccount: check behaviour
This begins the effort of fully covering internal/app. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/app/spaccount_test.go')
-rw-r--r--internal/app/spaccount_test.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/internal/app/spaccount_test.go b/internal/app/spaccount_test.go
new file mode 100644
index 00000000..8d96b4bc
--- /dev/null
+++ b/internal/app/spaccount_test.go
@@ -0,0 +1,90 @@
+package app
+
+import (
+ "maps"
+ "os"
+ "syscall"
+ "testing"
+
+ "hakurei.app/container"
+ "hakurei.app/container/stub"
+ "hakurei.app/hst"
+ "hakurei.app/message"
+ "hakurei.app/system"
+)
+
+func TestSpAccountOp(t *testing.T) {
+ config := hst.Template()
+
+ checkOpBehaviour(t, []opBehaviourTestCase{
+ {"invalid state", func() 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
+ }, spAccountOp{}, system.New(t.Context(), message.NewMsg(nil), checkExpectUid), nil, syscall.ENOTRECOVERABLE, nil, nil, nil, nil, nil, nil},
+
+ {"invalid user name", func() 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
+ }, spAccountOp{}, nil, nil, &hst.AppError{
+ Step: "finalise",
+ Err: os.ErrInvalid,
+ Msg: `invalid user name "9"`,
+ }, nil, nil, nil, nil, nil, nil},
+
+ {"success fallback username", func() outcomeOp { return spAccountOp{} }, func() *hst.Config {
+ c := hst.Template()
+ c.Container.Username = ""
+ return c
+ }, nil, []stub.Call{
+ // this op performs basic validation and does not make calls during toSystem
+ }, spAccountOp{}, system.New(t.Context(), message.NewMsg(nil), checkExpectUid), nil, nil, func(state *outcomeStateParams) {
+ state.params.Ops = new(container.Ops)
+ }, []stub.Call{
+ // this op configures the container state and does not make calls during toContainer
+ }, spAccountOp{}, &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")),
+ }, func(t *testing.T, state *outcomeStateParams) {
+ wantEnv := map[string]string{
+ "HOME": config.Container.Home.String(),
+ "USER": config.Container.Username,
+ "SHELL": config.Container.Shell.String(),
+ }
+ maps.Copy(wantEnv, config.Container.Env)
+ if !maps.Equal(state.env, wantEnv) {
+ t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv)
+ }
+ }, nil},
+
+ {"success", func() outcomeOp { return spAccountOp{} }, hst.Template, nil, []stub.Call{
+ // this op performs basic validation and does not make calls during toSystem
+ }, spAccountOp{}, system.New(t.Context(), message.NewMsg(nil), checkExpectUid), nil, nil, func(state *outcomeStateParams) {
+ state.params.Ops = new(container.Ops)
+ }, []stub.Call{
+ // this op configures the container state and does not make calls during toContainer
+ }, spAccountOp{}, &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")),
+ }, func(t *testing.T, state *outcomeStateParams) {
+ wantEnv := map[string]string{
+ "HOME": config.Container.Home.String(),
+ "USER": config.Container.Username,
+ "SHELL": config.Container.Shell.String(),
+ }
+ maps.Copy(wantEnv, config.Container.Env)
+ if !maps.Equal(state.env, wantEnv) {
+ t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv)
+ }
+ }, nil},
+ })
+}