< Summary

Class:Azure.Storage.Files.DataLake.Models.PathAccessControlExtensions
Assembly:Azure.Storage.Files.DataLake
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Files.DataLake\src\Models\PathAccessControlExtensions.cs
Covered lines:61
Uncovered lines:0
Coverable lines:61
Total lines:192
Line coverage:100% (61 of 61)
Covered branches:54
Total branches:54
Branch coverage:100% (54 of 54)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ParseOctalRolePermissions(...)-100%100%
ParseSymbolicRolePermissions(...)-100%100%
ToOctalRolePermissions(...)-100%100%
ToSymbolicRolePermissions(...)-100%100%
ToAccessControlListString(...)-100%100%
ParseAccessControlList(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Files.DataLake\src\Models\PathAccessControlExtensions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Globalization;
 7using System.Text;
 8
 9namespace Azure.Storage.Files.DataLake.Models
 10{
 11    /// <summary>
 12    /// Extension methods for RolePermissions.
 13    /// </summary>
 14    public static class PathAccessControlExtensions
 15    {
 16        /// <summary>
 17        /// Parses octal char to RolePermissions.
 18        /// </summary>
 19        public static RolePermissions ParseOctalRolePermissions(char c)
 20        {
 3221            RolePermissions rolePermissions = RolePermissions.None;
 22
 3223            int value = (int)char.GetNumericValue(c);
 24
 3225            if (value < 0 || value > 7)
 26            {
 427                throw Errors.MustBeBetweenInclusive(nameof(c), 0, 7, value);
 28            }
 29
 2830            if ((value & 4) > 0)
 31            {
 2032                rolePermissions |= RolePermissions.Read;
 33            }
 34
 2835            if ((value & 2) > 0)
 36            {
 2037                rolePermissions |= RolePermissions.Write;
 38            }
 39
 2840            if ((value & 1) > 0)
 41            {
 2042                rolePermissions |= RolePermissions.Execute;
 43            }
 44
 2845            return rolePermissions;
 46        }
 47
 48        /// <summary>
 49        /// Parses symbolic permissions string to RolePermissions.
 50        /// </summary>
 51        /// <param name="s">String to parse.</param>
 52        /// <param name="allowStickyBit">If sticky bit is allowed.</param>
 53        /// <returns><see cref="RolePermissions"/>.</returns>
 54        public static RolePermissions ParseSymbolicRolePermissions(string s, bool allowStickyBit = false)
 55        {
 160856            RolePermissions rolePermissions = RolePermissions.None;
 160857            ArgumentException argumentException = DataLakeErrors.RolePermissionsSymbolicInvalidCharacter(s);
 58
 160859            if (s == null)
 60            {
 461                throw Errors.ArgumentNull(nameof(s));
 62            }
 63
 160464            if (s.Length != 3)
 65            {
 866                throw DataLakeErrors.RolePermissionsSymbolicInvalidLength(s);
 67            }
 68
 159669            if (s[0] == 'r')
 70            {
 107671                rolePermissions |= RolePermissions.Read;
 72            }
 52073            else if (s[0] != '-')
 74            {
 475                throw argumentException;
 76            }
 77
 159278            if (s[1] == 'w')
 79            {
 88080                rolePermissions |= RolePermissions.Write;
 81            }
 71282            else if (s[1] != '-')
 83            {
 484                throw argumentException;
 85            }
 86
 158887            if (s[2] == 'x')
 88            {
 77289                rolePermissions |= RolePermissions.Execute;
 90            }
 81691            else if (allowStickyBit)
 92            {
 19893                if (s[2] == 't')
 94                {
 1295                    rolePermissions |= RolePermissions.Execute;
 96                }
 18697                else if (s[2] != 'T' && s[2] != '-')
 98                {
 299                    throw argumentException;
 100                }
 101            }
 618102            else if (s[2] != '-')
 103            {
 2104                throw argumentException;
 105            }
 106
 1584107            return rolePermissions;
 108        }
 109
 110        /// <summary>
 111        /// Returns the octal string representation of this RolePermissions.
 112        /// </summary>
 113        /// <returns>String.</returns>
 114        public static string ToOctalRolePermissions(this RolePermissions rolePermissions)
 115        {
 28116            int result = 0;
 117
 28118            if (rolePermissions.HasFlag(RolePermissions.Read))
 119            {
 20120                result |= 4;
 121            }
 122
 28123            if (rolePermissions.HasFlag(RolePermissions.Write))
 124            {
 20125                result |= 2;
 126            }
 127
 28128            if (rolePermissions.HasFlag(RolePermissions.Execute))
 129            {
 20130                result |= 1;
 131            }
 132
 28133            return result.ToString(CultureInfo.InvariantCulture);
 134        }
 135
 136        /// <summary>
 137        /// Returns the octal string respentation of this RolePermissions.
 138        /// </summary>
 139        /// <returns>String.</returns>
 140        public static string ToSymbolicRolePermissions(this RolePermissions rolePermissions)
 141        {
 748142            StringBuilder stringBuilder = new StringBuilder();
 143
 748144            stringBuilder.Append(rolePermissions.HasFlag(RolePermissions.Read) ? "r" : "-");
 748145            stringBuilder.Append(rolePermissions.HasFlag(RolePermissions.Write) ? "w" : "-");
 748146            stringBuilder.Append(rolePermissions.HasFlag(RolePermissions.Execute) ? "x" : "-");
 147
 748148            return stringBuilder.ToString();
 149        }
 150
 151        /// <summary>
 152        /// Converts the Access Control List to a <see cref="string"/>.
 153        /// </summary>
 154        /// <param name="accessControlList">The Access Control List to serialize</param>
 155        /// <returns>string.</returns>
 156        public static string ToAccessControlListString(IList<PathAccessControlItem> accessControlList)
 157        {
 102158            if (accessControlList == null)
 159            {
 2160                return null;
 161            }
 162
 100163            IList<string> serializedAcl = new List<string>();
 980164            foreach (PathAccessControlItem ac in accessControlList)
 165            {
 390166                serializedAcl.Add(ac.ToString());
 167            }
 100168            return string.Join(",", serializedAcl);
 169        }
 170
 171        /// <summary>
 172        /// Deseralizes an access control list string  into a list of PathAccessControlEntries.
 173        /// </summary>
 174        /// <param name="s">The string to parse.</param>
 175        /// <returns>A List of <see cref="PathAccessControlItem"/>.</returns>
 176        public static IList<PathAccessControlItem> ParseAccessControlList(string s)
 177        {
 226178            if (s == null)
 179            {
 2180                return null;
 181            }
 182
 224183            string[] strings = s.Split(',');
 224184            List<PathAccessControlItem> accessControlList = new List<PathAccessControlItem>();
 1948185            foreach (string entry in strings)
 186            {
 750187                accessControlList.Add(PathAccessControlItem.Parse(entry));
 188            }
 224189            return accessControlList;
 190        }
 191    }
 192}