All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/2] Add tst_hexdump utility
@ 2021-07-26 15:17 Martin Doucha
  2021-07-26 15:17 ` [LTP] [PATCH 2/2] Replace the xxd utility with tst_hexdump Martin Doucha
  2021-07-30 13:52 ` [LTP] [PATCH 1/2] Add tst_hexdump utility Petr Vorel
  0 siblings, 2 replies; 3+ messages in thread
From: Martin Doucha @ 2021-07-26 15:17 UTC (permalink / raw)
  To: ltp

tst_hexdump implements conversion between binary and hexadecimal values in both
directions for shell tests.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/lib/Makefile      |  2 +-
 testcases/lib/tst_hexdump.c | 55 +++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 testcases/lib/tst_hexdump.c

diff --git a/testcases/lib/Makefile b/testcases/lib/Makefile
index 98d9e4613..38813e640 100644
--- a/testcases/lib/Makefile
+++ b/testcases/lib/Makefile
@@ -11,6 +11,6 @@ INSTALL_TARGETS		:= *.sh
 MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
 			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
 			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
-			   tst_get_median
+			   tst_get_median tst_hexdump
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/lib/tst_hexdump.c b/testcases/lib/tst_hexdump.c
new file mode 100644
index 000000000..f83b8bfbf
--- /dev/null
+++ b/testcases/lib/tst_hexdump.c
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 SUSE LLC <mdoucha@suse.cz>
+ *
+ * Convert bytes from standard input to hexadecimal representation.
+ *
+ * Parameters:
+ * -d   Convert hexadecimal values from standard input to binary representation
+ *      instead.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+int decode_hex(void)
+{
+	int ret;
+	unsigned int val;
+
+	while ((ret = scanf("%2x", &val)) == 1)
+		putchar(val);
+
+	return ret != EOF || ferror(stdin);
+}
+
+int encode_hex(void)
+{
+	int val;
+
+	for (val = getchar(); val >= 0 && val <= 0xff; val = getchar())
+		printf("%02x", val);
+
+	return val != EOF || ferror(stdin);
+}
+
+int main(int argc, char **argv)
+{
+	int ret, decode = 0;
+
+	while ((ret = getopt(argc, argv, "d"))) {
+		if (ret < 0)
+			break;
+
+		switch (ret) {
+		case 'd':
+			decode = 1;
+			break;
+		}
+	}
+
+	if (decode)
+		return decode_hex();
+	else
+		return encode_hex();
+}
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [LTP] [PATCH 2/2] Replace the xxd utility with tst_hexdump
  2021-07-26 15:17 [LTP] [PATCH 1/2] Add tst_hexdump utility Martin Doucha
@ 2021-07-26 15:17 ` Martin Doucha
  2021-07-30 13:52 ` [LTP] [PATCH 1/2] Add tst_hexdump utility Petr Vorel
  1 sibling, 0 replies; 3+ messages in thread
From: Martin Doucha @ 2021-07-26 15:17 UTC (permalink / raw)
  To: ltp

The xxd utility is part of Vim suite and may not be available on embedded
systems. Replace it with internal LTP helper program.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---

Note: I couldn't test ima_selinux.sh changes because SLES kernel don't support
measure func=CRITICAL_DATA yet. ima_keys.sh appears to work correctly.

 testcases/kernel/security/integrity/ima/tests/ima_keys.sh   | 6 +++---
 .../kernel/security/integrity/ima/tests/ima_selinux.sh      | 4 +---
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/testcases/kernel/security/integrity/ima/tests/ima_keys.sh b/testcases/kernel/security/integrity/ima/tests/ima_keys.sh
index 3476b8007..995a55fed 100755
--- a/testcases/kernel/security/integrity/ima/tests/ima_keys.sh
+++ b/testcases/kernel/security/integrity/ima/tests/ima_keys.sh
@@ -6,7 +6,7 @@
 #
 # Verify that keys are measured correctly based on policy.
 
-TST_NEEDS_CMDS="cmp cut grep sed xxd"
+TST_NEEDS_CMDS="cmp cut grep sed"
 TST_CNT=2
 TST_NEEDS_DEVICE=1
 TST_SETUP=setup
@@ -82,7 +82,7 @@ test1()
 		algorithm=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f1)
 		keyring=$(echo "$line" | cut -d' ' -f5)
 
-		echo "$line" | cut -d' ' -f6 | xxd -r -p > $test_file
+		echo "$line" | cut -d' ' -f6 | tst_hexdump -d > $test_file
 
 		if ! expected_digest="$(compute_digest $algorithm $test_file)"; then
 			tst_res TCONF "cannot compute digest for $algorithm"
@@ -126,7 +126,7 @@ test2()
 		tst_brk TBROK "unable to import a certificate into $keyring_name keyring"
 
 	grep $keyring_name $ASCII_MEASUREMENTS | tail -n1 | cut -d' ' -f6 | \
-		xxd -r -p > $temp_file
+		tst_hexdump -d > $temp_file
 
 	if [ ! -s $temp_file ]; then
 		tst_res TFAIL "keyring $keyring_name not found in $ASCII_MEASUREMENTS"
