Posh-SYSLOG.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#TODO: what about if we cannot connect to the TCP port? Need to test that!
#TODO: hostname resolution issues/errors need to be handled

Import-Module .\Posh-SYSLOG.psm1 -Force

Describe 'Send-SyslogMessage' {
    Mock -ModuleName Posh-SYSLOG Get-Date { return (New-Object datetime(2000,1,1)) }
    
    Mock -ModuleName Posh-SYSLOG Get-CimInstance { return @{partofdomain = $false; DNSHostname = 'TestHostname'} }

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

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

    # Open TCP 514 so we can test TCP connections (without hitting the network)
    $TCPEndpoint = New-Object System.Net.IPEndPoint ([IPAddress]::Loopback,514)
    $TCPListener = New-Object System.Net.Sockets.TcpListener $TCPEndpoint
    $TCPListener.start()

    Context 'Get-NetworkAdapter = Tests' {
        It 'Does not accept null IP Address' {
            {Get-NetworkAdapter -IPAddress $null} | Should Throw
        }

        It 'Does not accept empty string for an IP Address' {
            {Get-NetworkAdapter -IPAddress ''} | Should Throw
        }

        It 'Does not accept invalid IP Addresses (1)' {
            {Get-NetworkAdapter -IPAddress 'cat'} | Should Throw
        }

        It 'Does not accept invalid IP Addresses (2)' {
            {Get-NetworkAdapter -IPAddress '321.321.321.321'} | Should Throw
        }

        It 'Does not accept invalid IP Addresses (3)' {
            {Get-NetworkAdapter -IPAddress '321.123.123.123'} | Should Throw
        }

        It 'Does not accept invalid IP Addresses (4)' {
            {Get-NetworkAdapter -IPAddress '123.321.123.123'} | Should Throw
        }

        It 'Does not accept invalid IP Addresses (5)' {
            {Get-NetworkAdapter -IPAddress '123.123.321.123'} | Should Throw
        }

        It 'Does not accept invalid IP Addresses (6)' {
            {Get-NetworkAdapter -IPAddress '123.123.123.321'} | Should Throw
        }

        It 'Does not accept invalid IP Addresses (7)' {
            {Get-NetworkAdapter -IPAddress '123'} | Should Throw
        }

        It 'Does accept a valid ip address' {
            {Get-NetworkAdapter -IPAddress '123.123.123.123'} | Should Not Throw
        }
    }

    Context 'Get-SyslogHostname = UDP Client Tests' {
        $UDPCLient = New-Object -TypeName System.Net.Sockets.UdpClient
        $UDPCLient.Connect('127.0.0.1', '514')

        It 'Uses the FQDN if the computer is domain joined' {
            Mock -ModuleName Posh-SYSLOG Get-CimInstance { return @{partofdomain = $true; DNSHostname = 'TestHostname'; Domain = 'contoso.com'} }
            $TestResult = Get-SyslogHostname -Socket $UDPCLient.Client
            Mock -ModuleName Posh-SYSLOG Get-CimInstance { return @{partofdomain = $false; DNSHostname = 'TestHostname'} }
            $TestResult | Should Be 'TestHostname.contoso.com'
        }

        It 'Uses a Static IP address, on the correct interface that the server is reached on, if no FQDN and not hostname specified' {
            Mock -ModuleName Posh-SYSLOG Get-NetworkAdapter {return @{PrefixOrigin = 'Manual'}}
            $TestResult = Get-SyslogHostname -Socket $UDPCLient.Client
            $TestResult | Should Be '127.0.0.1'
        }

        It 'Uses the Windows computer name, if no static ip or FQDN' {
            Mock -ModuleName Posh-SYSLOG Get-NetworkAdapter {return $null} 
            $TestResult = Get-SyslogHostname -Socket $UDPCLient.Client
            $TestResult | Should Be 'TestHostname'
        }
    }

    Context 'Get-SyslogHostname = TCP Client Tests' {
        $TCPCLient = New-Object -TypeName System.Net.Sockets.TcpClient
        $TCPCLient.Connect('127.0.0.1', '514')

        It 'Uses the FQDN if the computer is domain joined' {
            Mock -ModuleName Posh-SYSLOG Get-CimInstance { return @{partofdomain = $true; DNSHostname = 'TestHostname'; Domain = 'contoso.com'} }
            $TestResult = Get-SyslogHostname -Socket $TCPCLient.Client
            Mock -ModuleName Posh-SYSLOG Get-CimInstance { return @{partofdomain = $false; DNSHostname = 'TestHostname'} }
            $TestResult | Should Be 'TestHostname.contoso.com'
        }

        It 'Uses a Static IP address, on the correct interface that the server is reached on, if no FQDN and not hostname specified' {
            Mock -ModuleName Posh-SYSLOG Get-NetworkAdapter {return @{PrefixOrigin = 'Manual'}}
            $TestResult = Get-SyslogHostname -Socket $TCPCLient.Client
            $TestResult | Should Be '127.0.0.1'
        }

        It 'Uses the Windows computer name, if no static ip or FQDN' {
            Mock -ModuleName Posh-SYSLOG Get-NetworkAdapter {return $null} 
            $TestResult = Get-SyslogHostname -Socket $TCPCLient.Client
            $TestResult | Should Be 'TestHostname'
        }
    }

    Context 'Connect-UDPClient = Tests' {
        It 'Should not accept a null value for the server' {
            {Connect-UDPClient -Server $null -Port 514} | Should Throw 'The argument is null or empty'
        }

        It 'Should not accept an empty string for the server' {
            {Connect-UDPClient -Server '' -Port 514} | Should Throw 'The argument is null or empty'
        }

        It 'Should accept an IP address for the server' {
            {Connect-UDPClient -Server '127.0.0.1' -Port 514} | Should not throw
        }

        It 'Should accept an hostname string for the server' {
            {Connect-UDPClient -Server 'localhost' -Port 514} | Should not throw
        }
        
        It 'Should not accept a null value for the port' {
            {Connect-UDPClient -Server '127.0.0.1' -Port $null} | Should Throw 'Cannot validate argument on parameter'
        }

        It 'Should not accept an invalid value for the port' {
            {Connect-UDPClient -Server '127.0.0.1' -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' {
            {Connect-UDPClient -Server '127.0.0.1' -Port 514} | Should Not Throw
        }
        
        It 'creates a UDP client' {
            {Connect-UDPClient -Server '127.0.0.1' -Port 514} | should not be $null
        }
    }

    Context 'Connect-TCPClient = Tests' {
        It 'Should not accept a null value for the server' {
            {Connect-TCPClient -Server $null -Port 514} | Should Throw 'The argument is null or empty'
        }

        It 'Should not accept an empty string for the server' {
            {Connect-TCPClient -Server '' -Port 514} | Should Throw 'The argument is null or empty'
        }

        It 'Should accept an IP address for the server' {
            {Connect-UDPClient -Server '127.0.0.1' -Port 514} | Should not throw
        }

        It 'Should accept an hostname string for the server' {
            {Connect-UDPClient -Server 'localhost' -Port 514} | Should not throw
        }
        
        It 'Should not accept a null value for the port' {
            {Connect-TCPClient -Server '127.0.0.1' -Port $null} | Should Throw 'Cannot validate argument on parameter'
        }

        It 'Should not accept an invalid value for the port' {
            {Connect-TCPClient -Server '127.0.0.1' -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' {
            {Connect-TCPClient -Server '127.0.0.1' -Port 514} | Should Not Throw
        }
        
        It 'creates a TCP client' {
            {Connect-TCPClient -Server '127.0.0.1' -Port 514} | should not be $null
        }
    }

    Context 'Get-TCPWriter = Tests' {
        $TCPClient = Connect-TCPClient -Server '127.0.0.1' -port 514

        It 'creates a TCPWriter' {
            {Get-TCPWriter -TcpClient $TCPClient} | should not be $null
        }
    }

    Context 'Disconnect-UDPClient = Tests' {
        $UDPClient = Connect-UDPClient -Server '127.0.0.1' -port 514
        
        It 'disconnects the UDP client' {
            Disconnect-UDPClient $UDPClient
            $UDPClient.Client | should be $null
        }
    }

    Context 'Disconnect-TCPWriter = Tests' {
        $TCPClient = Connect-TCPClient -Server '127.0.0.1' -port 514
        $TCPWriter = Get-TCPWriter -TcpClient $TCPClient

        It 'disconnects the TCP Writer' {
            Disconnect-TCPWriter -TcpWriter $TCPWriter
            $TCPWriter.BaseStream | should be $null
        }
    }

    Context 'Disconnect-TCPClient = Tests' {
        $TCPClient = Connect-TCPClient -Server '127.0.0.1' -port 514

        It 'disconnects the TCP client' {
            Disconnect-TCPClient $TCPClient
            $TCPClient.Client | should be $null
        }
    }

    Context 'Send-SyslogMessage = Parameter Validation' {
        Mock -ModuleName Posh-SYSLOG Send-UDPMessage { return $null }
        Mock -ModuleName Posh-SYSLOG Send-TCPMessage { return $null }

        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 convert null to type "Syslog_Severity"'
        }

        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 process argument transformation on parameter'
        }

        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 convert null to type "Syslog_Facility"'
        }

        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 process argument transformation on parameter'
        }

        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 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 'Cannot validate argument on parameter'
        }

        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 'Cannot validate argument on parameter'
        }

        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'
        }
    }

    Context 'Send-SyslogMessage = Pipeline input' {
        Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $Global: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($Global: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($Global:TestResult) | should be $ExpectedResult
        }
    }

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

        It 'Calculates the correct priority of 0 if Facility is Kern and Severity is Emergency' {
            $ExpectedResult = '<0>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }

        It 'Calculates the correct priority of 7 if Facility is Kern and Severity is Debug' {
            $ExpectedResult = '<7>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }   

        It 'Calculates the correct priority of 24 if Facility is daemon and Severity is Emergency' {
            $ExpectedResult = '<24>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }

        It 'Calculates the correct priority of 31 if Facility is daemon and Severity is Debug' {
            $ExpectedResult = '<31>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }
    }

    Context 'Send-SyslogMessage = RFC 3164 Message Format' {
        Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $Global: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 Posh-SYSLOG.Tests.ps1 Test Syslog Message'
            $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -RFC3164
            $Encoding.GetString($Global: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 Posh-SYSLOG.Tests.ps1 Test Syslog Message'
            $null = Send-SyslogMessage -Server '127.0.0.1' -Message 'Test Syslog Message' -Severity 'Alert' -Facility 'auth' -RFC3164
            $Encoding.GetString($Global:TestResult) | should be $ExpectedResult
        }
    }

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

        It 'Should send RFC5424 formatted message' {
            $ExpectedResult = '<33>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }
    }

    Context 'Send-SyslogMessage = Hostname determination' {
        Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $Global:TestResult = $Datagram; return $null }

        It 'Uses any hostname it is given' {
            $ExpectedResult = '<33>1 {0} SomeRandomHostName Posh-SYSLOG.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 'SomeRandomHostName'
            $Encoding.GetString($Global:TestResult) | should be $ExpectedResult
        }

        It 'Uses the FQDN if the computer is domain joined' {
            Mock -ModuleName Posh-SYSLOG Get-CimInstance { return @{partofdomain = $true; DNSHostname = 'TestHostname'; Domain = 'contoso.com'} }

            $ExpectedResult = '<33>1 {0} TestHostname.contoso.com Posh-SYSLOG.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'

            Mock -ModuleName Posh-SYSLOG Get-CimInstance { return @{partofdomain = $false; DNSHostname = 'TestHostname'} }

            $Encoding.GetString($Global:TestResult) | should be $ExpectedResult
        }

        It 'Uses a Static IP address, on the correct interface that the server is reached on, if no FQDN and not hostname specified' {
            Mock -ModuleName Posh-SYSLOG Get-NetworkAdapter {return @{PrefixOrigin = 'Manual'}}

            $ExpectedResult = '<33>1 {0} 127.0.0.1 Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }

        It 'Uses the Windows computer name, if no static ip or FQDN' {
            Mock -ModuleName Posh-SYSLOG Get-NetworkAdapter {return $null}

            $ExpectedResult = '<33>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }
    }

    Context 'Send-SyslogMessage = Message Length UDP' {
        Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $Global: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 Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }
        It 'truncates RFC 3164 messages to 1k' {
            $ExpectedResult = ('<33>Jan 1 00:00:00 TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }
    }

    Context 'Send-SyslogMessage = Message Length TCP' {
        Mock -CommandName Send-TCPMessage -ModuleName Posh-SYSLOG { $Global: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 Posh-SYSLOG.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($Global:TestResult) | should be $FramedResult
        }
        It 'truncates RFC 3164 messages to 1k' {
            $ExpectedResult = ('<33>Jan 1 00:00:00 TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $FramedResult
        }
    }

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

        It 'sends using TCP transport' {
            $ExpectedResult = '<33>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $FramedResult
        }

        It 'sends using TCP transport with Octet-Counting as the framing' {
            $ExpectedResult = '<33>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $FramedResult
        }
        It 'sends using TCP transport with Non-Transparent-Framing as the framing' {
            $ExpectedResult = '<33>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }

        It 'sends using TCP transport with no framing' {
            $ExpectedResult = '<33>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }

    }

    Context 'Send-SyslogMessage = Application Name Selection' {
        Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $Global: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($Global:TestResult) | should be $ExpectedResult
        }

        It 'uses myInvocation.ScriptName if one is available' {
            $ExpectedResult = '<33>1 {0} TestHostname Posh-SYSLOG.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($Global:TestResult) | should be $ExpectedResult
        }
    }

    Context 'Send-SyslogMessage = Generic Tests' {
        Mock -CommandName Send-UDPMessage -ModuleName Posh-SYSLOG { $Global: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
        }
    }

    Context 'Script Analyzer' {
        It 'Does not have any issues with the Script Analyser' {
            Invoke-ScriptAnalyzer .\Functions\*.ps1 | Should be $null
        }
    }

    $TCPListener.stop()
}