Skip to content
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

Common: Added Test-IPAddress function #449

Merged
merged 25 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1dd0e17
Initial failing test
valainisgt Apr 25, 2020
82023fc
Fixed command not found error
valainisgt Apr 25, 2020
5083296
Added implementation
valainisgt Apr 25, 2020
3906041
Added more test cases
valainisgt Apr 25, 2020
372fc13
Added IPv6 test cases
valainisgt Apr 25, 2020
a97d1cb
Added invalid address test case
valainisgt Apr 25, 2020
2f66143
Added test case for IPv4 Mismatch
valainisgt Apr 25, 2020
be4d571
Added test case for IPv6 mismatch error
valainisgt Apr 25, 2020
ef81b28
Added localized formatting string
valainisgt Apr 25, 2020
d1189e8
Updated test cases to look more like existing
valainisgt Apr 25, 2020
ae92163
Removed import from previous commit
valainisgt Apr 25, 2020
afd42d1
Fix issue #408
valainisgt Apr 25, 2020
26d5e92
Description of changes in changelog
valainisgt Apr 25, 2020
4f10a99
Removed unused DefaultGatewayAddress localization strings. Updated t…
valainisgt Apr 26, 2020
af49697
Removed unused DnsServerAddress localization strings. Updated tests …
valainisgt Apr 26, 2020
4e95581
Removed unused IPAddress localization strings. Updated tests to use …
valainisgt Apr 26, 2020
83f9c30
Removed unused Route localization strings. Updated tests to use nece…
valainisgt Apr 26, 2020
b45296b
typo
valainisgt Apr 26, 2020
56c4348
Merge branch 'master' into refactor-ipaddress-validation
PlagueHO Apr 29, 2020
d826310
Changed function name from Test-IPAddress to Assert-IPAddress
valainisgt Apr 30, 2020
a8a6929
Updated Assert-IPAddress to not require address family parameter. Ad…
valainisgt Apr 30, 2020
298682d
Updated Assert-IPAddress to pass tests
valainisgt Apr 30, 2020
a60daf0
Updated Assert-IPAddress to hold onto the potentially parsed IP Addre…
valainisgt Apr 30, 2020
b708560
Updated Assert-IPAddress
valainisgt Apr 30, 2020
4d110e2
Source code formatting updates.
valainisgt May 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
for the DSC resource.
- Added build task `Generate_Wiki_Content` to generate the wiki content
that can be used to update the GitHub Wiki.
- Common
- Added Assert-IPAddress function to reduce code duplication - Fixes
[Issue #408](https://github.com/dsccommunity/NetworkingDsc/issues/408).

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,30 +298,7 @@ function Assert-ResourceProperty

if ($Address)
{
if (-not ([System.Net.IPAddress]::TryParse($Address, [ref]0)))
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressFormatError -f $Address) `
-ArgumentName 'Address'
}

$detectedAddressFamily = ([System.Net.IPAddress] $Address).AddressFamily.ToString()

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork.ToString()) `
-and ($AddressFamily -ne 'IPv4'))
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressIPv4MismatchError -f $Address, $AddressFamily) `
-ArgumentName 'AddressFamily'
}

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6.ToString()) `
-and ($AddressFamily -ne 'IPv6'))
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressIPv6MismatchError -f $Address, $AddressFamily) `
-ArgumentName 'AddressFamily'
}
Assert-IPAddress -Address $Address -AddressFamily $AddressFamily
}
} # Assert-ResourceProperty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ ConvertFrom-StringData @'
DefaultGatewayExistsButShouldNotMessage = Default gateway exists but it should not.
DefaultGatewayExistsAndShouldMessage = Default Gateway does not exist which is correct.
InterfaceNotAvailableError = Interface "{0}" is not available. Please select a valid interface and try again.
AddressFormatError = Address "{0}" is not in the correct format. Please correct the Address parameter in the configuration and try again.
AddressIPv4MismatchError = Address "{0}" is in IPv4 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
AddressIPv6MismatchError = Address "{0}" is in IPv6 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -319,29 +319,7 @@ function Assert-ResourceProperty
-ArgumentName 'InterfaceAlias'
}

if ( -not ([System.Net.IPAddress]::TryParse($Address, [ref]0)))
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressFormatError -f $Address) `
-ArgumentName 'Address'
}

