< Summary

Class:Microsoft.Azure.KeyVault.WebKey.Base64UrlJsonConverter
Assembly:Microsoft.Azure.KeyVault.WebKey
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.WebKey\src\Base64UrlJsonConverter.cs
Covered lines:16
Uncovered lines:7
Coverable lines:23
Total lines:98
Line coverage:69.5% (16 of 23)
Covered branches:8
Total branches:14
Branch coverage:57.1% (8 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ToBase64UrlString(...)-66.67%50%
FromBase64UrlString(...)-66.67%50%
Pad(...)-100%100%
CanConvert(...)-0%0%
ReadJson(...)-83.33%75%
WriteJson(...)-75%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.WebKey\src\Base64UrlJsonConverter.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4//
 5
 6using System;
 7using Newtonsoft.Json;
 8using Newtonsoft.Json.Linq;
 9
 10namespace Microsoft.Azure.KeyVault.WebKey
 11{
 12    internal class Base64UrlJsonConverter : JsonConverter
 13    {
 14        /// <summary>
 15        /// Converts a byte array to a Base64Url encoded string
 16        /// </summary>
 17        /// <param name="input">The byte array to convert</param>
 18        /// <returns>The Base64Url encoded form of the input</returns>
 19        private static string ToBase64UrlString(byte[] input)
 20        {
 340621            if (input == null)
 022                throw new ArgumentNullException("input");
 23
 340624            return Convert.ToBase64String(input).TrimEnd('=').Replace('+', '-').Replace('/', '_');
 25        }
 26
 27        /// <summary>
 28        /// Converts a Base64Url encoded string to a byte array
 29        /// </summary>
 30        /// <param name="input">The Base64Url encoded string</param>
 31        /// <returns>The byte array represented by the enconded string</returns>
 32        private static byte[] FromBase64UrlString(string input)
 33        {
 387034            if (string.IsNullOrEmpty(input))
 35            {
 036                throw new ArgumentNullException("input");
 37            }
 38
 387039            return Convert.FromBase64String(Pad(input.Replace('-', '+').Replace('_', '/')));
 40        }
 41
 42        /// <summary>
 43        /// Adds padding to the input
 44        /// </summary>
 45        /// <param name="input"> the input string </param>
 46        /// <returns> the padded string </returns>
 47        private static string Pad(string input)
 48        {
 387049            var count = 3 - ((input.Length + 3) % 4);
 50
 387051            if (count == 0)
 52            {
 113253                return input;
 54            }
 55
 273856            return input + new string('=', count);
 57        }
 58
 59        public override bool CanConvert(Type objectType)
 60        {
 061            if (objectType == typeof(byte[]))
 062                return true;
 63
 064            return false;
 65        }
 66
 67        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali
 68        {
 395069            if (objectType != typeof(byte[]))
 70            {
 071                return serializer.Deserialize(reader, objectType);
 72            }
 73            else
 74            {
 395075                var value = serializer.Deserialize<string>(reader);
 76
 395077                if (!string.IsNullOrEmpty(value))
 78                {
 387079                    return FromBase64UrlString(value);
 80                }
 81            }
 82
 8083            return null;
 84        }
 85
 86        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 87        {
 340688            if (value.GetType() != typeof(byte[]))
 89            {
 090                JToken.FromObject(value).WriteTo(writer);
 91            }
 92            else
 93            {
 340694                JToken.FromObject(ToBase64UrlString((byte[])value)).WriteTo(writer);
 95            }
 340696        }
 97    }
 98}