-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added detection for base64 or base64Url (#38)
* Added API * Splitted Base64Tests * Added tests * Removed Invalid DetectEncoding doesn't make any guarantee about the validity of the given encoded data. * Implementation * Added fast for single scan fast = true: O(n), doesn't result in Unknown if base64 and base64Url are mixed fast = false: O(2n), can result in Unknows if base64 and base64Url are mixed * Demo added for DetectEncoding * Renamed DetectEncoding.cs to DetectEncoding_T.cs * Tests for the public entry
- Loading branch information
Showing
7 changed files
with
491 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace gfoidl.Base64 | ||
{ | ||
public enum EncodingType | ||
{ | ||
Base64, | ||
Base64Url, | ||
Unknown | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Runtime.InteropServices; | ||
using NUnit.Framework; | ||
|
||
namespace gfoidl.Base64.Tests.Base64Tests | ||
{ | ||
[TestFixture(typeof(byte))] | ||
[TestFixture(typeof(char))] | ||
public class Default<T> where T : unmanaged | ||
{ | ||
[Test] | ||
public void Default___base64_is_used() | ||
{ | ||
byte[] data = { 0xFF, 0xFE, 0x00 }; | ||
string expected = Convert.ToBase64String(data); | ||
|
||
string actual = Base64.Default.Encode(data); | ||
|
||
Assert.AreEqual(expected, actual); | ||
} | ||
//--------------------------------------------------------------------- | ||
[Test] | ||
public void Default_with_buffers___base64_is_used() | ||
{ | ||
byte[] data = { 0x00 }; | ||
const int encodedLength = 4; | ||
Span<T> base64 = stackalloc T[encodedLength]; | ||
OperationStatus status; | ||
int consumed, written; | ||
|
||
if (typeof(T) == typeof(byte)) | ||
{ | ||
status = Base64.Default.Encode(data, MemoryMarshal.AsBytes(base64), out consumed, out written); | ||
} | ||
else if (typeof(T) == typeof(char)) | ||
{ | ||
status = Base64.Default.Encode(data, MemoryMarshal.Cast<T, char>(base64), out consumed, out written); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException(); // just in case new types are introduced in the future | ||
} | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.AreEqual(OperationStatus.Done, status); | ||
Assert.AreEqual(1, consumed); | ||
Assert.AreEqual(4, written); | ||
}); | ||
|
||
Span<byte> decoded = stackalloc byte[10]; | ||
|
||
if (typeof(T) == typeof(byte)) | ||
{ | ||
status = Base64.Default.Decode(MemoryMarshal.AsBytes(base64), decoded, out consumed, out written); | ||
} | ||
else if (typeof(T) == typeof(char)) | ||
{ | ||
status = Base64.Default.Decode(MemoryMarshal.Cast<T, char>(base64), decoded, out consumed, out written); | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException(); // just in case new types are introduced in the future | ||
} | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.AreEqual(OperationStatus.Done, status); | ||
Assert.AreEqual(4, consumed); | ||
Assert.AreEqual(1, written); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace gfoidl.Base64.Tests.Base64Tests | ||
{ | ||
[TestFixture] | ||
public class DetectEncoding | ||
{ | ||
[Test] | ||
public void Base64_given_byte___OK() | ||
{ | ||
byte[] base64 = { (byte)'a', (byte)'+', (byte)'b', (byte)'/' }; | ||
|
||
EncodingType actual = Base64.DetectEncoding(base64); | ||
|
||
Assert.AreEqual(EncodingType.Base64, actual); | ||
} | ||
//--------------------------------------------------------------------- | ||
[Test] | ||
public void Base64_given_char___OK() | ||
{ | ||
string base64 = "a+b/"; | ||
|
||
EncodingType actual = Base64.DetectEncoding(base64.AsSpan()); | ||
|
||
Assert.AreEqual(EncodingType.Base64, actual); | ||
} | ||
} | ||
} |
Oops, something went wrong.