-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
070cca7
commit d02c026
Showing
3 changed files
with
60 additions
and
2 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
54 changes: 54 additions & 0 deletions
54
tests/PSRule.Rules.Azure.Tests/NetworkSecurityGroupEvaluatorTests.cs
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,54 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Management.Automation; | ||
|
||
namespace PSRule.Rules.Azure.Data.Network; | ||
|
||
#nullable enable | ||
|
||
public sealed class NetworkSecurityGroupEvaluatorTests | ||
{ | ||
[Fact] | ||
public void Outbound_WhenMatchingRule_ShouldReturnAccess() | ||
{ | ||
var evaluator = new NetworkSecurityGroupEvaluator(); | ||
evaluator.With([Rule(access: "Deny", destinationAddressPrefix: "VirtualNetwork", destinationPortRanges: ["3389"])]); | ||
|
||
Assert.Equal(Access.Deny, evaluator.Outbound("VirtualNetwork", 3389)); | ||
} | ||
|
||
[Fact] | ||
public void With_WhenDestinationPortRangeIsEmptyString_ShouldUseDestinationPortRanges() | ||
{ | ||
var evaluator = new NetworkSecurityGroupEvaluator(); | ||
evaluator.With([Rule(access: "Deny", destinationPortRange: "", destinationPortRanges: ["80", "443"])]); | ||
|
||
Assert.Equal(Access.Deny, evaluator.Outbound("Virtual Network", 443)); | ||
} | ||
|
||
#region Helper methods | ||
|
||
private static PSObject Rule(string direction = "Outbound", string access = "Allow", string protocol = "Tcp", string? destinationAddressPrefix = default, string? destinationPortRange = default, string[]? destinationPortRanges = default) | ||
{ | ||
var properties = new PSObject(); | ||
properties.Properties.Add(new PSNoteProperty("direction", direction)); | ||
properties.Properties.Add(new PSNoteProperty("access", access)); | ||
properties.Properties.Add(new PSNoteProperty("protocol", protocol)); | ||
properties.Properties.Add(new PSNoteProperty("destinationAddressPrefix", destinationAddressPrefix)); | ||
|
||
if (destinationPortRange != null) | ||
properties.Properties.Add(new PSNoteProperty("destinationPortRange", destinationPortRange)); | ||
|
||
if (destinationPortRanges != null) | ||
properties.Properties.Add(new PSNoteProperty("destinationPortRanges", destinationPortRanges)); | ||
|
||
var result = new PSObject(); | ||
result.Properties.Add(new PSNoteProperty("properties", properties)); | ||
return result; | ||
} | ||
|
||
#endregion Helper methods | ||
} | ||
|
||
#nullable restore |