< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.EncryptedData
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/EncryptedData.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 96
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
EnsureDecryptionSet()0%440%
ForceEncryption()100%110%
GetDecryptedBuffer()100%110%
ReadCipherData(...)100%110%
ReadCipherData(...)100%110%
SetPlainText()100%110%
SetUpDecryption(...)0%440%
SetUpEncryption(...)0%440%
WriteCipherData(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/EncryptedData.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.Security.Cryptography;
 6using System.Xml;
 7
 8namespace CoreWCF.Security
 9{
 10    internal class EncryptedData : EncryptedType
 11    {
 012        internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.EncryptedData;
 013        internal static readonly string ElementType = XmlEncryptionStrings.ElementType;
 014        internal static readonly string ContentType = XmlEncryptionStrings.ContentType;
 15        private SymmetricAlgorithm _algorithm;
 16        private byte[] _decryptedBuffer;
 17        private ArraySegment<byte> _buffer;
 18        private byte[] _iv;
 19        private byte[] _cipherText;
 20
 21        protected override XmlDictionaryString OpeningElementName
 22        {
 023            get { return ElementName; }
 24        }
 25
 26        private void EnsureDecryptionSet()
 27        {
 028            if (State == EncryptionState.DecryptionSetup)
 29            {
 030                SetPlainText();
 31            }
 032            else if (State != EncryptionState.Decrypted)
 33            {
 034                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.BadEncryptionS
 35            }
 036        }
 37
 38        protected override void ForceEncryption()
 39        {
 040            CryptoHelper.GenerateIVAndEncrypt(_algorithm, _buffer, out _iv, out _cipherText);
 041            State = EncryptionState.Encrypted;
 042            _buffer = new ArraySegment<byte>(CryptoHelper.EmptyBuffer);
 043        }
 44
 45        public byte[] GetDecryptedBuffer()
 46        {
 047            EnsureDecryptionSet();
 048            return _decryptedBuffer;
 49        }
 50
 51        protected override void ReadCipherData(XmlDictionaryReader reader)
 52        {
 053            _cipherText = reader.ReadContentAsBase64();
 054        }
 55
 56        protected override void ReadCipherData(XmlDictionaryReader reader, long maxBufferSize)
 57        {
 058            _cipherText = SecurityUtils.ReadContentAsBase64(reader, maxBufferSize);
 059        }
 60
 61        private void SetPlainText()
 62        {
 063            _decryptedBuffer = CryptoHelper.ExtractIVAndDecrypt(_algorithm, _cipherText, 0, _cipherText.Length);
 064            State = EncryptionState.Decrypted;
 065        }
 66
 67        public void SetUpDecryption(SymmetricAlgorithm algorithm)
 68        {
 069            if (State != EncryptionState.Read)
 70            {
 071                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.BadEncryptionS
 72            }
 73
 074            _algorithm = algorithm ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(algorithm)
 075            State = EncryptionState.DecryptionSetup;
 076        }
 77
 78        public void SetUpEncryption(SymmetricAlgorithm algorithm, ArraySegment<byte> buffer)
 79        {
 080            if (State != EncryptionState.New)
 81            {
 082                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.BadEncryptionS
 83            }
 84
 085            _algorithm = algorithm ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(algorithm)
 086            _buffer = buffer;
 087            State = EncryptionState.EncryptionSetup;
 088        }
 89
 90        protected override void WriteCipherData(XmlDictionaryWriter writer)
 91        {
 092            writer.WriteBase64(_iv, 0, _iv.Length);
 093            writer.WriteBase64(_cipherText, 0, _cipherText.Length);
 094        }
 95    }
 96}