Skip to content

Commit f106121

Browse files
committed
Fix tests for rule grub2_pti_argument
Some test scenarios for rule `grub2_pti_argument` are failing. The reason is that `pti` is a substring of `options`. This is caused by recent change that make the equal sign `=` optional. This change has been introduced by #13726. After this change some regular expressions for the `pti` argument started to match `options`. We will prevent that by using word bounds marks (`\<` and `\>`) in regular expressions. Also, as a part of this commit we will fix coding style. Fixes: #13732
1 parent fc72965 commit f106121

9 files changed

+31
-31
lines changed

shared/templates/grub2_bootloader_argument/tests/arg_not_in_entries.fail.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ source common.sh
1515
# Removes argument from kernel command line in /boot/loader/entries/*.conf
1616

1717
for file in /boot/loader/entries/*.conf ; do
18-
if grep -q '^.*{{{ ESCAPED_ARG_NAME }}}=\?.*' "$file" ; then
19-
sed -i 's/\(^.*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*\)/\1 \2/' "$file"
18+
if grep -q '^.*\<{{{ ARG_NAME }}}\>=\?.*' "$file" ; then
19+
sed -i 's/\(^.*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*\)/\1 \2/' "$file"
2020
fi
2121
# ensure that grubenv is not referenced
2222
if grep -q '\$kernelopts' "$file"; then

shared/templates/grub2_bootloader_argument/tests/arg_not_in_etcdefaultgrub.fail.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ source common.sh
1212
{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
1313

1414
# Removes argument from kernel command line in /etc/default/grub
15-
if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ARG_NAME}}}=\?.*"' '/etc/default/grub' ; then
16-
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
15+
if grep -q '^GRUB_CMDLINE_LINUX=.*\<{{{ ARG_NAME }}}\>=\?.*"' '/etc/default/grub' ; then
16+
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
1717
fi

shared/templates/grub2_bootloader_argument/tests/arg_not_in_etcdefaultgrub_recovery_disabled.fail.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ source common.sh
1111
{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
1212

1313
# Removes the argument from kernel command line in /etc/default/grub
14-
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ARG_NAME}}}=\?.*"' '/etc/default/grub' ; then
15-
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
14+
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*\<{{{ ARG_NAME }}}\>=\?.*"' '/etc/default/grub' ; then
15+
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
1616
fi
1717

1818
# removing the parameter from the no recovery kernel parameters as well
19-
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
19+
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
2020

2121
# disabling recovery
2222
sed -i 's/\(^.*GRUB_DISABLE_RECOVERY=\).*/\1true/' '/etc/default/grub'

shared/templates/grub2_bootloader_argument/tests/correct_recovery_disabled.pass.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ source common.sh
1616
{{{ grub2_bootloader_argument_remediation(ARG_NAME, ARG_NAME_VALUE) }}}
1717

1818
# Correct the form of default kernel command line in GRUB /etc/default/grub and applies value through Grubby
19-
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ ARG_NAME }}}=\?.*"' '/etc/default/grub' ; then
19+
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*\<{{{ ARG_NAME }}}\>=\?.*"' '/etc/default/grub' ; then
2020
# modify the GRUB command-line if an arg=value already exists
21-
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ ARG_NAME }}}=\?[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/' '/etc/default/grub'
21+
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/' '/etc/default/grub'
2222
else
2323
# no arg is present, append it
2424
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)"/\1 {{{ ARG_NAME_VALUE }}}"/' '/etc/default/grub'
2525
fi
2626

2727
# removing the parameter from the no recovery kernel parameters as well
28-
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ ARG_NAME }}}=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
28+
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
2929

3030
# disabling recovery
3131
sed -i 's/\(^.*GRUB_DISABLE_RECOVERY=\).*/\1true/' '/etc/default/grub'

shared/templates/grub2_bootloader_argument/tests/correct_value_grubenv_only.pass.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ source common.sh
1111