$detectedAddressFamily = ([System.Net.IPAddress]$Address).AddressFamily.ToString()
if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork.ToString()) `
-and ($AddressFamily -ne 'IPv4'))
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressIPv4MismatchError -f $Address,$AddressFamily) `
-ArgumentName 'Address'
}

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6.ToString()) `
-and ($AddressFamily -ne 'IPv6'))
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressIPv6MismatchError -f $Address,$AddressFamily) `
-ArgumentName 'Address'
}
Assert-IPAddress -Address $Address -AddressFamily $AddressFamily
} # Assert-ResourceProperty

Export-ModuleMember -function *-TargetResource
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ ConvertFrom-StringData @'
DNSServersHaveBeenSetCorrectlyMessage = DNS server addresses were set to the desired state.
DNSServersHaveBeenSetToDHCPMessage = DNS server addresses were set to the desired state of DHCP.
InterfaceNotAvailableError = Interface "{0}" is not available. Please select a valid interface and try again.
AddressFormatError = Address "{0}" is not in the correct format. Please correct the Address parameter in the configuration and try again.
AddressIPv4MismatchError = Address "{0}" is in IPv4 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
AddressIPv6MismatchError = Address "{0}" is in IPv6 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
'@
25 changes: 1 addition & 24 deletions source/DSCResources/DSC_IPAddress/DSC_IPAddress.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -478,30 +478,7 @@ function Assert-ResourceProperty
{
$singleIP = ($singleIPAddress -split '/')[0]

if (-not ([System.Net.Ipaddress]::TryParse($singleIP, [ref]0)))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressFormatError) -f $singleIPAddress) `
-ArgumentName 'IPAddress'
}

$detectedAddressFamily = ([System.Net.IPAddress]$singleIP).AddressFamily.ToString()

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork.ToString()) `
-and ($AddressFamily -ne 'IPv4'))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressIPv4MismatchError) -f $singleIPAddress, $AddressFamily) `
-ArgumentName 'IPAddress'
}

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6.ToString()) `
-and ($AddressFamily -ne 'IPv6'))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressIPv6MismatchError) -f $singleIPAddress, $AddressFamily) `
-ArgumentName 'IPAddress'
}
Assert-IPAddress -Address $singleIP -AddressFamily $AddressFamily
}

foreach ($prefixLength in $prefixLengthArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ ConvertFrom-StringData @'
PrefixLengthDoesNotMatchMessage = Prefix Length does NOT match desired state. Expected {0}, actual {1}.
PrefixLengthMatchMessage = Prefix Length is in desired state.
InterfaceNotAvailableError = Interface "{0}" is not available. Please select a valid interface and try again.
AddressFormatError = Address "{0}" is not in the correct format. Please correct the Address parameter in the configuration and try again.
AddressIPv4MismatchError = Address "{0}" is in IPv4 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
AddressIPv6MismatchError = Address "{0}" is in IPv6 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
PrefixLengthError = A Prefix Length of {0} is not valid for {1} addresses. Please correct the Prefix Length and try again.
'@
50 changes: 2 additions & 48 deletions source/DSCResources/DSC_Route/DSC_Route.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -622,56 +622,10 @@ function Assert-ResourceProperty
$components = $DestinationPrefix -split '/'
$prefix = $components[0]

if (-not ([System.Net.Ipaddress]::TryParse($prefix, [ref]0)))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressFormatError) -f $prefix) `
-ArgumentName 'DestinationPrefix'
}

$detectedAddressFamily = ([System.Net.IPAddress] $prefix).AddressFamily.ToString()

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork.ToString()) `
-and ($AddressFamily -ne 'IPv4'))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressIPv4MismatchError) -f $prefix, $AddressFamily) `
-ArgumentName 'DestinationPrefix'
}

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6.ToString()) `
-and ($AddressFamily -ne 'IPv6'))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressIPv6MismatchError) -f $prefix, $AddressFamily) `
-ArgumentName 'DestinationPrefix'
}
Assert-IPAddress -Address $prefix -AddressFamily $AddressFamily

