-
Notifications
You must be signed in to change notification settings - Fork 59
Added unit tests to TechnitiumLibrary.Net.Firewall #36
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
8
commits into
TechnitiumSoftware:master
Choose a base branch
from
zbalkan:feat/unit-test/technitiumlibrary.net.firewall
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8989180
Added unit test project, action and status badge to TechnitiumLibrary
zbalkan 13d1164
Added unit tests
zbalkan 9fd7bbd
Downgraded solution version
zbalkan de1e1fe
Fixed workflow
zbalkan 4187fb2
Fixed test class name
zbalkan 30db6c1
Updated workflow file
zbalkan 64efaf9
Added OSCondition
zbalkan 47cf1f6
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@v2 | ||
|
|
||
| - 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.UnitTests\TechnitiumLibrary.UnitTests.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) |
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)] |
80 changes: 80 additions & 0 deletions
80
TechnitiumLibrary.UnitTests/TechnitiumLibrary.Net.Firewall/WindowsFirewallTests.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,80 @@ | ||
| /* | ||
| Technitium Library | ||
| Copyright (C) 2026 Shreyas Zare (shreyas@technitium.com) | ||
| Copyright (C) 2026 Zafer Balkan (zafer@zaferbalkan.com) | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| */ | ||
|
|
||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using TechnitiumLibrary.Net.Firewall; | ||
|
|
||
| namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary.Net.Firewall | ||
| { | ||
| [TestClass] | ||
| public sealed class WindowsFirewallTests | ||
| { | ||
| [TestMethod] | ||
| [OSCondition(OperatingSystems.Windows)] | ||
| public void AddPort_ShouldThrow_WhenUnsupportedProtocol() | ||
| { | ||
| // Protocol ICMPv4 cannot be added using AddPort | ||
| Assert.ThrowsExactly<Exception>(() => WindowsFirewall.AddPort("bad", Protocol.ICMPv4, port: 55, enable: true)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [OSCondition(OperatingSystems.Windows)] | ||
| public void RemovePort_ShouldThrow_WhenUnsupportedProtocol() | ||
| { | ||
| // RemovePort validates only TCP, UDP, ANY | ||
| Assert.ThrowsExactly<Exception>(() => WindowsFirewall.RemovePort(Protocol.IGMP, 123)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [OSCondition(OperatingSystems.Windows)] | ||
| public void PortExists_ShouldThrow_WhenUnsupportedProtocol() | ||
| { | ||
| Assert.ThrowsExactly<Exception>(() => WindowsFirewall.PortExists(Protocol.IGMP, 44)); | ||
zbalkan marked this conversation as resolved.
Show resolved
Hide resolved
zbalkan marked this conversation as resolved.
Show resolved
Hide resolved
zbalkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| [OSCondition(OperatingSystems.Windows)] | ||
| public void RuleExistsVista_ShouldReturnDoesNotExist_WhenInputsClearlyNotMatchingAnything() | ||
| { | ||
| // Since firewall is not guaranteed to have this rule, | ||
| // safest expected response is DoesNotExists. | ||
| RuleStatus result = WindowsFirewall.RuleExistsVista( | ||
| name: "__Definitely_Not_A_Real_Rule__", | ||
| applicationPath: "__Fake__"); | ||
|
|
||
| Assert.AreEqual(RuleStatus.DoesNotExists, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [OSCondition(OperatingSystems.Windows)] | ||
| public void ApplicationExists_ShouldReturnDoesNotExist_WhenApplicationIsNotRegistered() | ||
| { | ||
| // Public observable guarantee: | ||
| // if the system has no such application entry → DoesNotExists | ||
|
|
||
| const string fakePath = "C:\\DefinitelyNotExisting\\app.exe"; | ||
|
|
||
| RuleStatus status = WindowsFirewall.ApplicationExists(fakePath); | ||
|
|
||
| Assert.AreEqual(RuleStatus.DoesNotExists, status); | ||
| } | ||
| } | ||
| } | ||
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.Net.Firewall\TechnitiumLibrary.Net.Firewall.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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
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 |
|---|---|---|
|
|
@@ -317,4 +317,4 @@ public static byte[] FromBase32HexString(string data) | |
|
|
||
| #endregion | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -112,4 +112,4 @@ public static int GetArrayHashCode<T>(this IReadOnlyCollection<T> value) | |
| return hashCode; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.