< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Claims.Claim
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Claims/Claim.cs
Line coverage
50%
Covered lines: 32
Uncovered lines: 31
Coverable lines: 63
Total lines: 241
Line coverage: 50.7%
Branch coverage
36%
Covered branches: 13
Total branches: 36
Branch coverage: 36.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%8871.42%
.ctor(...)100%11100%
CreateDnsClaim(...)50%2266.66%
CreateHashClaim(...)0%220%
CreateMailAddressClaim(...)0%220%
CreateNameClaim(...)50%2266.66%
CreateRsaClaim(...)50%2266.66%
CreateSpnClaim(...)50%2266.66%
CreateThumbprintClaim(...)0%220%
CreateUpnClaim(...)0%220%
CreateUriClaim(...)0%220%
CreateWindowsSidClaim(...)0%220%
CreateX500DistinguishedNameClaim(...)50%2266.66%
Equals(...)100%22100%
GetHashCode()0%220%
ToString()100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Claims/Claim.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Globalization;
 7using System.Net.Mail;
 8using System.Runtime.Serialization;
 9using System.Security.Cryptography;
 10using System.Security.Cryptography.X509Certificates;
 11using System.Security.Principal;
 12using CoreWCF.Security;
 13
 14namespace CoreWCF.IdentityModel.Claims
 15{
 16    public class Claim
 17    {
 18        private static Claim s_system;
 19
 20        [DataMember(Name = "ClaimType")]
 21        private readonly string _claimType;
 22        [DataMember(Name = "Resource")]
 23        private readonly object _resource;
 24        [DataMember(Name = "Right")]
 25        private readonly string _right;
 26        private IEqualityComparer<Claim> _comparer;
 27
 31128        private Claim(string claimType, object resource, string right, IEqualityComparer<Claim> comparer)
 29        {
 31130            if (claimType == null)
 31            {
 032                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claimType));
 33            }
 34
 31135            if (claimType.Length <= 0)
 36            {
 037                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(claimType), SR.ArgumentCannotBeEmpty
 38            }
 39
 31140            if (right == null)
 41            {
 042                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(right));
 43            }
 44
 31145            if (right.Length <= 0)
 46            {
 047                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(right), SR.ArgumentCannotBeEmptyStri
 48            }
 49
 50            // String interning is only supported in .Net standard 1.7. Api tool says this is slow?
 51            //this.claimType = StringUtil.OptimizeString(claimType);
 31152            _claimType = claimType;
 31153            _resource = resource;
 54            //this.right = StringUtil.OptimizeString(right);
 31155            _right = right;
 31156            _comparer = comparer;
 31157        }
 58
 30059        public Claim(string claimType, object resource, string right) : this(claimType, resource, right, null)
 60        {
 30061        }
 62
 63        public static IEqualityComparer<Claim> DefaultComparer
 64        {
 65            get
 66            {
 067                return EqualityComparer<Claim>.Default;
 68            }
 69        }
 70
 71        public static Claim System
 72        {
 73            get
 74            {
 375                if (s_system == null)
 76                {
 377                    s_system = new Claim(ClaimTypes.System, XsiConstants.System, Rights.Identity);
 78                }
 79
 380                return s_system;
 81            }
 82        }
 83
 84        public object Resource
 85        {
 2286            get { return _resource; }
 87        }
 88
 89        public string ClaimType
 90        {
 7291            get { return _claimType; }
 92        }
 93
 94        public string Right
 95        {
 3296            get { return _right; }
 97        }
 98
 99        // Turn key claims
 100        public static Claim CreateDnsClaim(string dns)
 101        {
 7102            if (dns == null)
 103            {
 0104                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dns));
 105            }
 106
 7107            return new Claim(ClaimTypes.Dns, dns, Rights.PossessProperty, ClaimComparer.Dns);
 108        }
 109
 110        //public static Claim CreateDenyOnlyWindowsSidClaim(SecurityIdentifier sid)
 111        //{
 112        //    if (sid == null)
 113        //        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(sid));
 114
 115        //    return new Claim(ClaimTypes.DenyOnlySid, sid, Rights.PossessProperty);
 116        //}
 117
 118        public static Claim CreateHashClaim(byte[] hash)
 119        {
 0120            if (hash == null)
 0121                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(hash));
 122
 0123            return new Claim(ClaimTypes.Hash, SecurityUtils.CloneBuffer(hash), Rights.PossessProperty, ClaimComparer.Has
 124        }
 125
 126        public static Claim CreateMailAddressClaim(MailAddress mailAddress)
 127        {
 0128            if (mailAddress == null)
 129            {
 0130                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(mailAddress));
 131            }
 132
 0133            return new Claim(ClaimTypes.Email, mailAddress, Rights.PossessProperty);
 134        }
 135
 136        public static Claim CreateNameClaim(string name)
 137        {
 14138            if (name == null)
 139            {
 0140                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name));
 141            }
 142
 14143            return new Claim(ClaimTypes.Name, name, Rights.PossessProperty);
 144        }
 145
 146        public static Claim CreateRsaClaim(RSA rsa)
 147        {
 2148            if (rsa == null)
 149            {
 0150                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rsa));
 151            }
 152
 2153            return new Claim(ClaimTypes.Rsa, rsa, Rights.PossessProperty, ClaimComparer.Rsa);
 154        }
 155
 156        public static Claim CreateSpnClaim(string spn)
 157        {
 12158            if (spn == null)
 159            {
 0160                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(spn));
 161            }
 162
 12163            return new Claim(ClaimTypes.Spn, spn, Rights.PossessProperty);
 164        }
 165
 166        public static Claim CreateThumbprintClaim(byte[] thumbprint)
 167        {
 0168            if (thumbprint == null)
 169            {
 0170                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(thumbprint));
 171            }
 172
 0173            return new Claim(ClaimTypes.Thumbprint, SecurityUtils.CloneBuffer(thumbprint), Rights.PossessProperty, Claim
 174        }
 175
 176        public static Claim CreateUpnClaim(string upn)
 177        {
 0178            if (upn == null)
 179            {
 0180                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(upn));
 181            }
 182
 0183            return new Claim(ClaimTypes.Upn, upn, Rights.PossessProperty, ClaimComparer.Upn);
 184        }
 185
 186        public static Claim CreateUriClaim(Uri uri)
 187        {
 0188            if (uri == null)
 189            {
 0190                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(uri));
 191            }
 192
 0193            return new Claim(ClaimTypes.Uri, uri, Rights.PossessProperty);
 194        }
 195
 196        public static Claim CreateWindowsSidClaim(SecurityIdentifier sid)
 197        {
 0198            if (sid == null)
 199            {
 0200                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(sid));
 201            }
 202
 0203            return new Claim(ClaimTypes.Sid, sid, Rights.PossessProperty);
 204        }
 205
 206        public static Claim CreateX500DistinguishedNameClaim(X500DistinguishedName x500DistinguishedName)
 207        {
 2208            if (x500DistinguishedName == null)
 209            {
 0210                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(x500DistinguishedName));
 211            }
 212
 2213            return new Claim(ClaimTypes.X500DistinguishedName, x500DistinguishedName, Rights.PossessProperty, ClaimCompa
 214        }
 215
 216        public override bool Equals(object obj)
 217        {
 12218            if (_comparer == null)
 219            {
 6220                _comparer = ClaimComparer.GetComparer(_claimType);
 221            }
 222
 12223            return _comparer.Equals(this, obj as Claim);
 224        }
 225
 226        public override int GetHashCode()
 227        {
 0228            if (_comparer == null)
 229            {
 0230                _comparer = ClaimComparer.GetComparer(_claimType);
 231            }
 232
 0233            return _comparer.GetHashCode(this);
 234        }
 235
 236        public override string ToString()
 237        {
 19238            return string.Format(CultureInfo.CurrentCulture, "{0}: {1}", _right, _claimType);
 239        }
 240    }
 241}