diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-12-27 17:33:12 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-12-27 17:33:12 +0900 |
| commit | 775a9f57c9ec3bc6877e3973a8c4972558707375 (patch) | |
| tree | 7410791c10ca3d8adfd8a17914ef799d55be1b5a /cmd/sharefs/fuse_test.go | |
| parent | 2f8ca8337633a199aae9b4cdd71c66ed1c9529a5 (diff) | |
cmd/sharefs: check option parsing behaviour
This change makes it possible to check parseOpts behaviour as part of Go tests.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'cmd/sharefs/fuse_test.go')
| -rw-r--r-- | cmd/sharefs/fuse_test.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/cmd/sharefs/fuse_test.go b/cmd/sharefs/fuse_test.go new file mode 100644 index 00000000..d25bc888 --- /dev/null +++ b/cmd/sharefs/fuse_test.go @@ -0,0 +1,113 @@ +package main + +import ( + "bytes" + "log" + "reflect" + "testing" + + "hakurei.app/container/check" +) + +func TestParseOpts(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + args []string + want setupState + wantLog string + wantOk bool + }{ + {"zero length", []string{}, setupState{}, "", false}, + + {"not absolute", []string{"sharefs", + "-o", "source=nonexistent", + "-o", "setuid=1023", + "-o", "setgid=1023", + }, setupState{}, "sharefs: path \"nonexistent\" is not absolute\n", false}, + + {"not specified", []string{"sharefs", + "-o", "setuid=1023", + "-o", "setgid=1023", + }, setupState{}, "", false}, + + {"invalid setuid", []string{"sharefs", + "-o", "source=/proc/nonexistent", + "-o", "setuid=ff", + "-o", "setgid=1023", + }, setupState{ + Source: check.MustAbs("/proc/nonexistent"), + }, "sharefs: invalid value for option setuid\n", false}, + + {"invalid setgid", []string{"sharefs", + "-o", "source=/proc/nonexistent", + "-o", "setuid=1023", + "-o", "setgid=ff", + }, setupState{ + Source: check.MustAbs("/proc/nonexistent"), + Setuid: 1023, + }, "sharefs: invalid value for option setgid\n", false}, + + {"simple", []string{"sharefs", + "-o", "source=/proc/nonexistent", + }, setupState{ + Source: check.MustAbs("/proc/nonexistent"), + Setuid: -1, + Setgid: -1, + }, "", true}, + + {"root", []string{"sharefs", + "-o", "source=/proc/nonexistent", + "-o", "setuid=1023", + "-o", "setgid=1023", + }, setupState{ + Source: check.MustAbs("/proc/nonexistent"), + Setuid: 1023, + Setgid: 1023, + }, "", true}, + + {"setuid", []string{"sharefs", + "-o", "source=/proc/nonexistent", + "-o", "setuid=1023", + }, setupState{ + Source: check.MustAbs("/proc/nonexistent"), + Setuid: 1023, + Setgid: -1, + }, "", true}, + + {"setgid", []string{"sharefs", + "-o", "source=/proc/nonexistent", + "-o", "setgid=1023", + }, setupState{ + Source: check.MustAbs("/proc/nonexistent"), + Setuid: -1, + Setgid: 1023, + }, "", true}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + var ( + got setupState + buf bytes.Buffer + ) + args := copyArgs(tc.args...) + defer freeArgs(&args) + unsafeAddArgument(&args, "-odefault_permissions\x00") + + if ok := parseOpts(&args, &got, log.New(&buf, "sharefs: ", 0)); ok != tc.wantOk { + t.Errorf("parseOpts: ok = %v, want %v", ok, tc.wantOk) + } + + if !reflect.DeepEqual(&got, &tc.want) { + t.Errorf("parseOpts: setup = %#v, want %#v", got, tc.want) + } + + if buf.String() != tc.wantLog { + t.Errorf("parseOpts: log =\n%s\nwant\n%s", buf.String(), tc.wantLog) + } + }) + } +} |
