< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.SessionSecurityTokenCacheKey
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SessionSecurityTokenCacheKey.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 154
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%440%
op_Equality(...)0%220%
op_Inequality(...)100%110%
Equals(...)0%10100%
GetHashCode()0%220%
ToString()0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SessionSecurityTokenCacheKey.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.Text;
 6
 7namespace CoreWCF.IdentityModel.Tokens
 8{
 9    /// <summary>
 10    /// When caching an <see cref="SessionSecurityToken"/> there are two indexes required. One is the ContextId
 11    /// that is unique across all <see cref="SessionSecurityToken"/> and the next is KeyGeneration which is
 12    /// unique within a session. When an <see cref="SessionSecurityToken"/> is issued it has only a ContextId. When
 13    /// the <see cref="SessionSecurityToken"/> is renewed the KeyGeneration is added as an second index to the
 14    /// <see cref="SessionSecurityToken"/>. Now the renewed <see cref="SessionSecurityToken"/> is uniquely identifiable 
 15    /// KeyGeneration.
 16    /// The class <see cref="SessionSecurityTokenCacheKey"/> is used as the index
 17    /// to the <see cref="SessionSecurityToken"/> cache. This index will always have a valid ContextId specified
 18    /// but the KeyGeneration may be null. There is also an optional EndpointId
 19    /// which gives the endpoint to which the token is scoped.
 20    /// </summary>
 21    public class SessionSecurityTokenCacheKey
 22    {
 23        /// <summary>
 24        /// Creates an instance of <see cref="SessionSecurityTokenCacheKey"/> which
 25        /// is used as an index while caching <see cref="SessionSecurityToken"/>.
 26        /// </summary>
 27        /// <param name="endpointId">The endpoint Id to which the <see cref="SessionSecurityToken"/> is scoped.</param>
 28        /// <param name="contextId">UniqueId of the <see cref="SessionSecurityToken"/>.</param>
 29        /// <param name="keyGeneration">UniqueId which is available when the <see cref="SessionSecurityToken"/> is renew
 30        /// null when caching a new <see cref="SessionSecurityToken"/>.</param>
 031        public SessionSecurityTokenCacheKey(string endpointId, System.Xml.UniqueId contextId, System.Xml.UniqueId keyGen
 32        {
 033            EndpointId = endpointId ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointI
 034            ContextId = contextId ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId))
 035            KeyGeneration = keyGeneration;
 036        }
 37
 38        /// <summary>
 39        /// Gets or sets a value indicating whether KeyGeneration can be ignored
 40        /// while doing index comparison.
 41        /// </summary>
 042        public bool IgnoreKeyGeneration { get; set; }
 43
 44        /// <summary>
 45        /// Gets the ContextId of the <see cref="SessionSecurityToken"/>
 46        /// </summary>
 047        public System.Xml.UniqueId ContextId { get; }
 48
 49        /// <summary>
 50        /// Gets the EndpointId to which this cache entry is scoped.
 51        /// </summary>
 052        public string EndpointId { get; }
 53
 54        /// <summary>
 55        /// Gets the KeyGeneration of the <see cref="SessionSecurityToken"/>
 56        /// </summary>
 057        public System.Xml.UniqueId KeyGeneration { get; }
 58
 59        /// <summary>
 60        /// Implements the equality operator for <see cref="SessionSecurityTokenCacheKey"/>.
 61        /// </summary>
 62        /// <param name="first">First object to compare.</param>
 63        /// <param name="second">Second object to compare.</param>
 64        /// <returns>'true' if both objects are equal.</returns>
 65        public static bool operator ==(SessionSecurityTokenCacheKey first, SessionSecurityTokenCacheKey second)
 66        {
 067            if (ReferenceEquals(first, null))
 68            {
 069                return ReferenceEquals(second, null);
 70            }
 71
 072            return first.Equals(second);
 73        }
 74
 75        /// <summary>
 76        /// Implements the inequality operator for <see cref="SessionSecurityTokenCacheKey"/>.
 77        /// </summary>
 78        /// <param name="first">First object to compare.</param>
 79        /// <param name="second">Second object to compare.</param>
 80        /// <returns>'true' if both the objects are different.</returns>
 81        public static bool operator !=(SessionSecurityTokenCacheKey first, SessionSecurityTokenCacheKey second)
 82        {
 083            return !(first == second);
 84        }
 85
 86        /// <summary>
 87        /// Checks if the given object is the same as the current object.
 88        /// </summary>
 89        /// <param name="obj">The object to be compared.</param>
 90        /// <returns>'true' if both are the same object else false.</returns>
 91        public override bool Equals(object obj)
 92        {
 093            if (obj is SessionSecurityTokenCacheKey)
 94            {
 095                SessionSecurityTokenCacheKey key2 = obj as SessionSecurityTokenCacheKey;
 096                if (key2.ContextId != ContextId)
 97                {
 098                    return false;
 99                }
 100
 0101                if (!StringComparer.Ordinal.Equals(key2.EndpointId, EndpointId))
 102                {
 0103                    return false;
 104                }
 105
 106                // If KeyGeneration can be ignored on either one of them then we
 107                // don't do KeyGeneration comparison.
 0108                if (!IgnoreKeyGeneration && !key2.IgnoreKeyGeneration)
 109                {
 0110                    return key2.KeyGeneration == KeyGeneration;
 111                }
 112
 0113                return true;
 114            }
 115
 0116            return false;
 117        }
 118
 119        /// <summary>
 120        /// Returns a Hash code for this object.
 121        /// </summary>
 122        /// <returns>Hash code for the object as a Integer.</returns>
 123        public override int GetHashCode()
 124        {
 0125            if (KeyGeneration == null)
 126            {
 0127                return ContextId.GetHashCode();
 128            }
 129            else
 130            {
 0131                return ContextId.GetHashCode() ^ KeyGeneration.GetHashCode();
 132            }
 133        }
 134
 135        /// <summary>
 136        /// Implements ToString() to provide a unique identifier.
 137        /// </summary>
 138        /// <returns>This key, in string form.</returns>
 139        public override string ToString()
 140        {
 0141            StringBuilder sb = new StringBuilder();
 0142            sb.Append(EndpointId);
 0143            sb.Append(';');
 0144            sb.Append(ContextId.ToString());
 0145            sb.Append(';');
 0146            if (!IgnoreKeyGeneration && KeyGeneration != null)
 147            {
 0148                sb.Append(KeyGeneration.ToString());
 149            }
 150
 0151            return sb.ToString();
 152        }
 153    }
 154}