]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add a stroul test
authorNeil Horman <nhorman@openssl.org>
Fri, 12 Jul 2024 15:01:02 +0000 (11:01 -0400)
committerTomas Mraz <tomas@openssl.org>
Thu, 18 Jul 2024 17:07:52 +0000 (19:07 +0200)
Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24861)

test/build.info
test/recipes/02-test_strtoul.t [new file with mode: 0644]
test/strtoultest.c [new file with mode: 0644]

index 80394085b96dc59369ff61dc09062d9afc4bf054..692b474e883abd3aa3babe0c39d40efb79e4be98 100644 (file)
@@ -64,7 +64,7 @@ IF[{- !$disabled{tests} -}]
           ca_internals_test bio_tfo_test membio_test bio_dgram_test list_test \
           fips_version_test x509_test hpke_test pairwise_fail_test \
           nodefltctxtest evp_xof_test x509_load_cert_file_test bio_meth_test \
-          x509_acert_test x509_req_test
+          x509_acert_test x509_req_test strtoultest
 
   IF[{- !$disabled{'rpk'} -}]
     PROGRAMS{noinst}=rpktest
@@ -1215,6 +1215,10 @@ ENDIF
   INCLUDE[x509_req_test]=../include ../apps/include
   DEPEND[x509_req_test]=../libcrypto libtestutil.a
 
+  SOURCE[strtoultest]=strtoultest.c
+  INCLUDE[strtoultest]=../include ../apps/include
+  DEPEND[strtoultest]=../libcrypto libtestutil.a
+
 {-
    use File::Spec::Functions;
    use File::Basename;
diff --git a/test/recipes/02-test_strtoul.t b/test/recipes/02-test_strtoul.t
new file mode 100644 (file)
index 0000000..bbebf02
--- /dev/null
@@ -0,0 +1,20 @@
+#! /usr/bin/env perl
+# Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the Apache License 2.0 (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+
+use OpenSSL::Test::Simple;
+use OpenSSL::Test;
+use OpenSSL::Test::Utils;
+
+BEGIN {
+    setup("strotoultest");
+}
+
+plan tests => 1;
+
+ok(run(test(["strtoultest"])), "running strtoul test");
diff --git a/test/strtoultest.c b/test/strtoultest.c
new file mode 100644 (file)
index 0000000..267cdf1
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/crypto.h>
+#include <internal/nelem.h>
+#include "testutil.h"
+
+struct strtoul_test_entry {
+    char *input; /* the input string */
+    int base; /* the base we are converting in */
+    unsigned long expect_val; /* the expected value we should get */
+    int expect_err;  /* the expected error we expect to recieve */
+    size_t expect_endptr_offset; /* the expected endptr offset, +1 for NULL */
+};
+
+static struct strtoul_test_entry strtoul_tests[] = {
+    /* pass on conv "0" to 0 */
+    {
+        "0", 0, 0, 1, 1
+    },
+    /* pass on conv "12345" to 12345 */
+    {
+        "12345", 0, 12345, 1, 5
+    },
+    /* pass on conv "0x12345" to 0x12345, base 16 */
+    {
+        "0x12345", 0, 0x12345, 1, 7
+    },
+    /* pass on base 10 translation, endptr points to 'x' */
+    {
+        "0x12345", 10, 0, 1, 1
+    },
+#if defined(__WORDSIZE) && __WORDSIZE == 64
+    /* pass on ULONG_MAX translation */
+    {
+        "18446744073709551615", 0, ULONG_MAX, 1, 20
+    },
+#else
+    {
+        "4294967295", 0, ULONG_MAX, 1, 10
+    },
+#endif
+    /* fail on negative input */
+    {
+        "-1", 0, 0, 0, 0
+    },
+    /* fail on non-numerical input */
+    {
+        "abcd", 0, 0, 0, 0
+    },
+    /* pass on decimal input */
+    {
+        "1.0", 0, 1, 1, 1
+    },
+    /* Fail on decimal input without leading number */
+    {
+        ".1", 0, 0, 0, 0
+    }
+};
+
+static int test_strtoul(int idx)
+{
+    unsigned long val;
+    char *endptr = NULL;
+    int err;
+    struct strtoul_test_entry *test = &strtoul_tests[idx];
+
+    /*
+     * For each test, convert the string to an unsigned long
+     */
+    err = OPENSSL_strtoul(test->input, &endptr, test->base, &val);
+
+    /*
+     * Check to ensure the error returned is expected
+     */
+    if (!TEST_int_eq(err, test->expect_err))
+        return 0;
+    /*
+     * Confirm that the endptr points to where we expect
+     */
+    if (!TEST_ptr_eq(endptr, &test->input[test->expect_endptr_offset]))
+        return 0;
+    /*
+     * And check that we received the proper translated value
+     * Note, we only check the value if the conversion passed
+     */
+    if (test->expect_err == 1) {
+        if (!TEST_ulong_eq(val, test->expect_val))
+            return 0;
+    }
+    return 1;
+
+}
+
+int setup_tests(void)
+{
+    ADD_ALL_TESTS(test_strtoul, OSSL_NELEM(strtoul_tests));
+    return 1;
+}