grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v18 08/25] asn1_test: changes for grub compatibility


From: Gary Lin
Subject: [PATCH v18 08/25] asn1_test: changes for grub compatibility
Date: Fri, 28 Jun 2024 16:18:51 +0800

Do a few things to make asn1 tests compile as part of grub:

- include asn1_test.h only

- rename the main functions to the test names

- remove 'verbose' and the unnecessary printf()

- print the error messages with grub_printf()

- return either 0 or 1 to reflect the results of the tests

- replace the system functions with grub functions, ex: memcmp() ->
  grub_memcmp()

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Gary Lin <glin@suse.com>
---
 ..._test-changes-for-grub-compatibility.patch | 844 ++++++++++++++++++
 1 file changed, 844 insertions(+)
 create mode 100644 
grub-core/lib/libtasn1-patches/0006-asn1_test-changes-for-grub-compatibility.patch

diff --git 
a/grub-core/lib/libtasn1-patches/0006-asn1_test-changes-for-grub-compatibility.patch
 
b/grub-core/lib/libtasn1-patches/0006-asn1_test-changes-for-grub-compatibility.patch
new file mode 100644
index 000000000..63631f9ff
--- /dev/null
+++ 
b/grub-core/lib/libtasn1-patches/0006-asn1_test-changes-for-grub-compatibility.patch
@@ -0,0 +1,844 @@
+From 650259866464a0f54df0c18fbcb9068d6536cfc3 Mon Sep 17 00:00:00 2001
+From: Gary Lin <glin@suse.com>
+Date: Tue, 18 Jun 2024 14:33:01 +0800
+Subject: [PATCH 6/6] asn1_test: changes for grub compatibility
+
+Do a few things to make asn1 tests compile as part of grub:
+
+- include asn1_test.h only
+
+- rename the main functions to the test names
+
+- remove 'verbose' and the unnecessary printf()
+
+- print the error messages with grub_printf()
+
+- return either 0 or 1 to reflect the results of the tests
+
+- replace the system functions with grub functions, ex: memcmp() ->
+  grub_memcmp()
+
+Signed-off-by: Daniel Axtens <dja@axtens.net>
+Signed-off-by: Gary Lin <glin@suse.com>
+---
+ grub-core/tests/asn1/tests/CVE-2018-1000654.c | 32 +++-----
+ grub-core/tests/asn1/tests/Test_overflow.c    | 71 +++++-------------
+ grub-core/tests/asn1/tests/Test_simple.c      | 51 ++++++-------
+ grub-core/tests/asn1/tests/Test_strings.c     | 40 ++++------
+ .../tests/asn1/tests/object-id-decoding.c     | 28 +++----
+ .../tests/asn1/tests/object-id-encoding.c     | 42 ++++-------
+ grub-core/tests/asn1/tests/octet-string.c     | 75 ++++++-------------
+ grub-core/tests/asn1/tests/reproducers.c      | 32 +++-----
+ 8 files changed, 126 insertions(+), 245 deletions(-)
+
+diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c 
b/grub-core/tests/asn1/tests/CVE-2018-1000654.c
+index 0c22b7012..6320af338 100644
+--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c
++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c
+@@ -22,51 +22,41 @@
+ /* Description: reproducer for CVE-2018-1000654                       */
+ /****************************************************************/
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
++#include "asn1_test.h"
++
+ #include "CVE-2018-1000654-1_asn1_tab.h"
+ #include "CVE-2018-1000654-2_asn1_tab.h"
+ 
+ int
+-main (int argc, char *argv[])
++test_CVE_2018_1000654 (void)
+ {
+-  int result, verbose = 0;
++  int result;
+   asn1_node definitions = NULL;
+   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
+ 
+-  if (argc > 1)
+-    verbose = 1;
+-
+-  printf ("Test 1\n");
+-
+   result =
+     asn1_array2tree (CVE_2018_1000654_1_asn1_tab, &definitions,
+                    errorDescription);
+   if (result != ASN1_RECURSION)
+     {
+-      asn1_perror (result);
+-      printf ("ErrorDescription = %s\n\n", errorDescription);
+-      exit (1);
++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
++                 asn1_strerror (result), errorDescription);
++      return 1;
+     }
+ 
+   asn1_delete_structure (&definitions);
+ 
+-  printf ("Test 2\n");
+-
+   result =
+     asn1_array2tree (CVE_2018_1000654_2_asn1_tab, &definitions,
+                    errorDescription);
+   if (result != ASN1_RECURSION)
+     {
+-      asn1_perror (result);
+-      printf ("ErrorDescription = %s\n\n", errorDescription);
+-      exit (1);
++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
++                 asn1_strerror (result), errorDescription);
++      return 1;
+     }
+ 
+   asn1_delete_structure (&definitions);
+ 
+-  if (verbose)
+-    printf ("Success\n");
+-  exit (0);
++  return 0;
+ }
+diff --git a/grub-core/tests/asn1/tests/Test_overflow.c 
b/grub-core/tests/asn1/tests/Test_overflow.c
+index c61dea4bb..7367f1af1 100644
+--- a/grub-core/tests/asn1/tests/Test_overflow.c
++++ b/grub-core/tests/asn1/tests/Test_overflow.c
+@@ -20,22 +20,13 @@
+ 
+ /* Written by Simon Josefsson */
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
+-#include <limits.h>
+-
+-#include "libtasn1.h"
++#include "asn1_test.h"
+ 
+ int
+-main (int argc, char **argv)
++test_overflow(void)
+ {
+   /* Test that values larger than long are rejected.  This has worked
+      fine with all versions of libtasn1. */
+-  int verbose = 0;
+-
+-  if (argc > 1)
+-    verbose = 1;
+ 
+   {
+     unsigned char der[] = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
+@@ -44,23 +35,18 @@ main (int argc, char **argv)
+ 
+     l = asn1_get_length_der (der, sizeof der, &len);
+ 
+-    if (l == -2L)
+-      {
+-      if (verbose)
+-        puts ("OK: asn1_get_length_der bignum");
+-      }
+-    else
++    if (l != -2L)
+       {
+-      printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len);
++      grub_printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, 
len);
+       return 1;
+       }
+   }
+ 
+   /* Test that values larger than int but smaller than long are
+      rejected.  This limitation was introduced with libtasn1 2.12. */
+-  if (LONG_MAX > INT_MAX)
++#if (GRUB_LONG_MAX > GRUB_INT_MAX)
+     {
+-      unsigned long num = ((long) UINT_MAX) << 2;
++      unsigned long num = ((long) GRUB_UINT_MAX) << 2;
+       unsigned char der[20];
+       int der_len;
+       long l;
+@@ -70,18 +56,14 @@ main (int argc, char **argv)
+ 
+       l = asn1_get_length_der (der, der_len, &len);
+ 
+-      if (l == -2L)
+-      {
+-        if (verbose)
+-          puts ("OK: asn1_get_length_der intnum");
+-      }
+-      else
++      if (l != -2L)
+       {
+-        printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l,
+-                len);
++        grub_printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l,
++                     len);
+         return 1;
+       }
+     }
++#endif
+ 
+   /* Test that values larger than would fit in the input string are
+      rejected.  This problem was fixed in libtasn1 2.12. */
+@@ -97,15 +79,10 @@ main (int argc, char **argv)
+     der_len = sizeof (der);
+     l = asn1_get_length_der (der, der_len, &len);
+ 
+-    if (l == -4L)
++    if (l != -4L)
+       {
+-      if (verbose)
+-        puts ("OK: asn1_get_length_der overflow-small");
+-      }
+-    else
+-      {
+-      printf ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n",
+-              l, len);
++      grub_printf ("ERROR: asn1_get_length_der overflow-small (l %ld len 
%d)\n",
++                   l, len);
+       return 1;
+       }
+   }
+@@ -124,15 +101,10 @@ main (int argc, char **argv)
+     der_len = sizeof (der);
+     l = asn1_get_length_der (der, der_len, &len);
+ 
+-    if (l == -4L)
+-      {
+-      if (verbose)
+-        puts ("OK: asn1_get_length_der overflow-large1");
+-      }
+-    else
++    if (l != -4L)
+       {
+-      printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n",
+-              l, len);
++      grub_printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len 
%d)\n",
++                   l, len);
+       return 1;
+       }
+   }
+@@ -151,15 +123,10 @@ main (int argc, char **argv)
+     der_len = sizeof (der);
+     l = asn1_get_length_der (der, der_len, &len);
+ 
+-    if (l == -2L)
+-      {
+-      if (verbose)
+-        puts ("OK: asn1_get_length_der overflow-large2");
+-      }
+-    else
++    if (l != -2L)
+       {
+-      printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n",
+-              l, len);
++      grub_printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len 
%d)\n",
++                   l, len);
+       return 1;
+       }
+   }
+diff --git a/grub-core/tests/asn1/tests/Test_simple.c 
b/grub-core/tests/asn1/tests/Test_simple.c
+index 6cd07e069..19613cae8 100644
+--- a/grub-core/tests/asn1/tests/Test_simple.c
++++ b/grub-core/tests/asn1/tests/Test_simple.c
+@@ -20,11 +20,7 @@
+  *
+  */
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
+-
+-#include "libtasn1.h"
++#include "asn1_test.h"
+ 
+ struct tv
+ {
+@@ -77,7 +73,7 @@ static const struct tv tv[] = {
+ };
+ 
+ int
+-main (int argc, char *argv[])
++test_simple (void)
+ {
+   int result;
+   unsigned char der[100];
+@@ -85,7 +81,7 @@ main (int argc, char *argv[])
+   int der_len = sizeof (der);
+   int str_size = sizeof (str);
+   int ret_len, bit_len;
+-  size_t i;
++  grub_size_t i;
+ 
+   {
+     unsigned int etype = 38;
+@@ -98,7 +94,7 @@ main (int argc, char *argv[])
+     result = asn1_encode_simple_der (etype, my_str, my_str_len, tl, &tl_len);
+     if (result != ASN1_VALUE_NOT_VALID)
+       {
+-      fprintf (stderr, "asn1_encode_simple_der out of range etype\n");
++      grub_printf ("asn1_encode_simple_der out of range etype\n");
+       return 1;
+       }
+   }
+@@ -109,7 +105,7 @@ main (int argc, char *argv[])
+   result = asn1_get_bit_der (der, 0, &ret_len, str, str_size, &bit_len);
+   if (result != ASN1_GENERIC_ERROR)
+     {
+-      fprintf (stderr, "asn1_get_bit_der zero\n");
++      grub_printf ("asn1_get_bit_der zero\n");
+       return 1;
+     }
+ 
+@@ -131,9 +127,9 @@ main (int argc, char *argv[])
+       }
+ #endif
+ 
+-      if (der_len != tv[i].derlen || memcmp (der, tv[i].der, der_len) != 0)
++      if (der_len != tv[i].derlen || grub_memcmp (der, tv[i].der, der_len) != 
0)
+       {
+-        fprintf (stderr, "asn1_bit_der iter %lu\n", (unsigned long) i);
++        grub_printf ("asn1_bit_der iter %lu\n", (unsigned long) i);
+         return 1;
+       }
+ 
+@@ -144,8 +140,7 @@ main (int argc, char *argv[])
+       if (result != ASN1_SUCCESS || ret_len != tv[i].derlen
+         || bit_len != tv[i].bitlen)
+       {
+-        fprintf (stderr, "asn1_get_bit_der iter %lu, err: %d\n",
+-                 (unsigned long) i, result);
++        grub_printf ("asn1_get_bit_der iter %lu, err: %d\n", (unsigned long) 
i, result);
+         return 1;
+       }
+     }
+@@ -160,65 +155,65 @@ main (int argc, char *argv[])
+ 
+   /* 03 04 06 6e 5d c0  DER encoding */
+ 
+-  memcpy (der, "\x04\x06\x6e\x5d\xc0", 5);
++  grub_memcpy (der, "\x04\x06\x6e\x5d\xc0", 5);
+   der_len = 5;
+ 
+   result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len);
+   if (result != ASN1_SUCCESS || ret_len != 5
+-      || bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0)
++      || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xc0", 3) != 0)
+     {
+-      fprintf (stderr, "asn1_get_bit_der example\n");
++      grub_printf ("asn1_get_bit_der example\n");
+       return 1;
+     }
+ 
+   der_len = sizeof (der);
+   asn1_bit_der (str, bit_len, der, &der_len);
+-  if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
++  if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
+     {
+-      fprintf (stderr, "asn1_bit_der example roundtrip\n");
++      grub_printf ("asn1_bit_der example roundtrip\n");
+       return 1;
+     }
+ 
+   /* 03 04 06 6e 5d e0 padded with "100000" */
+ 
+-  memcpy (der, "\x04\x06\x6e\x5d\xe0", 5);
++  grub_memcpy (der, "\x04\x06\x6e\x5d\xe0", 5);
+   der_len = 5;
+ 
+   result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len);
+   if (result != ASN1_SUCCESS || ret_len != 5
+-      || bit_len != 18 || memcmp (str, "\x6e\x5d\xe0", 3) != 0)
++      || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xe0", 3) != 0)
+     {
+-      fprintf (stderr, "asn1_get_bit_der example padded\n");
++      grub_printf ("asn1_get_bit_der example padded\n");
+       return 1;
+     }
+ 
+   der_len = sizeof (der);
+   asn1_bit_der (str, bit_len, der, &der_len);
+-  if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
++  if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
+     {
+-      fprintf (stderr, "asn1_bit_der example roundtrip\n");
++      grub_printf ("asn1_bit_der example roundtrip\n");
+       return 1;
+     }
+ 
+   /* 03 81 04 06 6e 5d c0 long form of length octets */
+ 
+-  memcpy (der, "\x81\x04\x06\x6e\x5d\xc0", 6);
++  grub_memcpy (der, "\x81\x04\x06\x6e\x5d\xc0", 6);
+   der_len = 6;
+ 
+   result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len);
+ 
+   if (result != ASN1_SUCCESS || ret_len != 6
+-      || bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0)
++      || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xc0", 3) != 0)
+     {
+-      fprintf (stderr, "asn1_get_bit_der example long form\n");
++      grub_printf ("asn1_get_bit_der example long form\n");
+       return 1;
+     }
+ 
+   der_len = sizeof (der);
+   asn1_bit_der (str, bit_len, der, &der_len);
+-  if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
++  if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
+     {
+-      fprintf (stderr, "asn1_bit_der example roundtrip\n");
++      grub_printf ("asn1_bit_der example roundtrip\n");
+       return 1;
+     }
+ 
+diff --git a/grub-core/tests/asn1/tests/Test_strings.c 
b/grub-core/tests/asn1/tests/Test_strings.c
+index 27f7215e1..1807e71e4 100644
+--- a/grub-core/tests/asn1/tests/Test_strings.c
++++ b/grub-core/tests/asn1/tests/Test_strings.c
+@@ -20,11 +20,7 @@
+  *
+  */
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
+-
+-#include "libtasn1.h"
++#include "asn1_test.h"
+ 
+ struct tv
+ {
+@@ -74,7 +70,7 @@ static const struct tv ber[] = {
+ };
+ 
+ int
+-main (int argc, char *argv[])
++test_strings ()
+ {
+   int ret;
+   unsigned char tl[ASN1_MAX_TL_SIZE];
+@@ -93,17 +89,15 @@ main (int argc, char *argv[])
+                                   tl, &tl_len);
+       if (ret != ASN1_SUCCESS)
+       {
+-        fprintf (stderr, "Encoding error in %u: %s\n", i,
+-                 asn1_strerror (ret));
++        grub_printf ("Encoding error in %u: %s\n", i, asn1_strerror (ret));
+         return 1;
+       }
+       der_len = tl_len + tv[i].str_len;
+ 
+-      if (der_len != tv[i].der_len || memcmp (tl, tv[i].der, tl_len) != 0)
++      if (der_len != tv[i].der_len || grub_memcmp (tl, tv[i].der, tl_len) != 
0)
+       {
+-        fprintf (stderr,
+-                 "DER encoding differs in %u! (size: %u, expected: %u)\n",
+-                 i, der_len, tv[i].der_len);
++        grub_printf ("DER encoding differs in %u! (size: %u, expected: %u)\n",
++                     i, der_len, tv[i].der_len);
+         return 1;
+       }
+ 
+@@ -113,16 +107,14 @@ main (int argc, char *argv[])
+                               &str_len);
+       if (ret != ASN1_SUCCESS)
+       {
+-        fprintf (stderr, "Decoding error in %u: %s\n", i,
+-                 asn1_strerror (ret));
++        grub_printf ("Decoding error in %u: %s\n", i, asn1_strerror (ret));
+         return 1;
+       }
+ 
+-      if (str_len != tv[i].str_len || memcmp (str, tv[i].str, str_len) != 0)
++      if (str_len != tv[i].str_len || grub_memcmp (str, tv[i].str, str_len) 
!= 0)
+       {
+-        fprintf (stderr,
+-                 "DER decoded data differ in %u! (size: %u, expected: %u)\n",
+-                 i, der_len, tv[i].str_len);
++        grub_printf ("DER decoded data differ in %u! (size: %u, expected: 
%u)\n",
++                     i, der_len, tv[i].str_len);
+         return 1;
+       }
+     }
+@@ -136,19 +128,17 @@ main (int argc, char *argv[])
+                               &str_len, NULL);
+       if (ret != ASN1_SUCCESS)
+       {
+-        fprintf (stderr, "BER decoding error in %u: %s\n", i,
+-                 asn1_strerror (ret));
++        grub_printf ("BER decoding error in %u: %s\n", i, asn1_strerror 
(ret));
+         return 1;
+       }
+ 
+-      if (str_len != ber[i].str_len || memcmp (b, ber[i].str, str_len) != 0)
++      if (str_len != ber[i].str_len || grub_memcmp (b, ber[i].str, str_len) 
!= 0)
+       {
+-        fprintf (stderr,
+-                 "BER decoded data differ in %u! (size: %u, expected: %u)\n",
+-                 i, str_len, ber[i].str_len);
++        grub_printf ("BER decoded data differ in %u! (size: %u, expected: 
%u)\n",
++                     i, str_len, ber[i].str_len);
+         return 1;
+       }
+-      free (b);
++      grub_free(b);
+     }
+ 
+ 
+diff --git a/grub-core/tests/asn1/tests/object-id-decoding.c 
b/grub-core/tests/asn1/tests/object-id-decoding.c
+index 06a6c52a2..edfb8beb4 100644
+--- a/grub-core/tests/asn1/tests/object-id-decoding.c
++++ b/grub-core/tests/asn1/tests/object-id-decoding.c
+@@ -18,11 +18,7 @@
+  *
+  */
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
+-
+-#include "libtasn1.h"
++#include "asn1_test.h"
+ 
+ struct tv
+ {
+@@ -74,11 +70,11 @@ static const struct tv tv[] = {
+ };
+ 
+ int
+-main (int argc, char *argv[])
++test_object_id_decoding (void)
+ {
+   char str[128];
+   int ret, ret_len;
+-  size_t i;
++  grub_size_t i;
+ 
+   for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
+     {
+@@ -89,10 +85,8 @@ main (int argc, char *argv[])
+                               sizeof (str));
+       if (ret != tv[i].expected_error)
+       {
+-        fprintf (stderr,
+-                 "%d: asn1_get_object_id_der iter %lu: got '%s' expected 
%d\n",
+-                 __LINE__, (unsigned long) i, asn1_strerror (ret),
+-                 tv[i].expected_error);
++        grub_printf ("%d: asn1_get_object_id_der iter %lu: got '%s' expected 
%d\n",
++                     __LINE__, (unsigned long) i, asn1_strerror(ret), 
tv[i].expected_error);
+         return 1;
+       }
+ 
+@@ -101,17 +95,15 @@ main (int argc, char *argv[])
+ 
+       if (ret_len != tv[i].der_len - 1)
+       {
+-        fprintf (stderr,
+-                 "%d: iter %lu: error in DER, length returned is %d, had 
%d\n",
+-                 __LINE__, (unsigned long) i, ret_len, tv[i].der_len - 1);
++        grub_printf ("%d: iter %lu: error in DER, length returned is %d, had 
%d\n",
++                     __LINE__, (unsigned long)i, ret_len, tv[i].der_len-1);
+         return 1;
+       }
+ 
+-      if (strcmp (tv[i].oid, str) != 0)
++      if (grub_strcmp (tv[i].oid, str) != 0)
+       {
+-        fprintf (stderr,
+-                 "%d: strcmp iter %lu: got invalid OID: %s, expected: %s\n",
+-                 __LINE__, (unsigned long) i, str, tv[i].oid);
++        grub_printf ("%d: strcmp iter %lu: got invalid OID: %s, expected: 
%s\n",
++                     __LINE__, (unsigned long) i, str, tv[i].oid);
+         return 1;
+       }
+ 
+diff --git a/grub-core/tests/asn1/tests/object-id-encoding.c 
b/grub-core/tests/asn1/tests/object-id-encoding.c
+index 1a3396986..d02af885a 100644
+--- a/grub-core/tests/asn1/tests/object-id-encoding.c
++++ b/grub-core/tests/asn1/tests/object-id-encoding.c
+@@ -18,11 +18,7 @@
+  *
+  */
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
+-
+-#include "libtasn1.h"
++#include "asn1_test.h"
+ 
+ struct tv
+ {
+@@ -84,10 +80,10 @@ static const struct tv tv[] = {
+ };
+ 
+ int
+-main (int argc, char *argv[])
++test_object_id_encoding(void)
+ {
+   unsigned char der[128];
+-  int ret, der_len, i, j;
++  int ret, der_len, i;
+ 
+   for (i = 0; i < (int) (sizeof (tv) / sizeof (tv[0])); i++)
+     {
+@@ -97,33 +93,21 @@ main (int argc, char *argv[])
+       {
+         if (ret == tv[i].expected_error)
+           continue;
+-        fprintf (stderr,
+-                 "%d: iter %lu, encoding of OID failed: %s\n",
+-                 __LINE__, (unsigned long) i, asn1_strerror (ret));
++        grub_printf ("%d: iter %lu, encoding of OID failed: %s\n",
++                     __LINE__, (unsigned long) i, asn1_strerror(ret));
+         return 1;
+       }
+       else if (ret != tv[i].expected_error)
+-      {
+-        fprintf (stderr,
+-                 "%d: iter %lu, encoding of OID %s succeeded when expecting 
failure\n",
+-                 __LINE__, (unsigned long) i, tv[i].oid);
+-        return 1;
+-      }
++        {
++        grub_printf ("%d: iter %lu, encoding of OID %s succeeded when 
expecting failure\n",
++                     __LINE__, (unsigned long) i, tv[i].oid);
++          return 1;
++        }
+ 
+-      if (der_len != tv[i].der_len || memcmp (der, tv[i].der, der_len) != 0)
++      if (der_len != tv[i].der_len || grub_memcmp(der, tv[i].der, der_len) != 
0)
+       {
+-        fprintf (stderr,
+-                 "%d: iter %lu, re-encoding of OID %s resulted to different 
string (%d vs %d bytes)\n",
+-                 __LINE__, (unsigned long) i, tv[i].oid, der_len,
+-                 tv[i].der_len);
+-        fprintf (stderr, "\nGot:\t\t");
+-        for (j = 0; j < der_len; j++)
+-          fprintf (stderr, "%.2x", der[j]);
+-
+-        fprintf (stderr, "\nExpected:\t");
+-        for (j = 0; j < tv[i].der_len; j++)
+-          fprintf (stderr, "%.2x", tv[i].der[j]);
+-        fprintf (stderr, "\n");
++        grub_printf ("%d: iter %lu, re-encoding of OID %s resulted to 
different string (%d vs %d bytes)\n",
++                     __LINE__, (unsigned long) i, tv[i].oid, der_len, 
tv[i].der_len);
+ 
+         return 1;
+       }
+diff --git a/grub-core/tests/asn1/tests/octet-string.c 
b/grub-core/tests/asn1/tests/octet-string.c
+index 69eb18a62..8c24e67e0 100644
+--- a/grub-core/tests/asn1/tests/octet-string.c
++++ b/grub-core/tests/asn1/tests/octet-string.c
+@@ -20,11 +20,8 @@
+  *
+  */
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
++#include "asn1_test.h"
+ 
+-#include "libtasn1.h"
+ 
+ struct tv
+ {
+@@ -112,15 +109,15 @@ static const struct tv tv[] = {
+ };
+ 
+ int
+-main (int argc, char *argv[])
++test_octet_string (void)
+ {
+   unsigned char str[100];
+   unsigned char der[100];
+   int der_len = sizeof (der);
+   int str_size = sizeof (str);
+   unsigned char *tmp = NULL;
+-  int ret, ret_len, j;
+-  size_t i;
++  int ret, ret_len;
++  grub_size_t i;
+ 
+   for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
+     {
+@@ -135,9 +132,8 @@ main (int argc, char *argv[])
+                               sizeof (str), &str_size);
+         if (ret != tv[i].expected_error)
+           {
+-            fprintf (stderr,
+-                     "%d: asn1_get_octet_der: %s: got %d expected %d\n",
+-                     __LINE__, tv[i].name, ret, tv[i].expected_error);
++            grub_printf ("%d: asn1_get_octet_der: %s: got %d expected %d\n",
++                         __LINE__, tv[i].name, ret, tv[i].expected_error);
+             return 1;
+           }
+         if (tv[i].expected_error)
+@@ -145,28 +141,18 @@ main (int argc, char *argv[])
+ 
+         if (ret_len != tv[i].der_len - 1)
+           {
+-            fprintf (stderr,
+-                     "%d: error in DER, length returned is %d, had %d\n",
+-                     __LINE__, ret_len, tv[i].der_len - 1);
++            grub_printf ("%d: error in DER, length returned is %d, had %d\n",
++                         __LINE__, ret_len, tv[i].der_len - 1);
+             return 1;
+           }
+ 
+         if (str_size != tv[i].len
+-            || memcmp (tv[i].string, str, tv[i].len) != 0)
++            || grub_memcmp (tv[i].string, str, tv[i].len) != 0)
+           {
+-            fprintf (stderr,
+-                     "%d: memcmp: %s: got invalid decoding\n",
+-                     __LINE__, tv[i].name);
+-
+-            fprintf (stderr, "\nGot:\t\t");
+-            for (j = 0; j < str_size; j++)
+-              fprintf (stderr, "%.2x", str[j]);
+-
+-            fprintf (stderr, "\nExpected:\t");
+-            for (j = 0; j < tv[i].len; j++)
+-              fprintf (stderr, "%.2x", tv[i].string[j]);
+-            fprintf (stderr, "\n");
+-            return 1;
++            grub_printf ("%d: memcmp: %s: got invalid decoding\n",
++                         __LINE__, tv[i].name);
++
++              return 1;
+           }
+ 
+         /* Encode */
+@@ -174,10 +160,9 @@ main (int argc, char *argv[])
+         asn1_octet_der (str, str_size, der, &der_len);
+ 
+         if (der_len != tv[i].der_len - 1
+-            || memcmp (tv[i].der_str + 1, der, tv[i].der_len - 1) != 0)
++            || grub_memcmp (tv[i].der_str + 1, der, tv[i].der_len - 1) != 0)
+           {
+-            fprintf (stderr,
+-                     "encoding: %s: got invalid encoding\n", tv[i].name);
++            grub_printf ("encoding: %s: got invalid encoding\n", tv[i].name);
+             return 1;
+           }
+       }
+@@ -189,10 +174,9 @@ main (int argc, char *argv[])
+                               (unsigned int *) &der_len);
+       if (ret != tv[i].expected_error)
+       {
+-        fprintf (stderr,
+-                 "%d: asn1_decode_simple_ber: %s: got %s expected %s\n",
+-                 __LINE__, tv[i].name, asn1_strerror (ret),
+-                 asn1_strerror (tv[i].expected_error));
++        grub_printf ("%d: asn1_decode_simple_ber: %s: got %s expected %s\n",
++                     __LINE__, tv[i].name, asn1_strerror(ret),
++                     asn1_strerror(tv[i].expected_error));
+         return 1;
+       }
+       if (tv[i].expected_error)
+@@ -200,28 +184,17 @@ main (int argc, char *argv[])
+ 
+       if (der_len != tv[i].der_len)
+       {
+-        fprintf (stderr,
+-                 "%d: error: %s: DER, length returned is %d, had %d\n",
+-                 __LINE__, tv[i].name, der_len, tv[i].der_len);
++        grub_printf ("%d: error: %s: DER, length returned is %d, had %d\n",
++                     __LINE__, tv[i].name, der_len, tv[i].der_len);
+         return 1;
+       }
+ 
+-      if (str_size != tv[i].len || memcmp (tv[i].string, tmp, tv[i].len) != 0)
++      if (str_size != tv[i].len || grub_memcmp (tv[i].string, tmp, tv[i].len) 
!= 0)
+       {
+-        fprintf (stderr,
+-                 "%d: memcmp: %s: got invalid decoding\n",
+-                 __LINE__, tv[i].name);
+-        fprintf (stderr, "\nGot:\t\t");
+-        for (j = 0; j < str_size; j++)
+-          fprintf (stderr, "%.2x", tmp[j]);
+-
+-        fprintf (stderr, "\nExpected:\t");
+-        for (j = 0; j < tv[i].len; j++)
+-          fprintf (stderr, "%.2x", tv[i].string[j]);
+-        fprintf (stderr, "\n");
+-        return 1;
++        grub_printf ("%d: memcmp: %s: got invalid decoding\n", __LINE__, 
tv[i].name);
++          return 1;
+       }
+-      free (tmp);
++      grub_free (tmp);
+       tmp = NULL;
+ 
+     }
+diff --git a/grub-core/tests/asn1/tests/reproducers.c 
b/grub-core/tests/asn1/tests/reproducers.c
+index a09d8b021..750c15416 100644
+--- a/grub-core/tests/asn1/tests/reproducers.c
++++ b/grub-core/tests/asn1/tests/reproducers.c
+@@ -22,13 +22,9 @@
+ /* Description: run reproducers for several fixed issues        */
+ /****************************************************************/
+ 
+-#include <stdio.h>
+-#include <string.h>
+-#include <stdlib.h>
++#include "asn1_test.h"
+ 
+-#include <libtasn1.h>
+-
+-#include <int.h>
++#define CONST_DOWN        (1U<<29)
+ 
+ /* produces endless loop (fixed by d4b624b2):
+  * The following translates into a single node with all pointers
+@@ -53,21 +49,18 @@ const asn1_static_node tab[] = {
+ };
+ 
+ int
+-main (int argc, char *argv[])
++test_reproducers (void)
+ {
+-  int result, verbose = 0;
++  int result;
+   asn1_node definitions = NULL;
+   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
+ 
+-  if (argc > 1)
+-    verbose = 1;
+-
+   result = asn1_array2tree (endless_asn1_tab, &definitions, errorDescription);
+   if (result != ASN1_SUCCESS)
+     {
+-      asn1_perror (result);
+-      printf ("ErrorDescription = %s\n\n", errorDescription);
+-      exit (EXIT_FAILURE);
++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
++                 asn1_strerror (result), errorDescription);
++      return 1;
+     }
+ 
+   asn1_delete_structure (&definitions);
+@@ -76,15 +69,12 @@ main (int argc, char *argv[])
+   result = asn1_array2tree (tab, &definitions, errorDescription);
+   if (result != ASN1_SUCCESS)
+     {
+-      asn1_perror (result);
+-      printf ("ErrorDescription = %s\n\n", errorDescription);
+-      exit (EXIT_FAILURE);
++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
++                 asn1_strerror (result), errorDescription);
++      return 1;
+     }
+ 
+   asn1_delete_structure (&definitions);
+ 
+-  if (verbose)
+-    printf ("Success\n");
+-
+-  exit (EXIT_SUCCESS);
++  return 0;
+ }
+-- 
+2.35.3
+
-- 
2.35.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]