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) } }) } }