summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/param_build_test.c193
-rw-r--r--test/params_api_test.c89
2 files changed, 251 insertions, 31 deletions
diff --git a/test/param_build_test.c b/test/param_build_test.c
index 270cb45b56..6419582e11 100644
--- a/test/param_build_test.c
+++ b/test/param_build_test.c
@@ -14,10 +14,12 @@
#include "internal/nelem.h"
#include "testutil.h"
-static int template_public_test(void)
+static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END };
+
+static int template_public_test(int tstid)
{
OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
- OSSL_PARAM *params = NULL, *p;
+ OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
BIGNUM *bn = NULL, *bn_res = NULL;
int i;
long int l;
@@ -30,7 +32,6 @@ static int template_public_test(void)
int res = 0;
if (!TEST_ptr(bld)
- || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
|| !TEST_true(OSSL_PARAM_BLD_push_long(bld, "l", 42))
|| !TEST_true(OSSL_PARAM_BLD_push_int32(bld, "i32", 1532))
|| !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
@@ -43,9 +44,31 @@ static int template_public_test(void)
sizeof("foo")))
|| !TEST_true(OSSL_PARAM_BLD_push_utf8_ptr(bld, "utf8_p", "bar-boom",
0))
- || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
- /* Check int */
- || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
+ || !TEST_true(OSSL_PARAM_BLD_push_int(bld, "i", -6))
+ || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
+ goto err;
+
+ switch(tstid) {
+ case 0:
+ params = params_blt;
+ break;
+ case 1:
+ params = OSSL_PARAM_merge(params_blt, params_empty);
+ break;
+ case 2:
+ params = OSSL_PARAM_dup(params_blt);
+ break;
+ case 3:
+ p1 = OSSL_PARAM_merge(params_blt, params_empty);
+ params = OSSL_PARAM_dup(p1);
+ break;
+ default:
+ p1 = OSSL_PARAM_dup(params_blt);
+ params = OSSL_PARAM_merge(p1, params_empty);
+ break;
+ }
+ /* Check int */
+ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|| !TEST_true(OSSL_PARAM_get_int(p, &i))
|| !TEST_str_eq(p->key, "i")
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_INTEGER)
@@ -104,7 +127,10 @@ static int template_public_test(void)
goto err;
res = 1;
err:
- OSSL_PARAM_BLD_free_params(params);
+ OPENSSL_free(p1);
+ if (params != params_blt)
+ OPENSSL_free(params);
+ OSSL_PARAM_BLD_free_params(params_blt);
OSSL_PARAM_BLD_free(bld);
OPENSSL_free(utf);
BN_free(bn);
@@ -112,7 +138,7 @@ err:
return res;
}
-static int template_private_test(void)
+static int template_private_test(int tstid)
{
int *data1 = NULL, *data2 = NULL, j;
const int data1_num = 12;
@@ -120,7 +146,7 @@ static int template_private_test(void)
const int data2_num = 5;
const int data2_size = data2_num * sizeof(int);
OSSL_PARAM_BLD *bld = NULL;
- OSSL_PARAM *params = NULL, *p;
+ OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
unsigned int i;
unsigned long int l;
uint32_t i32;
@@ -151,9 +177,29 @@ static int template_private_test(void)
data1_size))
|| !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld, "oct_p", data2,
data2_size))
- || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
- /* Check unsigned int */
- || !TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
+ || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
+ goto err;
+ switch(tstid) {
+ case 0:
+ params = params_blt;
+ break;
+ case 1:
+ params = OSSL_PARAM_merge(params_blt, params_empty);
+ break;
+ case 2:
+ params = OSSL_PARAM_dup(params_blt);
+ break;
+ case 3:
+ p1 = OSSL_PARAM_merge(params_blt, params_empty);
+ params = OSSL_PARAM_dup(p1);
+ break;
+ default:
+ p1 = OSSL_PARAM_dup(params_blt);
+ params = OSSL_PARAM_merge(p1, params_empty);
+ break;
+ }
+ /* Check unsigned int */
+ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
|| !TEST_false(CRYPTO_secure_allocated(p->data))
|| !TEST_true(OSSL_PARAM_get_uint(p, &i))
|| !TEST_str_eq(p->key, "i")
@@ -216,7 +262,10 @@ static int template_private_test(void)
goto err;
res = 1;
err:
- OSSL_PARAM_BLD_free_params(params);
+ OSSL_PARAM_free(p1);
+ if (params != params_blt)
+ OSSL_PARAM_free(params);
+ OSSL_PARAM_BLD_free_params(params_blt);
OSSL_PARAM_BLD_free(bld);
OPENSSL_secure_free(data1);
OPENSSL_secure_free(data2);
@@ -268,12 +317,126 @@ err:
return res;
}
+static int builder_merge_test(void)
+{
+ static int data1[] = { 2, 3, 5, 7, 11, 15, 17 };
+ static unsigned char data2[] = { 2, 4, 6, 8, 10 };
+ OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
+ OSSL_PARAM_BLD *bld2 = OSSL_PARAM_BLD_new();
+ OSSL_PARAM *params = NULL, *params_blt = NULL, *params2_blt = NULL, *p;
+ unsigned int i;
+ unsigned long int l;
+ uint32_t i32;
+ uint64_t i64;
+ size_t st;
+ BIGNUM *bn_priv = NULL, *bn_priv_res = NULL;
+ BIGNUM *bn_pub = NULL, *bn_pub_res = NULL;
+ int res = 0;
+
+ if (!TEST_ptr(bld)
+ || !TEST_true(OSSL_PARAM_BLD_push_uint(bld, "i", 6))
+ || !TEST_true(OSSL_PARAM_BLD_push_ulong(bld, "l", 42))
+ || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
+ || !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
+ || !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
+ || !TEST_ptr(bn_priv = BN_secure_new())
+ || !TEST_true(BN_set_word(bn_priv, 1729))
+ || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber_priv", bn_priv))
+ || !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
+ goto err;
+
+ if (!TEST_ptr(bld2)
+ || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld2, "oct_s", data1,
+ sizeof(data1)))
+ || !TEST_true(OSSL_PARAM_BLD_push_octet_ptr(bld2, "oct_p", data2,
+ sizeof(data2)))
+ || !TEST_true(OSSL_PARAM_BLD_push_uint32(bld2, "i32", 99))
+ || !TEST_ptr(bn_pub = BN_new())
+ || !TEST_true(BN_set_word(bn_pub, 0x42))
+ || !TEST_true(OSSL_PARAM_BLD_push_BN(bld2, "bignumber_pub", bn_pub))
+ || !TEST_ptr(params2_blt = OSSL_PARAM_BLD_to_param(bld2)))
+ goto err;
+
+ if (!TEST_ptr(params = OSSL_PARAM_merge(params_blt, params2_blt)))
+ goto err;
+
+ if (!TEST_ptr(p = OSSL_PARAM_locate(params, "i"))
+ || !TEST_true(OSSL_PARAM_get_uint(p, &i))
+ || !TEST_str_eq(p->key, "i")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(int))
+ || !TEST_uint_eq(i, 6)
+ /* Check unsigned int32 */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i32"))
+ || !TEST_true(OSSL_PARAM_get_uint32(p, &i32))
+ || !TEST_str_eq(p->key, "i32")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(int32_t))
+ || !TEST_uint_eq((unsigned int)i32, 99)
+ /* Check unsigned int64 */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "i64"))
+ || !TEST_str_eq(p->key, "i64")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(int64_t))
+ || !TEST_true(OSSL_PARAM_get_uint64(p, &i64))
+ || !TEST_ulong_eq((unsigned long)i64, 9999999)
+ /* Check unsigned long int */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "l"))
+ || !TEST_str_eq(p->key, "l")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(unsigned long int))
+ || !TEST_true(OSSL_PARAM_get_ulong(p, &l))
+ || !TEST_ulong_eq(l, 42)
+ /* Check size_t */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "st"))
+ || !TEST_str_eq(p->key, "st")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_size_t_eq(p->data_size, sizeof(size_t))
+ || !TEST_true(OSSL_PARAM_get_size_t(p, &st))
+ || !TEST_size_t_eq(st, 65537)
+ /* Check octet string */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_s"))
+ || !TEST_str_eq(p->key, "oct_s")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_STRING)
+ || !TEST_mem_eq(p->data, p->data_size, data1, sizeof(data1))
+ /* Check octet pointer */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "oct_p"))
+ || !TEST_str_eq(p->key, "oct_p")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
+ || !TEST_mem_eq(*(void **)p->data, p->data_size, data2, sizeof(data2))
+ /* Check BN */
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_pub"))
+ || !TEST_str_eq(p->key, "bignumber_pub")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_true(OSSL_PARAM_get_BN(p, &bn_pub_res))
+ || !TEST_int_eq(BN_cmp(bn_pub_res, bn_pub), 0)
+ || !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber_priv"))
+ || !TEST_str_eq(p->key, "bignumber_priv")
+ || !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
+ || !TEST_true(OSSL_PARAM_get_BN(p, &bn_priv_res))
+ || !TEST_int_eq(BN_cmp(bn_priv_res, bn_priv), 0))
+ goto err;
+ res = 1;
+err:
+ OSSL_PARAM_free(params);
+ OSSL_PARAM_free(params_blt);
+ OSSL_PARAM_free(params2_blt);
+ OSSL_PARAM_BLD_free(bld);
+ OSSL_PARAM_BLD_free(bld2);
+ BN_free(bn_priv);
+ BN_free(bn_priv_res);
+ BN_free(bn_pub);
+ BN_free(bn_pub_res);
+ return res;
+}
+
int setup_tests(void)
{
- ADD_TEST(template_public_test);
+ ADD_ALL_TESTS(template_public_test, 5);
/* Only run the secure memory testing if we have secure memory available */
if (CRYPTO_secure_malloc_init(1<<16, 16))
- ADD_TEST(template_private_test);
+ ADD_ALL_TESTS(template_private_test, 5);
ADD_TEST(builder_limit_test);
+ ADD_TEST(builder_merge_test);
return 1;
}
diff --git a/test/params_api_test.c b/test/params_api_test.c
index e47c2643ad..48e2f8920a 100644
--- a/test/params_api_test.c
+++ b/test/params_api_test.c
@@ -455,7 +455,7 @@ static int test_param_real(void)
&& TEST_double_eq(p, 3.14159);
}
-static int test_param_construct(void)
+static int test_param_construct(int tstid)
{
static const char *int_names[] = {
"int", "long", "int32", "int64"
@@ -467,6 +467,10 @@ static int test_param_construct(void)
0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23,
0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22
};
+ OSSL_PARAM *p = NULL, *p1 = NULL;
+ static const OSSL_PARAM params_empty[] = {
+ OSSL_PARAM_END
+ };
OSSL_PARAM params[20];
char buf[100], buf2[100], *bufp, *bufp2;
unsigned char ubuf[100];
@@ -501,13 +505,29 @@ static int test_param_construct(void)
params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, 0);
params[n] = OSSL_PARAM_construct_end();
+ switch(tstid) {
+ case 0:
+ p = params;
+ break;
+ case 1:
+ p = OSSL_PARAM_merge(params, params_empty);
+ break;
+ case 2:
+ p = OSSL_PARAM_dup(params);
+ break;
+ default:
+ p1 = OSSL_PARAM_dup(params);
+ p = OSSL_PARAM_merge(p1, params_empty);
+ break;
+ }
+
/* Search failure */
- if (!TEST_ptr_null(OSSL_PARAM_locate(params, "fnord")))
+ if (!TEST_ptr_null(OSSL_PARAM_locate(p, "fnord")))
goto err;
/* All signed integral types */
for (j = 0; j < OSSL_NELEM(int_names); j++) {
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, int_names[j]))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, int_names[j]))
|| !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j)))
|| !TEST_true(OSSL_PARAM_get_int64(cp, &i64))
|| !TEST_size_t_eq(cp->data_size, cp->return_size)
@@ -518,7 +538,7 @@ static int test_param_construct(void)
}
/* All unsigned integral types */
for (j = 0; j < OSSL_NELEM(uint_names); j++) {
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, uint_names[j]))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, uint_names[j]))
|| !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j)))
|| !TEST_true(OSSL_PARAM_get_uint64(cp, &u64))
|| !TEST_size_t_eq(cp->data_size, cp->return_size)
@@ -528,15 +548,16 @@ static int test_param_construct(void)
}
}
/* Real */
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "double"))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "double"))
|| !TEST_true(OSSL_PARAM_set_double(cp, 3.14))
|| !TEST_true(OSSL_PARAM_get_double(cp, &d2))
|| !TEST_size_t_eq(cp->return_size, sizeof(double))
- || !TEST_double_eq(d, d2))
+ || !TEST_double_eq(d2, 3.14)
+ || (tstid <= 1 && !TEST_double_eq(d, d2)))
goto err;
/* UTF8 string */
bufp = NULL;
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8str"))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8str"))
|| !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef"))
|| !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
|| !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
@@ -552,15 +573,15 @@ static int test_param_construct(void)
/* UTF8 pointer */
/* Note that the size of a UTF8 string does *NOT* include the NUL byte */
bufp = buf;
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8ptr"))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "utf8ptr"))
|| !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz"))
|| !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
- || !TEST_str_eq(bufp, "tuvwxyz")
|| !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2))
- || !TEST_ptr_eq(bufp2, bufp))
+ || !TEST_str_eq(bufp2, "tuvwxyz")
+ || (tstid <= 1 && !TEST_ptr_eq(bufp2, bufp)))
goto err;
/* OCTET string */
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "octstr"))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octstr"))
|| !TEST_true(OSSL_PARAM_set_octet_string(cp, "abcdefghi",
sizeof("abcdefghi")))
|| !TEST_size_t_eq(cp->return_size, sizeof("abcdefghi")))
@@ -580,19 +601,19 @@ static int test_param_construct(void)
goto err;
/* OCTET pointer */
vp = &l;
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "octptr"))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "octptr"))
|| !TEST_true(OSSL_PARAM_set_octet_ptr(cp, &ul, sizeof(ul)))
|| !TEST_size_t_eq(cp->return_size, sizeof(ul))
- || !TEST_ptr_eq(vp, &ul))
+ || (tstid <= 1 && !TEST_ptr_eq(vp, &ul)))
goto err;
/* Match the return size to avoid trailing garbage bytes */
cp->data_size = cp->return_size;
if (!TEST_true(OSSL_PARAM_get_octet_ptr(cp, (const void **)&vp2, &k))
|| !TEST_size_t_eq(k, sizeof(ul))
- || !TEST_ptr_eq(vp2, vp))
+ || (tstid <= 1 && !TEST_ptr_eq(vp2, vp)))
goto err;
/* BIGNUM */
- if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "bignum"))
+ if (!TEST_ptr(cp = OSSL_PARAM_locate(p, "bignum"))
|| !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL))
|| !TEST_true(OSSL_PARAM_set_BN(cp, bn))
|| !TEST_size_t_eq(cp->data_size, cp->return_size))
@@ -604,6 +625,9 @@ static int test_param_construct(void)
goto err;
ret = 1;
err:
+ if (p != params)
+ OPENSSL_free(p);
+ OPENSSL_free(p1);
OPENSSL_free(vpn);
BN_free(bn);
BN_free(bn2);
@@ -637,6 +661,38 @@ static int test_param_modified(void)
return 1;
}
+static int test_param_copy_null(void)
+{
+ int ret, val;
+ int a = 1, b = 2, i = 0;
+ OSSL_PARAM *cp1 = NULL, *cp2 = NULL, *p;
+ OSSL_PARAM param[3];
+
+ param[i++] = OSSL_PARAM_construct_int("a", &a);
+ param[i++] = OSSL_PARAM_construct_int("b", &b);
+ param[i] = OSSL_PARAM_construct_end();
+
+ ret = TEST_ptr_null(OSSL_PARAM_dup(NULL))
+ && TEST_ptr(cp1 = OSSL_PARAM_merge(NULL, param))
+ && TEST_ptr(p = OSSL_PARAM_locate(cp1, "a"))
+ && TEST_true(OSSL_PARAM_get_int(p, &val))
+ && TEST_int_eq(val, 1)
+ && TEST_ptr(p = OSSL_PARAM_locate(cp1, "b"))
+ && TEST_true(OSSL_PARAM_get_int(p, &val))
+ && TEST_int_eq(val, 2)
+ && TEST_ptr(cp2 = OSSL_PARAM_merge(param, NULL))
+ && TEST_ptr(p = OSSL_PARAM_locate(cp2, "a"))
+ && TEST_true(OSSL_PARAM_get_int(p, &val))
+ && TEST_int_eq(val, 1)
+ && TEST_ptr(p = OSSL_PARAM_locate(cp2, "b"))
+ && TEST_true(OSSL_PARAM_get_int(p, &val))
+ && TEST_int_eq(val, 2)
+ && TEST_ptr_null(OSSL_PARAM_merge(NULL, NULL));
+ OSSL_PARAM_free(cp2);
+ OSSL_PARAM_free(cp1);
+ return ret;
+}
+
int setup_tests(void)
{
ADD_ALL_TESTS(test_param_int, OSSL_NELEM(raw_values));
@@ -651,7 +707,8 @@ int setup_tests(void)
ADD_ALL_TESTS(test_param_uint64, OSSL_NELEM(raw_values));
ADD_ALL_TESTS(test_param_bignum, OSSL_NELEM(raw_values));
ADD_TEST(test_param_real);
- ADD_TEST(test_param_construct);
+ ADD_ALL_TESTS(test_param_construct, 4);
ADD_TEST(test_param_modified);
+ ADD_TEST(test_param_copy_null);
return 1;
}