Tests/HelperFunctions.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<############################################################################################
 # File: BaselineManagement\HelperFunctions.psm1
 # This suite contains Tests that are
 # used for validating the BaselineManagement Module.
 ############################################################################################>

$script:TestSourceRoot = $PSScriptRoot
$ModuleBase = Split-Path $script:TestSourceRoot

$HelperModule = Join-Path $ModuleBase 'HelperFunctions.psm1'
$SampleXML = Join-Path $ModuleBase 'Tests\Sample.XML'
$SamplePOL = Join-Path $ModuleBase "Tests\Registry.Pol"
$SampleGPTemp = Join-Path $ModuleBase "Tests\gptTmpl.inf"
$SampleComment = Join-Path $ModuleBase "Tests\Comment.cmtx"
$SampleREGXML = Join-Path $ModuleBase "Tests\Registry.xml"
$JSONSample = Join-Path $ModuleBase "Tests\sample.json"

Import-Module $HelperModule -Force
Import-Module PSDesiredStateConfiguration -Force
Import-Module Pester -Force

Describe "DSC String Helper Tests" {
    Context "Write-DSCString" {
        
        # This only works when it feels like it.
        Mock Get-DSCResource -ParameterFilter { $Module -eq "TestModule_01"} -Verifiable { return [psobject]@{Name="TestResource_01";Properties=@(@{Name="Name";IsMandatory=$true}, @{Name="Value";IsMandatory=$true})}}
        Mock Get-DSCResource -ParameterFilter { $Module -eq "TestModule_02"} -Verifiable { return [psobject]@{Name="TestResource_02";Properties=@(@{Name="Name";IsMandatory=$true}, @{Name="Value";IsMandatory=$true})}}

        $CONFIG_Params = @{Configuration=$true;Name="TestConfig"}
        $CONFIG_ModuleParams = @{ModuleName = @("PSDesiredStateConfiguration");ModuleImport=$true}
        $CONFIG_Node = @{Node=$true;Name="localhost"}
        $CONFIG_ResourceParams = @{Resource=$true;Type="Registry";Name="Test";Parameters=@{Key="HKLM:\SOFTWARE";ValueName = "TestResource";ValueData="Test";ValueType="DWORD" }}
        $CONFIG_Invoke = @{InvokeConfiguration=$true;Name="TestConfig";OutputPath=$(Join-Path -Path "C:\Temp" -ChildPath Output)}
        
        It "Creates a Configuration Open Block" {
            Write-DSCString @CONFIG_Params | Should Match "Configuration $($CONFIG_Params.Name)`n{`n`n`t" 
        }

        $CONFIG_Params.Add("Comment", "Test Comment")
        It "Comments a Configuration Open Block" {
            Write-DSCString @CONFIG_Params | Should Match "<#`n$($CONFIG_Params.Comment)`n#>" 
        }

        It "Creates Module Import Strings" {
            Write-DSCString @CONFIG_ModuleParams | Should Match "(?s)$(($CONFIG_ModuleParams.ModuleName | %{"'$_'"}) -join ", ")"
            #Assert-MockCalled -CommandName Get-DscResource -Times 2
        }

        It "Creates a Resource Block" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?m)$($CONFIG_ResourceParams.Type) '$($CONFIG_ResourceParams.Name)'.*$((($CONFIG_ResourceParams.Parameters.GetEnumerator() | %{"$($_.Key) = '$($_.Value)'"}) -join '|')*4)}"
        }

        It "Parses String Values Properly" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?s)'$($CONFIG_ResourceParams.Parameters.ValueData)'"
        }

        [int]$CONFIG_ResourceParams.Parameters.ValueData = 3
        It "Parses Integer Values Properly" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?s)$($CONFIG_ResourceParams.Parameters.ValueData)"
        }

        [bool]$CONFIG_ResourceParams.Parameters.ValueData = $True
        It "Parses Boolean Values Properly" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?s)\`$$($CONFIG_ResourceParams.Parameters.ValueData)"
        }

        [string[]]$CONFIG_ResourceParams.Parameters.ValueData = "One", "Two", "Three"
        It "Parses Array String Values Properly" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?s)$(($CONFIG_ResourceParams.Parameters.ValueData | %{"'$_'"}) -join ", ")"
        }

        [int[]]$CONFIG_ResourceParams.Parameters.ValueData = 1,2,3
        It "Parses Array Integer Values Properly" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?s)$(($CONFIG_ResourceParams.Parameters.ValueData | %{"$_"}) -join ", ")"
        }

        It "Detects Resource Conflicts" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?s)<#$($CONFIG_ResourceParams.Type).*}#>" 
        }

        $CONFIG_ResourceParams.Add("CommentOUT", $True)
        It "Comments out Resources when asked" {
            Write-DSCString @CONFIG_ResourceParams | Should Match "(?s)<#$($CONFIG_ResourceParams.Type).*}#>" 
        }

        It "Creates Node Blocks" {
            Write-DSCString @CONFIG_Node | Should Match "Node $($CONFIG_Node.Name)`n`t{`n"
        }

        It "Closes Configuration Blocks" {
            Write-DSCString -CloseConfigurationBlock | Should Match "`n}`n"
        }

        It "Closes Node Blocks" {
            Write-DSCString -CloseNodeBlock | Should Match "`t}"
        }

        It "Creates Invoke Configuration Strings" {
            Write-DSCString @CONFIG_Invoke | Should Match "$($CONFIG_Invoke.Name) -OutputPath '$($CONFIG_Invoke.OutputPath.Replace("\", "\\"))'"
        }
    }
    
    Context "Complete-Configuration" {
        Mock -Verifiable -CommandName Get-PSCallStack { return [psobject]@(@{Command="None"}, @{Command="PesterTest"}) }
        $Configuration = @"
Configuration Test
{
    Import-DSCResource -ModuleName PSDesiredStateConfiguration
    Node localhost
    {
        Service Spooler
        {
            Name = 'Spooler'
            State = 'Running'
        }
    }
}
 
Test -OutputPath $($script:TestSourceRoot)
"@

    $Configuration_ERROR = @"
Configuration Test
{
    Import-DSCResource -ModuleName PSDesiredStateConfiguration
    Node localhost
    {
        Service Spooler
        {
            State = 'Running'
        }
    }
}
 
Test -OutputPath $($script:TestSourceRoot)
"@

        It "Compiles a Configuration" {
            Complete-Configuration -ConfigString $Configuration -OutputPath $script:TestSourceRoot
            $MOF = (Join-Path -Path $script:TestSourceRoot -ChildPath "localhost.mof")
            $MOF | Should Exist
            Remove-Item $MOF 
        }

        It "Creates Error files on Failure" {
            Complete-Configuration -ConfigString $Configuration_ERROR -OutputPath $script:TestSourceRoot -ErrorAction SilentlyContinue
            $ErrorFile = (Join-Path -Path $script:TestSourceRoot -ChildPath "WTF.ps1.error")
            $ErrorFile | Should Exist
            Remove-Item $ErrorFile
        }
    }
}

