-
Notifications
You must be signed in to change notification settings - Fork 125
/
permissions_test.go
102 lines (94 loc) · 2.82 KB
/
permissions_test.go
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
//go:build !windows
package gosnowflake
import (
"fmt"
"golang.org/x/sys/unix"
"os"
"path"
"testing"
)
func TestConfigPermissions(t *testing.T) {
testCases := []struct {
filePerm int
isValid bool
}{
{filePerm: 0700, isValid: true},
{filePerm: 0600, isValid: true},
{filePerm: 0500, isValid: true},
{filePerm: 0400, isValid: true},
{filePerm: 0300, isValid: true},
{filePerm: 0200, isValid: true},
{filePerm: 0100, isValid: true},
{filePerm: 0707, isValid: false},
{filePerm: 0706, isValid: false},
{filePerm: 0705, isValid: true},
{filePerm: 0704, isValid: true},
{filePerm: 0703, isValid: false},
{filePerm: 0702, isValid: false},
{filePerm: 0701, isValid: true},
{filePerm: 0770, isValid: false},
{filePerm: 0760, isValid: false},
{filePerm: 0750, isValid: true},
{filePerm: 0740, isValid: true},
{filePerm: 0730, isValid: false},
{filePerm: 0720, isValid: false},
{filePerm: 0710, isValid: true},
}
oldMask := unix.Umask(0000)
defer unix.Umask(oldMask)
for _, tc := range testCases {
t.Run(fmt.Sprintf("0%o", tc.filePerm), func(t *testing.T) {
tempFile := path.Join(t.TempDir(), fmt.Sprintf("filePerm_%o", tc.filePerm))
err := os.WriteFile(tempFile, nil, os.FileMode(tc.filePerm))
assertNilE(t, err)
defer os.Remove(tempFile)
err = validateCfgPerm(tempFile)
if err != nil && tc.isValid {
t.Error(err)
}
})
}
}
func TestLogDirectoryPermissions(t *testing.T) {
testCases := []struct {
dirPerm int
limitedToUser bool
}{
{dirPerm: 0700, limitedToUser: true},
{dirPerm: 0600, limitedToUser: false},
{dirPerm: 0500, limitedToUser: false},
{dirPerm: 0400, limitedToUser: false},
{dirPerm: 0300, limitedToUser: false},
{dirPerm: 0200, limitedToUser: false},
{dirPerm: 0100, limitedToUser: false},
{dirPerm: 0707, limitedToUser: false},
{dirPerm: 0706, limitedToUser: false},
{dirPerm: 0705, limitedToUser: false},
{dirPerm: 0704, limitedToUser: false},
{dirPerm: 0703, limitedToUser: false},
{dirPerm: 0702, limitedToUser: false},
{dirPerm: 0701, limitedToUser: false},
{dirPerm: 0770, limitedToUser: false},
{dirPerm: 0760, limitedToUser: false},
{dirPerm: 0750, limitedToUser: false},
{dirPerm: 0740, limitedToUser: false},
{dirPerm: 0730, limitedToUser: false},
{dirPerm: 0720, limitedToUser: false},
{dirPerm: 0710, limitedToUser: false},
}
oldMask := unix.Umask(0000)
defer unix.Umask(oldMask)
for _, tc := range testCases {
t.Run(fmt.Sprintf("0%o", tc.dirPerm), func(t *testing.T) {
tempDir := path.Join(t.TempDir(), fmt.Sprintf("filePerm_%o", tc.dirPerm))
err := os.Mkdir(tempDir, os.FileMode(tc.dirPerm))
assertNilE(t, err)
defer os.Remove(tempDir)
result, _, err := isDirAccessCorrect(tempDir)
if err != nil && tc.limitedToUser {
t.Error(err)
}
assertEqualE(t, result, tc.limitedToUser)
})
}
}