Skip to content

Commit

Permalink
Catch NotSupportedException trying GetEncoding (#146)
Browse files Browse the repository at this point in the history
* 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 <jamesjohnmcguire@gmail.com>
Co-authored-by: Julian Verdurmen <304NotModified@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 27, 2022
1 parent 30a7b17 commit 87f1a83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/DetectionDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 21 additions & 4 deletions tests/CodepageNameTest.cs → tests/DetectionDetailTests.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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<string> 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);
}
}
}
}

0 comments on commit 87f1a83

Please sign in to comment.