diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b964ee4..1930ade5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/source/DSCResources/DSC_DefaultGatewayAddress/DSC_DefaultGatewayAddress.psm1 b/source/DSCResources/DSC_DefaultGatewayAddress/DSC_DefaultGatewayAddress.psm1 index ca980b24..62bec729 100644 --- a/source/DSCResources/DSC_DefaultGatewayAddress/DSC_DefaultGatewayAddress.psm1 +++ b/source/DSCResources/DSC_DefaultGatewayAddress/DSC_DefaultGatewayAddress.psm1 @@ -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 diff --git a/source/DSCResources/DSC_DefaultGatewayAddress/en-US/DSC_DefaultGatewayAddress.strings.psd1 b/source/DSCResources/DSC_DefaultGatewayAddress/en-US/DSC_DefaultGatewayAddress.strings.psd1 index 8025ff85..66f757a1 100644 --- a/source/DSCResources/DSC_DefaultGatewayAddress/en-US/DSC_DefaultGatewayAddress.strings.psd1 +++ b/source/DSCResources/DSC_DefaultGatewayAddress/en-US/DSC_DefaultGatewayAddress.strings.psd1 @@ -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. '@ diff --git a/source/DSCResources/DSC_DnsServerAddress/DSC_DnsServerAddress.psm1 b/source/DSCResources/DSC_DnsServerAddress/DSC_DnsServerAddress.psm1 index fe3e2470..f382ba9f 100644 --- a/source/DSCResources/DSC_DnsServerAddress/DSC_DnsServerAddress.psm1 +++ b/source/DSCResources/DSC_DnsServerAddress/DSC_DnsServerAddress.psm1 @@ -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 diff --git a/source/DSCResources/DSC_DnsServerAddress/en-US/DSC_DnsServerAddress.strings.psd1 b/source/DSCResources/DSC_DnsServerAddress/en-US/DSC_DnsServerAddress.strings.psd1 index c4ce3598..b331456f 100644 --- a/source/DSCResources/DSC_DnsServerAddress/en-US/DSC_DnsServerAddress.strings.psd1 +++ b/source/DSCResources/DSC_DnsServerAddress/en-US/DSC_DnsServerAddress.strings.psd1 @@ -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. '@ diff --git a/source/DSCResources/DSC_IPAddress/DSC_IPAddress.psm1 b/source/DSCResources/DSC_IPAddress/DSC_IPAddress.psm1 index a8c2c2be..0d1301e8 100644 --- a/source/DSCResources/DSC_IPAddress/DSC_IPAddress.psm1 +++ b/source/DSCResources/DSC_IPAddress/DSC_IPAddress.psm1 @@ -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) diff --git a/source/DSCResources/DSC_IPAddress/en-US/DSC_IPAddress.strings.psd1 b/source/DSCResources/DSC_IPAddress/en-US/DSC_IPAddress.strings.psd1 index e05a7a7d..a0ea4554 100644 --- a/source/DSCResources/DSC_IPAddress/en-US/DSC_IPAddress.strings.psd1 +++ b/source/DSCResources/DSC_IPAddress/en-US/DSC_IPAddress.strings.psd1 @@ -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. '@ diff --git a/source/DSCResources/DSC_Route/DSC_Route.psm1 b/source/DSCResources/DSC_Route/DSC_Route.psm1 index 5a3d28a5..e0430897 100644 --- a/source/DSCResources/DSC_Route/DSC_Route.psm1 +++ b/source/DSCResources/DSC_Route/DSC_Route.psm1 @@ -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 diff --git a/source/DSCResources/DSC_Route/en-US/DSC_Route.strings.psd1 b/source/DSCResources/DSC_Route/en-US/DSC_Route.strings.psd1 index c99c8dae..256a7fa6 100644 --- a/source/DSCResources/DSC_Route/en-US/DSC_Route.strings.psd1 +++ b/source/DSCResources/DSC_Route/en-US/DSC_Route.strings.psd1 @@ -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. '@ diff --git a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 index a02a19dd..40b3e352 100644 --- a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 +++ b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 @@ -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' ` @@ -1433,5 +1495,6 @@ Export-ModuleMember -Function @( 'Test-DscObjectHasProperty' 'ConvertTo-HashTable', 'ConvertTo-CimInstance', - 'Format-Win32NetworkAdapterFilterByNetConnectionID' + 'Format-Win32NetworkAdapterFilterByNetConnectionID', + 'Assert-IPAddress' ) diff --git a/source/Modules/NetworkingDsc.Common/en-US/NetworkingDsc.Common.strings.psd1 b/source/Modules/NetworkingDsc.Common/en-US/NetworkingDsc.Common.strings.psd1 index 5514253f..b5056012 100644 --- a/source/Modules/NetworkingDsc.Common/en-US/NetworkingDsc.Common.strings.psd1 +++ b/source/Modules/NetworkingDsc.Common/en-US/NetworkingDsc.Common.strings.psd1 @@ -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. '@ diff --git a/tests/Unit/DSC_DefaultGatewayAddress.Tests.ps1 b/tests/Unit/DSC_DefaultGatewayAddress.Tests.ps1 index 6246f970..2de5f315 100644 --- a/tests/Unit/DSC_DefaultGatewayAddress.Tests.ps1 +++ b/tests/Unit/DSC_DefaultGatewayAddress.Tests.ps1 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/Unit/DSC_DnsServerAddress.Tests.ps1 b/tests/Unit/DSC_DnsServerAddress.Tests.ps1 index 2a1f21fc..1005ce08 100644 --- a/tests/Unit/DSC_DnsServerAddress.Tests.ps1 +++ b/tests/Unit/DSC_DnsServerAddress.Tests.ps1 @@ -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' { @@ -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 @@ -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 @@ -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 diff --git a/tests/Unit/DSC_IPAddress.Tests.ps1 b/tests/Unit/DSC_IPAddress.Tests.ps1 index 7a1b40ea..7f6a0511 100644 --- a/tests/Unit/DSC_IPAddress.Tests.ps1 +++ b/tests/Unit/DSC_IPAddress.Tests.ps1 @@ -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_IPAddress\Get-TargetResource' -Tag 'Get' { Context 'Invoked with a single IP address' { Mock -CommandName Get-NetIPAddress -MockWith { @@ -587,8 +593,8 @@ try } $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressFormatError -f $testGetResourceParameters.IPAddress) ` - -ArgumentName 'IPAddress' + -Message ($commonLocalizedData.AddressFormatError -f $testGetResourceParameters.IPAddress) ` + -ArgumentName 'Address' { $result = Test-TargetResource @testGetResourceParameters } | Should -Throw $errorRecord } @@ -923,8 +929,8 @@ try } $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressFormatError -f $testGetResourceParameters.IPAddress) ` - -ArgumentName 'IPAddress' + -Message ($commonLocalizedData.AddressFormatError -f $testGetResourceParameters.IPAddress) ` + -ArgumentName 'Address' { $result = Test-TargetResource @testGetResourceParameters } | Should -Throw $errorRecord } @@ -1121,8 +1127,8 @@ try } $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressFormatError -f $assertResourcePropertyParameters.IPAddress) ` - -ArgumentName 'IPAddress' + -Message ($commonLocalizedData.AddressFormatError -f $assertResourcePropertyParameters.IPAddress) ` + -ArgumentName 'Address' { Assert-ResourceProperty @assertResourcePropertyParameters } | Should -Throw $errorRecord } @@ -1136,9 +1142,11 @@ try AddressFamily = 'IPv6' } + $address = ($assertResourcePropertyParameters.IPAddress -split '/')[0] + $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressIPv4MismatchError -f $assertResourcePropertyParameters.IPAddress, $assertResourcePropertyParameters.AddressFamily) ` - -ArgumentName 'IPAddress' + -Message ($commonLocalizedData.AddressIPv4MismatchError -f $address, $assertResourcePropertyParameters.AddressFamily) ` + -ArgumentName 'Address' { Assert-ResourceProperty @assertResourcePropertyParameters } | Should -Throw $errorRecord } @@ -1153,8 +1161,8 @@ try } $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressIPv6MismatchError -f $assertResourcePropertyParameters.IPAddress, $assertResourcePropertyParameters.AddressFamily) ` - -ArgumentName 'IPAddress' + -Message ($commonLocalizedData.AddressIPv6MismatchError -f $assertResourcePropertyParameters.IPAddress, $assertResourcePropertyParameters.AddressFamily) ` + -ArgumentName 'Address' { Assert-ResourceProperty @assertResourcePropertyParameters } | Should -Throw $errorRecord } diff --git a/tests/Unit/DSC_Route.Tests.ps1 b/tests/Unit/DSC_Route.Tests.ps1 index 3688c109..53c6d513 100644 --- a/tests/Unit/DSC_Route.Tests.ps1 +++ b/tests/Unit/DSC_Route.Tests.ps1 @@ -66,6 +66,11 @@ try PreferredLifetime = ([Timespan]::FromSeconds($testRoute.PreferredLifetime)) } + $commonLocalizedData = Get-LocalizedData ` + -ResourceName 'NetworkingDsc.Common' ` + -ScriptRoot (Join-Path -Path $PSScriptRoot ` + -ChildPath '..\..\source\Modules\NetworkingDsc.Common\') + Describe 'DSC_Route\Get-TargetResource' -Tag 'Get' { Context 'Route does not exist' { Mock -CommandName Get-NetRoute @@ -390,8 +395,8 @@ try $Splat.AddressFamily = 'IPv4' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressFormatError -f '10.0.300.0') ` - -ArgumentName 'DestinationPrefix' + -Message ($commonLocalizedData.AddressFormatError -f '10.0.300.0') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } @@ -407,8 +412,8 @@ try $Splat.AddressFamily = 'IPv6' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressFormatError -f 'fe8x::') ` - -ArgumentName 'DestinationPrefix' + -Message ($commonLocalizedData.AddressFormatError -f 'fe8x::') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } @@ -424,8 +429,8 @@ try $Splat.AddressFamily = 'IPv4' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressIPv6MismatchError -f 'fe80::', 'IPv4') ` - -ArgumentName 'DestinationPrefix' + -Message ($commonLocalizedData.AddressIPv6MismatchError -f 'fe80::', 'IPv4') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } @@ -441,8 +446,8 @@ try $Splat.AddressFamily = 'IPv6' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressIPv4MismatchError -f '10.0.0.0', 'IPv6') ` - -ArgumentName 'DestinationPrefix' + -Message ($commonLocalizedData.AddressIPv4MismatchError -f '10.0.0.0', 'IPv6') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } @@ -458,8 +463,8 @@ try $Splat.AddressFamily = 'IPv4' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressFormatError -f '10.0.300.0') ` - -ArgumentName 'NextHop' + -Message ($commonLocalizedData.AddressFormatError -f '10.0.300.0') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } @@ -475,8 +480,8 @@ try $Splat.AddressFamily = 'IPv6' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressFormatError -f 'fe9x::') ` - -ArgumentName 'NextHop' + -Message ($commonLocalizedData.AddressFormatError -f 'fe9x::') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } @@ -492,8 +497,8 @@ try $Splat.AddressFamily = 'IPv4' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressIPv6MismatchError -f 'fe90::', 'IPv4') ` - -ArgumentName 'NextHop' + -Message ($commonLocalizedData.AddressIPv6MismatchError -f 'fe90::', 'IPv4') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } @@ -509,8 +514,8 @@ try $Splat.AddressFamily = 'IPv6' $errorRecord = Get-InvalidArgumentRecord ` - -Message ($script:localizedData.AddressIPv4MismatchError -f '10.0.1.0', 'IPv6') ` - -ArgumentName 'NextHop' + -Message ($commonLocalizedData.AddressIPv4MismatchError -f '10.0.1.0', 'IPv6') ` + -ArgumentName 'Address' { Assert-ResourceProperty @Splat } | Should -Throw $errorRecord } diff --git a/tests/Unit/NetworkingDsc.Common.tests.ps1 b/tests/Unit/NetworkingDsc.Common.tests.ps1 index d8e7c2b4..a23ae686 100644 --- a/tests/Unit/NetworkingDsc.Common.tests.ps1 +++ b/tests/Unit/NetworkingDsc.Common.tests.ps1 @@ -2321,4 +2321,87 @@ InModuleScope $script:subModuleName { } } } + + Describe 'NetworkingDsc.Common\Assert-IPAddress' { + Context 'When invoking with valid IPv4 Address' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = '192.168.0.1' + AddressFamily = 'IPv4' + } + + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + } + } + Context 'When invoking with valid IPv6 Address' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = 'fe80:ab04:30F5:002b::1' + AddressFamily = 'IPv6' + } + + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + } + } + Context 'When invoking with invalid IP Address' { + It 'Should throw an AddressFormatError error' { + $testIPAddressParameters = @{ + Address = 'NotReal' + AddressFamily = 'IPv4' + } + + $errorRecord = Get-InvalidArgumentRecord ` + -Message ($script:localizedData.AddressFormatError -f $testIPAddressParameters.Address) ` + -ArgumentName 'Address' + + { $script:result = Assert-IPAddress @testIPAddressParameters } | Should -Throw $errorRecord + } + } + Context 'When invoking with IPv4 Address and family mismatch' { + It 'Should throw an AddressMismatchError error' { + $testIPAddressParameters = @{ + Address = '192.168.0.1' + AddressFamily = 'IPv6' + } + + $errorRecord = Get-InvalidArgumentRecord ` + -Message ($script:localizedData.AddressIPv4MismatchError -f $testIPAddressParameters.Address, $testIPAddressParameters.AddressFamily) ` + -ArgumentName 'AddressFamily' + + { $script:result = Assert-IPAddress @testIPAddressParameters } | Should -Throw $errorRecord + } + } + Context 'When invoking with IPv6 Address and family mismatch' { + It 'Should throw an AddressMismatchError error' { + $testIPAddressParameters = @{ + Address = 'fe80::' + AddressFamily = 'IPv4' + } + + $errorRecord = Get-InvalidArgumentRecord ` + -Message ($script:localizedData.AddressIPv6MismatchError -f $testIPAddressParameters.Address, $testIPAddressParameters.AddressFamily) ` + -ArgumentName 'AddressFamily' + + { $script:result = Assert-IPAddress @testIPAddressParameters } | Should -Throw $errorRecord + } + } + Context 'When invoking with valid IPv4 Address with no address family' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = '192.168.0.1' + } + + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + } + } + Context 'When invoking with valid IPv6 Address with no address family' { + It 'Should not throw an error' { + $testIPAddressParameters = @{ + Address = 'fe80:ab04:30F5:002b::1' + } + + { Assert-IPAddress @testIPAddressParameters } | Should -Not -Throw + } + } + } }