1212
# adds argument from kernel command line into /etc/default/grub
1313
file="/etc/default/grub"
14-
if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ARG_NAME}}}=\?.*"' "$file"; then
15-
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*"\)/\1 {{{ARG_NAME_VALUE}}} \2/' "$file"
14+
if grep -q '^GRUB_CMDLINE_LINUX=.*\<{{{ ARG_NAME }}}\>=\?.*"' "$file"; then
15+
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/' "$file"
1616
else
17-
sed -i 's/^GRUB_CMDLINE_LINUX=".*/GRUB_CMDLINE_LINUX="{{{ARG_NAME_VALUE}}}"/' "$file"
17+
sed -i 's/^GRUB_CMDLINE_LINUX=".*/GRUB_CMDLINE_LINUX="{{{ ARG_NAME_VALUE }}}"/' "$file"
1818
fi
1919

2020
# configure the argument in kernel command line in /boot/grub2/grubenv
2121
file="/boot/grub2/grubenv"
22-
if grep -q '^.*{{{ARG_NAME}}}=\?.*' "$file"; then
22+
if grep -q '^.*\<{{{ ARG_NAME }}}\>=\?.*' "$file"; then
2323
# modify the GRUB command-line if the arg already exists
24-
sed -i 's/\(^.*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*\)/\1 {{{ARG_NAME_VALUE}}} \2/' "$file"
24+
sed -i 's/\(^.*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*\)/\1 {{{ ARG_NAME_VALUE }}} \2/' "$file"
2525
else
2626
# no arg is present, append it
27-
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME_VALUE}}}/' "$file"
27+
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ ARG_NAME_VALUE }}}/' "$file"
2828
fi
2929

3030

shared/templates/grub2_bootloader_argument/tests/correct_value_mix_entries_and_grubenv.pass.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ source common.sh
1111

1212
# adds argument from kernel command line into /etc/default/grub
1313
file="/etc/default/grub"
14-
if grep -q '^GRUB_CMDLINE_LINUX=.*{{{ARG_NAME}}}=\?.*"' "$file"; then
15-
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*"\)/\1 {{{ARG_NAME_VALUE}}} \2/' "$file"
14+
if grep -q '^GRUB_CMDLINE_LINUX=.*\<{{{ ARG_NAME }}}\>=\?.*"' "$file"; then
15+
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 {{{ ARG_NAME_VALUE }}} \2/' "$file"
1616
else
17-
sed -i 's/^GRUB_CMDLINE_LINUX=".*/GRUB_CMDLINE_LINUX="{{{ARG_NAME_VALUE}}}"/' "$file"
17+
sed -i 's/^GRUB_CMDLINE_LINUX=".*/GRUB_CMDLINE_LINUX="{{{ ARG_NAME_VALUE }}}"/' "$file"
1818
fi
1919

2020
# configure the argument in kernel command line in /boot/grub2/grubenv
2121
file="/boot/grub2/grubenv"
22-
if grep -q '^.*{{{ARG_NAME}}}=\?.*' "$file"; then
22+
if grep -q '^.*\<{{{ ARG_NAME }}}\>=\?.*' "$file"; then
2323
# modify the GRUB command-line if the arg already exists
24-
sed -i 's/\(^.*\){{{ARG_NAME}}}=\?[^[:space:]]*\(.*\)/\1 {{{ARG_NAME_VALUE}}} \2/' "$file"
24+
sed -i 's/\(^.*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*\)/\1 {{{ ARG_NAME_VALUE }}} \2/' "$file"
2525
else
2626
# no arg is present, append it
27-
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ARG_NAME_VALUE}}}/' "$file"
27+
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ ARG_NAME_VALUE }}}/' "$file"
2828
fi
2929

3030

