From e12fd070747ef9551bee73d3022f393df8b8ba9a Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Fri, 13 Sep 2024 13:23:30 +0100 Subject: [PATCH] PS: Add more tests. --- .../controlflow/graph/Cfg.expected | 848 +++++++++++++++++- .../controlflow/graph/conditionals.ps1 | 129 +++ .../library-tests/controlflow/graph/loops.ps1 | 17 +- .../library-tests/controlflow/graph/try.ps1 | 194 ++++ 4 files changed, 1180 insertions(+), 8 deletions(-) create mode 100644 powershell/ql/test/library-tests/controlflow/graph/conditionals.ps1 create mode 100644 powershell/ql/test/library-tests/controlflow/graph/try.ps1 diff --git a/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected b/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected index 08e3368df90..73a78318b12 100644 --- a/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected +++ b/powershell/ql/test/library-tests/controlflow/graph/Cfg.expected @@ -1,3 +1,361 @@ +conditionals.ps1: +# 1| test-if +#-----| -> test-if-else + +# 1| conditionals.ps1 +#-----| -> {...} + +# 1| enter conditionals.ps1 +#-----| -> conditionals.ps1 + +# 1| exit conditionals.ps1 + +# 1| exit conditionals.ps1 (normal) +#-----| -> exit conditionals.ps1 + +# 1| {...} +#-----| -> test-if + +# 1| enter {...} +#-----| -> {...} + +# 1| {...} +#-----| -> param(...) + +# 2| param(...) +#-----| -> {...} + +# 2| {...} +#-----| -> if (...) {...} + +# 4| if (...) {...} +#-----| -> myBool + +# 4| myBool +#-----| -> myBool + +# 4| myBool +#-----| true -> {...} + +# 4| myBool +#-----| -> myBool + +# 5| {...} + +# 11| test-if-else +#-----| -> test-if-conj + +# 11| enter {...} +#-----| -> {...} + +# 11| {...} +#-----| -> param(...) + +# 12| param(...) +#-----| -> {...} + +# 12| {...} +#-----| -> if (...) {...} else {...} + +# 14| if (...) {...} else {...} +#-----| -> myBool + +# 14| myBool +#-----| -> myBool + +# 14| myBool +#-----| true -> {...} +#-----| false -> {...} + +# 14| myBool +#-----| -> myBool + +# 15| {...} + +# 19| {...} + +# 24| test-if-conj +#-----| -> test-if-else-conj + +# 24| enter {...} +#-----| -> {...} + +# 24| {...} +#-----| -> param(...) + +# 25| param(...) +#-----| -> {...} + +# 25| {...} +#-----| -> if (...) {...} + +# 27| if (...) {...} +#-----| -> ... -and ... + +# 27| myBool1 +#-----| false, true -> myBool2 + +# 27| ... -and ... +#-----| -> myBool1 + +# 27| ... -and ... +#-----| true -> {...} + +# 27| ... -and ... +#-----| -> ... -and ... + +# 27| myBool2 +#-----| false, true -> ... -and ... + +# 28| {...} + +# 34| test-if-else-conj +#-----| -> test-if-disj + +# 34| enter {...} +#-----| -> {...} + +# 34| {...} +#-----| -> param(...) + +# 35| param(...) +#-----| -> {...} + +# 35| {...} +#-----| -> if (...) {...} else {...} + +# 37| if (...) {...} else {...} +#-----| -> ... -and ... + +# 37| myBool1 +#-----| false, true -> myBool2 + +# 37| ... -and ... +#-----| -> myBool1 + +# 37| ... -and ... +#-----| true -> {...} +#-----| false -> {...} + +# 37| ... -and ... +#-----| -> ... -and ... + +# 37| myBool2 +#-----| false, true -> ... -and ... + +# 38| {...} + +# 42| {...} + +# 47| test-if-disj +#-----| -> test-if-else-disj + +# 47| enter {...} +#-----| -> {...} + +# 47| {...} +#-----| -> param(...) + +# 48| param(...) +#-----| -> {...} + +# 48| {...} +#-----| -> if (...) {...} + +# 50| if (...) {...} +#-----| -> ... -or ... + +# 50| myBool1 +#-----| false, true -> myBool2 + +# 50| ... -or ... +#-----| -> myBool1 + +# 50| ... -or ... +#-----| true -> {...} + +# 50| ... -or ... +#-----| -> ... -or ... + +# 50| myBool2 +#-----| false, true -> ... -or ... + +# 51| {...} + +# 57| test-if-else-disj +#-----| -> test-else-if + +# 57| enter {...} +#-----| -> {...} + +# 57| {...} +#-----| -> param(...) + +# 58| param(...) +#-----| -> {...} + +# 58| {...} +#-----| -> if (...) {...} else {...} + +# 60| if (...) {...} else {...} +#-----| -> ... -or ... + +# 60| myBool1 +#-----| false, true -> myBool2 + +# 60| ... -or ... +#-----| -> myBool1 + +# 60| ... -or ... +#-----| true -> {...} +#-----| false -> {...} + +# 60| ... -or ... +#-----| -> ... -or ... + +# 60| myBool2 +#-----| false, true -> ... -or ... + +# 61| {...} + +# 65| {...} + +# 70| test-else-if +#-----| -> test-else-if-else + +# 70| enter {...} +#-----| -> {...} + +# 70| {...} +#-----| -> param(...) + +# 71| param(...) +#-----| -> {...} + +# 71| {...} +#-----| -> if (...) {...} + +# 73| if (...) {...} +#-----| -> myBool1 + +# 73| myBool1 +#-----| -> myBool1 + +# 73| myBool1 +#-----| true -> {...} + +# 73| myBool1 +#-----| -> myBool1 + +# 74| {...} + +# 84| test-else-if-else +#-----| -> test-switch + +# 84| enter {...} +#-----| -> {...} + +# 84| {...} +#-----| -> param(...) + +# 85| param(...) +#-----| -> {...} + +# 85| {...} +#-----| -> if (...) {...} else {...} + +# 87| if (...) {...} else {...} +#-----| -> myBool1 + +# 87| myBool1 +#-----| -> myBool1 + +# 87| myBool1 +#-----| true -> {...} +#-----| false -> {...} + +# 87| myBool1 +#-----| -> myBool1 + +# 88| {...} + +# 96| {...} + +# 101| test-switch +#-----| -> test-switch-default + +# 101| enter {...} +#-----| -> {...} + +# 101| {...} +#-----| -> {...} + +# 102| switch(...) {...} +#-----| -> n + +# 102| {...} +#-----| -> switch(...) {...} + +# 102| n +#-----| -> n + +# 102| n + +# 102| n +#-----| -> n + +# 110| test-switch-default +#-----| -> test-switch-assign + +# 110| enter {...} +#-----| -> {...} + +# 110| {...} +#-----| -> {...} + +# 111| switch(...) {...} +#-----| -> n + +# 111| {...} +#-----| -> switch(...) {...} + +# 111| n +#-----| -> n + +# 111| n + +# 111| n +#-----| -> n + +# 123| test-switch-assign +#-----| -> exit conditionals.ps1 (normal) + +# 123| enter {...} +#-----| -> {...} + +# 123| {...} +#-----| -> {...} + +# 124| a +#-----| -> switch(...) {...} + +# 124| ...=... +#-----| -> a + +# 124| {...} +#-----| -> ...=... + +# 124| switch(...) {...} +#-----| -> n + +# 124| n +#-----| -> n + +# 124| n + +# 124| n +#-----| -> n + functions.ps1: # 1| Add-Numbers-Arguments #-----| -> foo @@ -311,6 +669,9 @@ loops.ps1: # 1| Test-While #-----| -> Test-Break +# 1| {...} +#-----| -> Test-While + # 1| enter loops.ps1 #-----| -> loops.ps1 @@ -322,9 +683,6 @@ loops.ps1: # 1| loops.ps1 #-----| -> {...} -# 1| {...} -#-----| -> Test-While - # 1| enter {...} #-----| -> {...} @@ -569,7 +927,7 @@ loops.ps1: #-----| -> ... -le ... # 33| Test-DoUntil -#-----| -> Tet-For +#-----| -> Test-For # 33| enter {...} #-----| -> {...} @@ -637,7 +995,7 @@ loops.ps1: # 38| 10 #-----| -> ... -ge ... -# 41| Tet-For +# 41| Test-For #-----| -> Test-ForEach # 41| enter {...} @@ -707,6 +1065,7 @@ loops.ps1: #-----| -> 1 # 44| ...+... +#-----| -> ... -le ... # 44| ...+... #-----| -> i @@ -736,7 +1095,7 @@ loops.ps1: #-----| -> ...+... # 49| Test-ForEach -#-----| -> exit loops.ps1 (normal) +#-----| -> Test-For-Ever # 49| enter {...} #-----| -> {...} @@ -754,3 +1113,480 @@ loops.ps1: #-----| -> ...=... # 50| ...,... + +# 58| Test-For-Ever +#-----| -> exit loops.ps1 (normal) + +# 58| enter {...} +#-----| -> {...} + +# 58| {...} +#-----| -> {...} + +# 59| a +#-----| -> 0 + +# 59| ...=... +#-----| -> a + +# 59| {...} +#-----| -> ...=... + +# 59| 0 +#-----| -> for(...;...;...) + +# 59| 0 +#-----| -> 0 + +# 61| for(...;...;...) + +try.ps1: +# 1| test-try-catch +#-----| -> test-try-with-throw-catch + +# 1| enter try.ps1 +#-----| -> try.ps1 + +# 1| exit try.ps1 + +# 1| exit try.ps1 (normal) +#-----| -> exit try.ps1 + +# 1| try.ps1 +#-----| -> {...} + +# 1| {...} +#-----| -> test-try-catch + +# 1| enter {...} +#-----| -> {...} + +# 1| {...} +#-----| -> {...} + +# 2| try {...} +#-----| -> {...} + +# 2| {...} +#-----| -> try {...} + +# 2| {...} +#-----| -> Write-Output + +# 3| Write-Output + +# 10| test-try-with-throw-catch +#-----| -> test-try-with-throw-catch-with-throw + +# 10| enter {...} +#-----| -> {...} + +# 10| {...} +#-----| -> {...} + +# 11| try {...} +#-----| -> {...} + +# 11| {...} +#-----| -> try {...} + +# 11| {...} +#-----| -> if (...) {...} + +# 12| if (...) {...} +#-----| -> b + +# 12| b +#-----| -> b + +# 12| b +#-----| true -> {...} + +# 12| b +#-----| -> b + +# 12| {...} +#-----| -> throw ... + +# 13| throw ... +#-----| -> 42 + +# 13| 42 +#-----| -> 42 + +# 13| 42 + +# 13| 42 +#-----| -> 42 + +# 21| test-try-with-throw-catch-with-throw +#-----| -> test-try-with-throw-catch-with-rethrow + +# 21| enter {...} +#-----| -> {...} + +# 21| {...} +#-----| -> {...} + +# 22| try {...} +#-----| -> {...} + +# 22| {...} +#-----| -> try {...} + +# 22| {...} +#-----| -> if (...) {...} + +# 23| if (...) {...} +#-----| -> b + +# 23| b +#-----| -> b + +# 23| b +#-----| true -> {...} + +# 23| b +#-----| -> b + +# 23| {...} +#-----| -> throw ... + +# 24| throw ... +#-----| -> 42 + +# 24| 42 +#-----| -> 42 + +# 24| 42 + +# 24| 42 +#-----| -> 42 + +# 32| test-try-with-throw-catch-with-rethrow +#-----| -> test-try-catch-specific-1 + +# 32| enter {...} +#-----| -> {...} + +# 32| {...} +#-----| -> {...} + +# 33| try {...} +#-----| -> {...} + +# 33| {...} +#-----| -> try {...} + +# 33| {...} +#-----| -> if (...) {...} + +# 34| if (...) {...} +#-----| -> b + +# 34| b +#-----| -> b + +# 34| b +#-----| true -> {...} + +# 34| b +#-----| -> b + +# 34| {...} +#-----| -> throw ... + +# 35| throw ... +#-----| -> 42 + +# 35| 42 +#-----| -> 42 + +# 35| 42 + +# 35| 42 +#-----| -> 42 + +# 43| test-try-catch-specific-1 +#-----| -> test-try-catch-specific-1 + +# 43| enter {...} +#-----| -> {...} + +# 43| {...} +#-----| -> {...} + +# 44| try {...} +#-----| -> {...} + +# 44| {...} +#-----| -> try {...} + +# 44| {...} +#-----| -> Write-Output + +# 45| Write-Output + +# 52| test-try-catch-specific-1 +#-----| -> test-try-two-catch-specific-1 + +# 52| enter {...} +#-----| -> {...} + +# 52| {...} +#-----| -> {...} + +# 53| try {...} +#-----| -> {...} + +# 53| {...} +#-----| -> try {...} + +# 53| {...} +#-----| -> Write-Output + +# 54| Write-Output + +# 61| test-try-two-catch-specific-1 +#-----| -> test-try-catch-specific-2 + +# 61| enter {...} +#-----| -> {...} + +# 61| {...} +#-----| -> {...} + +# 62| try {...} +#-----| -> {...} + +# 62| {...} +#-----| -> try {...} + +# 62| {...} +#-----| -> Write-Output + +# 63| Write-Output + +# 72| test-try-catch-specific-2 +#-----| -> test-try-two-catch-specific-2 + +# 72| enter {...} +#-----| -> {...} + +# 72| {...} +#-----| -> {...} + +# 73| try {...} +#-----| -> {...} + +# 73| {...} +#-----| -> try {...} + +# 73| {...} +#-----| -> Write-Output + +# 74| Write-Output + +# 81| test-try-two-catch-specific-2 +#-----| -> test-try-three-catch-specific-2 + +# 81| enter {...} +#-----| -> {...} + +# 81| {...} +#-----| -> {...} + +# 82| try {...} +#-----| -> {...} + +# 82| {...} +#-----| -> try {...} + +# 82| {...} +#-----| -> Write-Output + +# 83| Write-Output + +# 92| test-try-three-catch-specific-2 +#-----| -> test-try-catch-finally + +# 92| enter {...} +#-----| -> {...} + +# 92| {...} +#-----| -> {...} + +# 93| try {...} +#-----| -> {...} + +# 93| {...} +#-----| -> try {...} + +# 93| {...} +#-----| -> Write-Output + +# 94| Write-Output + +# 105| test-try-catch-finally +#-----| -> test-try-finally + +# 105| enter {...} +#-----| -> {...} + +# 105| {...} +#-----| -> {...} + +# 106| try {...} +#-----| -> {...} + +# 106| {...} +#-----| -> try {...} + +# 106| {...} +#-----| -> Write-Output + +# 107| Write-Output + +# 116| test-try-finally +#-----| -> test-try-finally-catch-specific-1 + +# 116| enter {...} +#-----| -> {...} + +# 116| {...} +#-----| -> {...} + +# 117| try {...} +#-----| -> {...} + +# 117| {...} +#-----| -> try {...} + +# 117| {...} +#-----| -> Write-Output + +# 118| Write-Output + +# 125| test-try-finally-catch-specific-1 +#-----| -> test-nested-try-inner-finally + +# 125| enter {...} +#-----| -> {...} + +# 125| {...} +#-----| -> {...} + +# 126| try {...} +#-----| -> {...} + +# 126| {...} +#-----| -> try {...} + +# 126| {...} +#-----| -> Write-Output + +# 127| Write-Output + +# 136| test-nested-try-inner-finally +#-----| -> test-nested-try-inner-finally + +# 136| enter {...} +#-----| -> {...} + +# 136| {...} +#-----| -> {...} + +# 137| try {...} +#-----| -> {...} + +# 137| {...} +#-----| -> try {...} + +# 137| {...} +#-----| -> try {...} + +# 138| try {...} +#-----| -> {...} + +# 138| {...} +#-----| -> Write-Output + +# 139| Write-Output + +# 149| test-nested-try-inner-finally +#-----| -> test-nested-try-outer-finally + +# 149| enter {...} +#-----| -> {...} + +# 149| {...} +#-----| -> {...} + +# 150| try {...} +#-----| -> {...} + +# 150| {...} +#-----| -> try {...} + +# 150| {...} +#-----| -> try {...} + +# 151| try {...} +#-----| -> {...} + +# 151| {...} +#-----| -> Write-Output + +# 152| Write-Output + +# 164| test-nested-try-outer-finally +#-----| -> test-nested-try-inner-outer-finally + +# 164| enter {...} +#-----| -> {...} + +# 164| {...} +#-----| -> {...} + +# 165| try {...} +#-----| -> {...} + +# 165| {...} +#-----| -> try {...} + +# 165| {...} +#-----| -> try {...} + +# 166| try {...} +#-----| -> {...} + +# 166| {...} +#-----| -> Write-Output + +# 167| Write-Output + +# 179| test-nested-try-inner-outer-finally +#-----| -> exit try.ps1 (normal) + +# 179| enter {...} +#-----| -> {...} + +# 179| {...} +#-----| -> {...} + +# 180| try {...} +#-----| -> {...} + +# 180| {...} +#-----| -> try {...} + +# 180| {...} +#-----| -> try {...} + +# 181| try {...} +#-----| -> {...} + +# 181| {...} +#-----| -> Write-Output + +# 182| Write-Output diff --git a/powershell/ql/test/library-tests/controlflow/graph/conditionals.ps1 b/powershell/ql/test/library-tests/controlflow/graph/conditionals.ps1 new file mode 100644 index 00000000000..8b143736490 --- /dev/null +++ b/powershell/ql/test/library-tests/controlflow/graph/conditionals.ps1 @@ -0,0 +1,129 @@ +function test-if { + param($myBool) + + if($myBool) + { + return 10; + } + return 11; +} + +function test-if-else { + param($myBool) + + if($myBool) + { + return 10; + } + else + { + return 11; + } +} + +function test-if-conj { + param($myBool1, $myBool2) + + if($myBool1 -and $myBool2) + { + return 10; + } + return 11; +} + +function test-if-else-conj { + param($myBool1, $myBool2) + + if($myBool1 -and $myBool2) + { + return 10; + } + else + { + return 11; + } +} + +function test-if-disj { + param($myBool1, $myBool2) + + if($myBool1 -or $myBool2) + { + return 10; + } + return 11; +} + +function test-if-else-disj { + param($myBool1, $myBool2) + + if($myBool1 -or $myBool2) + { + return 10; + } + else + { + return 11; + } +} + +function test-else-if { + param($myBool1, $myBool2) + + if($myBool1) + { + return 10; + } + elseif($myBoo2) + { + return 11; + } + return 12; +} + +function test-else-if-else { + param($myBool1, $myBool2) + + if($myBool1) + { + return 10; + } + elseif($myBoo2) + { + return 11; + } + else + { + return 12; + } +} + +function test-switch($n) { + switch($n) + { + 0: { return 0; } + 1: { return 1; } + 2: { return 2; } + } +} + +function test-switch-default($n) { + switch($n) + { + 0: { return 0; } + 1: { return 1; } + 2: { return 2; } + default: { + Write-Output "Error!" + return 3; + } + } +} + +function test-switch-assign($n) { + $a = switch($n) { + 0: { "0" } + 1: { "1" } + 2: { "2" } + } +} \ No newline at end of file diff --git a/powershell/ql/test/library-tests/controlflow/graph/loops.ps1 b/powershell/ql/test/library-tests/controlflow/graph/loops.ps1 index 6f0f5eded6e..a8d8ff969d6 100644 --- a/powershell/ql/test/library-tests/controlflow/graph/loops.ps1 +++ b/powershell/ql/test/library-tests/controlflow/graph/loops.ps1 @@ -38,7 +38,7 @@ function Test-DoUntil { } until ($a -ge 10) } -function Tet-For { +function Test-For { $a = 0 for ($i = 0; $i -le 10; $i = $i + 1) { @@ -53,4 +53,17 @@ function Test-ForEach { { $a = $a + 1 } -} \ No newline at end of file +} + +function Test-For-Ever { + $a = 0 + + for(;;) + { + if($a -le 10) + { + break; + } + } +} + diff --git a/powershell/ql/test/library-tests/controlflow/graph/try.ps1 b/powershell/ql/test/library-tests/controlflow/graph/try.ps1 new file mode 100644 index 00000000000..0dbf2ee8a82 --- /dev/null +++ b/powershell/ql/test/library-tests/controlflow/graph/try.ps1 @@ -0,0 +1,194 @@ +function test-try-catch { + try { + Write-Output "Hello!"; + } catch { + return 0; + } + return 1; +} + +function test-try-with-throw-catch($b) { + try { + if($b) { + throw 42; + } + } catch { + return 0; + } + return 1; +} + +function test-try-with-throw-catch-with-throw($b) { + try { + if($b) { + throw 42; + } + } catch { + throw ""; + } + return 1; +} + +function test-try-with-throw-catch-with-rethrow($b) { + try { + if($b) { + throw 42; + } + } catch { + throw; + } + return 1; +} + +function test-try-catch-specific-1 { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } + return 1; +} + +function test-try-catch-specific-1 { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } + return 1; +} + +function test-try-two-catch-specific-1 { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } catch { + return 1; + } + return 2; +} + +function test-try-catch-specific-2 { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException, SystemMmanagement.Automation.MethodInvocationeEception] { + return 0; + } + return 1; +} + +function test-try-two-catch-specific-2 { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException, SystemMmanagement.Automation.MethodInvocationeEception] { + return 0; + } catch [Exception] { + return 1; + } + return 2; +} + +function test-try-three-catch-specific-2 { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException, SystemMmanagement.Automation.MethodInvocationeEception] { + return 0; + } catch [Exception] { + return 1; + } catch { + return 2; + } + return 3; +} + +function test-try-catch-finally { + try { + Write-Output "Hello!"; + } catch { + return 0; + } finally { + Write-Output "Finally!"; + } + return 1; +} + +function test-try-finally { + try { + Write-Output "Hello!"; + } finally { + Write-Output "Finally!"; + } + return 1; +} + +function test-try-finally-catch-specific-1 { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } finally { + Write-Output "Finally!"; + } + return 1; +} + +function test-nested-try-inner-finally { + try { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } + } catch { + return 0; + } + return 1; +} + +function test-nested-try-inner-finally { + try { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } finally { + Write-Output "Finally!"; + } + } catch { + return 0; + } + return 1; +} + +function test-nested-try-outer-finally { + try { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } + } catch { + return 0; + } finally { + Write-Output "Finally!"; + } + return 1; +} + +function test-nested-try-inner-outer-finally { + try { + try { + Write-Output "Hello!"; + } catch [System.Net.WebException] { + return 0; + } finally { + Write-Output "Finally!"; + } + } catch { + return 0; + } finally { + Write-Output "Finally!"; + } + return 1; +} \ No newline at end of file