aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/sharefs/fuse_test.go
blob: e760cf758ac4f22692f6ff28fe7f4260f67488b1 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main

import (
	"bytes"
	"log"
	"reflect"
	"testing"

	"hakurei.app/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)
			}
		})
	}
}