-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tweak base32 implementation #131
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4a023a4
- Added tests for the base32 implementation.
robinvanpoppel 167cd91
Fix remaining unit tests and refactor
robinvanpoppel 67e2eec
Trim padding characters when generating QR code
robinvanpoppel 31931b4
Merge branch 'master' into base32
robinvanpoppel 7821b69
Add special validation for padding
robinvanpoppel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using FluentAssertions; | ||
using KeeTrayTOTP.Libraries; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace KeeTrayTOTP.Tests | ||
{ | ||
[TestClass] | ||
public class Base32Tests | ||
{ | ||
private const string InvalidSeed = "MZXW6!YTBOI======!"; | ||
|
||
[DataTestMethod] | ||
[DynamicData(nameof(GetBase32Data), DynamicDataSourceType.Method)] | ||
public void Encode_ShouldWorkOnRfc4648TestCases(Base32TestCase testCase) | ||
{ | ||
var actual = Base32.Encode(Encoding.UTF8.GetBytes(testCase.Decoded)); | ||
|
||
actual.Should().Be(testCase.Encoded); | ||
} | ||
|
||
[DataTestMethod] | ||
[DynamicData(nameof(GetBase32Data), DynamicDataSourceType.Method)] | ||
public void Decode_ShouldWorkOnRfc4648TestCases(Base32TestCase testCase) | ||
{ | ||
var actual = Base32.Decode(testCase.Encoded); | ||
|
||
var actualString = Encoding.UTF8.GetString(actual); | ||
|
||
actualString.Should().Be(testCase.Decoded); | ||
} | ||
|
||
[DataTestMethod] | ||
[DynamicData(nameof(GetBase32Data), DynamicDataSourceType.Method)] | ||
public void IsBase32Out_ShouldReturnTrueFor(Base32TestCase testCase) | ||
{ | ||
var actual = testCase.Encoded.IsBase32(out string invalidChars); | ||
|
||
actual.Should().BeTrue(); | ||
invalidChars.Should().BeNullOrEmpty(); | ||
} | ||
|
||
[DataTestMethod] | ||
[DynamicData(nameof(GetBase32Data), DynamicDataSourceType.Method)] | ||
public void IsBase32_ShouldReturnTrueFor(Base32TestCase testCase) | ||
{ | ||
var actual = testCase.Encoded.IsBase32(); | ||
|
||
actual.Should().BeTrue(); | ||
} | ||
|
||
[TestMethod] | ||
public void Decode_ShouldThrowWithInvalidSeed() | ||
{ | ||
Action act = () => Base32.Decode(InvalidSeed); | ||
|
||
act.Should().Throw<ArgumentException>().WithMessage("Base32 contains illegal characters."); | ||
} | ||
|
||
[TestMethod] | ||
public void ExtIsBase32_ShouldReturnFalseWithInvalidChars() | ||
{ | ||
var actual = InvalidSeed.IsBase32(out string invalidChars); | ||
|
||
actual.Should().BeFalse(); | ||
invalidChars.Should().Be("!======!"); | ||
} | ||
|
||
/// <summary> | ||
/// Testcases were taken from <see cref="https://tools.ietf.org/html/rfc4648"/> | ||
/// </summary> | ||
public static IEnumerable<object[]> GetBase32Data() | ||
{ | ||
yield return new object[] { new Base32TestCase("", "") }; | ||
yield return new object[] { new Base32TestCase("f", "MY======") }; | ||
yield return new object[] { new Base32TestCase("fo", "MZXQ====") }; | ||
yield return new object[] { new Base32TestCase("foo", "MZXW6===") }; | ||
yield return new object[] { new Base32TestCase("foob", "MZXW6YQ=") }; | ||
yield return new object[] { new Base32TestCase("fooba", "MZXW6YTB") }; | ||
yield return new object[] { new Base32TestCase("foobar", "MZXW6YTBOI======") }; | ||
} | ||
|
||
public sealed class Base32TestCase | ||
{ | ||
public Base32TestCase(string decoded, string encoded) | ||
{ | ||
this.Decoded = decoded; | ||
this.Encoded = encoded; | ||
} | ||
|
||
/// <summary> | ||
/// Input for Encoding, expected Output for Decoding | ||
/// </summary> | ||
public string Decoded { get; } | ||
|
||
/// <summary> | ||
/// String in Base32 encoding | ||
/// </summary> | ||
public string Encoded { get; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"Decoded = {Decoded}, Encoded = {Encoded}"; | ||
} | ||
} | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this should then only return
!!
as invalidChars, shouldn't it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would depend on the outcome above. I think it could say just '!', or '!!', but in this example the = is not used as padding and is invalid as well...