Describe "GPO Conversion Helper Tests" {
    
    Mock Write-DSCString -Verifiable { return @{} + $___BoundParameters___ } -ModuleName HelperFunctions

    Context "Comment.cmtx Data" {
        Write-Warning "Not Implemented Yet"
    }

    Context "Write-GPORegistryXMLData" {
        [xml]$RegistryXML = Get-Content $SampleREGXML

        $Settings = $RegistryXML.RegistrySettings.Registry

        It "Parses Registry XML" {
            $Settings | Should Not Be $Null
        }
        
        # Loop through every registry setting.
        foreach ($Setting in $Settings)
        {
            $Parameters = Write-GPORegistryXMLData -XML $Setting
            Write-Host $Parameters.Name

            It "Parses Registry XML Data" {
                    If ($Parameters.CommentOut.IsPresent)
                    {
                        Write-Host -ForegroundColor Green "This Resource was commented OUT for failure to adhere to Standards: Tests are Invalid"
                    }
                    else
                    {
                        $Parameters.Type | Should Be "Registry"
                        [string]::IsNullOrEmpty($Parameters.Parameters.ValueName) | Should Be $false
                        Test-Path -Path $Parameters.Parameters.Key -IsValid | Should Be $true
                        $TypeHash = @{"Binary"=[byte];"Dword"=[int];"ExpandString"=[string];"MultiString"=[string];"Qword"=[string];"String"=[string]}
                        ($Parameters.Parameters.ValueType -in @($TypeHash.Keys)) | Should Be $true
                        $Parameters.Parameters.ValueData | Should BeOfType $TypeHash[$Parameters.Parameters.ValueType]
                        [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                    }
                }
        }
    }

    Context "Write-POLRegistryData" {
        $registryPolicies = Read-PolFile -Path $SamplePOL

        It "Parses Registry Policies" {
            $registryPolicies | Should Not Be $Null
        }

        foreach ($Policy in $registryPolicies)
        {
            $Parameters = Write-POLRegistryData -Data $Policy
            Write-Host $Parameters.Name

            It "Parses Registry Data" {
                    If ($Parameters.CommentOut.IsPresent)
                    {
                        Write-Host -ForegroundColor Green "This Resource was commented OUT for failure to adhere to Standards: Tests are Invalid"
                    }
                    else
                    {
                        $Parameters.Type | Should Be "Registry"
                        [string]::IsNullOrEmpty($Parameters.Parameters.ValueName) | Should Be $false
                        Test-Path -Path $Parameters.Parameters.Key -IsValid | Should Be $true
                        $TypeHash = @{"Binary"=[byte];"Dword"=[int];"ExpandString"=[string];"MultiString"=[string];"Qword"=[string];"String"=[string]}
                        ($Parameters.Parameters.ValueType -in @($TypeHash.Keys)) | Should Be $true
                        $Parameters.Parameters.ValueData | Should BeOfType $TypeHash[$Parameters.Parameters.ValueType]
                        [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                    }
                }
        }
    }
        
    Context "GPtTempl.INF Data" {
        $ini = Get-IniContent $SampleGPTemp

        It "Parses INF files" {
            { Get-IniContent $SampleGPTemp } | Should Not Throw
            $ini | Should Not Be $null
        }

         # Loop through every heading.
        foreach ($key in $ini.Keys)
        {
            # Loop through every setting in the heading.
            foreach ($subKey in $ini[$key].Keys)
            {
                switch ($key)
                {
                    "Service General Setting"
                    {
                        It "Parses Service Data" {
                            $Parameters = Write-INFServiceData -Service $subkey -ServiceData $ini[$key][$subKey]
                            Write-Host $Parameters.Name
                        }
                    }

                    "Registry Values"
                    {
                        It "Parses Registry Values" {
                            If ($Parameters.CommentOut.IsPresent)
                            {
                                Write-Host -ForegroundColor Green "This Resource was commented OUT for failure to adhere to Standards: Tests are Invalid"
                            }
                            else
                            {
                                $Parameters = Write-INFRegistryData -Key $subkey -ValueData $ini[$key][$subKey]
                                Write-Host $Parameters.Name
                                $Parameters.Type | Should Be "Registry"
                                [string]::IsNullOrEmpty($Parameters.Parameters.ValueName) | Should Be $false
                                Test-Path -Path $Parameters.Parameters.Key -IsValid | Should Be $true
                                $TypeHash = @{"Binary"=[byte];"Dword"=[int];"ExpandString"=[string];"MultiString"=[string];"Qword"=[string];"String"=[string]}
                                ($Parameters.Parameters.ValueType -in @($TypeHash.Keys)) | Should Be $true
                                $Parameters.Parameters.ValueData | Should BeOfType $TypeHash[$Parameters.Parameters.ValueType]
                                [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                            }
                        }
                    }

                    "File Security"
                    {
                        It "Parses File ACL Data" {
                            $Parameters = Write-INFFileSecurityData -Path $subkey -ACLData $ini[$key][$subKey]
                            Write-Host $Parameters.Name
                            $Parameters.Type | Should Be xACL
                            [String]::IsNullOrEmpty($Parameters.Parameters.DACLString) | Should Be $false
                            Test-PAth -Path "$($Parameters.Parameters.Path)" -IsValid | Should Be $true
                            [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                        }
                    }
                
                    "Privilege Rights"
                    {
                        It "Parses Privilege Data" {
                            $Parameters = Write-INFPrivilegeData -Privilege $subkey -PrivilegeData $ini[$key][$subKey]
                            Write-Host $Parameters.Name
                            $Parameters.Type | Should Be "UserRightsAssignment"
                            [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                            $UserRightsHash.Values -contains $Parameters.Parameters.Policy | Should Be $true
                        }
                    }
                
                    "Kerberos Policy"
                    {
                        It "Parses Kerberos Data" {
                            $Parameters = Write-INFSecuritySettingData -Key $subKey -SecurityData $ini[$key][$subkey]
                            Write-Host $Parameters.Name
                            $Parameters.Type | Should Be "SecuritySetting"
                            [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                            $SecuritySettings -contains $Parameters.Parameters.Name | Should Be $true
                            $Parameters.Parameters.ContainsKey($Parameters.Parameters.Name) | Should Be $true
                        }
                    }
                
                    "Registry Keys"
                    {
                        It "Parses Registry ACL Data" {
                            $Parameters = Write-INFRegistryACLData -Path $subkey -ACLData $ini[$key][$subKey]
                            Write-Host $Parameters.Name
                            [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                            Test-Path -Path $Parameters.Parameters.Path -IsValid | Should Be $true
                            [string]::IsNullOrEmpty($Parameters.Parameters.DACLString) | SHould Be $false
                        }
                    }
                
                    "System Access"
                    {
                        It "Parses System Access Settings" {
                            $Parameters = Write-INFSecuritySettingData -Key $subKey -SecurityData $ini[$key][$subkey]
                            Write-Host $Parameters.Name
                            $Parameters.Type | Should Be "SecuritySetting"
                            [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                            $SecuritySettings -contains $Parameters.Parameters.Name | Should Be $true
                            $Parameters.Parameters.ContainsKey($Parameters.Parameters.Name) | Should Be $true
                        }
                    }
                }
            }
        }
    }
}

Describe "XML Conversion Helper Tests" {
    
    [XML]$XML = Get-Content $SampleXML
    
    # We need to setup a namespace to properly search the XML.
    $namespace = @{e="http://schemas.microsoft.com/SolutionAccelerator/SecurityCompliance"}
    
    # Grab all the DiscoveryInfo objects in the XML. They determine how to find the setting in question.
    $results = (Select-XML -XPath "//e:SettingDiscoveryInfo" -Xml $xml -Namespace $namespace).Node

    Mock Write-DSCString -Verifiable { return @{} + $___BoundParameters___ } -ModuleName HelperFunctions
    
    It "Parses XML into DiscoveryInfo Objects" {
        $results | Should Not Be $null
    }

    Function Get-ValueNodeData 
    {
        param
        (
            [System.Xml.XmlElement]$node
        )

        $Setting = "../.."
        $SettingDiscoveryInfo = ".."
    
        # Grab the ID/Name from the Setting value.
        $ID = $node.SelectNodes($Setting).id.Trim("{").TrimEnd("}")
            
        # Find the ValueData using the ID.
        $valueNodeData = (Select-XML -XPath "//e:SettingRef[@setting_ref='{$($id)}']" -Xml $xml -Namespace $namespace).Node
                        
        if ($valueNodeData -eq $null)
        {
            Write-Error "Could not find ValueNodeData of $id" 
            return $null
        }
        else 
        {
            return $valueNodeData
        }
    }

    Context "Write-XMLRegistryData" {
        
        $SampleREG_DI = $results.Where({$_.DiscoveryType -eq "Registry" -and $_.ChildNodes.name -contains "RegistryDiscoveryInfo"})
        $SampleREG_DI_Alt = $results.Where({$_.DiscoveryType -eq "Registry" -and $_.ChildNodes.name -notcontains "RegistryDiscoveryInfo"})
    
        for ($i = 0; $i -lt $SampleREG_DI.Count;$i++)
        {
            It "Finds RegistyDiscoveryInfo" {
                $SampleREG_DI[$i] | Should Not Be $Null
            }
        
            $SampleREG_VND = Get-ValueNodeData $SampleREG_DI[$i]    

            It "Retrieves Value Node Data" { 
                $SampleREG_VND | Should Not Be $Null
            }

            $Parameters = Write-XMLRegistryData -DiscoveryData $SampleREG_DI[$i] -ValueData $SampleREG_VND
            Write-Host $Parameters.Name

            It "Parses Registry Data" {
                If ($Parameters.CommentOut.IsPresent)
                {
                    Write-Host -ForegroundColor Green "This Resource was commented OUT for failure to adhere to Standards: Tests are Invalid"
                }
                else
                {
                    $Parameters.Type | Should Be "Registry"
                    [string]::IsNullOrEmpty($Parameters.Parameters.ValueName) | Should Be $false
                    Test-Path -Path $Parameters.Parameters.Key -IsValid | Should Be $true
                    $TypeHash = @{"Binary"=[byte];"Dword"=[int];"ExpandString"=[string];"MultiString"=[string];"Qword"=[string];"String"=[string]}
                    ($Parameters.Parameters.ValueType -in @($TypeHash.Keys)) | Should Be $true
                    $Parameters.Parameters.ValueData | Should BeOfType $TypeHash[$Parameters.Parameters.ValueType]
                    [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                }
            }

            It "Parses Comment Data" {
                [string]::IsNullOrEmpty($parameters.Comment) | Should Be $false
            }

            It "Converts Comments into StringData" {
                { Get-NodeDataFromComments -Comments $Parameters.Comment } | Should Not Throw
            }
        }

        for ($i = 0; $i -lt $SampleREG_DI_Alt.Count;$i++)
        {
            It "Finds Alternate RegistryDiscoveryInfo" {
                $SampleREG_DI_Alt[$i] | Should Not Be $Null
            }
    
            $SampleREG_VND_Alt = Get-ValueNodeData $SampleREG_DI_Alt[$i]

            It "Retrieves Alternate Value Node Data" { 
                $SampleREG_VND_Alt | Should Not Be $null
            }

            $Parameters_All = Write-XMLRegistryData -DiscoveryData $SampleREG_DI_Alt[$i] -ValueData $SampleREG_VND_Alt

            foreach ($Parameters in  $Parameters_All)
            {
                Write-Host $Parameters.Name

                It "Parses Registry Data" {
                    If ($Parameters.CommentOut.IsPresent)
                    {
                        Write-Host -ForegroundColor Green "This Resource was commented OUT for failure to adhere to Standards: Tests are Invalid"
                    }
                    else
                    {
                        $Parameters.Type | Should Be "Registry"
                        [string]::IsNullOrEmpty($Parameters.Parameters.ValueName) | Should Be $false
                        Test-Path -Path $Parameters.Parameters.Key -IsValid | Should Be $true
                        $TypeHash = @{"Binary"=[byte];"Dword"=[int];"ExpandString"=[string];"MultiString"=[string];"Qword"=[string];"String"=[string]}
                        ($Parameters.Parameters.ValueType -in @($TypeHash.Keys)) | Should Be $true
                        $Parameters.Parameters.ValueData | Should BeOfType $TypeHash[$Parameters.Parameters.ValueType]
                        [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                    }
                }

                It "Parses Comment Data" {
                    [string]::IsNullOrEmpty($parameters.Comment) | Should Be $false
                }

                It "Converts Comments into StringData" {
                    { Get-NodeDataFromComments -Comments $Parameters.Comment } | Should Not Throw
                }
            }
        }
    }
        
    Context "Write-XMLAuditData" {
        $SampleAUDIT_DI = $results.Where({$_.DiscoveryType -eq "AdvancedAuditPolicy"})

        for ($i = 0; $i -lt $SampleAUDIT_DI.Count;$i++)
        {
            $SampleAUDIT_VND = Get-ValueNodeData $SampleAUDIT_DI[$i]
            $Parameters = Write-XMLAuditData -DiscoveryData $SampleAUDIT_DI[$i] -ValueData $SampleAUDIT_VND
            Write-Host $Parameters.Name

            It "Parses AUDIT Data" {
                $Parameters.Type | Should BE "xAuditCategory"
                $AuditCategoryHash.Values -contains $Parameters.Parameters.SubCategory | Should Be $true
                @("Failure","NoAuditing","Success","SuccessAndFailure") -contains $Parameters.Parameters.AuditFlag | Should Be $true
                [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
            }

            It "Parses Comment Data" {
                [string]::IsNullOrEmpty($parameters.Comment) | Should Be $false
            }

            It "Converts Comments into StringData" {
                { Get-NodeDataFromComments -Comments $Parameters.Comment } | Should Not Throw
            }
        }
    }

    Context "Write-XMLPrivilegeData" {
        $SamplePRIV_DI = $results.Where({$_.DiscoveryType -eq "GeneratedScript (User Rights Assignment)"})

        for ($i = 0; $i -lt $SamplePRIV_DI.Count;$i++)
        {
            $SamplePRIV_VND = Get-ValueNodeData $SamplePRIV_DI[$i]
            $Parameters = Write-XMLPrivilegeData -DiscoveryData $SamplePRIV_DI[$i] -ValueData $SamplePRIV_VND
            Write-Host $Parameters.Name

            It "Parses PRIVILEGE Data" {
                $Parameters.Type | Should Be "UserRightsAssignment"
                [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                $UserRightsHash.Values -contains $Parameters.Parameters.Policy | Should Be $true
                # Cannot parse Privilege Data without a sample because it COULD actually be Empty
            }

            It "Parses Comment Data" {
                [string]::IsNullOrEmpty($parameters.Comment) | Should Be $false
            }

            It "Converts Comments into StringData" {
                { Get-NodeDataFromComments -Comments $Parameters.Comment } | Should Not Throw
            }
        }
    }

    Context "Write-XMLWMIData" {
        $SampleWMI_DI = $results.Where({$_.DiscoveryType -eq "WMI"})

        for ($i = 0; $i -lt $SampleWMI_DI.Count; $i++)
        {
            $SampleWMI_VND = Get-ValueNodeData $SampleWMI_DI[$i]
            $Parameters = Write-XMLWMIData -DiscoveryData $SampleWMI_DI[$i] -ValueData $SampleWMI_VND
            Write-Host $Parameters.Name

            It "Parses SecuritySetting Data" {
                $Parameters.Type | Should Be "SecuritySetting"
                [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                $SecuritySettings -contains $Parameters.Parameters.Name | Should Be $true
                $Parameters.Parameters.ContainsKey($Parameters.Parameters.Name) | Should Be $true
            }

            It "Parses Comment Data" {
                [string]::IsNullOrEmpty($parameters.Comment) | Should Be $false
            }

            It "Converts Comments into StringData" {
                { Get-NodeDataFromComments -Comments $Parameters.Comment } | Should Not Throw
            }
        }
    }
}

Describe "JSON Conversion Helper Tests" {
    
    $JSON = Get-Content -Path $JSONSample | ConvertFrom-Json
    $registryPolicies = $JSON.properties.RulesetsCollection.BaselineRuleset.rules.BaselineRegistryRule
    $AuditPolicies = $JSON.properties.RulesetsCollection.BaselineRuleset.rules.BaselineAuditPolicyRule
    $securityPolicies = $JSON.properties.RulesetsCollection.BaselineRuleset.rules.BaselineSecurityPolicyRule
    
    Context "Write-JSONRegistryData" {
        Mock Write-DSCString -Verifiable { return @{} + $___BoundParameters___ } -ModuleName HelperFunctions
        
        # Loop through all the registry settings.
        Foreach ($Policy in $registryPolicies)
        {
            $HiveHash = @{"LocalMachine" = "HKLM:"}
            $TypeHash = @{"Int"="DWORD";"String"= "String"}
            $Parameters = Write-JSONRegistryData -RegistryData  $Policy 
            Write-Host $Parameters.Name -ForegroundColor White
            It "Parses Regisry Data" {
                If ($Parameters.CommentOut.IsPresent)
                {
                    Write-Host -ForegroundColor Green "This Resource was commented OUT for failure to adhere to Standards: Tests are Invalid"
                }
                else
                {
                    $Parameters.Type | Should Be "Registry"
                    [string]::IsNullOrEmpty($Parameters.Parameters.ValueName) | Should Be $false
                    Test-Path -Path $Parameters.Parameters.Key -IsValid | Should Be $true
                    $TypeHash = @{"Binary"=[byte];"Dword"=[int];"ExpandString"=[string];"MultiString"=[string];"Qword"=[string];"String"=[string]}
                    ($Parameters.Parameters.ValueType -in @($TypeHash.Keys)) | Should Be $true
                    $Parameters.Parameters.ValueData | Should BeOfType $TypeHash[$Parameters.Parameters.ValueType]
                    [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                }
            }
            
            switch ($JSON_Entry)
            {
                {$_.RegValueType -eq "Int"}
                {
                    It "Parses Integers Values Properly" {
                        $Parameters.Parameters.ValueData | Should BeOfType ([int]) # Should BeOfType?
                    }
                }

                {$_.RegValueType -eq "String"}
                {
                    It "Parses String Values Properly" {
                    $Parameters.Parameters.ValueData | Should BeOfType ([string]) # Should BeOfType?
                    }
                }
                 
                {$_.RegValueType -eq "Binary"}
                {        
                    <#It "Parses Binary Values Properly" {
                        $Parameters = Write-JSONRegistryData -RegistryData $JSON_Sample
                    }#>

                }

                {$_.Enabled -eq $False}
                {
                    It "Respects the Enabled Flag" {
                        $Parameters.CommentOUT | Should Be $true
                    }
                }
            }
        }
    }
    
    Context "Write-JSONAuditData" {
        Mock Write-DSCString -Verifiable { return @{} + $___BoundParameters___ } -ModuleName HelperFunctions
        
        foreach ($Policy in $auditPolicies)
        {
            $Parameters = Write-JSONAuditData -AuditData $Policy 
            Write-Host $Parameters.Name -ForegroundColor White
            It "Parses Audit Data" {
                $Parameters.Type | Should Be xAuditCategory
                $Parameters.Parameters.SubCategory | Should Be $AuditCategoryHash[$Policy.AuditPolicyId]
                $Parameters.Parameters.AuditFlag | Should Be $Policy.ExpectedValue.Trim()
                [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
            }
        
            if (!$Policy.Enabled)
            {
                It "Respects the Enabled Flag" {
                    $Parameters.CommentOUT | Should Be $True
                }
            }
        }
    }
    
    Context "Write-JSONPrivilegeData" {
        Mock Write-DSCString -Verifiable { return @{} + $___BoundParameters___ } -ModuleName HelperFunctions
        
        foreach ($Policy in $securityPolicies)
        {
            $Parameters = Write-JSONPrivilegeData -PrivilegeData $Policy 
            Write-Host $Parameters.Name
                    
            switch ($Policy.SectionName)
            {
                "Service General Setting"
                {

                }

                "Registry Values"
                {

                }

                "File Security"
                {

                }
                
                "Privilege Rights"
                {            
                    It "Parses Privilege Data" {
                        $Parameters.Type | Should Be "UserRightsAssignment"
                        $Parameters.Parameters.Policy | Should Be $UserRightsHash[$Policy.SettingName]
                        $Parameters.Parameters.Identity -join ", " | Should Match ((($Policy.ExpectedValue.Replace("No One", "") -split ", ") | %{"(?=.*$_)"}) -join "")
                        [string]::IsNullOrEmpty($Parameters.Name) | Should Be $false
                    }
                }
                
                "Kerberos Policy"
                {
                
                }
                
                "Registry Keys"
                {

                }
                
                "System Access"
                {

                }      
             }

             if (!$Policy.Enabled)
             {
                It "Respects the Enabled Flag" {
                    $Parameters.CommentOUT | Should be $true
                }
            }
        }
    }
}