< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.SecurityKeyIdentifier
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityKeyIdentifier.cs
Line coverage
32%
Covered lines: 16
Uncovered lines: 34
Coverable lines: 50
Total lines: 142
Line coverage: 32%
Branch coverage
17%
Covered branches: 5
Total branches: 28
Branch coverage: 17.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)75%4485.71%
Add(...)50%4466.66%
CreateKey()0%440%
Find()0%220%
GetEnumerator()100%110%
MakeReadOnly()100%110%
ToString()0%660%
TryFind(...)0%440%
System.Collections.IEnumerable.GetEnumerator()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SecurityKeyIdentifier.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;
 6using System.Collections.Generic;
 7using System.Globalization;
 8using System.IO;
 9
 10namespace CoreWCF.IdentityModel.Tokens
 11{
 12    public class SecurityKeyIdentifier : IEnumerable<SecurityKeyIdentifierClause>
 13    {
 14        private const int InitialSize = 2;
 15        private readonly List<SecurityKeyIdentifierClause> _clauses;
 16
 3017        public SecurityKeyIdentifier()
 18        {
 3019            _clauses = new List<SecurityKeyIdentifierClause>(InitialSize);
 3020        }
 21
 5022        public SecurityKeyIdentifier(params SecurityKeyIdentifierClause[] clauses)
 23        {
 5024            if (clauses == null)
 25            {
 026                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(clauses));
 27            }
 5028            _clauses = new List<SecurityKeyIdentifierClause>(clauses.Length);
 20029            for (int i = 0; i < clauses.Length; i++)
 30            {
 5031                Add(clauses[i]);
 32            }
 5033        }
 34
 35        public SecurityKeyIdentifierClause this[int index]
 36        {
 7837            get { return _clauses[index]; }
 38        }
 39
 40        public bool CanCreateKey
 41        {
 42            get
 43            {
 044                for (int i = 0; i < Count; i++)
 45                {
 046                    if (this[i].CanCreateKey)
 47                    {
 048                        return true;
 49                    }
 50                }
 051                return false;
 52            }
 53        }
 54
 55        public int Count
 56        {
 10957            get { return _clauses.Count; }
 58        }
 59
 7960        public bool IsReadOnly { get; private set; }
 61
 62        public void Add(SecurityKeyIdentifierClause clause)
 63        {
 7964            if (IsReadOnly)
 65            {
 066                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO
 67            }
 7968            if (clause == null)
 69            {
 070                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(clause)));
 71            }
 7972            _clauses.Add(clause);
 7973        }
 74
 75        public SecurityKey CreateKey()
 76        {
 077            for (int i = 0; i < Count; i++)
 78            {
 079                if (this[i].CanCreateKey)
 80                {
 081                    return this[i].CreateKey();
 82                }
 83            }
 084            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.KeyIdentifierCann
 85        }
 86
 87        public TClause Find<TClause>() where TClause : SecurityKeyIdentifierClause
 88        {
 089            if (!TryFind<TClause>(out TClause clause))
 90            {
 091                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.NoKeyIdenti
 92            }
 093            return clause;
 94        }
 95
 96        public IEnumerator<SecurityKeyIdentifierClause> GetEnumerator()
 97        {
 098            return _clauses.GetEnumerator();
 99        }
 100
 101        public void MakeReadOnly()
 102        {
 0103            IsReadOnly = true;
 0104        }
 105
 106        public override string ToString()
 107        {
 0108            using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
 109            {
 0110                writer.WriteLine("SecurityKeyIdentifier");
 0111                writer.WriteLine("    (");
 0112                writer.WriteLine("    IsReadOnly = {0},", IsReadOnly);
 0113                writer.WriteLine("    Count = {0}{1}", Count, Count > 0 ? "," : "");
 0114                for (int i = 0; i < Count; i++)
 115                {
 0116                    writer.WriteLine("    Clause[{0}] = {1}{2}", i, this[i], i < Count - 1 ? "," : "");
 117                }
 0118                writer.WriteLine("    )");
 0119                return writer.ToString();
 120            }
 0121        }
 122
 123        public bool TryFind<TClause>(out TClause clause) where TClause : SecurityKeyIdentifierClause
 124        {
 0125            for (int i = 0; i < _clauses.Count; i++)
 126            {
 0127                if (_clauses[i] is TClause c)
 128                {
 0129                    clause = c;
 0130                    return true;
 131                }
 132            }
 0133            clause = null;
 0134            return false;
 135        }
 136
 137        IEnumerator IEnumerable.GetEnumerator()
 138        {
 0139            return GetEnumerator();
 140        }
 141    }
 142}