Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Get-SecureRandom.Tests.ps1190 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-SecureRandom DRT Unit Tests" -Tags "CI" {4    $testData = @(5        @{ Name = 'no params'; Maximum = $null; Minimum = $null; GreaterThan = -1; LessThan = ([int32]::MaxValue); Type = 'System.Int32' }6        @{ Name = 'only positive maximum number'; Maximum = 100; Minimum = $null; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' }7        @{ Name = 'maximum set to 0, Minimum to a negative number'; Maximum = 0; Minimum = -100; GreaterThan = -101; LessThan = 0; Type = 'System.Int32' }8        @{ Name = 'positive maximum, negative Minimum'; Maximum = 100; Minimum = -100; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' }9        @{ Name = 'both negative'; Maximum = -100; Minimum = -200; GreaterThan = -201; LessThan = -100; Type = 'System.Int32' }10        @{ Name = 'both negative with parentheses'; Maximum = (-100); Minimum = (-200); GreaterThan = -201; LessThan = -100; Type = 'System.Int32' }11        @{ Name = 'maximum enclosed in quote'; Maximum = '8'; Minimum = 5; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' }12        @{ Name = 'minimum enclosed in quote'; Maximum = 8; Minimum = '5'; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' }13        @{ Name = 'maximum with plus sign'; Maximum = +100; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' }14        @{ Name = 'maximum with plus sign and quote'; Maximum = '+100'; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' }15        @{ Name = 'both with quote'; Maximum = '+100'; Minimum = '-100'; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' }16        @{ Name = 'maximum set to kb'; Maximum = '1kb'; Minimum = 0; GreaterThan = -1; LessThan = 1024; Type = 'System.Int32' }17        @{ Name = 'maximum is Int64.MaxValue'; Maximum = ([int64]::MaxValue); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]::MaxValue); Type = 'System.Int64' }18        @{ Name = 'maximum is a 64-bit integer'; Maximum = ([int64]100); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' }19        @{ Name = 'maximum set to a large integer greater than int32.MaxValue'; Maximum = 100000000000; Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100000000000); Type = 'System.Int64' }20        @{ Name = 'maximum set to 0, Minimum set to a negative 64-bit integer'; Maximum = ([int64]0); Minimum = ([int64]-100); GreaterThan = ([int64]-101); LessThan = ([int64]0); Type = 'System.Int64' }21        @{ Name = 'maximum set to positive 64-bit number, min set to negative 64-bit number'; Maximum = ([int64]100); Minimum = ([int64]-100); GreaterThan = ([int64]-101); LessThan = ([int64]100); Type = 'System.Int64' }22        @{ Name = 'both are negative 64-bit number'; Maximum = ([int64]-100); Minimum = ([int64]-200); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' }23        @{ Name = 'both are negative 64-bit number with parentheses'; Maximum = ([int64](-100)); Minimum = ([int64](-200)); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' }24        @{ Name = 'max is 32-bit, min is 64-bit integer'; Maximum = '8'; Minimum = ([int64]5); GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' }25        @{ Name = 'max is 64-bit, min is 32-bit integer'; Maximum = ([int64]8); Minimum = '5'; GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' }26        @{ Name = 'max set to a 32-bit integer, min set to [int64]0'; Maximum = +100; Minimum = ([int64]0); GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' }27        @{ Name = 'max set to a 32-bit integer with quote'; Maximum = '+100'; Minimum = ([int64]0); GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' }28        @{ Name = 'max is [int64]0, min is a 32-bit integer'; Maximum = ([int64]0); Minimum = '-100'; GreaterThan = ([int64]-101); LessThan = ([int64]0); Type = 'System.Int64' }29        @{ Name = 'min set to 1MB, max set to a 64-bit integer greater than min'; Maximum = ([int64]1048585); Minimum = '1mb'; GreaterThan = ([int64]1048575); LessThan = ([int64]1048585); Type = 'System.Int64' }30        @{ Name = 'max set to 1tb, min set to 10 mb'; Maximum = '1tb'; Minimum = '10mb'; GreaterThan = ([int64]10485759); LessThan = ([int64]1099511627776); Type = 'System.Int64' }31        @{ Name = 'max is int64.MaxValue, min is Int64.MinValue'; Maximum = ([int64]::MaxValue); Minimum = ([int64]::MinValue); GreaterThan = ([int64]::MinValue); LessThan = ([int64]::MaxValue); Type = 'System.Int64' }32        @{ Name = 'both are int64.MaxValue plus a 32-bit integer'; Maximum = ([int64](([int]::MaxValue)+15)); Minimum = ([int64](([int]::MaxValue)+10)); GreaterThan = ([int64](([int]::MaxValue)+9)); LessThan = ([int64](([int]::MaxValue)+15)); Type = 'System.Int64' }33        @{ Name = 'both are greater than int32.MaxValue without specified type, and max with quote'; Maximum = '100099000001'; Minimum = 100000000001; GreaterThan = ([int64]10000000000); LessThan = ([int64]100099000001); Type = 'System.Int64' }34        @{ Name = 'both are greater than int32.MaxValue without specified type, and min with quote'; Maximum = 100000002230; Minimum = '100000002222'; GreaterThan = ([int64]100000002221); LessThan = ([int64]100000002230); Type = 'System.Int64' }35        @{ Name = 'max is greater than int32.MaxValue without specified type'; Maximum = 90000000000; Minimum = 4; GreaterThan = ([int64]3); LessThan = ([int64]90000000000); Type = 'System.Int64' }36        @{ Name = 'max is a double-precision number'; Maximum = 100.0; Minimum = $null; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' }37        @{ Name = 'both are double-precision numbers, min is negative.'; Maximum = 0.0; Minimum = -100.0; GreaterThan = -101.0; LessThan = 0.0; Type = 'System.Double' }38        @{ Name = 'both are double-precision number, max is positive, min is negative.'; Maximum = 100.0; Minimum = -100.0; GreaterThan = -101.0; LessThan = 100.0; Type = 'System.Double' }39        @{ Name = 'max is a double-precision number, min is int32'; Maximum = 8.0; Minimum = 5; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' }40        @{ Name = 'min is a double-precision number, max is int32'; Maximum = 8; Minimum = 5.0; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' }41        @{ Name = 'max set to a special double number'; Maximum = 20.; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' }42        @{ Name = 'max is double with quote'; Maximum = '20.'; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' }43        @{ Name = 'max is double with plus sign'; Maximum = +100.0; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' }44        @{ Name = 'max is double with plus sign and enclosed in quote'; Maximum = '+100.0'; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' }45        @{ Name = 'both set to the special numbers as 1.0e+xx '; Maximum = $null; Minimum = 1.0e+100; GreaterThan = 1.0e+99; LessThan = ([double]::MaxValue); Type = 'System.Double' }46        @{ Name = 'max is Double.MaxValue, min is Double.MinValue'; Maximum = ([double]::MaxValue); Minimum = ([double]::MinValue); GreaterThan = ([double]::MinValue); LessThan = ([double]::MaxValue); Type = 'System.Double' }47    )48 49    $testDataForError = @(50        @{ Name = 'Min is greater than max and all are positive 32-bit integer'; Maximum = 10; Minimum = 20}51        @{ Name = 'Min and Max are same and all are positive 32-bit integer'; Maximum = 20; Minimum = 20}52        @{ Name = 'Min is greater than max and all are negative 32-bit integer'; Maximum = -20; Minimum = -10}53        @{ Name = 'Min and Max are same and all are negative 32-bit integer'; Maximum = -20; Minimum = -20}54        @{ Name = 'Min is greater than max and all are positive double-precision number'; Maximum = 10.0; Minimum = 20.0}55        @{ Name = 'Min and Max are same and all are positive double-precision number'; Maximum = 20.0; Minimum = 20.0}56        @{ Name = 'Min is greater than max and all are negative double-precision number'; Maximum = -20.0; Minimum = -10.0}57        @{ Name = 'Min and Max are same and all are negative double-precision number'; Maximum = -20.0; Minimum = -20.0}58        @{ Name = 'Max is a negative number, min is the default number '; Maximum = -10; Minimum = $null}59    )60 61    # minimum is always set to the actual low end of the range, details refer to closed issue #887.62    It "Should return a correct random number for '<Name>'" -TestCases $testData {63        param($maximum, $minimum, $greaterThan, $lessThan, $type)64 65        $result = Get-SecureRandom -Maximum $maximum -Minimum $minimum66        $result | Should -BeGreaterThan $greaterThan67        $result | Should -BeLessThan $lessThan68        $result | Should -BeOfType $type69    }70 71    It "Should return correct random numbers for '<Name>' with Count specified" -TestCases $testData {72        param($maximum, $minimum, $greaterThan, $lessThan, $type)73 74        $result = Get-SecureRandom -Maximum $maximum -Minimum $minimum -Count 175        $result | Should -BeGreaterThan $greaterThan76        $result | Should -BeLessThan $lessThan77        $result | Should -BeOfType $type78 79        $result = Get-SecureRandom -Maximum $maximum -Minimum $minimum -Count 380        foreach ($randomNumber in $result) {81            $randomNumber | Should -BeGreaterThan $greaterThan82            $randomNumber | Should -BeLessThan $lessThan83            $randomNumber | Should -BeOfType $type84        }85    }86 87    It "Should be able to throw error when '<Name>'" -TestCases $testDataForError {88        param($maximum, $minimum)89        { Get-SecureRandom -Minimum $minimum -Maximum $maximum } | Should -Throw -ErrorId "MinGreaterThanOrEqualMax,Microsoft.PowerShell.Commands.GetSecureRandomCommand"90    }91}92 93Describe "Get-SecureRandom" -Tags "CI" {94    It "Should return a random number greater than -1" {95        Get-SecureRandom | Should -BeGreaterThan -196    }97 98    It "Should return a random number less than 100" {99        Get-SecureRandom -Maximum 100 | Should -BeLessThan 100100        Get-SecureRandom -Maximum 100 | Should -BeGreaterThan -1101    }102 103    It "Should return a random number less than 100 and greater than -100 " {104        $randomNumber = Get-SecureRandom -Minimum -100 -Maximum 100105        $randomNumber | Should -BeLessThan 100106        $randomNumber | Should -BeGreaterThan -101107    }108 109    It "Should return a random number less than 20.93 and greater than 10.7 " {110        $randomNumber = Get-SecureRandom -Minimum 10.7 -Maximum 20.93111        $randomNumber | Should -BeLessThan 20.93112        $randomNumber | Should -BeGreaterThan 10.7113    }114 115    It "Should return a number from 1,2,3,5,8,13 " {116        $randomNumber = Get-SecureRandom -InputObject 1, 2, 3, 5, 8, 13117        $randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13118    }119 120    It "Should return an array " {121        $randomNumber = Get-SecureRandom -InputObject 1, 2, 3, 5, 8, 13 -Count 3122        $randomNumber.Count | Should -Be 3123        ,$randomNumber | Should -BeOfType System.Array124    }125 126    It "Should return three random numbers for array of 1,2,3,5,8,13 " {127        $randomNumber = Get-SecureRandom -InputObject 1, 2, 3, 5, 8, 13 -Count 3128        $randomNumber.Count | Should -Be 3129        $randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13130        $randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13131        $randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13132        $randomNumber[3] | Should -BeNullOrEmpty133    }134 135    It "Should return all the numbers for array of 1,2,3,5,8,13 in no particular order" {136        $randomNumber = Get-SecureRandom -InputObject 1, 2, 3, 5, 8, 13 -Count ([int]::MaxValue)137        $randomNumber.Count | Should -Be 6138        $randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13139        $randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13140        $randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13141        $randomNumber[3] | Should -BeIn 1, 2, 3, 5, 8, 13142        $randomNumber[4] | Should -BeIn 1, 2, 3, 5, 8, 13143        $randomNumber[5] | Should -BeIn 1, 2, 3, 5, 8, 13144        $randomNumber[6] | Should -BeNullOrEmpty145    }146 147    It "Should return all the numbers for array of 1,2,3,5,8,13 in randomized order when the Shuffle switch is used" {148        $randomNumber = Get-SecureRandom -InputObject 1, 2, 3, 5, 8, 13 -Shuffle149        $randomNumber.Count | Should -Be 6150        $randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13151    }152 153    It "Should return a single random item when -Shuffle:`$false is used" {154        $randomNumber = Get-SecureRandom -InputObject 1, 2, 3, 5, 8, 13 -Shuffle:$false155        $randomNumber.Count | Should -Be 1156        $randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13157    }158 159    It "Should return for a string collection " {160        $randomNumber = Get-SecureRandom -InputObject "red", "yellow", "blue"161        $randomNumber | Should -Be ("red" -or "yellow" -or "blue")162    }163 164    It "Should return a number for hexadecimal " {165        $randomNumber = Get-SecureRandom 0x07FFFFFFFFF166        $randomNumber | Should -BeLessThan 549755813887167        $randomNumber | Should -BeGreaterThan 0168    }169 170    It "Should throw an error because the hexadecimal number is to large " {171        { Get-SecureRandom 0x07FFFFFFFFFFFFFFFF } | Should -Throw "Value was either too large or too small for a UInt32"172    }173 174    It "Should accept collection containing empty string for -InputObject" {175        1..10 | ForEach-Object {176            Get-SecureRandom -InputObject @('a','b','') | Should -BeIn 'a','b',''177        }178    }179 180    It "Should accept `$null in collection for -InputObject" {181        1..10 | ForEach-Object {182            Get-SecureRandom -InputObject @('a','b',$null) | Should -BeIn 'a','b',$null183        }184    }185 186    It 'Should not have a -SetSeed parameter' {187        (Get-Command Get-SecureRandom).Parameters['SetSeed'] | Should -BeNullOrEmpty188    }189}190