@@ -15,28 +15,51 @@ $Env:ModuleBase = $ModuleBase
15
15
16
16
Import-Module $ModuleBase \$ModuleName.psd1 - PassThru - ErrorAction Stop | Out-Null
17
17
18
+ Write-Warning - Message (' These tests require access to google.com (TCP 80 and 443)' )
19
+
18
20
# InModuleScope runs the test in module scope.
19
21
# It creates all variables and functions in module scope.
20
22
# As a result, test has access to all functions, variables and aliases
21
23
# in the module even if they're not exported.
22
24
InModuleScope $script :ModuleName {
23
25
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 ()
28
26
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
+ }
30
34
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
32
37
$TCPWriter = Get-TCPWriter - TcpClient $TCPClient
33
- $TCPWriter | should not be $null
38
+ $TCPWriter | Should - BeOfType System.IO.StreamWriter
34
39
Disconnect-TCPWriter - TcpWriter $TCPWriter
35
40
}
36
41
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
+ }
38
56
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
+ }
40
63
}
41
64
42
65
}
0 commit comments