# Validate the NextHop Parameter
if (-not ([System.Net.Ipaddress]::TryParse($NextHop, [ref]0)))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressFormatError) -f $NextHop) `
-ArgumentName 'NextHop'
}

$detectedAddressFamily = ([System.Net.IPAddress] $NextHop).AddressFamily.ToString()

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork.ToString()) `
-and ($AddressFamily -ne 'IPv4'))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressIPv4MismatchError) -f $NextHop, $AddressFamily) `
-ArgumentName 'NextHop'
}

if (($detectedAddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6.ToString()) `
-and ($AddressFamily -ne 'IPv6'))
{
New-InvalidArgumentException `
-Message $($($script:localizedData.AddressIPv6MismatchError) -f $NextHop, $AddressFamily) `
-ArgumentName 'NextHop'
}
Assert-IPAddress -Address $NextHop -AddressFamily $AddressFamily
}

Export-ModuleMember -Function *-TargetResource
3 changes: 0 additions & 3 deletions source/DSCResources/DSC_Route/en-US/DSC_Route.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ ConvertFrom-StringData @'
RouteExistsButShouldNotMessage = '{0}' route on '{1}' destination '{2}' nexthop '{3}' exists but should not. Change required.
RouteDoesNotExistAndShouldNotMessage = '{0}' route on '{1}' destination '{2}' nexthop '{3}' does not exist and should not. Change not required.
InterfaceNotAvailableError = Interface '{0}' is not available. Please select a valid interface and try again.
AddressFormatError = Address '{0}' is not in the correct format. Please correct the Address parameter in the configuration and try again.
AddressIPv4MismatchError = Address '{0}' is in IPv4 format, which does not match address family '{1}'. Please correct either of them in the configuration and try again.
AddressIPv6MismatchError = Address '{0}' is in IPv6 format, which does not match address family '{1}'. Please correct either of them in the configuration and try again.
'@
65 changes: 64 additions & 1 deletion source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,68 @@ function Format-Win32NetworkAdapterFilterByNetConnectionID
$returnNetAdapaterFilter
}

<#
.SYNOPSIS
Check the Address details are valid and do not conflict with Address family.
If any problems are detected an exception will be thrown.

.PARAMETER AddressFamily
IP address family.

.PARAMETER Address
IP Address
#>
function Assert-IPAddress
{
[CmdletBinding()]
param
(
[Parameter()]
[ValidateSet('IPv4', 'IPv6')]
[System.String]
$AddressFamily,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Address
)

[System.Net.IPAddress] $ipAddress = $null

if (-not ([System.Net.IPAddress]::TryParse($Address, [ref] $ipAddress)))
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressFormatError -f $Address) `
-ArgumentName 'Address'
}

if ($AddressFamily)
{
switch ($AddressFamily)
{
'IPv4'
{
if ($ipAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork.ToString())
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressIPv6MismatchError -f $Address, $AddressFamily) `
-ArgumentName 'AddressFamily'
}
}
'IPv6'
{
if ($ipAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetworkV6.ToString())
{
New-InvalidArgumentException `
-Message ($script:localizedData.AddressIPv4MismatchError -f $Address, $AddressFamily) `
-ArgumentName 'AddressFamily'
}
}
}
}
}

# Import Localization Strings
$script:localizedData = Get-LocalizedData `
-ResourceName 'NetworkingDsc.Common' `
Expand All @@ -1433,5 +1495,6 @@ Export-ModuleMember -Function @(
'Test-DscObjectHasProperty'
'ConvertTo-HashTable',
'ConvertTo-CimInstance',
'Format-Win32NetworkAdapterFilterByNetConnectionID'
'Format-Win32NetworkAdapterFilterByNetConnectionID',
'Assert-IPAddress'
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ ConvertFrom-StringData @'
MatchElementValueMessage = MATCH: Value [{0}] (type '{1}') for property '{2}' does match. Current state is '{3}' and desired state is '{4}'.
TestDscParameterResultMessage = Test-DscParameter result is '{0}'.
StartingReverseCheck = Starting with a reverse check.
AddressFormatError = Address "{0}" is not in the correct format. Please correct the Address parameter in the configuration and try again.
AddressIPv4MismatchError = Address "{0}" is in IPv4 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
AddressIPv6MismatchError = Address "{0}" is in IPv6 format, which does not match server address family {1}. Please correct either of them in the configuration and try again.
'@
11 changes: 8 additions & 3 deletions tests/Unit/DSC_DefaultGatewayAddress.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ try
}
}

