* [PATCH keyutils] test: Hide endianness
@ 2025-04-22 14:40 David Howells
2025-04-30 16:04 ` Jarkko Sakkinen
0 siblings, 1 reply; 2+ messages in thread
From: David Howells @ 2025-04-22 14:40 UTC (permalink / raw)
To: Jarkko Sakkinen; +Cc: dhowells, keyrings
Hi Jarkko,
Having tried a bunch of ways to determine the cpu endianness so that I can
generate model content to compare against the raw contents of a keying, I
think it might be better to sidestep the problem and rely on 'od' to byteswap
the raw contents and then compare. What do you think?
David
---
test: Hide endianness
Hide the endianness of the raw binary contents of a keyring by passing it
through /usr/bin/od which will automatically byteswap it in 4-byte chunks
rather than trying to detect the endianness by examining what may be a
binary file to obtain a string the format of which changes over time.
Signed-off-by: David Howells <dhowells@redhat.com>
---
tests/keyctl/reading/valid/runtest.sh | 9 ++-------
tests/toolbox.inc.sh | 36 +++++++++++++++++++++++-------------
2 files changed, 25 insertions(+), 20 deletions(-)
diff --git a/tests/keyctl/reading/valid/runtest.sh b/tests/keyctl/reading/valid/runtest.sh
index 2fb88b9..2527f13 100644
--- a/tests/keyctl/reading/valid/runtest.sh
+++ b/tests/keyctl/reading/valid/runtest.sh
@@ -40,13 +40,8 @@ expect_payload payload "67697a7a 617264"
# read the contents of the keyring as hex and match it to the key ID
marker "READ KEYRING"
-read_key $keyringid
-tmp=`printf %08x $keyid`
-if [ "$endian" = "LE" ]
-then
- tmp=`echo $tmp | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/'`
-fi
-expect_payload payload $tmp
+pipe_key_int32 $keyringid
+expect_payload payload $keyid
# remove read permission from the key and try reading it again
# - we should still have read permission because it's searchable in our
diff --git a/tests/toolbox.inc.sh b/tests/toolbox.inc.sh
index 609a6c7..6f4fb18 100644
--- a/tests/toolbox.inc.sh
+++ b/tests/toolbox.inc.sh
@@ -12,19 +12,6 @@
echo === $OUTPUTFILE ===
-endian=`file -L /proc/$$/exe`
-if expr "$endian" : '.* MSB \+\(pie executable\|executable\|shared object\).*' >&/dev/null
-then
- endian=BE
-elif expr "$endian" : '.* LSB \+\(pie executable\|executable\|shared object\).*' >&/dev/null
-then
- endian=LE
-else
- echo -e "+++ \e[31;1mCan't Determine Endianness\e[0m"
- echo "+++ Can't Determine Endianness" >>$OUTPUTFILE
- exit 2
-fi
-
maxtypelen=31
maxtype=`for ((i=0; i<$((maxtypelen)); i++)); do echo -n a; done`
@@ -1055,6 +1042,29 @@ function pipe_key ()
fi
}
+###############################################################################
+#
+# pipe a key's raw payload to od to stdout, displaying it as a sequence of
+# 32-bit numbers, appropriately byteswapped.
+#
+###############################################################################
+function pipe_key_int32 ()
+{
+ my_exitval=0
+ if [ "x$1" = "x--fail" ]
+ then
+ my_exitval=1
+ shift
+ fi
+
+ echo keyctl pipe $1 \| od -t u4 -A none >>$OUTPUTFILE
+ echo `keyctl pipe $1 | od -t u4 -A none` >>$OUTPUTFILE 2>&1
+ if [ $? != $my_exitval ]
+ then
+ failed
+ fi
+}
+
###############################################################################
#
# pipe a key's raw payload through md5sum
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH keyutils] test: Hide endianness
2025-04-22 14:40 [PATCH keyutils] test: Hide endianness David Howells
@ 2025-04-30 16:04 ` Jarkko Sakkinen
0 siblings, 0 replies; 2+ messages in thread
From: Jarkko Sakkinen @ 2025-04-30 16:04 UTC (permalink / raw)
To: David Howells; +Cc: keyrings
On Tue, Apr 22, 2025 at 03:40:19PM +0100, David Howells wrote:
> Hi Jarkko,
>
> Having tried a bunch of ways to determine the cpu endianness so that I can
> generate model content to compare against the raw contents of a keying, I
> think it might be better to sidestep the problem and rely on 'od' to byteswap
> the raw contents and then compare. What do you think?
It's a heck a lot less convoluted at least.
>
> David
> ---
> test: Hide endianness
>
> Hide the endianness of the raw binary contents of a keyring by passing it
> through /usr/bin/od which will automatically byteswap it in 4-byte chunks
> rather than trying to detect the endianness by examining what may be a
> binary file to obtain a string the format of which changes over time.
>
> Signed-off-by: David Howells <dhowells@redhat.com>
> ---
> tests/keyctl/reading/valid/runtest.sh | 9 ++-------
> tests/toolbox.inc.sh | 36 +++++++++++++++++++++++-------------
> 2 files changed, 25 insertions(+), 20 deletions(-)
>
> diff --git a/tests/keyctl/reading/valid/runtest.sh b/tests/keyctl/reading/valid/runtest.sh
> index 2fb88b9..2527f13 100644
> --- a/tests/keyctl/reading/valid/runtest.sh
> +++ b/tests/keyctl/reading/valid/runtest.sh
> @@ -40,13 +40,8 @@ expect_payload payload "67697a7a 617264"
>
> # read the contents of the keyring as hex and match it to the key ID
> marker "READ KEYRING"
> -read_key $keyringid
> -tmp=`printf %08x $keyid`
> -if [ "$endian" = "LE" ]
> -then
> - tmp=`echo $tmp | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/'`
> -fi
> -expect_payload payload $tmp
> +pipe_key_int32 $keyringid
> +expect_payload payload $keyid
>
> # remove read permission from the key and try reading it again
> # - we should still have read permission because it's searchable in our
> diff --git a/tests/toolbox.inc.sh b/tests/toolbox.inc.sh
> index 609a6c7..6f4fb18 100644
> --- a/tests/toolbox.inc.sh
> +++ b/tests/toolbox.inc.sh
> @@ -12,19 +12,6 @@
>
> echo === $OUTPUTFILE ===
>
> -endian=`file -L /proc/$$/exe`
> -if expr "$endian" : '.* MSB \+\(pie executable\|executable\|shared object\).*' >&/dev/null
> -then
> - endian=BE
> -elif expr "$endian" : '.* LSB \+\(pie executable\|executable\|shared object\).*' >&/dev/null
> -then
> - endian=LE
> -else
> - echo -e "+++ \e[31;1mCan't Determine Endianness\e[0m"
> - echo "+++ Can't Determine Endianness" >>$OUTPUTFILE
> - exit 2
> -fi
> -
> maxtypelen=31
> maxtype=`for ((i=0; i<$((maxtypelen)); i++)); do echo -n a; done`
>
> @@ -1055,6 +1042,29 @@ function pipe_key ()
> fi
> }
>
> +###############################################################################
> +#
> +# pipe a key's raw payload to od to stdout, displaying it as a sequence of
> +# 32-bit numbers, appropriately byteswapped.
> +#
> +###############################################################################
> +function pipe_key_int32 ()
> +{
> + my_exitval=0
> + if [ "x$1" = "x--fail" ]
> + then
> + my_exitval=1
> + shift
> + fi
> +
> + echo keyctl pipe $1 \| od -t u4 -A none >>$OUTPUTFILE
> + echo `keyctl pipe $1 | od -t u4 -A none` >>$OUTPUTFILE 2>&1
> + if [ $? != $my_exitval ]
> + then
> + failed
> + fi
> +}
> +
> ###############################################################################
> #
> # pipe a key's raw payload through md5sum
>
Yeah, I mean it's also a change from text processing tool to a binary
tool (from complex to simple), so IMHO this quite obvious:
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
BR, Jarkko
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-04-30 16:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-22 14:40 [PATCH keyutils] test: Hide endianness David Howells
2025-04-30 16:04 ` Jarkko Sakkinen
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.