From 679e719f9e4d9230482491bf253a0d538f030f08 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Thu, 17 Oct 2024 20:28:55 +0900 Subject: system: tests for all Op implementations except DBus Signed-off-by: Ophestra Umiker --- internal/system/mkdir_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 internal/system/mkdir_test.go (limited to 'internal/system/mkdir_test.go') diff --git a/internal/system/mkdir_test.go b/internal/system/mkdir_test.go new file mode 100644 index 00000000..20826940 --- /dev/null +++ b/internal/system/mkdir_test.go @@ -0,0 +1,73 @@ +package system + +import ( + "os" + "testing" +) + +func TestEnsure(t *testing.T) { + testCases := []struct { + name string + perm os.FileMode + }{ + {"/tmp/fortify.1971", 0701}, + {"/tmp/fortify.1971/tmpdir", 0700}, + {"/tmp/fortify.1971/tmpdir/150", 0700}, + {"/run/user/1971/fortify", 0700}, + } + for _, tc := range testCases { + t.Run(tc.name+"_"+tc.perm.String(), func(t *testing.T) { + sys := New(150) + sys.Ensure(tc.name, tc.perm) + (&tcOp{User, tc.name}).test(t, sys.ops, []Op{&Mkdir{User, tc.name, tc.perm, false}}, "Ensure") + }) + } +} + +func TestEphemeral(t *testing.T) { + testCases := []struct { + perm os.FileMode + tcOp + }{ + {0700, tcOp{Process, "/run/user/1971/fortify/ec07546a772a07cde87389afc84ffd13"}}, + {0701, tcOp{Process, "/tmp/fortify.1971/ec07546a772a07cde87389afc84ffd13"}}, + } + for _, tc := range testCases { + t.Run(tc.path+"_"+tc.perm.String()+"_"+TypeString(tc.et), func(t *testing.T) { + sys := New(150) + sys.Ephemeral(tc.et, tc.path, tc.perm) + tc.test(t, sys.ops, []Op{&Mkdir{tc.et, tc.path, tc.perm, true}}, "Ephemeral") + }) + } +} + +func TestMkdir_String(t *testing.T) { + testCases := []struct { + want string + ephemeral bool + et Enablement + }{ + {"Ensure", false, User}, + {"Ensure", false, Process}, + {"Ensure", false, EWayland}, + + {"Wayland", true, EWayland}, + {"X11", true, EX11}, + {"D-Bus", true, EDBus}, + {"PulseAudio", true, EPulse}, + } + for _, tc := range testCases { + t.Run(tc.want, func(t *testing.T) { + m := &Mkdir{ + et: tc.et, + path: "/nonexistent", + perm: 0701, + ephemeral: tc.ephemeral, + } + want := "mode: " + os.FileMode(0701).String() + " type: " + tc.want + " path: \"/nonexistent\"" + if got := m.String(); got != want { + t.Errorf("String() = %v, want %v", got, want) + } + }) + } +} -- cgit v1.3.1