diff --git a/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh b/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh
index c2af0e298..a4eb60b16 100755
--- a/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh
+++ b/testcases/kernel/security/integrity/ima/tests/ima_selinux.sh
@@ -100,8 +100,6 @@ test1()
 # configuration.
 test2()
 {
-	tst_check_cmds xxd || return
-
 	local measured_data state_file="$TST_TMPDIR/selinux_state.txt"
 	local data_source_name="selinux"
 	local pattern="data_sources=[^[:space:]]*$data_source_name"
@@ -127,7 +125,7 @@ test2()
 	digest=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f2)
 	algorithm=$(echo "$line" | cut -d' ' -f4 | cut -d':' -f1)
 
-	echo "$line" | cut -d' ' -f6 | xxd -r -p > $state_file
+	echo "$line" | cut -d' ' -f6 | tst_hexdump -d > $state_file
 
 	expected_digest="$(compute_digest $algorithm $state_file)" || \
 	tst_brk TCONF "cannot compute digest for $algorithm"
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [LTP] [PATCH 1/2] Add tst_hexdump utility
  2021-07-26 15:17 [LTP] [PATCH 1/2] Add tst_hexdump utility Martin Doucha
  2021-07-26 15:17 ` [LTP] [PATCH 2/2] Replace the xxd utility with tst_hexdump Martin Doucha
@ 2021-07-30 13:52 ` Petr Vorel
  1 sibling, 0 replies; 3+ messages in thread
From: Petr Vorel @ 2021-07-30 13:52 UTC (permalink / raw)
  To: ltp

Hi Martin,

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Thanks! I suppose I'll merge it as is, but few notes below:

As you use only -d in this patchset I suppose you want to have
it as a general utility. It might be worth to mention that xxd by default wraps by
16 cols (-c 16), which we don't do:

$ echo "06a441375b431e06280e2d4e199ba650c14c47d9" | ./testcases/lib/tst_hexdump; echo
303661343431333735623433316530363238306532643465313939626136353063313463343764390a

$ echo "06a441375b431e06280e2d4e199ba650c14c47d9" | xxd -p
303661343431333735623433316530363238306532643465313939626136
353063313463343764390a

Other option would be just make -d as default and remove encode_hex() until is
really needed.

Kind regards,
Petr


> tst_hexdump implements conversion between binary and hexadecimal values in both
> directions for shell tests.

> Signed-off-by: Martin Doucha <mdoucha@suse.cz>
> ---
>  testcases/lib/Makefile      |  2 +-
>  testcases/lib/tst_hexdump.c | 55 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 56 insertions(+), 1 deletion(-)
>  create mode 100644 testcases/lib/tst_hexdump.c

> diff --git a/testcases/lib/Makefile b/testcases/lib/Makefile
> index 98d9e4613..38813e640 100644
> --- a/testcases/lib/Makefile
> +++ b/testcases/lib/Makefile
> @@ -11,6 +11,6 @@ INSTALL_TARGETS		:= *.sh
>  MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
>  			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
>  			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
> -			   tst_get_median
> +			   tst_get_median tst_hexdump

>  include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/lib/tst_hexdump.c b/testcases/lib/tst_hexdump.c
> new file mode 100644
> index 000000000..f83b8bfbf
> --- /dev/null
> +++ b/testcases/lib/tst_hexdump.c
> @@ -0,0 +1,55 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 SUSE LLC <mdoucha@suse.cz>
> + *
> + * Convert bytes from standard input to hexadecimal representation.
> + *
> + * Parameters:
> + * -d   Convert hexadecimal values from standard input to binary representation
> + *      instead.
This could be printed in -h parameter if really meant to be a general tool.
But understand you didn't bother.

Kind regards,
Petr

> + */
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +
> +int decode_hex(void)
> +{
> +	int ret;
> +	unsigned int val;
> +
> +	while ((ret = scanf("%2x", &val)) == 1)
> +		putchar(val);
> +
> +	return ret != EOF || ferror(stdin);
> +}
> +
> +int encode_hex(void)
> +{
> +	int val;
> +
> +	for (val = getchar(); val >= 0 && val <= 0xff; val = getchar())
> +		printf("%02x", val);
> +
> +	return val != EOF || ferror(stdin);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +	int ret, decode = 0;
> +
> +	while ((ret = getopt(argc, argv, "d"))) {
> +		if (ret < 0)
> +			break;
> +
> +		switch (ret) {
> +		case 'd':
> +			decode = 1;
> +			break;
> +		}
> +	}
> +
> +	if (decode)
> +		return decode_hex();
> +	else
> +		return encode_hex();
> +}

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-30 13:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-26 15:17 [LTP] [PATCH 1/2] Add tst_hexdump utility Martin Doucha
2021-07-26 15:17 ` [LTP] [PATCH 2/2] Replace the xxd utility with tst_hexdump Martin Doucha
2021-07-30 13:52 ` [LTP] [PATCH 1/2] Add tst_hexdump utility Petr Vorel

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.