Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Format-Hex.Tests.ps1664 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# This is a Pester test suite to validate the Format-Hex cmdlet in the Microsoft.PowerShell.Utility module.5 6<#7    Purpose:8        Verify Format-Hex displays the Hexadecimal value for the input data.9    Action:10        Run Format-Hex.11    Expected Result:12        Hexadecimal equivalent of the input data is displayed.13#>14 15Describe "FormatHex" -tags "CI" {16 17    BeforeAll {18 19        $newline = [Environment]::Newline20 21        Setup -d FormatHexDataDir22        $inputFile1 = New-Item -Path "$TestDrive/SourceFile-1.txt"23        $inputText1 = 'Hello World'24        Set-Content -LiteralPath $inputFile1.FullName -Value $inputText1 -NoNewline25 26        $inputFile2 = New-Item -Path "$TestDrive/SourceFile-2.txt"27        $inputText2 = 'More text'28        Set-Content -LiteralPath $inputFile2.FullName -Value $inputText2 -NoNewline29 30        $inputFile3 = New-Item -Path "$TestDrive/SourceFile literal [3].txt"31        $inputText3 = 'Literal path'32        Set-Content -LiteralPath $inputFile3.FullName -Value $inputText3 -NoNewline33 34        $inputFile4 = New-Item -Path "$TestDrive/SourceFile-4.txt"35        $inputText4 = 'Now is the winter of our discontent'36        Set-Content -LiteralPath $inputFile4.FullName -Value $inputText4 -NoNewline37 38        $certificateProvider = Get-ChildItem Cert:\CurrentUser\My\ -ErrorAction SilentlyContinue39        $thumbprint = $null40        $certProviderAvailable = $false41 42        if ($certificateProvider.Count -gt 0) {43            $thumbprint = $certificateProvider[0].Thumbprint44            $certProviderAvailable = $true45        }46 47        $skipTest = ([System.Management.Automation.Platform]::IsLinux -or [System.Management.Automation.Platform]::IsMacOS -or (-not $certProviderAvailable))48    }49 50    Context "InputObject Paramater" {51        BeforeAll {52            enum TestEnum {53                TestOne = 1; TestTwo = 2; TestThree = 3; TestFour = 454            }55            Add-Type -TypeDefinition @'56public enum TestSByteEnum : sbyte {57    One   = -1,58    Two   = -2,59    Three = -3,60    Four  = -461}62'@63        }64 65        $testCases = @(66            @{67                Name           = "Can process bool type 'fhx -InputObject `$true'"68                InputObject    = $true69                Count          = 170                ExpectedResult = "00000000   01 00 00 00"71            }72            @{73                Name           = "Can process byte type 'fhx -InputObject [byte]5'"74                InputObject    = [byte]575                Count          = 176                ExpectedResult = "00000000   05"77            }78            @{79                Name           = "Can process byte[] type 'fhx -InputObject [byte[]](1,2,3,4,5)'"80                InputObject    = [byte[]](1, 2, 3, 4, 5)81                Count          = 182                ExpectedResult = "00000000   01 02 03 04 05                                   ....."83            }84            @{85                Name           = "Can process int type 'fhx -InputObject 7'"86                InputObject    = 787                Count          = 188                ExpectedResult = "00000000   07 00 00 00                                      ...."89            }90            @{91                Name           = "Can process int[] type 'fhx -InputObject [int[]](5,6,7,8)'"92                InputObject    = [int[]](5, 6, 7, 8)93                Count          = 194                ExpectedResult = "00000000   05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00  ................"95            }96            @{97                Name           = "Can process int32 type 'fhx -InputObject [int32]2032'"98                InputObject    = [int32]203299                Count          = 1100                ExpectedResult = "00000000   F0 07 00 00                                      ð..."101            }102            @{103                Name           = "Can process int32[] type 'fhx -InputObject [int32[]](2032, 2033, 2034)'"104                InputObject    = [int32[]](2032, 2033, 2034)105                Count          = 1106                ExpectedResult = "0000000000000000   F0 07 00 00 F1 07 00 00 F2 07 00 00              ð...ñ...ò..."107            }108            @{109                Name           = "Can process Int64 type 'fhx -InputObject [Int64]9223372036854775807'"110                InputObject    = [Int64]9223372036854775807111                Count          = 1112                ExpectedResult = "0000000000000000   FF FF FF FF FF FF FF 7F                          ÿÿÿÿÿÿÿ�"113            }114            @{115                Name           = "Can process Int64[] type 'fhx -InputObject [Int64[]](9223372036852,9223372036853)'"116                InputObject    = [Int64[]](9223372036852, 9223372036853)117                Count          = 1118                ExpectedResult = "0000000000000000   F4 5A D0 7B 63 08 00 00 F5 5A D0 7B 63 08 00 00  ôZÐ{c...õZÐ{c..."119            }120            @{121                Name           = "Can process string type 'fhx -InputObject hello world'"122                InputObject    = "hello world"123                Count          = 1124                ExpectedResult = "0000000000000000   68 65 6C 6C 6F 20 77 6F 72 6C 64                 hello world"125            }126            @{127                Name           = "Can process PS-native enum array '[TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour') | fhx'"128                InputObject    = [TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour')129                Count          = 1130                ExpectedResult = "0000000000000000   01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00  ................"131            }132            @{133                Name           = "Can process C#-native sbyte enum array '[TestSByteEnum[]]('One', 'Two', 'Three', 'Four') | fhx'"134                InputObject    = [TestSByteEnum[]]('One', 'Two', 'Three', 'Four')135                Count          = 1136                ExpectedResult = "0000000000000000   FF FE FD FC                                      .þýü"137            }138        )139 140        It "<Name>" -TestCase $testCases {141 142            param ($Name, $InputObject, $Count, $ExpectedResult)143 144            $result = Format-Hex -InputObject $InputObject145 146            $result.count | Should -Be $Count147            $result | Should -BeOfType Microsoft.PowerShell.Commands.ByteCollection148            $result.ToString() | Should -MatchExactly $ExpectedResult149        }150    }151 152    Context "InputObject From Pipeline" {153        BeforeAll {154            enum TestEnum {155                TestOne = 1; TestTwo = 2; TestThree = 3; TestFour = 4156            }157            Add-Type -TypeDefinition @'158public enum TestSByteEnum : sbyte {159    One   = -1,160    Two   = -2,161    Three = -3,162    Four  = -4163}164'@165        }166 167        $testCases = @(168            @{169                Name           = "Can process bool type '`$true | fhx'"170                InputObject    = $true171                Count          = 1172                ExpectedResult = "0000000000000000   01"173            }174            @{175                Name           = "Can process byte type '[byte]5 | fhx'"176                InputObject    = [byte]5177                Count          = 1178                ExpectedResult = "0000000000000000   05"179            }180            @{181                Name           = "Can process byte[] type '[byte[]](1,2) | fhx'"182                InputObject    = [byte[]](1, 2)183                Count          = 1184                ExpectedResult = "0000000000000000   01 02                                            ��"185            }186            @{187                Name           = "Can process int type '7 | fhx'"188                InputObject    = 7189                Count          = 1190                ExpectedResult = "0000000000000000   07 00 00 00                                      �   "191            }192            @{193                Name           = "Can process int[] type '[int[]](5,6) | fhx'"194                InputObject    = [int[]](5, 6)195                Count          = 1196                ExpectedResult = "0000000000000000   05 00 00 00 06 00 00 00                          �   �   "197            }198            @{199                Name           = "Can process int32 type '[int32]2032 | fhx'"200                InputObject    = [int32]2032201                Count          = 1202                ExpectedResult = "0000000000000000   F0 07 00 00                                      ð�  "203            }204            @{205                Name           = "Can process int32[] type '[int32[]](2032, 2033) | fhx'"206                InputObject    = [int32[]](2032, 2033)207                Count          = 1208                ExpectedResult = "0000000000000000   F0 07 00 00 F1 07 00 00                          ð�  ñ�  "209            }210            @{211                Name           = "Can process Int64 type '[Int64]9223372036854775807 | fhx'"212                InputObject    = [Int64]9223372036854775807213                Count          = 1214                ExpectedResult = "0000000000000000   FF FF FF FF FF FF FF 7F                          ÿÿÿÿÿÿÿ�"215            }216            @{217                Name           = "Can process Int64[] type '[Int64[]](9223372036852,9223372036853) | fhx'"218                InputObject    = [Int64[]](9223372036852, 9223372036853)219                Count          = 1220                ExpectedResult = "0000000000000000   F4 5A D0 7B 63 08 00 00 F5 5A D0 7B 63 08 00 00  ôZÐ{c�  õZÐ{c�  "221            }222            @{223                Name           = "Can process string type 'hello world | fhx'"224                InputObject    = "hello world"225                Count          = 1226                ExpectedResult = "0000000000000000   68 65 6C 6C 6F 20 77 6F 72 6C 64                 hello world"227            }228            @{229                Name                 = "Can process string type amidst other types { 1, 2, 3, 'hello world' | fhx }"230                InputObject          = 1, 2, 3, "hello world"231                Count                = 2232                ExpectedResult       = "0000000000000000   01 00 00 00 02 00 00 00 03 00 00 00              �   �   �   "233                ExpectedSecondResult = "0000000000000000   68 65 6C 6C 6F 20 77 6F 72 6C 64                 hello world"234            }235            @{236                Name                 = "Can process jagged array type '[sbyte[]](-15, 18, 21, -5), [byte[]](1, 2, 3, 4, 5, 6) | fhx'"237                InputObject          = [sbyte[]](-15, 18, 21, -5), [byte[]](1, 2, 3, 4, 5, 6)238                Count                = 2239                ExpectedResult       = "0000000000000000   F1 12 15 FB                                      ñ��û"240                ExpectedSecondResult = "0000000000000000   01 02 03 04 05 06                                ������"241            }242            @{243                Name                 = "Can process jagged array type '[bool[]](`$true, `$false), [int[]](1, 2, 3, 4) | fhx'"244                InputObject          = [bool[]]($true, $false), [int[]](1, 2, 3, 4)245                Count                = 2246                ExpectedResult       = "0000000000000000   01 00 00 00 00 00 00 00                          �"247                ExpectedSecondResult = "0000000000000000   01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00  �   �   �   �"248            }249            @{250                Name           = "Can process PS-native enum array '[TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour') | fhx'"251                InputObject    = [TestEnum[]]('TestOne', 'TestTwo', 'TestThree', 'TestFour')252                Count          = 1253                ExpectedResult = "0000000000000000   01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00  �   �   �   �   "254            }255            @{256                Name           = "Can process C#-native sbyte enum array '[TestSByteEnum[]]('One', 'Two', 'Three', 'Four') | fhx'"257                InputObject    = [TestSByteEnum[]]('One', 'Two', 'Three', 'Four')258                Count          = 1259                ExpectedResult = "0000000000000000   FF FE FD FC                                      ÿþýü"260            }261        )262 263        It "<Name>" -TestCases $testCases {264 265            param ($Name, $InputObject, $Count, $ExpectedResult, $ExpectedSecondResult)266 267            $result = $InputObject | Format-Hex268 269            $result.Count | Should -Be $Count270            $result | Should -BeOfType Microsoft.PowerShell.Commands.ByteCollection271            $result[0].ToString() | Should -MatchExactly $ExpectedResult272 273            if ($result.count -gt 1) {274                $result[1].ToString() | Should -MatchExactly $ExpectedSecondResult275            }276        }277 278        $heterogenousInputCases = @(279            @{280                InputScript     = { [sbyte[]](-15, 18, 21, -5), "hello", [byte[]](1..6), 1, 2, 3, 4 }281                Count           = 4282                ExpectedResults = @(283                    "0000000000000000   F1 12 15 FB                                      ñ��û"284                    "0000000000000000   68 65 6C 6C 6F                                   hello"285                    "0000000000000000   01 02 03 04 05 06                                ������"286                    "0000000000000000   01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00  �   �   �   �   "287                )288                ExpectedLabels  = @(289                    "System.SByte[]"290                    "System.String"291                    "System.Byte"292                    "System.Int32"293                ).ForEach{ [regex]::Escape($_) } -join '|'294            }295            @{296                InputScript     = { $inputFile1, "Mountains are merely mountains", 1, 4, 5, 3, [ushort[]](1..10) }297                Count           = 6298                ExpectedResults = @(299                    "0000000000000000   48 65 6C 6C 6F 20 57 6F 72 6C 64                 Hello World"300                    "0000000000000000   4D 6F 75 6E 74 61 69 6E 73 20 61 72 65 20 6D 65  Mountains are me"301                    "0000000000000010   72 65 6C 79 20 6D 6F 75 6E 74 61 69 6E 73        rely mountains"302                    "0000000000000000   01 00 00 00 04 00 00 00 05 00 00 00 03 00 00 00  �   �   �   �   "303                    "0000000000000000   01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00  � � � � � � � � "304                    "0000000000000010   09 00 0A 00                                      � � "305                )306                ExpectedLabels  = @(307                    $inputFile1.FullName308                    "System.String"309                    "System.Int32"310                    "System.UInt16[]"311                ).ForEach{ [regex]::Escape($_) } -join '|'312            }313            @{314                InputScript     = { $true, $false, $true, 123, 100, 76, $true, $false }315                Count           = 3316                ExpectedResults = @(317                    "0000000000000000   01 00 00 00 00 00 00 00 01 00 00 00              �       �"318                    "0000000000000000   7B 00 00 00 64 00 00 00 4C 00 00 00              {   d   L"319                    "0000000000000000   01 00 00 00 00 00 00 00                          �"320                )321                ExpectedLabels  = @(322                    "System.Boolean"323                    "System.Int32"324                ).ForEach{ [regex]::Escape($_) } -join '|'325            }326        )327 328        It 'can process jagged input: <InputScript>' -TestCases $heterogenousInputCases {329            param($InputScript, $Count, $ExpectedResults, $ExpectedLabels)330 331            $Results = & $InputScript | Format-Hex332            $Results | Should -HaveCount $Count333            $ExpectedResults | Should -HaveCount $Count334 335            for ($Number = 0; $Number -lt $Results.Count; $Number++) {336                $Results[$Number] | Should -MatchExactly $ExpectedResults[$Number]337                $Results[$Number].Label | Should -MatchExactly $ExpectedLabels338            }339        }340    }341 342    Context "Path and LiteralPath Parameters" {343 344        $testDirectory = $inputFile1.DirectoryName345 346        $testCases = @(347            @{348                Name           = "Can process file content from given file path 'fhx -Path `$inputFile1'"349                PathCase       = $true350                Path           = $inputFile1351                Count          = 1352                ExpectedResult = $inputText1353            }354            @{355                Name                 = "Can process file content from all files in array of file paths 'fhx -Path `$inputFile1, `$inputFile2'"356                PathCase             = $true357                Path                 = @($inputFile1, $inputFile2)358                Count                = 2359                ExpectedResult       = $inputText1360                ExpectedSecondResult = $inputText2361            }362            @{363                Name                 = "Can process file content from all files when resolved to multiple paths 'fhx -Path '`$testDirectory\SourceFile-*''"364                PathCase             = $true365                Path                 = "$testDirectory\SourceFile-*"366                Count                = 2367                ExpectedResult       = $inputText1368                ExpectedSecondResult = $inputText2369            }370            @{371                Name           = "Can process file content from given file path 'fhx -LiteralPath `$inputFile3'"372                Path           = $inputFile3373                Count          = 1374                ExpectedResult = $inputText3375            }376            @{377                Name                 = "Can process file content from all files in array of file paths 'fhx -LiteralPath `$inputFile1, `$inputFile3'"378                Path                 = @($inputFile1, $inputFile3)379                Count                = 2380                ExpectedResult       = $inputText1381                ExpectedSecondResult = $inputText3382            }383        )384 385        It "<Name>" -TestCase $testCases {386 387            param ($Name, $PathCase, $Path, $ExpectedResult, $ExpectedSecondResult)388 389            if ($PathCase) {390                $result = Format-Hex -Path $Path391            } else {392                # LiteralPath393                $result = Format-Hex -LiteralPath $Path394            }395 396            $result | Should -BeOfType Microsoft.PowerShell.Commands.ByteCollection397            $result[0].ToString() | Should -MatchExactly $ExpectedResult398 399            if ($result.count -gt 1) {400                $result[1].ToString() | Should -MatchExactly $ExpectedSecondResult401            }402        }403 404        It 'properly accepts -LiteralPath input from a FileInfo object' {405            $FilePath = 'TestDrive:\FHX-LitPathTest.txt'406            "Hello World!" | Set-Content -Path $FilePath407            $FileObject = Get-Item -Path $FilePath408 409            $result = $FileObject | Format-Hex410            if ($IsWindows) {411                $Result.Bytes[-1] | Should -Be 0x0A412                $Result.Bytes[-2] | Should -Be 0x0D413                $Result.Bytes.Length | Should -Be 14414            } else {415                $Result.Bytes[-1] | Should -Be 0x0A416                $Result.Bytes.Length | Should -Be 13417            }418        }419    }420 421    Context "Encoding Parameter" {422        $testCases = @(423            @{424                Name           = "Can process ASCII encoding 'fhx -InputObject 'hello' -Encoding ASCII'"425                Encoding       = "ASCII"426                Count          = 1427                ExpectedResult = "0000000000000000   68 65 6C 6C 6F                                   hello"428            }429            @{430                Name           = "Can process BigEndianUnicode encoding 'fhx -InputObject 'hello' -Encoding BigEndianUnicode'"431                Encoding       = "BigEndianUnicode"432                Count          = 1433                ExpectedResult = "0000000000000000   00 68 00 65 00 6C 00 6C 00 6F                     h e l l o"434            }435            @{436                Name                 = "Can process BigEndianUTF32 encoding 'fhx -InputObject 'hello' -Encoding BigEndianUTF32'"437                Encoding             = "BigEndianUTF32"438                Count                = 2439                ExpectedResult       = "0000000000000000   00 00 00 68 00 00 00 65 00 00 00 6C 00 00 00 6C     h   e   l   l"440                ExpectedSecondResult = "0000000000000010   00 00 00 6F                                         o"441            }442            @{443                Name           = "Can process Unicode encoding 'fhx -InputObject 'hello' -Encoding Unicode'"444                Encoding       = "Unicode"445                Count          = 1446                ExpectedResult = "0000000000000000   68 00 65 00 6C 00 6C 00 6F 00                    h e l l o "447            }448            @{449                Name           = "Can process UTF7 encoding 'fhx -InputObject 'hello' -Encoding UTF7'"450                Encoding       = "UTF7"451                Count          = 1452                ExpectedResult = "0000000000000000   68 65 6C 6C 6F                                   hello"453            }454            @{455                Name           = "Can process UTF8 encoding 'fhx -InputObject 'hello' -Encoding UTF8'"456                Encoding       = "UTF8"457                Count          = 1458                ExpectedResult = "0000000000000000   68 65 6C 6C 6F                                   hello"459            }460            @{461                Name                 = "Can process UTF32 encoding 'fhx -InputObject 'hello' -Encoding UTF32'"462                Encoding             = "UTF32"463                Count                = 2464                ExpectedResult       = "0000000000000000   68 00 00 00 65 00 00 00 6C 00 00 00 6C 00 00 00  h   e   l   l   "465                ExpectedSecondResult = "0000000000000010   6F 00 00 00                                      o   "466            }467        )468 469        It "<Name>" -TestCase $testCases {470 471            param ($Name, $Encoding, $Count, $ExpectedResult)472 473            $result = Format-Hex -InputObject 'hello' -Encoding $Encoding474 475            $result.count | Should -Be $Count476            $result | Should -BeOfType Microsoft.PowerShell.Commands.ByteCollection477            $result[0].ToString() | Should -MatchExactly $ExpectedResult478        }479    }480 481    Context "Validate Error Scenarios" {482 483        $testDirectory = $inputFile1.DirectoryName484 485        $testCases = @(486            @{487                Name                          = "Does not support non-FileSystem Provider paths 'fhx -Path 'Cert:\CurrentUser\My\`$thumbprint' -ErrorAction Stop'"488                PathParameterErrorCase        = $true489                Path                          = "Cert:\CurrentUser\My\$thumbprint"490                ExpectedFullyQualifiedErrorId = "FormatHexOnlySupportsFileSystemPaths,Microsoft.PowerShell.Commands.FormatHex"491            }492            @{493                Name                          = "Type Not Supported 'fhx -InputObject @{'hash' = 'table'} -ErrorAction Stop'"494                InputObjectErrorCase          = $true495                Path                          = $inputFile1496                InputObject                   = @{ "hash" = "table" }497                ExpectedFullyQualifiedErrorId = "FormatHexTypeNotSupported,Microsoft.PowerShell.Commands.FormatHex"498            }499        )500 501        It "<Name>" -Skip:$skipTest -TestCase $testCases {502 503            param ($Name, $PathParameterErrorCase, $Path, $InputObject, $InputObjectErrorCase, $ExpectedFullyQualifiedErrorId)504 505            {506                if ($PathParameterErrorCase) {507                    $result = Format-Hex -Path $Path -ErrorAction Stop508                }509                if ($InputObjectErrorCase) {510                    $result = Format-Hex -InputObject $InputObject -ErrorAction Stop511                }512            } | Should -Throw -ErrorId $ExpectedFullyQualifiedErrorId513        }514    }515 516    Context "Continues to Process Valid Paths" {517 518        $testCases = @(519            @{520                Name                          = "If given invalid path in array, continues to process valid paths 'fhx -Path `$invalidPath, `$inputFile1  -ErrorVariable e -ErrorAction SilentlyContinue'"521                PathCase                      = $true522                InvalidPath                   = "$($inputFile1.DirectoryName)\fakefile8888845345345348709.txt"523                ExpectedFullyQualifiedErrorId = "FileNotFound,Microsoft.PowerShell.Commands.FormatHex"524            }525            @{526                Name                          = "If given a non FileSystem path in array, continues to process valid paths 'fhx -Path `$invalidPath, `$inputFile1  -ErrorVariable e -ErrorAction SilentlyContinue'"527                PathCase                      = $true528                InvalidPath                   = "Cert:\CurrentUser\My\$thumbprint"529                ExpectedFullyQualifiedErrorId = "FormatHexOnlySupportsFileSystemPaths,Microsoft.PowerShell.Commands.FormatHex"530            }531            @{532                Name                          = "If given a non FileSystem path in array (with LiteralPath), continues to process valid paths 'fhx -Path `$invalidPath, `$inputFile1  -ErrorVariable e -ErrorAction SilentlyContinue'"533                InvalidPath                   = "Cert:\CurrentUser\My\$thumbprint"534                ExpectedFullyQualifiedErrorId = "FormatHexOnlySupportsFileSystemPaths,Microsoft.PowerShell.Commands.FormatHex"535            }536        )537 538        It "<Name>" -Skip:$skipTest -TestCase $testCases {539 540            param ($Name, $PathCase, $InvalidPath, $ExpectedFullyQualifiedErrorId)541 542            $output = $null543            $errorThrown = $null544 545            if ($PathCase) {546                $output = Format-Hex -Path $InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue547            } else {548                # LiteralPath549                $output = Format-Hex -LiteralPath $InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue550            }551 552            $errorThrown.FullyQualifiedErrorId | Should -MatchExactly $ExpectedFullyQualifiedErrorId553 554            $output.Length | Should -Be 1555            $output[0].ToString() | Should -MatchExactly $inputText1556        }557    }558 559    Context "Cmdlet Functionality" {560 561        It "Path is default Parameter Set 'fhx `$inputFile1'" {562 563            $result = Format-Hex $inputFile1564 565            $result | Should -Not -BeNullOrEmpty566            , $result | Should -BeOfType Microsoft.PowerShell.Commands.ByteCollection567            $actualResult = $result.ToString()568            $actualResult | Should -MatchExactly $inputText1569        }570 571        It "Validate file input from Pipeline 'Get-ChildItem `$inputFile1 | Format-Hex'" {572 573            $result = Get-ChildItem $inputFile1 | Format-Hex574 575            $result | Should -Not -BeNullOrEmpty576            , $result | Should -BeOfType Microsoft.PowerShell.Commands.ByteCollection577            $actualResult = $result.ToString()578            $actualResult | Should -MatchExactly $inputText1579        }580 581        It "Validate that streamed text does not have buffer underrun problems ''a' * 30 | Format-Hex'" {582 583            $result = "a" * 30 | Format-Hex584 585            $result | Should -Not -BeNullOrEmpty586            $result | Should -BeOfType Microsoft.PowerShell.Commands.ByteCollection587            $result[0].ToString() | Should -MatchExactly "0000000000000000   61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61  aaaaaaaaaaaaaaaa"588            $result[1].ToString() | Should -MatchExactly "0000000000000010   61 61 61 61 61 61 61 61 61 61 61 61 61 61        aaaaaaaaaaaaaa  "589        }590 591        It "Validate that files do not have buffer underrun problems 'Format-Hex -Path `$InputFile4'" {592 593            $result = Format-Hex -Path $InputFile4594 595            $result | Should -Not -BeNullOrEmpty596            $result.Count | Should -Be 3597            $result[0].ToString() | Should -MatchExactly "0000000000000000   4E 6F 77 20 69 73 20 74 68 65 20 77 69 6E 74 65  Now is the winte"598            $result[1].ToString() | Should -MatchExactly "0000000000000010   72 20 6F 66 20 6F 75 72 20 64 69 73 63 6F 6E 74  r of our discont"599            $result[2].ToString() | Should -MatchExactly "0000000000000020   65 6E 74                                         ent             "600        }601    }602 603    Context "Count and Offset parameters" {604        It "Count = length" {605 606            $result = Format-Hex -Path $InputFile4 -Count $inputText4.Length607 608            $result | Should -Not -BeNullOrEmpty609            $result.Count | Should -Be 3610            $result[0].ToString() | Should -MatchExactly "0000000000000000   4E 6F 77 20 69 73 20 74 68 65 20 77 69 6E 74 65  Now is the winte"611            $result[1].ToString() | Should -MatchExactly "0000000000000010   72 20 6F 66 20 6F 75 72 20 64 69 73 63 6F 6E 74  r of our discont"612            $result[2].ToString() | Should -MatchExactly "0000000000000020   65 6E 74                                         ent             "613        }614 615        It "Count = 1" {616            $result = Format-Hex -Path $inputFile4 -Count 1617            $result.ToString() | Should -MatchExactly    "0000000000000000   4E                                               N               "618        }619 620        It "Offset = length" {621            $result = Format-Hex -Path $InputFile4 -Offset $inputText4.Length622            $result | Should -BeNullOrEmpty623 624            $result = Format-Hex -InputObject $inputText4 -Offset $inputText4.Length625            $result | Should -BeNullOrEmpty626        }627 628        It "Offset = 1" {629 630            $result = Format-Hex -Path $InputFile4 -Offset 1631 632            $result | Should -Not -BeNullOrEmpty633            $result.Count | Should -Be 3634            $result[0].ToString() | Should -MatchExactly "0000000000000001   6F 77 20 69 73 20 74 68 65 20 77 69 6E 74 65 72  ow is the winter"635            $result[1].ToString() | Should -MatchExactly "0000000000000011   20 6F 66 20 6F 75 72 20 64 69 73 63 6F 6E 74 65   of our disconte"636            $result[2].ToString() | Should -MatchExactly "0000000000000021   6E 74                                            nt              "637        }638 639        It "Count = 1 and Offset = 1" {640            $result = Format-Hex -Path $inputFile4 -Count 1 -Offset 1641            $result.ToString() | Should -MatchExactly    "0000000000000001   6F                                               o               "642        }643 644        It "Count should be > 0" {645            { Format-Hex -Path $inputFile4 -Count 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.FormatHex"646        }647 648        It "Offset should be >= 0" {649            { Format-Hex -Path $inputFile4 -Offset -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.FormatHex"650        }651 652        It "Offset = 0" {653 654            $result = Format-Hex -Path $InputFile4 -Offset 0655 656            $result | Should -Not -BeNullOrEmpty657            $result.Count | Should -Be 3658            $result[0].ToString() | Should -MatchExactly "0000000000000000   4E 6F 77 20 69 73 20 74 68 65 20 77 69 6E 74 65  Now is the winte"659            $result[1].ToString() | Should -MatchExactly "0000000000000010   72 20 6F 66 20 6F 75 72 20 64 69 73 63 6F 6E 74  r of our discont"660            $result[2].ToString() | Should -MatchExactly "0000000000000020   65 6E 74                                         ent             "661        }662    }663}664