|
BeforeAll { |
|
. ./"ServiceTags-Helpers.ps1" |
|
} |
|
|
|
# Describe block is the Pester test group |
|
Describe 'Test-IpInRange' { |
|
|
|
# Test case: IP is within the range |
|
It 'Should return $true when IP is within the CIDR range' { |
|
$ip = "192.168.1.10" |
|
$cidr = "192.168.1.0/24" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $true |
|
$result | Should -Be $true |
|
} |
|
|
|
# Test case: IP is outside the range |
|
It 'Should return $false when IP is outside the CIDR range' { |
|
$ip = "192.168.2.10" |
|
$cidr = "192.168.1.0/24" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: Exact match at the boundary of the CIDR range |
|
It 'Should return $true for the lowest IP in the CIDR range' { |
|
$ip = "192.168.1.0" |
|
$cidr = "192.168.1.0/24" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $true |
|
$result | Should -Be $true |
|
} |
|
|
|
# Test case: Highest IP in the range |
|
It 'Should return $true for the highest IP in the CIDR range' { |
|
$ip = "192.168.1.255" |
|
$cidr = "192.168.1.0/24" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $true |
|
$result | Should -Be $true |
|
} |
|
|
|
# Test case: Edge case with large CIDR range |
|
It 'Should return $true when IP is within a large CIDR range' { |
|
$ip = "10.0.0.5" |
|
$cidr = "10.0.0.0/8" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $true |
|
$result | Should -Be $true |
|
} |
|
|
|
# Test case: First IP outside the range (just below the range) |
|
It 'Should return $false for the first IP outside the CIDR range (below)' { |
|
$ip = "192.168.0.255" |
|
$cidr = "192.168.1.0/24" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: Last IP outside the range (just above the range) |
|
It 'Should return $false for the first IP outside the CIDR range (above)' { |
|
$ip = "192.168.2.0" |
|
$cidr = "192.168.1.0/24" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
} |
|
|
|
Describe 'Test-IpInRange with non-byte-aligned CIDR masks' { |
|
|
|
# Test case: IP is within the /19 CIDR range |
|
It 'Should return $true when IP is within the /19 CIDR range' { |
|
$ip = "192.168.32.15" |
|
$cidr = "192.168.32.0/19" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $true |
|
$result | Should -Be $true |
|
} |
|
|
|
# Test case: IP is outside the /19 CIDR range |
|
It 'Should return $false when IP is outside the /19 CIDR range' { |
|
$ip = "192.168.64.1" |
|
$cidr = "192.168.32.0/19" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: IP is within the /23 CIDR range |
|
It 'Should return $true when IP is within the /23 CIDR range' { |
|
$ip = "10.0.1.5" |
|
$cidr = "10.0.0.0/23" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $true |
|
$result | Should -Be $true |
|
} |
|
|
|
# Test case: IP is outside the /23 CIDR range |
|
It 'Should return $false when IP is outside the /23 CIDR range' { |
|
$ip = "10.0.2.1" |
|
$cidr = "10.0.0.0/23" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: IP is within the /21 CIDR range |
|
It 'Should return $true when IP is within the /21 CIDR range' { |
|
$ip = "172.16.4.1" |
|
$cidr = "172.16.0.0/21" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $true |
|
$result | Should -Be $true |
|
} |
|
|
|
# Test case: IP is outside the /21 CIDR range |
|
It 'Should return $false when IP is outside the /21 CIDR range' { |
|
$ip = "172.16.8.1" |
|
$cidr = "172.16.0.0/21" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
} |
|
|
|
Describe 'Test-IpInRange with first and last IP outside non-byte-aligned CIDR masks' { |
|
|
|
# Test case: First IP outside the /19 CIDR range (just below) |
|
It 'Should return $false for the first IP outside the /19 CIDR range (below)' { |
|
$ip = "192.168.31.255" |
|
$cidr = "192.168.32.0/19" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: Last IP outside the /19 CIDR range (just above) |
|
It 'Should return $false for the last IP outside the /19 CIDR range (above)' { |
|
$ip = "192.168.64.0" |
|
$cidr = "192.168.32.0/19" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: First IP outside the /23 CIDR range (just below) |
|
It 'Should return $false for the first IP outside the /23 CIDR range (below)' { |
|
$ip = "10.0.2.1" |
|
$cidr = "10.0.0.0/23" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: Last IP outside the /23 CIDR range (just above) |
|
It 'Should return $false for the last IP outside the /23 CIDR range (above)' { |
|
$ip = "10.0.2.0" |
|
$cidr = "10.0.0.0/23" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: First IP outside the /21 CIDR range (just below) |
|
It 'Should return $false for the first IP outside the /21 CIDR range (below)' { |
|
$ip = "172.15.255.255" |
|
$cidr = "172.16.0.0/21" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
|
|
# Test case: Last IP outside the /21 CIDR range (just above) |
|
It 'Should return $false for the last IP outside the /21 CIDR range (above)' { |
|
$ip = "172.16.8.0" |
|
$cidr = "172.16.0.0/21" |
|
|
|
# Call the function and check the result |
|
$result = Test-IpInRange -IpAddress $ip -CIDR $cidr |
|
|
|
# The result should be $false |
|
$result | Should -Be $false |
|
} |
|
} |