public void ObjectEquality()
        {
            object @public       = StorageActor.Public;
            object authenticated = StorageActor.Authenticated;
            object userX         = StorageActor.User("x");
            object groupX        = StorageActor.Group("x");

            Assert.True(@public.Equals(@public));
            Assert.True(authenticated.Equals(authenticated));
            Assert.True(userX.Equals(userX));
            Assert.True(groupX.Equals(groupX));

            Assert.False(@public.Equals(authenticated));
            Assert.False(@public.Equals(userX));
            Assert.False(@public.Equals(groupX));

            Assert.False(authenticated.Equals(@public));
            Assert.False(authenticated.Equals(userX));
            Assert.False(authenticated.Equals(groupX));
            Assert.False(userX.Equals(@public));
            Assert.False(userX.Equals(authenticated));
            Assert.False(userX.Equals(StorageActor.User("y")));
            Assert.False(userX.Equals(groupX));
            Assert.False(groupX.Equals(@public));
            Assert.False(groupX.Equals(authenticated));
            Assert.False(groupX.Equals(userX));
            Assert.False(groupX.Equals(StorageActor.Group("y")));

            // invalid
            Assert.False(((object)new StorageActor((StorageActorType)16, null)).Equals(new StorageActor((StorageActorType)16, null)));
            Assert.False(authenticated.Equals(2));
        }
        internal StorageSecurity GetPermissions(GoogleObject obj)
        {
            var builder = ImmutableDictionary.CreateBuilder <StorageActor, StoragePermissions>();

            if (null != obj.Acl)
            {
                foreach (var objectAccessControl in obj.Acl)
                {
                    switch (objectAccessControl.Entity)
                    {
                    case "allAuthenticatedUsers":
                        builder.Add(StorageActor.Authenticated, objectAccessControl.GetStoragePermissions());
                        break;

                    case "allUsers":
                        builder.Add(StorageActor.Public, objectAccessControl.GetStoragePermissions());
                        break;

                    case string entity when entity.StartsWith("user-"):
                        builder.Add(StorageActor.User(objectAccessControl.Email ?? objectAccessControl.EntityId), objectAccessControl.GetStoragePermissions());

                        break;

                    case string entity when entity.StartsWith("group-"):
                        builder.Add(StorageActor.Group(objectAccessControl.Email ?? objectAccessControl.EntityId), objectAccessControl.GetStoragePermissions());

                        break;
                    }
                }
            }
            return(new StorageSecurity(builder.ToImmutable()));
        }
        public void Equality()
        {
            Assert.Equal(StorageActor.Public, StorageActor.Public);
            Assert.Equal(StorageActor.Authenticated, StorageActor.Authenticated);
            Assert.Equal(StorageActor.User("x"), StorageActor.User("x"));
            Assert.Equal(StorageActor.Group("x"), StorageActor.Group("x"));

            Assert.NotEqual(StorageActor.Public, StorageActor.Authenticated);
            Assert.NotEqual(StorageActor.Public, StorageActor.User("x"));
            Assert.NotEqual(StorageActor.Public, StorageActor.Group("x"));

            Assert.NotEqual(StorageActor.Authenticated, StorageActor.Public);
            Assert.NotEqual(StorageActor.Authenticated, StorageActor.User("x"));
            Assert.NotEqual(StorageActor.Authenticated, StorageActor.Group("x"));

            Assert.NotEqual(StorageActor.User("x"), StorageActor.Public);
            Assert.NotEqual(StorageActor.User("x"), StorageActor.Authenticated);
            Assert.NotEqual(StorageActor.User("x"), StorageActor.User("y"));
            Assert.NotEqual(StorageActor.User("x"), StorageActor.Group("x"));

            Assert.NotEqual(StorageActor.Group("x"), StorageActor.Public);
            Assert.NotEqual(StorageActor.Group("x"), StorageActor.Authenticated);
            Assert.NotEqual(StorageActor.Group("x"), StorageActor.User("x"));
            Assert.NotEqual(StorageActor.Group("x"), StorageActor.Group("y"));

            // invalid
            Assert.NotEqual(new StorageActor((StorageActorType)16, null), new StorageActor((StorageActorType)16, null));
        }
        public void NullArguments()
        {
            Assert.Throws <ArgumentNullException>(() => StorageActor.Group(null));
            Assert.Throws <ArgumentNullException>(() => StorageActor.User(null));

            Assert.Throws <ArgumentException>(() => StorageActor.Group(string.Empty));
            Assert.Throws <ArgumentException>(() => StorageActor.User(string.Empty));
        }
 public void HashCode()
 {
     Assert.Equal(StorageActor.Public.GetHashCode(), StorageActor.Public.GetHashCode());
     Assert.Equal(StorageActor.Authenticated.GetHashCode(), StorageActor.Authenticated.GetHashCode());
     Assert.Equal(StorageActor.User("x").GetHashCode(), StorageActor.User("x").GetHashCode());
     Assert.Equal(StorageActor.Group("x").GetHashCode(), StorageActor.Group("x").GetHashCode());
     Assert.Equal(-1, new StorageActor((StorageActorType)16, null).GetHashCode());
 }
Example #6
0
        internal override StorageSecurity GetSecurity(string absolutePath)
        {
            if (0 != Syscall.stat(absolutePath, out var stat))
            {
                throw new LinuxIOException(Syscall.GetLastError());
            }
            var uid               = stat.st_uid;
            var uname             = LinuxHelpers.GetUserName(uid);
            var gid               = stat.st_gid;
            var gname             = LinuxHelpers.GetGroupName(gid);
            var publicPermissions = LinuxHelpers.GetOtherPermissions(stat.st_mode);
            var userPermissions   = LinuxHelpers.GetOwnerPermissions(stat.st_mode);
            var groupPermissions  = LinuxHelpers.GetGroupPermissions(stat.st_mode);
            var builder           = ImmutableDictionary.CreateBuilder <StorageActor, StoragePermissions>();

            builder.Add(StorageActor.Public, publicPermissions);
            builder.Add(StorageActor.User(uname), userPermissions);
            builder.Add(StorageActor.Group(gname), groupPermissions);
            return(new StorageSecurity(builder.ToImmutable()));
        }
        public static bool TryGetObjectAccessControl(this StoragePermissions permissions, StorageActor actor, out ObjectAccessControl ac)
        {
            if (permissions == StoragePermissions.None)
            {
                ac = null;
                return(false);
            }
            ac      = new ObjectAccessControl();
            ac.Role = permissions == StoragePermissions.Read ? "READER" : "OWNER";
            switch (actor.ActorType)
            {
            case StorageActorType.Authenticated:
                ac.Entity = "allAuthenticatedUsers";
                break;

            case StorageActorType.Public:
                ac.Entity = "allUsers";
                break;

            case StorageActorType.User:
                ac.Entity   = $"user-{actor.Id}";
                ac.EntityId = actor.Id;
                break;

            case StorageActorType.Group:
                ac.Entity   = $"group-{actor.Id}";
                ac.EntityId = actor.Id;
                break;

            default:
                ac = null;
                return(false);
            }
            return(true);
        }
Example #8
0
 public static IStorageSecurity UpdateGroupPermissions(this IStorageSecurity security, string id, StoragePermissions permissions)
 => security.UpdatePermissions(StorageActor.Group(id), permissions);
Example #9
0
 public static StoragePermissions GetGroupPermissions(this IStorageSecurity security, string id)
 => security.GetPermissions(StorageActor.Group(id));
Example #10
0
 public static StoragePermissions GetUserPermissions(this IStorageSecurity security, string id)
 => security.GetPermissions(StorageActor.User(id));