AdGuardHome/internal/aghos/permission_windows_internal_test.go

136 lines
3.1 KiB
Go
Raw Permalink Normal View History

Pull request 2294: AGDNS-2455 Windows permissions Closes #7314. Squashed commit of the following: commit f8b6ffeec2f0f96c947cf896c75d05efaca77caf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Oct 29 14:14:41 2024 +0300 all: fix chlog commit 9417b7dc510296c096f234e2f340dad5a6faf627 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 19:41:30 2024 +0300 aghos: imp doc commit b91f0e72a70a8e1392bd07b50714d8b83cc4e33e Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 19:26:15 2024 +0300 all: rm bin commit 9008ee93b181794c5082894bfa5ce4c76153f93d Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 18:23:54 2024 +0300 all: revert permcheck commit bcc85d50f5f39269713979c6509a9acd220570b8 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 17:48:55 2024 +0300 all: use aghos more commit 993e351712fbf004a6f96e06061ba2321c1c46e1 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 16:24:56 2024 +0300 all: fix more bugs commit a22b0d265eb0fa747e136363558b97de54e593b8 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 18:30:52 2024 +0300 all: fix bugs commit a2309f812ad3fd83d26c373b67756ea3074f4854 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 17:05:08 2024 +0300 all: fix chlog, imp api commit 42c3f8e91c49998068bc208166de20efe49c3dcb Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 16:04:47 2024 +0300 scripts: fix docs commit 9e781ff18db58ed9be35e259ecf3c669a4d41e02 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 16:03:19 2024 +0300 scripts: imp docs commit 1dbc7849828cc4933bb5edc3257f158ac292d48e Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 15:55:16 2024 +0300 all: use new functions, add tests commit dcbabaf4e37149a73969c52c9bfac2b9d9127a67 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 13:23:50 2024 +0300 aghos: add stat commit 72d7c0f881835725e65db63ac2dd1c5f7a409036 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 24 17:10:30 2024 +0300 aghos: add windows functions
2024-10-29 14:28:59 +03:00
//go:build windows
package aghos
import (
"io/fs"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/windows"
)
func TestPermToMasks(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
perm fs.FileMode
wantUser windows.ACCESS_MASK
wantGroup windows.ACCESS_MASK
wantOther windows.ACCESS_MASK
isDir bool
}{{
name: "all",
perm: 0b111_111_111,
wantUser: fileReadRights | fileWriteRights | fileExecuteRights,
wantGroup: fileReadRights | fileWriteRights | fileExecuteRights,
wantOther: fileReadRights | fileWriteRights | fileExecuteRights,
isDir: false,
}, {
name: "user_write",
perm: 0b010_000_000,
wantUser: fileWriteRights,
wantGroup: 0,
wantOther: 0,
isDir: false,
}, {
name: "group_read",
perm: 0b000_100_000,
wantUser: 0,
wantGroup: fileReadRights,
wantOther: 0,
isDir: false,
}, {
name: "all_dir",
perm: 0b111_111_111,
wantUser: dirReadRights | dirWriteRights | dirExecuteRights,
wantGroup: dirReadRights | dirWriteRights | dirExecuteRights,
wantOther: dirReadRights | dirWriteRights | dirExecuteRights,
isDir: true,
}, {
name: "user_write_dir",
perm: 0b010_000_000,
wantUser: dirWriteRights,
wantGroup: 0,
wantOther: 0,
isDir: true,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
user, group, other := permToMasks(tc.perm, tc.isDir)
assert.Equal(t, tc.wantUser, user)
assert.Equal(t, tc.wantGroup, group)
assert.Equal(t, tc.wantOther, other)
})
}
}
func TestMasksToPerm(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
user windows.ACCESS_MASK
group windows.ACCESS_MASK
other windows.ACCESS_MASK
wantPerm fs.FileMode
isDir bool
}{{
name: "all",
user: fileReadRights | fileWriteRights | fileExecuteRights,
group: fileReadRights | fileWriteRights | fileExecuteRights,
other: fileReadRights | fileWriteRights | fileExecuteRights,
wantPerm: 0b111_111_111,
isDir: false,
}, {
name: "user_write",
user: fileWriteRights,
group: 0,
other: 0,
wantPerm: 0b010_000_000,
isDir: false,
}, {
name: "group_read",
user: 0,
group: fileReadRights,
other: 0,
wantPerm: 0b000_100_000,
isDir: false,
}, {
name: "no_access",
user: 0,
group: 0,
other: 0,
wantPerm: 0,
isDir: false,
}, {
name: "all_dir",
user: dirReadRights | dirWriteRights | dirExecuteRights,
group: dirReadRights | dirWriteRights | dirExecuteRights,
other: dirReadRights | dirWriteRights | dirExecuteRights,
wantPerm: 0b111_111_111,
isDir: true,
}, {
name: "user_write_dir",
user: dirWriteRights,
group: 0,
other: 0,
wantPerm: 0b010_000_000,
isDir: true,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// Don't call [fs.FileMode.Perm] since the result is expected to
// contain only the permission bits.
assert.Equal(t, tc.wantPerm, masksToPerm(tc.user, tc.group, tc.other, tc.isDir))
})
}
}