-
Notifications
You must be signed in to change notification settings - Fork 72
Added unit tests to TechnitiumLibrary #30
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
Open
zbalkan
wants to merge
5
commits into
TechnitiumSoftware:master
Choose a base branch
from
zbalkan:feat/unit-test/technitiumlibrary
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,41 @@ | ||
| name: Unit testing (Windows / MSBuild) | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| push: | ||
| branches: ["master"] | ||
| pull_request: | ||
| branches: ["master"] | ||
| schedule: | ||
| - cron: "0 0 * * 0" # weekly, Sunday 00:00 UTC | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: windows-latest | ||
|
|
||
| env: | ||
| SOLUTION_NAME: TechnitiumLibrary.sln | ||
| BUILD_CONFIGURATION: Debug | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Install .NET 9 SDK | ||
| uses: actions/setup-dotnet@v4 | ||
| with: | ||
| dotnet-version: 9.0.x | ||
|
|
||
| - name: Add MSBuild to PATH | ||
| uses: microsoft/setup-msbuild@v1 | ||
|
|
||
| - name: Restore | ||
| run: msbuild ${{ env.SOLUTION_NAME }} /t:Restore | ||
|
|
||
| - name: Build | ||
| run: msbuild ${{ env.SOLUTION_NAME }} /m /p:Configuration=${{ env.BUILD_CONFIGURATION }} | ||
|
|
||
| - name: Test (msbuild) | ||
| run: msbuild TechnitiumLibrary.Tests\TechnitiumLibrary.Tests.csproj /t:Test /p:Configuration=${{ env.BUILD_CONFIGURATION }} | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| # TechnitiumLibrary | ||
| A library for .net based applications. | ||
|
|
||
| ## Quality Assurance | ||
|
|
||
| [](https://github.com/TechnitiumSoftware/TechnitiumLibrary/actions/workflows/unit-testing.yml) |
This file contains hidden or 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,3 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] |
15 changes: 15 additions & 0 deletions
15
TechnitiumLibrary.UnitTests/TechnitiumLibrary.UnitTests.csproj
This file contains hidden or 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,15 @@ | ||
| <Project Sdk="MSTest.Sdk/4.0.1"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <ImplicitUsings>disable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <UseVSTest>true</UseVSTest> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\TechnitiumLibrary\TechnitiumLibrary.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
259 changes: 259 additions & 0 deletions
259
TechnitiumLibrary.UnitTests/TechnitiumLibrary/Base32Tests.cs
This file contains hidden or 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,259 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Text; | ||
|
|
||
| namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary | ||
| { | ||
| [TestClass] | ||
| public class Base32Tests | ||
| { | ||
| // RFC vectors for Base32 | ||
| private static readonly (string clear, string enc)[] RfcVectors = | ||
| { | ||
| ("f", "MY======"), | ||
| ("fo", "MZXQ===="), | ||
| ("foo", "MZXW6==="), | ||
| ("foob", "MZXW6YQ="), | ||
| ("fooba", "MZXW6YTB"), | ||
| ("foobar", "MZXW6YTBOI======") | ||
| }; | ||
|
|
||
| // Values that must decode and encode back identically | ||
| private static readonly string[] RoundTripValues = | ||
| { | ||
| "", "10", "test130", "test", "8", "0", "=", "foobar" | ||
| }; | ||
|
|
||
| // Arbitrary real-world binary sample from PHP tests | ||
| private static readonly byte[] RandomBytes = | ||
| Convert.FromBase64String("HgxBl1kJ4souh+ELRIHm/x8yTc/cgjDmiCNyJR/NJfs="); | ||
|
|
||
|
|
||
| // -------------------- RFC vectors -------------------- | ||
|
|
||
| [TestMethod] | ||
| public void ToBase32String_RfcVectors_ProduceExpectedOutput() | ||
| { | ||
| foreach ((string clear, string encoded) in RfcVectors) | ||
| { | ||
| // Arrange | ||
| byte[] data = Encoding.ASCII.GetBytes(clear); | ||
|
|
||
| // Act | ||
| string result = Base32.ToBase32String(data); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(encoded, result, "Base32 encoding must match RFC vectors."); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32String_RfcVectors_DecodeCorrectly() | ||
| { | ||
| foreach ((string clear, string encoded) in RfcVectors) | ||
| { | ||
| // Arrange | ||
| byte[] expected = Encoding.ASCII.GetBytes(clear); | ||
|
|
||
| // Act | ||
| byte[] result = Base32.FromBase32String(encoded); | ||
|
|
||
| // Assert | ||
| CollectionAssert.AreEqual(expected, result, "Decoding must invert RFC vectors."); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // -------------------- RandomBytes encoding/decoding -------------------- | ||
|
|
||
| [TestMethod] | ||
| public void ToBase32String_RandomBytes_MatchesExpectedEncoding() | ||
| { | ||
| // Given test fixture from PHP | ||
| const string expected = "DYGEDF2ZBHRMULUH4EFUJAPG74PTETOP3SBDBZUIENZCKH6NEX5Q===="; | ||
|
|
||
| // Act | ||
| string actual = Base32.ToBase32String(RandomBytes); | ||
|
|
||
| Assert.AreEqual(expected, actual, "Binary encoding must be stable and deterministic."); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32String_RandomBytes_ReturnsOriginalInput() | ||
| { | ||
| // Arrange | ||
| const string encoded = "DYGEDF2ZBHRMULUH4EFUJAPG74PTETOP3SBDBZUIENZCKH6NEX5Q===="; | ||
|
|
||
| // Act | ||
| byte[] decoded = Base32.FromBase32String(encoded); | ||
|
|
||
| // Assert | ||
| CollectionAssert.AreEqual(RandomBytes, decoded); | ||
| } | ||
|
|
||
|
|
||
| // -------------------- General encode/decode identity tests -------------------- | ||
|
|
||
| [TestMethod] | ||
| public void EncodeDecode_RoundTrip_GivenKnownClearInputs_ReturnsOriginalValues() | ||
| { | ||
| foreach (string clear in RoundTripValues) | ||
| { | ||
| // Arrange | ||
| byte[] bytes = Encoding.UTF8.GetBytes(clear); | ||
|
|
||
| // Act | ||
| string encoded = Base32.ToBase32String(bytes); | ||
| byte[] decoded = Base32.FromBase32String(encoded); | ||
|
|
||
| // Assert | ||
| string decodedText = Encoding.UTF8.GetString(decoded); | ||
| Assert.AreEqual(clear, decodedText, "Encode + decode must round-trip."); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // -------------------- Explicit edge case tests -------------------- | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32String_GivenEmptyString_ReturnsEmptyArray() | ||
| { | ||
| byte[] result = Base32.FromBase32String(""); | ||
| Assert.IsEmpty(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ToBase32String_GivenEmptyBytes_ReturnsEmptyString() | ||
| { | ||
| string result = Base32.ToBase32String(Array.Empty<byte>()); | ||
| Assert.IsEmpty(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32String_GivenNullString_ThrowsException() | ||
| { | ||
| Assert.ThrowsExactly<NullReferenceException>(() => Base32.FromBase32String(null)); | ||
|
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32HexString_GivenNullString_ThrowsException() | ||
| { | ||
| Assert.ThrowsExactly<NullReferenceException>(() => Base32.FromBase32HexString(null)); | ||
|
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32String_GivenStringWithSpace_ThrowsException() | ||
| { | ||
| Assert.ThrowsExactly<IndexOutOfRangeException>(() => Base32.FromBase32String("MZXW6YTBOI====== ")); | ||
|
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32HexString_GivenNullStringSpace_ThrowsException() | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
| { | ||
| Assert.ThrowsExactly<IndexOutOfRangeException>(() => Base32.FromBase32HexString("MZXW6YTBOI====== ")); | ||
| } | ||
| } | ||
|
|
||
| [TestClass] | ||
| public class Base32HexTests | ||
| { | ||
| private static readonly (string clear, string enc)[] RfcVectors = | ||
| { | ||
| ("f", "CO======"), | ||
| ("fo", "CPNG===="), | ||
| ("foo", "CPNMU==="), | ||
| ("foob", "CPNMUOG="), | ||
| ("fooba", "CPNMUOJ1"), | ||
| ("foobar", "CPNMUOJ1E8======"), | ||
| }; | ||
|
|
||
| private static readonly string[] RoundTripValues = | ||
| { | ||
| "", "10", "test130", "test", "8", "0", "=", "foobar" | ||
| }; | ||
|
|
||
| private static readonly byte[] RandomBytes = | ||
| Convert.FromBase64String("HgxBl1kJ4souh+ELRIHm/x8yTc/cgjDmiCNyJR/NJfs="); | ||
|
|
||
|
|
||
| // ---------------- RFC vectors ---------------- | ||
|
|
||
| [TestMethod] | ||
| public void ToBase32HexString_RfcVectors_ProduceExpectedOutput() | ||
| { | ||
| foreach ((string clear, string encoded) in RfcVectors) | ||
| { | ||
| byte[] data = Encoding.ASCII.GetBytes(clear); | ||
| string result = Base32.ToBase32HexString(data); | ||
| Assert.AreEqual(encoded, result, "Hex encoding must match RFC vectors."); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32HexString_RfcVectors_DecodeCorrectly() | ||
| { | ||
| foreach ((string clear, string encoded) in RfcVectors) | ||
| { | ||
| byte[] expected = Encoding.ASCII.GetBytes(clear); | ||
| byte[] result = Base32.FromBase32HexString(encoded); | ||
| CollectionAssert.AreEqual(expected, result); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // ---------------- Known binary test ---------------- | ||
|
|
||
| [TestMethod] | ||
| public void ToBase32HexString_RandomBytes_MatchesExpectedEncoding() | ||
| { | ||
| const string expected = "3O6435QP17HCKBK7S45K90F6VSFJ4JEFRI131PK84DP2A7UD4NTG===="; | ||
| string result = Base32.ToBase32HexString(RandomBytes); | ||
| Assert.AreEqual(expected, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32HexString_RandomBytes_ReturnsOriginalInput() | ||
| { | ||
| const string encoded = "3O6435QP17HCKBK7S45K90F6VSFJ4JEFRI131PK84DP2A7UD4NTG===="; | ||
| byte[] decoded = Base32.FromBase32HexString(encoded); | ||
| CollectionAssert.AreEqual(RandomBytes, decoded); | ||
| } | ||
|
|
||
|
|
||
| // ---------------- Roundtrip tests ---------------- | ||
|
|
||
| [TestMethod] | ||
| public void EncodeDecode_RoundTrip_GivenKnownClearInputs_ReturnsOriginal() | ||
| { | ||
| foreach (string clear in RoundTripValues) | ||
| { | ||
| byte[] bytes = Encoding.UTF8.GetBytes(clear); | ||
| string encoded = Base32.ToBase32HexString(bytes); | ||
| byte[] decodedBytes = Base32.FromBase32HexString(encoded); | ||
| string decoded = Encoding.UTF8.GetString(decodedBytes); | ||
|
|
||
| Assert.AreEqual(clear, decoded); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // ---------------- Explicit empty edge cases ---------------- | ||
|
|
||
| [TestMethod] | ||
| public void FromBase32HexString_GivenEmpty_ReturnsEmptyArray() | ||
| { | ||
| byte[] result = Base32.FromBase32HexString(""); | ||
| Assert.IsEmpty(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ToBase32HexString_GivenEmptyBytes_ReturnsEmptyString() | ||
| { | ||
| string result = Base32.ToBase32HexString(Array.Empty<byte>()); | ||
| Assert.IsEmpty(result); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.