$commonLocalizedData = Get-LocalizedData `
-ResourceName 'NetworkingDsc.Common' `
-ScriptRoot (Join-Path -Path $PSScriptRoot `
-ChildPath '..\..\source\Modules\NetworkingDsc.Common\')

Describe 'DSC_DefaultGatewayAddress\Get-TargetResource' -Tag 'Get' {
Context 'When interface has a default gateway set' {
Mock -CommandName Get-NetRoute -MockWith $getNetRouteIpv4_Mock
Expand Down Expand Up @@ -220,7 +225,7 @@ try
}

$errorRecord = Get-InvalidArgumentRecord `
-Message ($script:localizedData.AddressFormatError -f $assertResourcePropertyParameters.Address) `
-Message ($commonLocalizedData.AddressFormatError -f $assertResourcePropertyParameters.Address) `
-ArgumentName 'Address'

{ Assert-ResourceProperty @assertResourcePropertyParameters } | Should -Throw $ErrorRecord
Expand All @@ -236,7 +241,7 @@ try
}

$errorRecord = Get-InvalidArgumentRecord `
-Message ($script:localizedData.AddressIPv4MismatchError -f $assertResourcePropertyParameters.Address, $assertResourcePropertyParameters.AddressFamily) `
-Message ($commonLocalizedData.AddressIPv4MismatchError -f $assertResourcePropertyParameters.Address, $assertResourcePropertyParameters.AddressFamily) `
-ArgumentName 'AddressFamily'

{ Assert-ResourceProperty @assertResourcePropertyParameters } | Should -Throw $ErrorRecord
Expand All @@ -252,7 +257,7 @@ try
}

$errorRecord = Get-InvalidArgumentRecord `
-Message ($script:localizedData.AddressIPv6MismatchError -f $assertResourcePropertyParameters.Address, $assertResourcePropertyParameters.AddressFamily) `
-Message ($commonLocalizedData.AddressIPv6MismatchError -f $assertResourcePropertyParameters.Address, $assertResourcePropertyParameters.AddressFamily) `
-ArgumentName 'AddressFamily'

{ Assert-ResourceProperty @assertResourcePropertyParameters } | Should -Throw $ErrorRecord
Expand Down
12 changes: 9 additions & 3 deletions tests/Unit/DSC_DnsServerAddress.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Invoke-TestSetup
try
{
InModuleScope $script:dscResourceName {

$commonLocalizedData = Get-LocalizedData `
-ResourceName 'NetworkingDsc.Common' `
-ScriptRoot (Join-Path -Path $PSScriptRoot `
-ChildPath '..\..\source\Modules\NetworkingDsc.Common\')

Describe 'DSC_DnsServerAddress\Get-TargetResource' -Tag 'Get' {
Context 'Test IPv4' {
Context 'Invoking with an IPv4 address and one address is currently set' {
Expand Down Expand Up @@ -608,7 +614,7 @@ try
}

$errorRecord = Get-InvalidArgumentRecord `
-Message ($script:localizedData.AddressFormatError -f $assertResourcePropertySplat.Address) `
-Message ($commonLocalizedData.AddressFormatError -f $assertResourcePropertySplat.Address) `
-ArgumentName 'Address'

{ Assert-ResourceProperty @assertResourcePropertySplat } | Should -Throw $ErrorRecord
Expand All @@ -625,7 +631,7 @@ try
}

$errorRecord = Get-InvalidArgumentRecord `
-Message ($script:localizedData.AddressIPv4MismatchError -f $assertResourcePropertySplat.Address, $assertResourcePropertySplat.AddressFamily) `
-Message ($commonLocalizedData.AddressIPv4MismatchError -f $assertResourcePropertySplat.Address, $assertResourcePropertySplat.AddressFamily) `
-ArgumentName 'Address'

{ Assert-ResourceProperty @assertResourcePropertySplat } | Should -Throw $ErrorRecord
Expand All @@ -642,7 +648,7 @@ try
}

$errorRecord = Get-InvalidArgumentRecord `
-Message ($script:localizedData.AddressIPv6MismatchError -f $assertResourcePropertySplat.Address, $assertResourcePropertySplat.AddressFamily) `
-Message ($commonLocalizedData.AddressIPv6MismatchError -f $assertResourcePropertySplat.Address, $assertResourcePropertySplat.AddressFamily) `
-ArgumentName 'Address'

{ Assert-ResourceProperty @assertResourcePropertySplat } | Should -Throw $ErrorRecord
Expand Down
Loading