public void TestSetAcl()

in AdlsDotNetSDKUnitTest/SdkUnitTest.cs [1958:2025]


        public void TestSetAcl()
        {
            string path = $"{UnitTestDir}/SetAclEntries";
            _adlsClient.CreateDirectory(path, "");
            _adlsClient.SetPermission(path, "770");
            string testFile = path + "/SetAcl.txt";
            using (var ostream = _adlsClient.CreateFile(testFile, IfExists.Overwrite, "775"))
            {
                byte[] buff = Encoding.UTF8.GetBytes("Hello test i am");
                ostream.Write(buff, 0, buff.Length);
            }
            AdlsClient nonOwner1 = SetupNonOwnerClient1();
            Assert.IsFalse(nonOwner1.CheckAccess(path, "r--"));
            AdlsClient nonOwner2 = SetupNonOwnerClient2();
            Assert.IsFalse(nonOwner2.CheckAccess(path, "r--"));
            List<AclEntry> aclList = new List<AclEntry>() {
            new AclEntry(AclType.user, NonOwner1ObjectId, AclScope.Access, AclAction.ReadWrite),
            //Add the default permission ACLs
            new AclEntry(AclType.user, "", AclScope.Access, AclAction.All),
            new AclEntry(AclType.group, "", AclScope.Access, AclAction.All),
            new AclEntry(AclType.other, "", AclScope.Access, AclAction.None)
            };
            _adlsClient.SetAcl(path, aclList);
            //Non owner 1
            Assert.IsTrue(nonOwner1.CheckAccess(path, "rw-"));
            Assert.IsFalse(nonOwner1.CheckAccess(path, "--x"));
            try
            {
                byte[] buff = new byte[25];
                using (var istream = nonOwner1.GetReadStream(testFile))
                {
                    istream.Read(buff, 0, buff.Length);
                }
                Assert.Fail("nonowner1 should not have execute permission on the directory so ReadStream should raise an exception");
            }
            catch (IOException)
            {
            }
            //Non owner 2
            Assert.IsFalse(nonOwner2.CheckAccess(path, "--x"));
            Assert.IsFalse(nonOwner2.CheckAccess(path, "rw-"));
            aclList.Clear();
            aclList.Add(new AclEntry(AclType.user, NonOwner2ObjectId, AclScope.Access, AclAction.ExecuteOnly));
            //Add the default permission ACLs
            aclList.Add(new AclEntry(AclType.user, "", AclScope.Access, AclAction.All));
            aclList.Add(new AclEntry(AclType.group, "", AclScope.Access, AclAction.All));
            aclList.Add(new AclEntry(AclType.other, "", AclScope.Access, AclAction.None));
            _adlsClient.SetAcl(path, aclList);

            Assert.IsFalse(nonOwner1.CheckAccess(path, "rw-"));
            Assert.IsFalse(nonOwner1.CheckAccess(path, "--x"));

            Assert.IsTrue(nonOwner2.CheckAccess(path, "--x"));
            Assert.IsFalse(nonOwner2.CheckAccess(path, "rw-"));
            try
            {
                byte[] buff = new byte[25];
                using (var istream = nonOwner2.GetReadStream(testFile))
                {
                    istream.Read(buff, 0, buff.Length);
                }

            }
            catch (IOException)
            {
                Assert.Fail("The nonowner2 should have execute permission on the directory");
            }
        }