Skip to content

Commit 19fb18f

Browse files
committed
Made the ServerHostname param mandatory, added some net tests
1 parent eb335c1 commit 19fb18f

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

private/Get-TCPWriter.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
$UseTLS,
3434

3535
# Server Hostname to validate against the certificate presented during TLS validation
36-
[Parameter(Mandatory = $false,
36+
[Parameter(Mandatory = $true,
3737
ParameterSetName = 'UseTLS')]
3838
[string]
3939
$ServerHostname,

tests/Get-TCPWriter.Tests.ps1

+32-9
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,51 @@ $Env:ModuleBase = $ModuleBase
1515

1616
Import-Module $ModuleBase\$ModuleName.psd1 -PassThru -ErrorAction Stop | Out-Null
1717

18+
Write-Warning -Message ('These tests require access to google.com (TCP 80 and 443)')
19+
1820
# InModuleScope runs the test in module scope.
1921
# It creates all variables and functions in module scope.
2022
# As a result, test has access to all functions, variables and aliases
2123
# in the module even if they're not exported.
2224
InModuleScope $script:ModuleName {
2325
Describe "Basic function unit tests" -Tags Build , Unit{
24-
# Open TCP 514 so we can test TCP connections (without hitting the network)
25-
$TCPEndpoint = New-Object System.Net.IPEndPoint ([IPAddress]::Loopback,514)
26-
$TCPListener = New-Object System.Net.Sockets.TcpListener $TCPEndpoint
27-
$TCPListener.start()
2826

29-
$TCPClient = Connect-TCPClient -Server '127.0.0.1' -port 514
27+
It 'Connects to a known port and does not throw' {
28+
$TCPClient = Connect-TCPClient -Server 'google.com' -port 80
29+
{
30+
$TCPWriter = Get-TCPWriter -TcpClient $TCPClient
31+
Disconnect-TCPWriter -TcpWriter $TCPWriter
32+
} | should not throw
33+
}
3034

31-
It 'creates a TCPWriter' {
35+
It 'Connects to a known port and returns a TCP writer' {
36+
$TCPClient = Connect-TCPClient -Server 'google.com' -port 80
3237
$TCPWriter = Get-TCPWriter -TcpClient $TCPClient
33-
$TCPWriter | should not be $null
38+
$TCPWriter | Should -BeOfType System.IO.StreamWriter
3439
Disconnect-TCPWriter -TcpWriter $TCPWriter
3540
}
3641

37-
Disconnect-TCPClient $TCPClient
42+
It 'Connects to a known port over TLS and returns a TCP writer' {
43+
$TCPClient = Connect-TCPClient -Server 'google.com' -port 443
44+
{
45+
$TCPWriter = Get-TCPWriter -TcpClient $TCPClient -UseTLS -ServerHostname 'google.com'
46+
Disconnect-TCPWriter -TcpWriter $TCPWriter
47+
} | should not throw
48+
}
49+
50+
It 'Throws an error if connecting and the certificate does not match' {
51+
$TCPClient = Connect-TCPClient -Server 'google.com' -port 443
52+
$TCPWriter = Get-TCPWriter -TcpClient $TCPClient -UseTLS -ServerHostname 'google.com'
53+
$TCPWriter | Should -BeOfType System.IO.StreamWriter
54+
Disconnect-TCPWriter -TcpWriter $TCPWriter
55+
}
3856

39-
$TCPListener.stop()
57+
It 'Does not throw an error if connecting and the certificate does not match and -DoNotValidateTLSCertificate is used and returns a TCP writer' {
58+
$TCPClient = Connect-TCPClient -Server 'google.com' -port 443
59+
$TCPWriter = Get-TCPWriter -TcpClient $TCPClient -UseTLS -ServerHostname 'notgoogle.com' -DoNotValidateTLSCertificate
60+
$TCPWriter | Should -BeOfType System.IO.StreamWriter
61+
Disconnect-TCPWriter -TcpWriter $TCPWriter
62+
}
4063
}
4164

4265
}

0 commit comments

Comments
 (0)