forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatHex.Tests.ps1
More file actions
493 lines (429 loc) · 18.6 KB
/
Copy pathFormatHex.Tests.ps1
File metadata and controls
493 lines (429 loc) · 18.6 KB
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
# This is a Pester test suite to validate the Format-Hex cmdlet in the Microsoft.PowerShell.Utility module.
#
# Copyright (c) Microsoft Corporation, 2015
#
<#
Purpose:
Verify Format-Hex displays the Hexadecimal value for the input data.
Action:
Run Format-Hex.
Expected Result:
Hexadecimal equivalent of the input data is displayed.
#>
function RunInputObjectTestCase($testCase)
{
It "$($testCase.Name)" {
$result = Format-Hex -InputObject $testCase.InputObject
$result | Should Not Be $null
$result.count | Should Be $testCase.Count
$result | Should BeOfType 'Microsoft.PowerShell.Commands.ByteCollection'
$result.ToString() | Should Match $testCase.ExpectedResult
}
}
function RunObjectFromPipelineTestCase($testCase)
{
It "$($testCase.Name)" {
$result = $testCase.InputObject | Format-Hex
$result | Should Not Be $null
$result.count | Should Be $testCase.Count
$result | Should BeOfType 'Microsoft.PowerShell.Commands.ByteCollection'
$result[0].ToString() | Should Match $testCase.ExpectedResult
if ($result.count > 1)
{
$result[1].ToString() | Should Match $testCase.ExpectedSecondResult
}
}
}
function RunPathAndLiteralPathParameterTestCase($testCase)
{
It "$($testCase.Name)" {
if ($testCase.PathCase)
{
$result = Format-Hex -Path $testCase.Path
}
else # LiteralPath
{
$result = Format-Hex -LiteralPath $testCase.Path
}
$result | Should Not Be $null
$result | Should BeOfType 'Microsoft.PowerShell.Commands.ByteCollection'
$result[0].ToString() | Should Match $testCase.ExpectedResult
if ($result.count > 1)
{
$result[1].ToString() | Should Match $testCase.ExpectedSecondResult
}
}
}
function RunEncodingTestCase($testCase)
{
It "$($testCase.Name)" {
$result = Format-Hex -InputObject 'hello' -Encoding $testCase.Encoding
$result | Should Not Be $null
$result.count | Should Be $testCase.Count
$result | Should BeOfType 'Microsoft.PowerShell.Commands.ByteCollection'
$result[0].ToString() | Should Match $testCase.ExpectedResult
}
}
function RunContinuesToProcessCase($testCase)
{
$skipTest = ([System.Management.Automation.Platform]::IsLinux -or [System.Management.Automation.Platform]::IsOSX)
It "$($testCase.Name)" -Skip:$($skipTest -or (-not $certProviderAvailable)) {
$output = $null
$errorThrown = $null
if ($testCase.PathCase)
{
$output = Format-Hex -Path $testCase.InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue
}
else # LiteralPath
{
$output = Format-Hex -LiteralPath $testCase.InvalidPath, $inputFile1 -ErrorVariable errorThrown -ErrorAction SilentlyContinue
}
$errorThrown | Should Not Be $null
$errorThrown.FullyQualifiedErrorId | Should Match $testCase.ExpectedFullyQualifiedErrorId
$output | Should Not Be $null
$output[0].ToString() | Should Match $inputText1
}
}
function RunExpectedErrorTestCase($testCase)
{
$skipTest = ([System.Management.Automation.Platform]::IsLinux -or [System.Management.Automation.Platform]::IsOSX)
It "$($testCase.Name)" -Skip:$($skipTest -or (-not $certProviderAvailable)) {
try
{
if ($testCase.PathParameterErrorCase)
{
$result = Format-Hex -Path $testCase.Path -ErrorAction Stop
}
if ($testCase.InputObjectErrorCase)
{
$result = Format-Hex -InputObject $testCase.InputObject -ErrorAction Stop
}
}
catch
{
$thrownError = $_
}
$thrownError | Should Not Be $null
$thrownError.FullyQualifiedErrorId | Should Match $testCase.ExpectedFullyQualifiedErrorId
}
}
Describe "FormatHex" -tags "CI" {
BeforeAll {
Setup -d FormatHexDataDir
$inputText1 = 'Hello World'
$inputText2 = 'This is a bit more text'
$inputText3 = 'Literal path'
$inputFile1 = setup -f "FormatHexDataDir/SourceFile-1.txt" -content $inputText1 -pass
$inputFile2 = setup -f "FormatHexDataDir/SourceFile-2.txt" -content $inputText2 -pass
$inputFile3 = setup -f "FormatHexDataDir/SourceFile literal [3].txt" -content $inputText3 -pass
$certificateProvider = Get-ChildItem Cert:\CurrentUser\My\ -ErrorAction SilentlyContinue
$thumbprint = $null
$certProviderAvailable = $false
if ($certificateProvider.Count -gt 0)
{
$thumbprint = $certificateProvider[0].Thumbprint
$certProviderAvailable = $true
}
}
Context "InputObject Paramater" {
$testCases = @(
@{
Name = "Can process byte type 'fhx -InputObject [byte]5'"
InputObject = [byte]5
Count = 1
ExpectedResult = "00000000 05"
}
@{
Name = "Can process byte[] type 'fhx -InputObject [byte[]](1,2,3,4,5)'"
InputObject = [byte[]](1,2,3,4,5)
Count = 1
ExpectedResult = "00000000 01 02 03 04 05 ....."
}
@{
Name = "Can process int type 'fhx -InputObject 7'"
InputObject = 7
Count = 1
ExpectedResult = "00000000 07 00 00 00 ...."
}
@{
Name = "Can process int[] type 'fhx -InputObject [int[]](5,6,7,8)'"
InputObject = [int[]](5,6,7,8)
Count = 1
ExpectedResult = "00000000 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 ................"
}
@{
Name = "Can process int32 type 'fhx -InputObject [int32]2032'"
InputObject = [int32]2032
Count = 1
ExpectedResult = "00000000 F0 07 00 00 ð..."
}
@{
Name = "Can process int32[] type 'fhx -InputObject [int32[]](2032, 2033, 2034)'"
InputObject = [int32[]](2032, 2033, 2034)
Count = 1
ExpectedResult = "00000000 F0 07 00 00 F1 07 00 00 F2 07 00 00 ð...ñ...ò..."
}
@{
Name = "Can process Int64 type 'fhx -InputObject [Int64]9223372036854775807'"
InputObject = [Int64]9223372036854775807
Count = 1
ExpectedResult = "00000000 FF FF FF FF FF FF FF 7F ......."
}
@{
Name = "Can process Int64[] type 'fhx -InputObject [Int64[]](9223372036852,9223372036853)'"
InputObject = [Int64[]](9223372036852,9223372036853)
Count = 1
ExpectedResult = "00000000 F4 5A D0 7B 63 08 00 00 F5 5A D0 7B 63 08 00 00 ôZÐ{c...õZÐ{c..."
}
@{
Name = "Can process string type 'fhx -InputObject hello world'"
InputObject = "hello world"
Count = 1
ExpectedResult = "00000000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello world"
}
)
foreach ($testCase in $testCases)
{
RunInputObjectTestCase $testCase
}
}
Context "InputObject From Pipeline" {
$testCases = @(
@{
Name = "Can process byte type '[byte]5 | fhx'"
InputObject = [byte]5
Count = 1
ExpectedResult = "00000000 05"
}
@{
Name = "Can process byte[] type '[byte[]](1,2) | fhx'"
InputObject = [byte[]](1,2)
Count = 2
ExpectedResult = "00000000 01 ."
ExpectedSecondResult = "00000000 02 ."
}
@{
Name = "Can process int type '7 | fhx'"
InputObject = 7
Count = 1
ExpectedResult = "00000000 07 00 00 00 ...."
}
@{
Name = "Can process int[] type '[int[]](5,6) | fhx'"
InputObject = [int[]](5,6)
Count = 2
ExpectedResult = "00000000 05 00 00 00 ...."
ExpectedSecondResult = "00000000 06 00 00 00 ...."
}
@{
Name = "Can process int32 type '[int32]2032 | fhx'"
InputObject = [int32]2032
Count = 1
ExpectedResult = "00000000 F0 07 00 00 ð..."
}
@{
Name = "Can process int32[] type '[int32[]](2032, 2033) | fhx'"
InputObject = [int32[]](2032, 2033)
Count = 2
ExpectedResult = "00000000 F0 07 00 00 ð..."
ExpectedSecondResult = "00000000 F1 07 00 00 ñ..."
}
@{
Name = "Can process Int64 type '[Int64]9223372036854775807 | fhx'"
InputObject = [Int64]9223372036854775807
Count = 1
ExpectedResult = "00000000 FF FF FF FF FF FF FF 7F ......."
}
@{
Name = "Can process Int64[] type '[Int64[]](9223372036852,9223372036853) | fhx'"
InputObject = [Int64[]](9223372036852,9223372036853)
Count = 2
ExpectedResult = "00000000 F4 5A D0 7B 63 08 00 00 ôZÐ{c..."
ExpectedSecondResult = "00000000 F5 5A D0 7B 63 08 00 00 õZÐ{c..."
}
@{
Name = "Can process string type 'hello world | fhx'"
InputObject = "hello world"
Count = 1
ExpectedResult = "00000000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello world"
}
)
foreach ($testCase in $testCases)
{
RunObjectFromPipelineTestCase $testCase
}
}
Context "Path Paramater" {
$testDirectory = $inputFile1.DirectoryName
$testCases = @(
@{
Name = "Can process file content from given file path 'fhx -Path `$inputFile1'"
PathCase = $true
Path = $inputFile1
Count = 1
ExpectedResult = $inputText1
}
@{
Name = "Can process file content from all files in array of file paths 'fhx -Path `$inputFile1, `$inputFile2'"
PathCase = $true
Path = @($inputFile1, $inputFile2)
Count = 2
ExpectedResult = $inputText1
ExpectedSecondResult = $inputText2
}
@{
Name = "Can process file content from all files when resolved to multiple paths 'fhx -Path '`$testDirectory\SourceFile-*''"
PathCase = $true
Path = "$testDirectory\SourceFile-*"
Count = 2
ExpectedResult = $inputText1
ExpectedSecondResult = $inputText2
}
)
foreach ($testCase in $testCases)
{
RunPathAndLiteralPathParameterTestCase $testCase
}
}
Context "LiteralPath Paramater" {
$testCases = @(
@{
Name = "Can process file content from given file path 'fhx -LiteralPath `$inputFile3'"
Path = $inputFile3
Count = 1
ExpectedResult = $inputText3
}
@{
Name = "Can process file content from all files in array of file paths 'fhx -LiteralPath `$inputFile1, `$inputFile3'"
Path = @($inputFile1, $inputFile3)
Count = 2
ExpectedResult = $inputText1
ExpectedSecondResult = $inputText3
}
)
foreach ($testCase in $testCases)
{
RunPathAndLiteralPathParameterTestCase $testCase
}
}
Context "Encoding Parameter" {
$testCases = @(
@{
Name = "Can process ASCII encoding 'fhx -InputObject 'hello' -Encoding ASCII'"
Encoding = "ASCII"
Count = 1
ExpectedResult = "00000000 68 65 6C 6C 6F hello"
}
@{
Name = "Can process BigEndianUnicode encoding 'fhx -InputObject 'hello' -Encoding BigEndianUnicode'"
Encoding = "BigEndianUnicode"
Count = 1
ExpectedResult = "00000000 00 68 00 65 00 6C 00 6C 00 6F .h.e.l.l.o"
}
@{
Name = "Can process Unicode encoding 'fhx -InputObject 'hello' -Encoding Unicode'"
Encoding = "Unicode"
Count = 1
ExpectedResult = "00000000 68 00 65 00 6C 00 6C 00 6F 00 h.e.l.l.o."
}
@{
Name = "Can process UTF7 encoding 'fhx -InputObject 'hello' -Encoding UTF7'"
Encoding = "UTF7"
Count = 1
ExpectedResult = "00000000 68 65 6C 6C 6F hello"
}
@{
Name = "Can process UTF8 encoding 'fhx -InputObject 'hello' -Encoding UTF8'"
Encoding = "UTF8"
Count = 1
ExpectedResult = "00000000 68 65 6C 6C 6F hello"
}
@{
Name = "Can process UTF32 encoding 'fhx -InputObject 'hello' -Encoding UTF32'"
Encoding = "UTF32"
Count = 1
ExpectedResult = "00000000 68 00 00 00 65 00 00 00 6C 00 00 00 6C 00 00 00 h...e...l...l...`r`n00000010 6F 00 00 00 o..."
}
)
foreach ($testCase in $testCases)
{
RunEncodingTestCase $testCase
}
}
Context "Validate Error Scenarios" {
$testDirectory = $inputFile1.DirectoryName
$testCases = @(
@{
Name = "Does not support non-FileSystem Provider paths 'fhx -Path 'Cert:\CurrentUser\My\`$thumbprint' -ErrorAction Stop'"
PathParameterErrorCase = $true
Path = "Cert:\CurrentUser\My\$thumbprint"
ExpectedFullyQualifiedErrorId = "FormatHexOnlySupportsFileSystemPaths,Microsoft.PowerShell.Commands.FormatHex"
}
@{
Name = "Type Not Supported 'fhx -InputObject @{'hash' = 'table'} -ErrorAction Stop'"
InputObjectErrorCase = $true
Path = $inputFile1
InputObject = @{ "hash" = "table" }
ExpectedFullyQualifiedErrorId = "FormatHexTypeNotSupported,Microsoft.PowerShell.Commands.FormatHex"
}
)
foreach ($testCase in $testCases)
{
RunExpectedErrorTestCase $testCase
}
}
Context "Continues to Process Valid Paths" {
$testCases = @(
@{
Name = "If given invalid path in array, continues to process valid paths 'fhx -Path `$invalidPath, `$inputFile1 -ev e -ErrorAction SilentlyContinue'"
PathCase = $true
InvalidPath = "$($inputFile1.DirectoryName)\fakefile8888845345345348709.txt"
ExpectedFullyQualifiedErrorId = "FileNotFound,Microsoft.PowerShell.Commands.FormatHex"
}
@{
Name = "If given a non FileSystem path in array, continues to process valid paths 'fhx -Path `$invalidPath, `$inputFile1 -ev e -ErrorAction SilentlyContinue'"
PathCase = $true
InvalidPath = "Cert:\CurrentUser\My\$thumbprint"
ExpectedFullyQualifiedErrorId = "FormatHexOnlySupportsFileSystemPaths,Microsoft.PowerShell.Commands.FormatHex"
}
@{
Name = "If given a non FileSystem path in array (with LiteralPath), continues to process valid paths 'fhx -Path `$invalidPath, `$inputFile1 -ev e -ErrorAction SilentlyContinue'"
InvalidPath = "Cert:\CurrentUser\My\$thumbprint"
ExpectedFullyQualifiedErrorId = "FormatHexOnlySupportsFileSystemPaths,Microsoft.PowerShell.Commands.FormatHex"
}
)
foreach ($testCase in $testCases)
{
RunContinuesToProcessCase $testCase
}
}
Context "Cmdlet Functionality" {
It "Path is default Parameter Set 'fhx `$inputFile1'" {
$result = Format-Hex $inputFile1
$result | Should Not BeNullOrEmpty
,$result | Should BeOfType 'Microsoft.PowerShell.Commands.ByteCollection'
$actualResult = $result.ToString()
$actualResult | Should Match $inputText1
}
It "Validate file input from Pipeline 'Get-ChildItem `$inputFile1 | Format-Hex'" {
$result = Get-ChildItem $inputFile1 | Format-Hex
$result | Should Not BeNullOrEmpty
,$result | Should BeOfType 'Microsoft.PowerShell.Commands.ByteCollection'
$actualResult = $result.ToString()
$actualResult | Should Match $inputText1
}
It "Validate that streamed text does not have buffer underrun problems ''a' * 30 | Format-Hex'" {
$result = "a" * 30 | Format-Hex
$result | Should Not BeNullOrEmpty
,$result | Should BeOfType 'Microsoft.PowerShell.Commands.ByteCollection'
$result.ToString() | Should be "00000000 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 aaaaaaaaaaaaaaaa`r`n00000010 61 61 61 61 61 61 61 61 61 61 61 61 61 61 aaaaaaaaaaaaaa "
}
It "Validate that files do not have buffer underrun problems 'Format-Hex -path `$InputFile2'" {
$result = Format-Hex -path $InputFile2
$result | Should Not BeNullOrEmpty
$result.Count | should be 2
$result[0].ToString() | Should be "00000000 54 68 69 73 20 69 73 20 61 20 62 69 74 20 6D 6F This is a bit mo"
$result[1].ToString() | Should be "00000010 72 65 20 74 65 78 74 re text "
}
}
}