-
Notifications
You must be signed in to change notification settings - Fork 61
Added tests for TechnitiumLibrary.Net, kept the failing tests on purpose #33
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
16
commits into
TechnitiumSoftware:master
Choose a base branch
from
zbalkan:feat/unit-test/technitiumlibrary.net
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 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
13eb1c4
Added unit test project, action and status badge to TechnitiumLibrary
zbalkan c8df1cb
Added tests for TechnitiumLibrary.Net, kept the failing tests on purpose
zbalkan 24af2bd
Fixed workflow
zbalkan 0b21203
Updated workflow file
zbalkan 221e5e8
Fixed disposal
zbalkan 1f7267c
Disposed TcpListener properly
zbalkan 004b42f
Properly disposed resources
zbalkan 2845eb2
Removed unused variable
zbalkan 735cd12
Used discard pattern for unused variables
zbalkan 2ee460c
Removed unnecessary helper
zbalkan 741bf75
Fixed the exception type in Constructor_InvalidJson_Throws
zbalkan b1b62f3
Fixed thrown exception type of Constructor_CharacterStringTooLong_Throws
zbalkan 2c94e05
Fixed thrown exception type of Constructor_NonAsciiCharacter_Throws
zbalkan ff67f45
Fixed DNAT-related tests of TransparentProxyServer
zbalkan e76048e
Fixed HTTP request and response tests
zbalkan 49291c3
Added copyright
zbalkan 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 }} | ||
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) |
86 changes: 86 additions & 0 deletions
86
...sts/TechnitiumLibrary.Net/Dns/ResourceRecords/CanonicallySerializedResourceRecordTests.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,86 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.IO; | ||
| using System.Net; | ||
| using TechnitiumLibrary.Net.Dns.ResourceRecords; | ||
|
|
||
| namespace TechnitiumLibrary.Tests.TechnitiumLibrary.Net.Dns.ResourceRecords | ||
| { | ||
| [TestClass] | ||
| public class CanonicallySerializedResourceRecordTests | ||
| { | ||
| [TestMethod] | ||
| public void CompareTo_UsesCanonicalRdataOrdering() | ||
| { | ||
| using MemoryStream buffer = new(); | ||
|
|
||
| var low = CanonicallySerializedResourceRecord.Create( | ||
| "example.com", | ||
| DnsResourceRecordType.A, | ||
| DnsClass.IN, | ||
| 60, | ||
| new DnsARecordData(IPAddress.Parse("192.0.2.1")), | ||
| buffer); | ||
|
|
||
| var high = CanonicallySerializedResourceRecord.Create( | ||
| "example.com", | ||
| DnsResourceRecordType.A, | ||
| DnsClass.IN, | ||
| 60, | ||
| new DnsARecordData(IPAddress.Parse("192.0.2.200")), | ||
| buffer); | ||
|
|
||
| Assert.IsTrue(low.CompareTo(high) < 0); | ||
| Assert.IsTrue(high.CompareTo(low) > 0); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Create_CanonicalizesOwnerName_ToLowercase() | ||
| { | ||
| using MemoryStream buffer = new(); | ||
|
|
||
| var record = CanonicallySerializedResourceRecord.Create( | ||
| name: "Example.COM", | ||
| type: DnsResourceRecordType.A, | ||
| @class: DnsClass.IN, | ||
| originalTtl: 3600, | ||
| rData: new DnsARecordData(IPAddress.Parse("192.0.2.1")), | ||
| buffer: buffer); | ||
|
|
||
| using MemoryStream ms = new(); | ||
| record.WriteTo(ms); | ||
|
|
||
| string wire = System.Text.Encoding.ASCII.GetString(ms.ToArray()); | ||
| StringAssert.Contains(wire, "example"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WriteTo_IsDeterministic_ForSameInput() | ||
| { | ||
| using MemoryStream buffer = new(); | ||
|
|
||
| var a = CanonicallySerializedResourceRecord.Create( | ||
| "example.com", | ||
| DnsResourceRecordType.A, | ||
| DnsClass.IN, | ||
| 60, | ||
| new DnsARecordData(IPAddress.Parse("192.0.2.1")), | ||
| buffer); | ||
|
|
||
| var b = CanonicallySerializedResourceRecord.Create( | ||
| "example.com", | ||
| DnsResourceRecordType.A, | ||
| DnsClass.IN, | ||
| 60, | ||
| new DnsARecordData(IPAddress.Parse("192.0.2.1")), | ||
| buffer); | ||
|
|
||
| using MemoryStream m1 = new(); | ||
| using MemoryStream m2 = new(); | ||
|
|
||
| a.WriteTo(m1); | ||
| b.WriteTo(m2); | ||
|
|
||
| CollectionAssert.AreEqual(m1.ToArray(), m2.ToArray()); | ||
| } | ||
| } | ||
| } |
106 changes: 106 additions & 0 deletions
106
TechnitiumLibrary.Tests/TechnitiumLibrary.Net/Dns/ResourceRecords/DnsAAAARecordDataTests.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,106 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.IO; | ||
| using System.Net; | ||
| using System.Text; | ||
| using TechnitiumLibrary.Net.Dns; | ||
| using TechnitiumLibrary.Net.Dns.ResourceRecords; | ||
|
|
||
| namespace TechnitiumLibrary.Tests.TechnitiumLibrary.Net.Dns.ResourceRecords | ||
| { | ||
| [TestClass] | ||
| public class DnsAAAARecordDataTests | ||
| { | ||
| [TestMethod] | ||
| public void Constructor_IPv4Address_Throws() | ||
| { | ||
| IPAddress ipv4 = IPAddress.Parse("192.0.2.1"); | ||
|
|
||
| Assert.ThrowsExactly<DnsClientException>(() => | ||
| new DnsAAAARecordData(ipv4)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Constructor_ValidIPv6Address_Succeeds() | ||
| { | ||
| IPAddress address = IPAddress.Parse("2001:db8::1"); | ||
|
|
||
| var rdata = new DnsAAAARecordData(address); | ||
|
|
||
| Assert.AreEqual(address, rdata.Address); | ||
| Assert.AreEqual(16, rdata.UncompressedLength); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_DifferentAddress_IsFalse() | ||
| { | ||
| var a = new DnsAAAARecordData(IPAddress.Parse("2001:db8::1")); | ||
| var b = new DnsAAAARecordData(IPAddress.Parse("2001:db8::2")); | ||
|
|
||
| Assert.IsFalse(a.Equals(b)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_SameAddress_IsTrue() | ||
| { | ||
| IPAddress address = IPAddress.Parse("2001:db8::1"); | ||
|
|
||
| var a = new DnsAAAARecordData(address); | ||
| var b = new DnsAAAARecordData(IPAddress.Parse("2001:db8::1")); | ||
|
|
||
| Assert.IsTrue(a.Equals(b)); | ||
| Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void RoundTrip_StreamConstructor_PreservesEquality() | ||
| { | ||
| IPAddress address = IPAddress.Parse("2001:db8::dead:beef"); | ||
|
|
||
| var original = new DnsResourceRecord( | ||
| "example", | ||
| DnsResourceRecordType.AAAA, | ||
| DnsClass.IN, | ||
| 300, | ||
| new DnsAAAARecordData(address)); | ||
|
|
||
| byte[] wire = Serialize(original); | ||
|
|
||
| using MemoryStream ms = new(wire); | ||
| var parsed = new DnsResourceRecord(ms); | ||
|
|
||
| Assert.AreEqual(original, parsed); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void SerializeTo_ProducesExpectedJson() | ||
| { | ||
| var rdata = new DnsAAAARecordData(IPAddress.Parse("2001:db8::1")); | ||
|
|
||
| using MemoryStream ms = new(); | ||
| using var writer = new System.Text.Json.Utf8JsonWriter(ms); | ||
|
|
||
| rdata.SerializeTo(writer); | ||
| writer.Flush(); | ||
|
|
||
| string json = Encoding.UTF8.GetString(ms.ToArray()); | ||
|
|
||
| StringAssert.Contains(json, "IPAddress"); | ||
| StringAssert.Contains(json, "2001:db8::1"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UncompressedLength_IsAlways16() | ||
| { | ||
| var rdata = new DnsAAAARecordData(IPAddress.Parse("2001:db8::abcd")); | ||
|
|
||
| Assert.AreEqual(16, rdata.UncompressedLength); | ||
| } | ||
|
|
||
| private static byte[] Serialize(DnsResourceRecord rr) | ||
| { | ||
| using MemoryStream ms = new(); | ||
| rr.WriteTo(ms); | ||
| return ms.ToArray(); | ||
| } | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
TechnitiumLibrary.Tests/TechnitiumLibrary.Net/Dns/ResourceRecords/DnsALIASRecordDataTests.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,114 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using TechnitiumLibrary.Net.Dns; | ||
| using TechnitiumLibrary.Net.Dns.ResourceRecords; | ||
|
|
||
| namespace TechnitiumLibrary.Tests.TechnitiumLibrary.Net.Dns.ResourceRecords | ||
| { | ||
| [TestClass] | ||
| public class DnsALIASRecordDataTests | ||
| { | ||
| [TestMethod] | ||
| public void Constructor_ValidInput_Succeeds() | ||
| { | ||
| var rdata = new DnsALIASRecordData( | ||
| DnsResourceRecordType.A, | ||
| "example.net."); | ||
|
|
||
| Assert.AreEqual(DnsResourceRecordType.A, rdata.Type); | ||
| Assert.AreEqual("example.net", rdata.Domain); // It is expected to remove explicit root dot | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_DifferentType_IsFalse() | ||
| { | ||
| var a = new DnsALIASRecordData( | ||
| DnsResourceRecordType.A, | ||
| "example.com."); | ||
|
|
||
| var b = new DnsALIASRecordData( | ||
| DnsResourceRecordType.AAAA, | ||
| "example.com."); | ||
|
|
||
| Assert.IsFalse(a.Equals(b)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Equals_SameTypeAndDomain_IgnoresCase() | ||
| { | ||
| var a = new DnsALIASRecordData( | ||
| DnsResourceRecordType.AAAA, | ||
| "Example.COM."); | ||
|
|
||
| var b = new DnsALIASRecordData( | ||
| DnsResourceRecordType.AAAA, | ||
| "example.com."); | ||
|
|
||
| Assert.IsTrue(a.Equals(b)); | ||
| Assert.AreEqual(a.GetHashCode(), b.GetHashCode()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void RoundTrip_StreamConstructor_PreservesEquality() | ||
| { | ||
| var original = new DnsResourceRecord( | ||
| "example", | ||
| DnsResourceRecordType.ALIAS, | ||
| DnsClass.IN, | ||
| 300, | ||
| new DnsALIASRecordData( | ||
| DnsResourceRecordType.A, | ||
| "target.example.")); | ||
|
|
||
| byte[] wire = Serialize(original); | ||
|
|
||
| using MemoryStream ms = new(wire); | ||
| var parsed = new DnsResourceRecord(ms); | ||
|
|
||
| Assert.AreEqual(original, parsed); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void SerializeTo_ProducesExpectedJson() | ||
| { | ||
| var rdata = new DnsALIASRecordData( | ||
| DnsResourceRecordType.AAAA, | ||
| "example.net."); | ||
|
|
||
| using MemoryStream ms = new(); | ||
| using var writer = new System.Text.Json.Utf8JsonWriter(ms); | ||
|
|
||
| rdata.SerializeTo(writer); | ||
| writer.Flush(); | ||
|
|
||
| string json = Encoding.UTF8.GetString(ms.ToArray()); | ||
|
|
||
| StringAssert.Contains(json, "Type"); | ||
| StringAssert.Contains(json, "AAAA"); | ||
| StringAssert.Contains(json, "Domain"); | ||
| StringAssert.Contains(json, "example.net"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void UncompressedLength_IncludesTypePrefix() | ||
| { | ||
| var rdata = new DnsALIASRecordData( | ||
| DnsResourceRecordType.A, | ||
| "example.com."); | ||
|
|
||
| int baseLength = rdata.Domain.Length; // not exact, but sanity check | ||
|
|
||
| Assert.IsTrue(rdata.UncompressedLength > baseLength); | ||
| Assert.IsTrue(rdata.UncompressedLength >= 2); | ||
| } | ||
|
|
||
| private static byte[] Serialize(DnsResourceRecord rr) | ||
| { | ||
| using MemoryStream ms = new(); | ||
| rr.WriteTo(ms); | ||
| return ms.ToArray(); | ||
| } | ||
| } | ||
| } |
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.