Test network boundaries with Pester
Write a small test suite that detects broken subnet boundaries, default routes, and validation.
Boundary examples make a test meaningful
Prefix.ps1 supplies the known-good subnet helper. Create exactly three Pester tests: one for /31 and /32 membership including negative neighbours, one for the /0 range at both extremes, and one for malformed inputs.
Load the helper in BeforeAll, so each invocation of Pester reads the current saved implementation. Keep test definitions in Prefix.Tests.ps1. Pester is already bundled in the browser; no module download is needed.
BeforeAll { . '/home/user/Prefix.ps1' }
Describe 'IPv4 prefixes' {
It 'includes a host prefix' {
Test-IPv4Subnet '203.0.113.7' '203.0.113.7/32' | Should -BeTrue
}
}
Prove that the suite can notice a defect
The course checker temporarily substitutes three broken helpers: one rejects /31 and /32 membership, one rejects /0, and one returns false instead of throwing for an invalid prefix. Your suite must pass with the supplied helper and fail with each broken variant.
Constant assertions can pass without exercising a command. Include positive and negative membership cases, and wrap invalid calls in script blocks for Should -Throw. A false membership result is valid for an address outside a subnet; it must not also disguise malformed input.
Run your saved suite with -PassThru -Output None and emit its result object. These tests establish the function's declared contract, not the behaviour of real routers or an operating system's network stack.
Go deeper
. '/home/user/Prefix.ps1'
Test-IPv4Subnet '192.0.2.11' '192.0.2.10/31'
Put it into practice.
Write /home/user/Prefix.Tests.ps1 with exactly three unskipped tests: /31 and /32 positive/negative boundaries, /0 at 0.0.0.0 and 255.255.255.255, and malformed inputs including /33. Load Prefix.ps1 in BeforeAll, run with -PassThru -Output None, and emit the result. Your suite must reject all three broken variants. Do not edit the supplied helper.
See an explained solution
@'
BeforeAll { . '/home/user/Prefix.ps1' }
Describe 'IPv4 prefix guarantees' {
It 'handles point-to-point and host boundaries' {
Test-IPv4Subnet '192.0.2.10' '192.0.2.10/31' | Should -BeTrue
Test-IPv4Subnet '192.0.2.11' '192.0.2.10/31' | Should -BeTrue
Test-IPv4Subnet '192.0.2.12' '192.0.2.10/31' | Should -BeFalse
Test-IPv4Subnet '203.0.113.7' '203.0.113.7/32' | Should -BeTrue
Test-IPv4Subnet '203.0.113.8' '203.0.113.7/32' | Should -BeFalse
}
It 'handles the entire default prefix' {
Test-IPv4Subnet '0.0.0.0' '0.0.0.0/0' | Should -BeTrue
Test-IPv4Subnet '255.255.255.255' '0.0.0.0/0' | Should -BeTrue
}
It 'rejects malformed input' {
{ Test-IPv4Subnet '192.0.2.1' '192.0.2.0/33' } | Should -Throw
{ Test-IPv4Subnet '20.2' '192.0.2.0/24' } | Should -Throw
}
}
'@ | Set-Content '/home/user/Prefix.Tests.ps1'
Invoke-Pester '/home/user/Prefix.Tests.ps1' -PassThru -Output None
The three tests cover distinct guarantees, and mutation checks prove that their assertions observe behaviour rather than merely report success.