summaryrefslogtreecommitdiff
path: root/internal/io/fs/permissions/permission_test.go
blob: 3d9f8e394e2dadc26e73078e9dea4761e7f07779 (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
//go:build linuxacl

package permissions

import (
	"os"
	"os/exec"
	"os/user"
	"strings"
	"testing"
)

const (
	setfacl string = "/usr/bin/setfacl"
	file    string = "/tmp/acltest"
)

func TestLinuxACL(t *testing.T) {
	setfacl := "/usr/bin/setfacl"
	file := "/tmp/acltest"

	// Delete file if it exists.
	if _, err := os.Stat(file); err == nil {
		os.Remove(file)
	}

	f, err := os.Create(file)
	if err != nil {
		t.Errorf("%v", err)
	}
	defer func() {
		f.Close()
		//os.Remove(file)
	}()

	user, err := user.Current()
	if err != nil {
		t.Errorf("Unable to retrieve current user: %v", err)
	}

	// Test 1: Remove all permissions and perform a permission check
	cmd := exec.Command(setfacl, "-b", "-m", "u::---,g::---,o::---", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}
	if ok, _ := ToRead(user.Username, file); ok {
		t.Errorf("Didn't expect permissions to read file!")
	}

	// Test 2: Add read permission to file owner
	cmd = exec.Command(setfacl, "-b", "-m", "u::r--,g::---,o::---", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}
	if ok, err := ToRead(user.Username, file); !ok {
		t.Errorf("Expected permissions to read file: %v", err)
	}

	// Test 3: Add read permission to file group
	cmd = exec.Command(setfacl, "-b", "-m", "u::---,g::r--,o::---", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}
	if ok, err := ToRead(user.Username, file); !ok {
		t.Errorf("Expected permissions to read file: %v", err)
	}

	// Test 4: Add read permission to others
	cmd = exec.Command(setfacl, "-b", "-m", "u::---,g::---,o::r--", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}

	if ok, err := ToRead(user.Username, file); !ok {
		t.Errorf("Expected permissions to read file: %v", err)
	}

	// Test 5: Remove read permission from mask
	cmd = exec.Command(setfacl, "-m", "m::---", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}
	if ok, _ := ToRead(user.Username, file); ok {
		t.Errorf("Didn't expect permissions to read file!")
	}
	cmd = exec.Command(setfacl, "-m", "m::r--", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}

	// Test 6: Add read permission to specific group
	cmd = exec.Command(setfacl, "-b", "-m", "u::---,g:"+user.Username+":r--,o::---", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}
	if ok, err := ToRead(user.Username, file); !ok {
		t.Errorf("Expected permissions to read file for user %v: %v", user.Username, err)
	}

	// Test 7: Remove all permissions but mask
	cmd = exec.Command(setfacl, "-b", "-m", "u::---,g::---,o::---", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}
	cmd = exec.Command(setfacl, "-m", "m::r--", file)
	if err := cmd.Run(); err != nil {
		t.Errorf("%s -> %v", strings.Join(cmd.Args, " "), err)
	}
	if ok, _ := ToRead(user.Username, file); ok {
		t.Errorf("Didn't expect permissions to read file!")
	}
}