Fix copyright year issues
[openssl.git] / test / params_api_test.c
index 0a68b9c462e0dba93e6beae2cac1b0c33d7767af..62e458c99323289158e8d2061bb78d91a305f26b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
@@ -78,7 +78,7 @@ static int test_param_type_extra(OSSL_PARAM *param, const unsigned char *cmp,
     const int signd = param->data_type == OSSL_PARAM_INTEGER;
 
     /*
-     * Set the unmodified sentinal directly because there is no param array
+     * Set the unmodified sentinel directly because there is no param array
      * for these tests.
      */
     param->return_size = OSSL_PARAM_UNMODIFIED;
@@ -390,8 +390,8 @@ static int test_param_size_t(int n)
 static int test_param_time_t(int n)
 {
     time_t in, out;
-    unsigned char buf[MAX_LEN], cmp[sizeof(size_t)];
-    const size_t len = raw_values[n].len >= sizeof(size_t)
+    unsigned char buf[MAX_LEN], cmp[sizeof(time_t)];
+    const size_t len = raw_values[n].len >= sizeof(time_t)
                        ? sizeof(time_t) : raw_values[n].len;
     OSSL_PARAM param = OSSL_PARAM_time_t("a", NULL);
 
@@ -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,36 +548,40 @@ 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"))
+        || !TEST_size_t_eq(cp->return_size, sizeof("abcdef") - 1)
         || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0))
-        || !TEST_str_eq(bufp, "abcdef"))
+        || !TEST_str_eq(bufp, "abcdef")) {
+        OPENSSL_free(bufp);
         goto err;
+    }
     OPENSSL_free(bufp);
     bufp = buf2;
     if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2)))
         || !TEST_str_eq(buf2, "abcdef"))
         goto err;
     /* 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"))
-        || !TEST_str_eq(bufp, "tuvwxyz")
+        || !TEST_size_t_eq(cp->return_size, sizeof("tuvwxyz") - 1)
         || !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")))
@@ -577,30 +601,33 @@ 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))
         goto err;
     /* Match the return size to avoid trailing garbage bytes */
     cp->data_size = cp->return_size;
-    if(!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
+    if (!TEST_true(OSSL_PARAM_get_BN(cp, &bn2))
         || !TEST_BN_eq(bn, bn2))
         goto err;
     ret = 1;
 err:
+    if (p != params)
+        OPENSSL_free(p);
+    OPENSSL_free(p1);
     OPENSSL_free(vpn);
     BN_free(bn);
     BN_free(bn2);
@@ -634,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));
@@ -648,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;
 }