@@ -35,7 +35,7 @@ rm -f /boot/loader/entries/*.conf
3535
echo 'version 5.0'
3636
echo 'linux /vmlinuz'
3737
echo 'initrd /initramfs'
38-
echo 'options root=UUID=abc-def rhgb ro quiet mock {{{ARG_NAME_VALUE}}}'
38+
echo 'options root=UUID=abc-def rhgb ro quiet mock {{{ ARG_NAME_VALUE }}}'
3939
echo 'grub_users $grub_users'
4040
echo 'grub_arg --unrestricted'
4141
} > /boot/loader/entries/mock.conf

shared/templates/grub2_bootloader_argument/tests/wrong_value_entries.fail.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ source common.sh
1616

1717
# Breaks argument from kernel command line in /boot/loader/entries/*.conf
1818
for file in /boot/loader/entries/*.conf ; do
19-
if grep -q '^.*{{{ ESCAPED_ARG_NAME }}}=\?.*' "$file" ; then
19+
if grep -q '^.*\<{{{ ARG_NAME }}}\>=\?.*' "$file" ; then
2020
# modify the GRUB command-line if an ={{{ARG_NAME}}} arg already exists
21-
sed -i 's/\(^.*\){{{ ARG_NAME }}}=\?[^[:space:]]*\(.*\)/\1 {{{ ARG_NAME_VALUE_WRONG }}} \2/' "$file"
21+
sed -i 's/\(^.*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*\)/\1 {{{ ARG_NAME_VALUE_WRONG }}} \2/' "$file"
2222
else
2323
# no {{{ARG_NAME}}}=arg is present, append it
2424
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ ARG_NAME_VALUE_WRONG }}}/' "$file"

shared/templates/grub2_bootloader_argument/tests/wrong_value_etcdefaultgrub_recovery_disabled.fail.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ source common.sh
1515
rm -f /etc/default/grub.d/*
1616

1717
# Correct the form of default kernel command line in GRUB /etc/default/grub and applies value through Grubby
18-
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*{{{ ESCAPED_ARG_NAME }}}=\?.*"' '/etc/default/grub' ; then
18+
if grep -q '^GRUB_CMDLINE_LINUX_DEFAULT=.*\<{{{ ARG_NAME }}}\>=\?.*"' '/etc/default/grub' ; then
1919
# modify the GRUB command-line if an arg=value already exists
20-
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\){{{ ARG_NAME }}}=\?[^[:space:]]*\(.*"\)/\1{{{ ARG_NAME_VALUE_WRONG }}} \2/' '/etc/default/grub'
20+
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1{{{ ARG_NAME_VALUE_WRONG }}} \2/' '/etc/default/grub'
2121
else
2222
# no arg is present, append it
2323
sed -i 's/\(^GRUB_CMDLINE_LINUX_DEFAULT=".*\)"/\1{{{ ARG_NAME_VALUE_WRONG }}}"/' '/etc/default/grub'
2424
fi
2525

2626
# removing the parameter from the no recovery kernel parameters as well
27-
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\){{{ ARG_NAME }}}=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
27+
sed -i 's/\(^GRUB_CMDLINE_LINUX=".*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*"\)/\1 \2/' '/etc/default/grub'
2828

2929
# disabling recovery
3030
sed -i 's/\(^.*GRUB_DISABLE_RECOVERY=\).*/\1true/' '/etc/default/grub'

shared/templates/grub2_bootloader_argument/tests/wrong_value_grubenv.fail.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ source common.sh
1717

1818
# Break the argument in kernel command line in /boot/grub2/grubenv
1919
file="/boot/grub2/grubenv"
20-
if grep -q '^.*{{{ARG_NAME}}}=\?.*' "$file" ; then
20+
if grep -q '^.*\<{{{ ARG_NAME }}}\>=\?.*' "$file" ; then
2121
# modify the GRUB command-line if the arg already exists
22-
sed -i 's/\(^.*\){{{ ARG_NAME }}}=\?[^[:space:]]*\(.*\)/\1 {{{ ARG_NAME_VALUE_WRONG }}}=wrong \2/' "$file"
22+
sed -i 's/\(^.*\)\<{{{ ARG_NAME }}}\>=\?[^[:space:]]*\(.*\)/\1 {{{ ARG_NAME_VALUE_WRONG }}}=wrong \2/' "$file"
2323
else
2424
# no arg is present, append it
25-
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ ARG_NAME_VALUE_WRONG }}}=wrong/' "$file"
25+
sed -i 's/\(^.*\(vmlinuz\|kernelopts\).*\)/\1 {{{ ARG_NAME_VALUE_WRONG }}}=wrong/' "$file"
2626
fi
2727

2828
# Ensure that grubenv is referenced through $kernelopts

0 commit comments

Comments
 (0)