From 87f1a83cbbda4e00ebfe3e5ad9695d9119b91a16 Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <5808377+304NotModified@users.noreply.github.com> Date: Mon, 27 Jun 2022 21:54:37 +0200 Subject: [PATCH] Catch NotSupportedException trying GetEncoding (#146) * Catch NotSupportedException trying GetEncoding A rare occurrence of aNotSupportedException will happen when the encoding is UTF-7. The exception message is "NotSupportedException: Support for UTF-7 is disabled. See https://aka.ms/dotnet-warnings/SYSLIB0001 for more information." Eliminating UTF-7 usage is far from the scope of this commit. But catching the NotSupportedException allows the library to continue. * added tests Co-authored-by: JamesMc Co-authored-by: Julian Verdurmen <304NotModified@users.noreply.github.com> --- src/DetectionDetail.cs | 4 ++- ...ageNameTest.cs => DetectionDetailTests.cs} | 25 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) rename tests/{CodepageNameTest.cs => DetectionDetailTests.cs} (69%) diff --git a/src/DetectionDetail.cs b/src/DetectionDetail.cs index d3767be..fa1eee1 100644 --- a/src/DetectionDetail.cs +++ b/src/DetectionDetail.cs @@ -97,7 +97,9 @@ internal static Encoding GetEncoding(string encodingShortName) { return Encoding.GetEncoding(encodingName); } - catch (ArgumentException) // unsupported name + catch (Exception exception) when + (exception is ArgumentException || // unsupported name + exception is NotSupportedException) { #if NETSTANDARD && !NETSTANDARD1_0 || NETCOREAPP3_0 return CodePagesEncodingProvider.Instance.GetEncoding(encodingName); diff --git a/tests/CodepageNameTest.cs b/tests/DetectionDetailTests.cs similarity index 69% rename from tests/CodepageNameTest.cs rename to tests/DetectionDetailTests.cs index 2027d44..49b9cc0 100644 --- a/tests/CodepageNameTest.cs +++ b/tests/DetectionDetailTests.cs @@ -1,13 +1,17 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Reflection; using NUnit.Framework; +using UtfUnknown; using UtfUnknown.Core; +using UtfUnknown.Core.Probers; namespace UtfUnknown.Tests { - public class CodepageNameTest + [TestFixture] + public class DetectionDetailTests { + [TestCaseSource(nameof(EncodingNames))] public void DetectionDetailGetEncodingIsNotNull(string codepageName) { @@ -24,11 +28,24 @@ public void DetectionDetailGetEncodingIsNotNull(string codepageName) CodepageName.X_ISO_10646_UCS_4_2143, CodepageName.X_ISO_10646_UCS_4_3412, }; - + private static readonly IReadOnlyList EncodingNames = typeof(CodepageName) .GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.CreateInstance) .Select(x => x.GetValue(null).ToString()) .Where(x => !UnsupportedEncodings.Contains(x)) .ToList(); + + + [Test] + public void GetEncodingShouldHandleIncorrectEncoding() + { + // Arrange + string encoding = "wrong"; + // Act + var result = DetectionDetail.GetEncoding(encoding); + + // Assert + Assert.AreEqual(null, result); + } } -} \ No newline at end of file +}