tests/Send-SyslogMessage.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
$script:ModuleName = 'Posh-SYSLOG'

# Removes all versions of the module from the session before importing
Get-Module $ModuleName | Remove-Module

$ModuleBase = Split-Path -Parent $MyInvocation.MyCommand.Path

# For tests in .\Tests subdirectory
if ((Split-Path $ModuleBase -Leaf) -eq 'Tests') {
    $ModuleBase = Split-Path $ModuleBase -Parent
}

## This variable is for the VSTS tasks and is to be used for referencing any mock artifacts
$Env:ModuleBase = $ModuleBase

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

# InModuleScope runs the test in module scope.
# It creates all variables and functions in module scope.
# As a result, test has access to all functions, variables and aliases
# in the module even if they're not exported.
InModuleScope $script:ModuleName {
    Describe "Basic function unit tests" -Tags Build , Unit{
        Mock -ModuleName Posh-SYSLOG -CommandName Get-Date -MockWith {
            return (New-Object datetime(2000,1,1))
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Connect-TCPClient -MockWith {
            return @{Client = New-Object -TypeName System.Net.Sockets.Socket -ArgumentList @([System.Net.Sockets.SocketType]::Stream, [System.Net.Sockets.ProtocolType]::Tcp)}
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Connect-UDPClient -MockWith {
            return @{Client = New-Object -TypeName System.Net.Sockets.Socket -ArgumentList @([System.Net.Sockets.SocketType]::Dgram, [System.Net.Sockets.ProtocolType]::Udp)}
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Disconnect-TCPClient -MockWith {
            # Nothing
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Disconnect-UDPClient -MockWith {
            # Nothing
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Disconnect-TCPWriter -MockWith {
            # Nothing
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Get-TCPWriter -MockWith {
            return (New-Object -TypeName System.IO.StreamWriter -ArgumentList  @([System.IO.Stream]::Null))
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Get-SyslogHostname -MockWith {
            return 'TestHostname'
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Send-TCPMessage -MockWith {
            $script:TestResult = $Datagram; return $null
        }
        Mock -ModuleName Posh-SYSLOG -CommandName Send-UDPMessage -MockWith {
            $script:TestResult = $Datagram; return $null
        }

        $ExpectedTimeStamp = (New-Object datetime(2000,1,1)).ToString('yyyy-MM-ddTHH:mm:ss.ffffffzzz')

        # Create an ASCII Encoding object
        $Encoding = [Text.Encoding]::ASCII

        Context 'Send-SyslogMessage = Parameter Validation' {
            It 'Should not accept a null value for the server' {
                {Send-SyslogMessage -Server $null -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth'} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept an empty string for the server' {
                {Send-SyslogMessage -Server '' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth'} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept a null value for the message' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message $null -Severity 'Alert' -Facility 'auth'} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept an empty string for the message' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message '' -Severity 'Alert' -Facility 'auth'} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept a null value for the severity' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity $null -Facility 'auth'} | Should Throw #'Cannot validate argument on parameter 'Severity'. The argument "" does not belong to the set "Emergency,Alert,Critical,Error,Warning,Notice,Informational,Debug" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.'
            }

            It 'Should not accept an empty string for the severity' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity '' -Facility 'auth'} | Should Throw #'Cannot validate argument on parameter 'Severity'. The argument "" does not belong to the set "Emergency,Alert,Critical,Error,Warning,Notice,Informational,Debug" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.'
            }

            foreach ($value in [Syslog_Severity].GetEnumNames()){
                It "Should accept $value for Syslog_Severity" {
                    {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity $value -Facility 'auth'} | Should not Throw
                }
            }

            It 'Should not accept a null value for the facility' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility $null} | Should Throw #'Cannot validate argument on parameter 'Facility'. The argument "" does not belong to the set "kern,user,mail,daemon,auth,syslog,lpr,news,uucp,clock,authpriv,ftp,ntp,logaudit,logalert,cron,local0,local1,local2,local3,local4,local5,local6,local7" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.'
            }

            It 'Should not accept an empty string for the facility' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility ''} | Should Throw #'Cannot validate argument on parameter 'Facility'. The argument "" does not belong to the set "kern,user,mail,daemon,auth,syslog,lpr,news,uucp,clock,authpriv,ftp,ntp,logaudit,logalert,cron,local0,local1,local2,local3,local4,local5,local6,local7" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.'
            }

            foreach ($value in [Syslog_Facility].GetEnumNames()){
                It "Should accept $value for Syslog_Facility" {
                    {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility $value} | Should not Throw
                }
            }

            It 'Should not accept a null value for the hostname' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Hostname $null} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept an empty string for the hostname' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Hostname ''} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept a null value for the application name' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -ApplicationName $null} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept an empty string for the application name' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -ApplicationName ''} | Should Throw 'The argument is null or empty'
            }

            It 'Should accept an valid string for the application name' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -ApplicationName 'ApplicationName'} | Should Not Throw
            }

            It 'Should not accept a null value for the ProcessID' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -ProcessID $null} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept an empty string for the ProcessID' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -ProcessID ''} | Should Throw 'The argument is null or empty'
            }

             It 'Should accept an valid string for the ProcessID' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -ProcessID '3214893'} | Should Not Throw
            }

            It 'Should not accept a null value for the MessageID' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -MessageID $null} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept an empty string for the MessageID' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -MessageID ''} | Should Throw 'The argument is null or empty'
            }

            It 'Should accept an valid string for the MessageID' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -MessageID 'messageid'} | Should Not Throw
            }

            It 'Should not accept a null value for the StructuredData' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -StructuredData $null} | Should Throw 'The argument is null or empty'
            }

            It 'Should not accept an empty string for the StructuredData' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -StructuredData ''} | Should Throw 'The argument is null or empty'
            }

            It 'Should accept an valid string for the StructuredData' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -StructuredData 'structureddata'} | Should Not Throw
            }

            It 'Should not accept a null value for the timestamp' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Timestamp $null} | Should Throw 'Cannot convert null to type "System.DateTime"'
            }

            It 'Should accept a valid value for the timestamp' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Timestamp $ExpectedTimeStamp} | Should Not Throw
            }

            It 'Should not accept a null value for the port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Port $null} | Should Throw 'Cannot validate argument on parameter'
            }

            It 'Should not accept an invalid value for the port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Port 456789789789} | Should Throw 'Error: "Value was either too large or too small for a UInt16.'
            }

            It 'Should accept an valid value for the port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Port 445} | Should Not Throw
            }

            It 'Should not accept a null value for the UDP port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -UDPPort $null} | Should Throw 'Cannot validate argument on parameter'
            }

            It 'Should not accept an invalid value for the UDP port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -UDPPort 456789789789} | Should Throw 'Error: "Value was either too large or too small for a UInt16.'
            }

            It 'Should accept an valid value for the UDP port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -UDPPort 445} | Should Not Throw
            }

            It 'Should not accept a null value for the TCP port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -TCPPort $null} | Should Throw 'Cannot validate argument on parameter'
            }

            It 'Should not accept an invalid value for the TCP port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -TCPPort 456789789789} | Should Throw 'Error: "Value was either too large or too small for a UInt16.'
            }

            It 'Should accept an valid value for the TCP port' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -TCPPort 445} | Should Not Throw
            }

            It 'Should Accept UDP as a Transport' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'UDP'} | Should Not Throw 'Cannot validate argument on parameter'
            }

            It 'Should Accept TCP as a Transport' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'TCP'} | Should Not Throw 'Cannot validate argument on parameter'
            }

            It 'Should Accept TCPwith TLS as a Transport' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'TCPwithTLS'} | Should Not Throw 'Cannot validate argument on parameter'
            }

            It 'Should not accept a null value for Transport' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport $null} | Should Throw
            }

            It 'Should not accept an invalid value for Transport' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'bob'} | Should Throw
            }

            It 'Should reject ProcessID parameter if -RFC3164 is specified' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -RFC3164 -ProcessID 1} | Should Throw 'Parameter set cannot be resolved using the specified named parameters'
            }

            It 'Should reject MessageID parameter if -RFC3164 is specified' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -RFC3164 -MessageID 1} | Should Throw 'Parameter set cannot be resolved using the specified named parameters'
            }

            It 'Should reject StructuredData parameter if -RFC3164 is specified' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -RFC3164 -StructuredData 1} | Should Throw 'Parameter set cannot be resolved using the specified named parameters'
            }

            It 'Should not accept null for SslProtocols' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'TCPwithTLS' -SslProtocols $null} | Should Throw
            }

            It 'Should not accept a empty string for SslProtocols' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'TCPwithTLS' -SslProtocols ''} | Should Throw
            }

            It 'Should not accept an invalid value for SslProtocols' {
                {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'TCPwithTLS' -SslProtocols 'bob'} | Should Throw
            }

            foreach ($value in [System.Security.Authentication.SslProtocols].GetEnumNames()){
                It "Should accept $value for SslProtocls" {
                    {Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Transport 'TCPwithTLS' -SslProtocols $value} | Should Not Throw
                }
            }

        }

        Context 'Send-SyslogMessage = Pipeline input' {
            Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            It 'Should accept valid input from the pipeline for RFC5424' {
                $PipelineInput = [PSCustomObject]@{
                    Message         = 'Test Syslog Message'
                    Severity        = 'Emergency'
                    Facility        = 'Kern'
                    ApplicationName = 'RandomAppName'
                    ProcessID       = 12345678
                    MessageID       = 'messageid'
                    StructuredData  = 'structuredata'
                    Timestamp       = $ExpectedTimeStamp
                }
                $ExpectedResult = '<0>1 {0} TestHostname RandomAppName 12345678 messageid structuredata Test Syslog Message' -f $ExpectedTimeStamp
                $null =  $PipelineInput | Send-SyslogMessage -Server 'localhost'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

            It 'Should accept valid input from the pipeline for RFC3164' {
                $PipelineInput = [PSCustomObject]@{
                    Message         = 'Test Syslog Message'
                    Severity        = 'Emergency'
                    Facility        = 'Kern'
                    ApplicationName = 'RandomAppName'
                    Timestamp       = $ExpectedTimeStamp
                    RFC3164         = $true
                }
                $ExpectedResult = '<0>Jan 1 00:00:00 TestHostname RandomAppName Test Syslog Message'
                $null =  $PipelineInput | Send-SyslogMessage -Server 'localhost' -RFC3164
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }
        }

        Context 'Send-SyslogMessage = Severity Level Calculations' {
            Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            It 'Calculates the correct priority of 0 if Facility is Kern and Severity is Emergency' {
                $ExpectedResult = '<0>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null =  Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Emergency' -Facility 'kern'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

            It 'Calculates the correct priority of 7 if Facility is Kern and Severity is Debug' {
                $ExpectedResult = '<7>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Debug' -Facility 'kern'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

            It 'Calculates the correct priority of 24 if Facility is daemon and Severity is Emergency' {
                $ExpectedResult = '<24>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Emergency' -Facility 'daemon'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

            It 'Calculates the correct priority of 31 if Facility is daemon and Severity is Debug' {
                $ExpectedResult = '<31>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Debug' -Facility 'daemon'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }
        }

        Context 'Send-SyslogMessage = RFC 3164 Message Format' {
            Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            It 'Should send RFC5424 formatted message with correct date format (10 to 31)' {
                Mock -ModuleName Posh-SYSLOG Get-Date { return (New-Object datetime(2000,1,10)) }

                $ExpectedResult = '<33>Jan 10 00:00:00 TestHostname Send-SyslogMessage.Tests.ps1 Test Syslog Message'
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -RFC3164
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

            It 'Should send RFC5424 formatted message with correct date format (1 to 9)' {
                Mock -ModuleName Posh-SYSLOG Get-Date { return (New-Object datetime(2000,1,1)) }

                $ExpectedResult = '<33>Jan 1 00:00:00 TestHostname Send-SyslogMessage.Tests.ps1 Test Syslog Message'
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -RFC3164
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }
        }

        Context 'Send-SyslogMessage = RFC 5424 message format' {
            Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            It 'Should send RFC5424 formatted message' {
                $ExpectedResult = '<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }
        }

        Context 'Send-SyslogMessage = Message Length UDP' {
            Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            $LongMsg = 'This is a very long syslog message. 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890'

            It 'truncates RFC 5424 messages to 2k' {
                $ExpectedResult = ('<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - {2}' -f $ExpectedTimeStamp, $PID, $LongMsg).Substring(0,2048)
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message $LongMsg -Severity 'Alert' -Facility 'auth'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }
            It 'truncates RFC 3164 messages to 1k' {
                $ExpectedResult = ('<33>Jan 1 00:00:00 TestHostname Send-SyslogMessage.Tests.ps1 {1}' -f $ExpectedTimeStamp, $LongMsg).Substring(0,1024)
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message $LongMsg -Severity 'Alert' -Facility 'auth' -RFC3164
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }
        }

        Context 'Send-SyslogMessage = Message Length TCP' {

            $LongMsg = 'This is a very long syslog message. 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890'

            It 'truncates RFC 5424 messages to 2k' {
                $ExpectedResult = ('<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - {2}' -f $ExpectedTimeStamp, $PID, $LongMsg).Substring(0,2048)
                $FramedResult = '2048 {0}' -f $ExpectedResult

                $null = Send-SyslogMessage -Server '127.0.0.1' -Message $LongMsg -Severity 'Alert' -Facility 'auth' -Transport TCP
                $Encoding.GetString($script:TestResult) | should be $FramedResult
            }
            It 'truncates RFC 3164 messages to 1k' {
                $ExpectedResult = ('<33>Jan 1 00:00:00 TestHostname Send-SyslogMessage.Tests.ps1 {1}' -f $ExpectedTimeStamp, $LongMsg).Substring(0,1024)
                $FramedResult = '1024 {0}' -f $ExpectedResult

                $null = Send-SyslogMessage -Server '127.0.0.1' -Message $LongMsg -Severity 'Alert' -Facility 'auth' -RFC3164 -Transport TCP
                $Encoding.GetString($script:TestResult) | should be $FramedResult
            }
        }

        Context 'Send-SyslogMessage = TCP Specific Tests' {
            #Mock -CommandName Send-TCPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            It 'sends using TCP transport' {
                $ExpectedResult = '<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $FramedResult = '{0} {1}' -f $ExpectedResult.Length, $ExpectedResult

                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Hostname TestHostname -Transport TCP
                $Encoding.GetString($script:TestResult) | should be $FramedResult
            }

            It 'sends using TCP transport with Octet-Counting as the framing' {
                $ExpectedResult = '<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $FramedResult = '{0} {1}' -f $ExpectedResult.Length, $ExpectedResult

                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Hostname TestHostname -Transport TCP -FramingMethod Octet-Counting
                $Encoding.GetString($script:TestResult) | should be $FramedResult
            }
            It 'sends using TCP transport with Non-Transparent-Framing as the framing' {
                $ExpectedResult = '<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message{2}' -f $ExpectedTimeStamp, $PID, "`n"
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Hostname TestHostname -Transport TCP -FramingMethod Non-Transparent-Framing
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

            It 'sends using TCP transport with no framing' {
                $ExpectedResult = '<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -Hostname TestHostname -Transport TCP -FramingMethod None
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

        }

        Context 'Send-SyslogMessage = Application Name Selection' {
            #Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            It 'Takes an Application Name as specified' {
                $ExpectedResult = '<33>1 {0} TestHostname SomeRandomName {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -ApplicationName 'SomeRandomName'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }

            It 'uses myInvocation.ScriptName if one is available' {
                $ExpectedResult = '<33>1 {0} TestHostname Send-SyslogMessage.Tests.ps1 {1} - - Test Syslog Message' -f $ExpectedTimeStamp, $PID
                $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth'
                $Encoding.GetString($script:TestResult) | should be $ExpectedResult
            }
        }

        Context 'Send-SyslogMessage = Generic Tests' {
            #Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $script:TestResult = $Datagram; return $null }

            It 'does not return any values' {
                $TestCase = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth'
                $TestCase | Should be $null
            }
        }
    }

}