summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorPetr Gotthard <petr.gotthard@centrum.cz>2021-02-06 21:47:20 +0100
committerRichard Levitte <levitte@openssl.org>2021-02-09 11:15:55 +0100
commit604b86d8d360e36fc2fc0d1611d05bf38699d297 (patch)
tree5969f02f05293556906af2a19a4a3066b331f8ad /test
parente60a748a13a244e8b13bacca18bad9bb3505aa90 (diff)
Enhanced integer parsing in OSSL_PARAM_allocate_from_text
Fixes #14041 and additional bugs discovered by the newly created tests. This patch: - Introduces support for 0x prefixed integers - Fixes parsing of negative integers (negative numbers were shifted by -2) - Fixes ability to parse maximal unsigned numbers ("too small buffer" error used to be reported incorrectly) - Fixes a memory leak when OSSL_PARAM_allocate_from_text fails leaving a temporary BN allocated Reviewed-by: Matt Caswell <matt@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/14093)
Diffstat (limited to 'test')
-rw-r--r--test/params_test.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/test/params_test.c b/test/params_test.c
index 8ee2e1594c..913df9eb8a 100644
--- a/test/params_test.c
+++ b/test/params_test.c
@@ -541,8 +541,81 @@ static int test_case(int i)
test_cases[i].prov));
}
+/*-
+ * OSSL_PARAM_allocate_from_text() tests
+ * =====================================
+ */
+
+static const OSSL_PARAM params_from_text[] = {
+ OSSL_PARAM_int32("int", NULL),
+ OSSL_PARAM_DEFN("short", OSSL_PARAM_INTEGER, NULL, sizeof(int16_t)),
+ OSSL_PARAM_DEFN("ushort", OSSL_PARAM_UNSIGNED_INTEGER, NULL, sizeof(uint16_t)),
+ OSSL_PARAM_END,
+};
+
+struct int_from_text_test_st {
+ const char *argname;
+ const char *strval;
+ long int intval;
+ int res;
+};
+
+static struct int_from_text_test_st int_from_text_test_cases[] = {
+ { "int", "", 0, 0 },
+ { "int", "0", 0, 1 },
+ { "int", "101", 101, 1 },
+ { "int", "-102", -102, 1 },
+ { "int", "12A", 12, 1 }, /* incomplete */
+ { "int", "0x12B", 0x12B, 1 },
+ { "hexint", "12C", 0x12C, 1 },
+ { "hexint", "0x12D", 0, 1 }, /* zero */
+ /* test check of the target buffer size */
+ { "int", "0x7fffffff", INT32_MAX, 1 },
+ { "int", "2147483647", INT32_MAX, 1 },
+ { "int", "2147483648", 0, 0 }, /* too small buffer */
+ { "int", "-2147483648", INT32_MIN, 1 },
+ { "int", "-2147483649", 0, 0 }, /* too small buffer */
+ { "short", "0x7fff", INT16_MAX, 1 },
+ { "short", "32767", INT16_MAX, 1 },
+ { "short", "32768", 0, 0 }, /* too small buffer */
+ { "ushort", "0xffff", UINT16_MAX, 1 },
+ { "ushort", "65535", UINT16_MAX, 1 },
+ { "ushort", "65536", 0, 0 }, /* too small buffer */
+};
+
+static int check_int_from_text(const struct int_from_text_test_st a)
+{
+ OSSL_PARAM param;
+ long int val = 0;
+ int res;
+
+ if (!OSSL_PARAM_allocate_from_text(&param, params_from_text,
+ a.argname, a.strval, 0, NULL)) {
+ if (a.res)
+ TEST_error("errant %s param \"%s\"", a.argname, a.strval);
+ return !a.res;
+ }
+
+ res = OSSL_PARAM_get_long(&param, &val);
+ OPENSSL_free(param.data);
+
+ if (res ^ a.res || val != a.intval) {
+ TEST_error("errant %s \"%s\" %li != %li",
+ a.argname, a.strval, a.intval, val);
+ return 0;
+ }
+
+ return a.res;
+}
+
+static int test_allocate_from_text(int i)
+{
+ return check_int_from_text(int_from_text_test_cases[i]);
+}
+
int setup_tests(void)
{
ADD_ALL_TESTS(test_case, OSSL_NELEM(test_cases));
+ ADD_ALL_TESTS(test_allocate_from_text, OSSL_NELEM(int_from_text_test_cases));
return 1;
}