From f3469c03e964b67009ca31f7222a45e14026b891 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 14 Jun 2022 17:57:10 +0900 Subject: [PATCH 1/4] fix(cryptsetup): update the list of actions --- completions/cryptsetup | 12 ++++++++---- test/t/test_cryptsetup.py | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/completions/cryptsetup b/completions/cryptsetup index dbe2a2cd80c..84ac8b1776c 100644 --- a/completions/cryptsetup +++ b/completions/cryptsetup @@ -91,10 +91,14 @@ _comp_cmd_cryptsetup() _comp_compgen_help [[ ${COMPREPLY-} == *= ]] && compopt -o nospace else - _comp_compgen -- -W 'open close resize status benchmark repair - erase luksFormat luksAddKey luksRemoveKey luksChangeKey - luksKillSlot luksUUID isLuks luksDump tcryptDump luksSuspend - luksResume luksHeaderBackup luksHeaderRestore' + _comp_compgen -- -W 'benchmark bitlkClose bitlkDump bitlkOpen close + config convert create erase isLuks loopaesClose loopaesOpen + luksAddKey luksChangeKey luksClose luksConfig luksConvertKey + luksDump luksErase luksFormat luksHeaderBackup + luksHeaderRestore luksKillSlot luksOpen luksRemoveKey + luksResume luksSuspend luksUUID open plainClose plainOpen + reencrypt refresh remove repair resize status tcryptClose + tcryptDump tcryptOpen token' fi fi diff --git a/test/t/test_cryptsetup.py b/test/t/test_cryptsetup.py index fdc981b8dcb..293226c4696 100644 --- a/test/t/test_cryptsetup.py +++ b/test/t/test_cryptsetup.py @@ -9,3 +9,7 @@ def test_1(self, completion): @pytest.mark.complete("cryptsetup -", require_cmd=True) def test_2(self, completion): assert completion + + @pytest.mark.complete("cryptsetup luksE", require_cmd=True) + def test_github_issue758(self, completion): + assert completion == "rase" From 54c268df0cfb33af633d5812200264889cd20c98 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sat, 18 Jun 2022 08:36:34 +0900 Subject: [PATCH 2/4] fix(cryptsetup): parse --help and man for actions --- completions/cryptsetup | 54 +++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/completions/cryptsetup b/completions/cryptsetup index 84ac8b1776c..d39e2e41d29 100644 --- a/completions/cryptsetup +++ b/completions/cryptsetup @@ -10,6 +10,51 @@ _comp_cmd_cryptsetup__device() _comp_compgen -c "${cur:-/dev/}" filedir } +_comp_cmd_cryptsetup__action() +{ + local REPLY IFS=$' \t\n' + _comp_dequote "$1" || return 1 + local cmd=${REPLY:-cryptsetup} + local -a actions + _comp_split -l actions "$( + { + LC_ALL=C "$cmd" --help 2>&1 | + sed -n '/^ is one of:/,/^[^[:space:]]/s/^[[:space:]]\{1,\}\([^[:space:]]\{1,\}\).*/\1/p' + LC_ALL=C man cryptsetup 2>&1 | + awk '/^[[:space:]]+[[:alnum:]_]+([[:space:]]+(-[^[:space:].]+|<[^<>]+>|\[[^][]+\]|or))*$/ {print $1}' + } | sort -u + )" + + if ((${#actions[@]} == 0)); then + # The fallback action list is extracted from the following source: + # https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup.c#L3154-3208 (search for "Handle aliases") + # https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup.c#L2831-2867 (search for "struct action_type") + # https://gitlab.com/cryptsetup/cryptsetup/-/blob/main/src/cryptsetup_args.h#L28-53 (see the macros "*_ACTION") + actions=(benchmark bitlkClose bitlkDump bitlkOpen close config convert + create erase isLuks loopaesClose loopaesOpen luksAddKey + luksChangeKey luksClose luksConfig luksConvertKey luksDump + luksErase luksFormat luksHeaderBackup luksHeaderRestore + luksKillSlot luksOpen luksRemoveKey luksResume luksSuspend luksUUID + open plainClose plainOpen reencrypt refresh remove repair resize + status tcryptClose tcryptDump tcryptOpen token) + + # We attempt to filter the supported actions by the strings in the binary. + local path + if path=$(type -P -- "$cmd" 2>/dev/null || type -P -- cryptsetup 2>/dev/null); then + local filtering_pattern + printf -v filtering_pattern '%s\n' "${actions[@]}" + filtering_pattern=${filtering_pattern%$'\n'} + + local filtered_actions + filtered_actions=$(strings "$path" | grep -Fx "$filtering_pattern" | sort -u) && + [[ $filtered_actions ]] && + _comp_split -l actions "$filtered_actions" + fi + fi + + _comp_compgen -- -W '"${actions[@]}"' +} + _comp_cmd_cryptsetup() { local cur prev words cword was_split comp_args @@ -91,14 +136,7 @@ _comp_cmd_cryptsetup() _comp_compgen_help [[ ${COMPREPLY-} == *= ]] && compopt -o nospace else - _comp_compgen -- -W 'benchmark bitlkClose bitlkDump bitlkOpen close - config convert create erase isLuks loopaesClose loopaesOpen - luksAddKey luksChangeKey luksClose luksConfig luksConvertKey - luksDump luksErase luksFormat luksHeaderBackup - luksHeaderRestore luksKillSlot luksOpen luksRemoveKey - luksResume luksSuspend luksUUID open plainClose plainOpen - reencrypt refresh remove repair resize status tcryptClose - tcryptDump tcryptOpen token' + _comp_cmd_cryptsetup__action "$1" fi fi From 4cd697b65fdb4bec26c244f5949b3ac764f78f5b Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sat, 18 Jun 2022 09:46:57 +0900 Subject: [PATCH 3/4] test(cryptsetup): add a skip condition --- test/t/test_cryptsetup.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/t/test_cryptsetup.py b/test/t/test_cryptsetup.py index 293226c4696..51a42f19c85 100644 --- a/test/t/test_cryptsetup.py +++ b/test/t/test_cryptsetup.py @@ -10,6 +10,10 @@ def test_1(self, completion): def test_2(self, completion): assert completion - @pytest.mark.complete("cryptsetup luksE", require_cmd=True) + @pytest.mark.complete( + "cryptsetup luksE", + require_cmd=True, + skipif='! strings "$(type -P cryptsetup)" | grep -qFx luksErase', + ) def test_github_issue758(self, completion): assert completion == "rase" From eb1ac5c46232b70501ce89e76c94027e8a8eeb7b Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sat, 18 Jun 2022 10:19:39 +0900 Subject: [PATCH 4/4] test(cryptsetup): update the skip condition --- completions/cryptsetup | 4 ++-- test/t/test_cryptsetup.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/completions/cryptsetup b/completions/cryptsetup index d39e2e41d29..3ade169b900 100644 --- a/completions/cryptsetup +++ b/completions/cryptsetup @@ -13,7 +13,7 @@ _comp_cmd_cryptsetup__device() _comp_cmd_cryptsetup__action() { local REPLY IFS=$' \t\n' - _comp_dequote "$1" || return 1 + _comp_dequote "${1-}" || return 1 local cmd=${REPLY:-cryptsetup} local -a actions _comp_split -l actions "$( @@ -46,7 +46,7 @@ _comp_cmd_cryptsetup__action() filtering_pattern=${filtering_pattern%$'\n'} local filtered_actions - filtered_actions=$(strings "$path" | grep -Fx "$filtering_pattern" | sort -u) && + filtered_actions=$(strings "$path" | command grep -Fx "$filtering_pattern" | sort -u) && [[ $filtered_actions ]] && _comp_split -l actions "$filtered_actions" fi diff --git a/test/t/test_cryptsetup.py b/test/t/test_cryptsetup.py index 51a42f19c85..d3fa9fb055d 100644 --- a/test/t/test_cryptsetup.py +++ b/test/t/test_cryptsetup.py @@ -13,7 +13,7 @@ def test_2(self, completion): @pytest.mark.complete( "cryptsetup luksE", require_cmd=True, - skipif='! strings "$(type -P cryptsetup)" | grep -qFx luksErase', + skipif=r'! { cryptsetup --help; man cryptsetup; } 2>/dev/null | command grep -qE "(^|[[:space:]])luksErase([[:space:]]|\$)"', ) def test_github_issue758(self, completion): assert completion == "rase"