public void UserPermissions() { // Always throw when used var mockNativeLibraryInterface = new Mock <INativeLibraryInterface>(MockBehavior.Strict); var posixPermissionInfo = new PosixPermissionInfo(mockNativeLibraryInterface.Object, 1000, 1); Assert.Equal(1000, posixPermissionInfo.OwnerId); Assert.True(posixPermissionInfo.TryGetUserPermissions(1000, out _)); posixPermissionInfo.OwnerPermissions = FilePermissions.Read; Assert.Equal(FilePermissions.Read, posixPermissionInfo.OwnerPermissions); Assert.True(posixPermissionInfo.TryGetUserPermissions(1000, out FilePermissions ownerPermissions)); Assert.Equal(FilePermissions.Read, ownerPermissions); posixPermissionInfo.SetUserPermissions(1000, FilePermissions.Write); Assert.True(posixPermissionInfo.TryGetUserPermissions(1000, out ownerPermissions)); Assert.Equal(FilePermissions.Write, ownerPermissions); posixPermissionInfo.SetUserPermissions(2000, FilePermissions.Read); Assert.True(posixPermissionInfo.TryGetUserPermissions(2000, out FilePermissions userPermissions)); Assert.Equal(FilePermissions.Read, userPermissions); Assert.Throws <ArgumentException>(() => posixPermissionInfo.RemoveUserPermissions(1000)); Assert.False(posixPermissionInfo.TryGetUserPermissions(3000, out _)); posixPermissionInfo.RemoveUserPermissions(3000); // Should not throw posixPermissionInfo.RemoveUserPermissions(2000); Assert.False(posixPermissionInfo.TryGetUserPermissions(2000, out _)); Assert.Throws <ArgumentNullException>(() => posixPermissionInfo.SetUserPermissions(null)); posixPermissionInfo.SetUserPermissions(new[] { new KeyValuePair <int, FilePermissions>(4000, FilePermissions.Execute) }); posixPermissionInfo.OwnerId = 4000; Assert.Equal(FilePermissions.Execute, posixPermissionInfo.OwnerPermissions); Assert.False(posixPermissionInfo.TryGetUserPermissions(1000, out _)); }
public void FromNative() { var mockNativeLibraryInterface = new Mock <INativeLibraryInterface>(MockBehavior.Strict); FilePermissions r = FilePermissions.Read; FilePermissions rw = FilePermissions.Read | FilePermissions.Write; FilePermissions rwx = FilePermissions.Read | FilePermissions.Write | FilePermissions.Execute; AccessControlListEntry[] acl = { new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.UserObj, TagQualifier = 1000, Permissions = rwx }, new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.GroupObj, TagQualifier = 1000, Permissions = rw }, new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.Other, TagQualifier = -1, Permissions = r }, new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.User, TagQualifier = 2000, Permissions = rw }, new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.User, TagQualifier = 3000, Permissions = r }, new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.Group, TagQualifier = 2000, Permissions = rw }, new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.Group, TagQualifier = 3000, Permissions = r }, new AccessControlListEntry { TagType = AccessControlListEntryTagTypes.Mask, TagQualifier = -1, Permissions = rw } }; NativePermissionDataContainer dataContainer = new NativePermissionDataContainer { AclSize = acl.Length, OwnerId = 1000, GroupId = 1000, OwnerPermissions = rwx, GroupPermissions = rw, OtherPermissions = r }; mockNativeLibraryInterface.Setup(obj => obj.GetPermissionData("file", 0, out dataContainer)).Returns(acl); var posixPermissionInfo = new PosixPermissionInfo(mockNativeLibraryInterface.Object, "file", 0); Assert.Equal(1000, posixPermissionInfo.OwnerId); Assert.Equal(rwx, posixPermissionInfo.OwnerPermissions); Assert.True(posixPermissionInfo.TryGetUserPermissions(2000, out var permissionsUser2000)); Assert.Equal(rw, permissionsUser2000); Assert.True(posixPermissionInfo.TryGetUserPermissions(3000, out var permissionsUser3000)); Assert.Equal(r, permissionsUser3000); Assert.Equal(1000, posixPermissionInfo.GroupId); Assert.Equal(rw, posixPermissionInfo.GroupPermissions); Assert.True(posixPermissionInfo.TryGetGroupPermissions(2000, out var permissionsGroup2000)); Assert.Equal(rw, permissionsGroup2000); Assert.True(posixPermissionInfo.TryGetGroupPermissions(3000, out var permissionsGroup3000)); Assert.Equal(r, permissionsGroup3000); Assert.Equal(r, posixPermissionInfo.OtherPermissions); }