Configurations/common.tmpl: Rework dependency resolution
[openssl.git] / test / params_test.c
index 7fc21da5782fceabe7d8fbd3b45a6ee40527c3fc..339a51694f6311052294dff4184a7e6245a587f3 100644 (file)
@@ -46,13 +46,13 @@ struct object_st {
     double p2;
     /*
      * Documented as an arbitrarly large unsigned integer.
-     * The data size must be large enough to accomodate.
+     * The data size must be large enough to accommodate.
      * Assumed data type OSSL_PARAM_UNSIGNED_INTEGER
      */
     BIGNUM *p3;
     /*
      * Documented as a C string.
-     * The data size must be large enough to accomodate.
+     * The data size must be large enough to accommodate.
      * Assumed data type OSSL_PARAM_UTF8_STRING
      */
     char *p4;
@@ -143,47 +143,44 @@ static int raw_set_params(void *vobj, const OSSL_PARAM *params)
                 return 0;
         } else if (strcmp(params->key, "p5") == 0) {
             strncpy(obj->p5, params->data, params->data_size);
+            obj->p5_l = strlen(obj->p5) + 1;
         } else if (strcmp(params->key, "p6") == 0) {
             obj->p6 = *(const char **)params->data;
+            obj->p6_l = params->data_size;
         }
 
     return 1;
 }
 
-static int raw_get_params(void *vobj, const OSSL_PARAM *params)
+static int raw_get_params(void *vobj, OSSL_PARAM *params)
 {
     struct object_st *obj = vobj;
 
     for (; params->key != NULL; params++)
         if (strcmp(params->key, "p1") == 0) {
-            if (params->return_size != NULL)
-                *params->return_size = sizeof(obj->p1);
+            params->return_size = sizeof(obj->p1);
             *(int *)params->data = obj->p1;
         } else if (strcmp(params->key, "p2") == 0) {
-            if (params->return_size != NULL)
-                *params->return_size = sizeof(obj->p2);
+            params->return_size = sizeof(obj->p2);
             *(double *)params->data = obj->p2;
         } else if (strcmp(params->key, "p3") == 0) {
             size_t bytes = BN_num_bytes(obj->p3);
 
-            if (params->return_size != NULL)
-                *params->return_size = bytes;
+            params->return_size = bytes;
             if (!TEST_size_t_ge(params->data_size, bytes))
                 return 0;
             BN_bn2nativepad(obj->p3, params->data, bytes);
         } else if (strcmp(params->key, "p4") == 0) {
             size_t bytes = strlen(obj->p4) + 1;
 
-            if (params->return_size != NULL)
-                *params->return_size = bytes;
+            params->return_size = bytes;
             if (!TEST_size_t_ge(params->data_size, bytes))
                 return 0;
             strcpy(params->data, obj->p4);
         } else if (strcmp(params->key, "p5") == 0) {
             size_t bytes = strlen(obj->p5) + 1;
 
-            if (params->return_size != NULL)
-                *params->return_size = bytes;
+            params->return_size = bytes;
             if (!TEST_size_t_ge(params->data_size, bytes))
                 return 0;
             strcpy(params->data, obj->p5);
@@ -196,8 +193,7 @@ static int raw_get_params(void *vobj, const OSSL_PARAM *params)
              */
             size_t bytes = strlen(obj->p6) + 1;
 
-            if (params->return_size != NULL)
-                *params->return_size = bytes;
+            params->return_size = bytes;
             *(const char **)params->data = obj->p6;
         }
 
@@ -213,38 +209,41 @@ static int api_set_params(void *vobj, const OSSL_PARAM *params)
     struct object_st *obj = vobj;
     const OSSL_PARAM *p = NULL;
 
-    if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
+    if ((p = OSSL_PARAM_locate_const(params, "p1")) != NULL
         && !TEST_true(OSSL_PARAM_get_int(p, &obj->p1)))
         return 0;
-    if ((p = OSSL_PARAM_locate(params, "p2")) != NULL
+    if ((p = OSSL_PARAM_locate_const(params, "p2")) != NULL
         && !TEST_true(OSSL_PARAM_get_double(p, &obj->p2)))
         return 0;
-    if ((p = OSSL_PARAM_locate(params, "p3")) != NULL
+    if ((p = OSSL_PARAM_locate_const(params, "p3")) != NULL
         && !TEST_true(OSSL_PARAM_get_BN(p, &obj->p3)))
         return 0;
-    if ((p = OSSL_PARAM_locate(params, "p4")) != NULL) {
+    if ((p = OSSL_PARAM_locate_const(params, "p4")) != NULL) {
         OPENSSL_free(obj->p4);
         obj->p4 = NULL;
         /* If the value pointer is NULL, we get it automatically allocated */
         if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &obj->p4, 0)))
             return 0;
     }
-    if ((p = OSSL_PARAM_locate(params, "p5")) != NULL) {
+    if ((p = OSSL_PARAM_locate_const(params, "p5")) != NULL) {
         char *p5_ptr = obj->p5;
         if (!TEST_true(OSSL_PARAM_get_utf8_string(p, &p5_ptr, sizeof(obj->p5))))
             return 0;
+        obj->p5_l = strlen(obj->p5) + 1;
+    }
+    if ((p = OSSL_PARAM_locate_const(params, "p6")) != NULL) {
+        if (!TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
+            return 0;
+        obj->p6_l = strlen(obj->p6) + 1;
     }
-    if ((p = OSSL_PARAM_locate(params, "p6")) != NULL
-        && !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &obj->p6)))
-        return 0;
 
     return 1;
 }
 
-static int api_get_params(void *vobj, const OSSL_PARAM *params)
+static int api_get_params(void *vobj, OSSL_PARAM *params)
 {
     struct object_st *obj = vobj;
-    const OSSL_PARAM *p = NULL;
+    OSSL_PARAM *p = NULL;
 
     if ((p = OSSL_PARAM_locate(params, "p1")) != NULL
         && !TEST_true(OSSL_PARAM_set_int(p, obj->p1)))
@@ -274,7 +273,7 @@ static int api_get_params(void *vobj, const OSSL_PARAM *params)
  */
 struct provider_dispatch_st {
     int (*set_params)(void *obj, const OSSL_PARAM *params);
-    int (*get_params)(void *obj, const OSSL_PARAM *params);
+    int (*get_params)(void *obj, OSSL_PARAM *params);
 };
 
 /* "raw" provider */
@@ -294,7 +293,7 @@ static const struct provider_dispatch_st provider_api = {
 
 /* In all our tests, these are variables that get manipulated as parameters
  *
- * These arrays consistenly do nothing with the "p2" parameter, and
+ * These arrays consistently do nothing with the "p2" parameter, and
  * always include a "foo" parameter.  This is to check that the
  * set_params and get_params calls ignore the lack of parameters that
  * the application isn't interested in, as well as ignore parameters
@@ -305,15 +304,10 @@ static int app_p1;                    /* "p1" */
 static double app_p2;                 /* "p2" is ignored */
 static BIGNUM *app_p3 = NULL;         /* "p3" */
 static unsigned char bignumbin[4096]; /* "p3" */
-static size_t bignumbin_l;            /* "p3" */
 static char app_p4[256];              /* "p4" */
-static size_t app_p4_l;               /* "p4" */
 static char app_p5[256];              /* "p5" */
-static size_t app_p5_l;               /* "p5" */
 static const char *app_p6 = NULL;     /* "p6" */
-static size_t app_p6_l;               /* "p6" */
 static unsigned char foo[1];          /* "foo" */
-static size_t foo_l;                  /* "foo" */
 
 #define app_p1_init 17           /* A random number */
 #define app_p2_init 47.11        /* Another random number */
@@ -341,14 +335,10 @@ static int init_app_variables(void)
     if (!BN_hex2bn(&app_p3, app_p3_init)
         || (l = BN_bn2nativepad(app_p3, bignumbin, sizeof(bignumbin))) < 0)
         return 0;
-    bignumbin_l = (size_t)l;
     strcpy(app_p4, app_p4_init);
-    app_p4_l = sizeof(app_p4_init);
     strcpy(app_p5, app_p5_init);
-    app_p5_l = sizeof(app_p5_init);
     app_p6 = app_p6_init;
     foo[0] = app_foo_init;
-    foo_l = sizeof(app_foo_init);
 
     return 1;
 }
@@ -358,28 +348,26 @@ static int init_app_variables(void)
  */
 
 /* An array of OSSL_PARAM, specific in the most raw manner possible */
-static const OSSL_PARAM static_raw_params[] = {
-    { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), NULL },
-    { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin),
-      &bignumbin_l },
-    { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), &app_p4_l },
-    { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), &app_p5_l },
-    { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6), &app_p6_l },
-    { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l },
-    { NULL, 0, NULL, 0, NULL }
+static OSSL_PARAM static_raw_params[] = {
+    { "p1", OSSL_PARAM_INTEGER, &app_p1, sizeof(app_p1), 0 },
+    { "p3", OSSL_PARAM_UNSIGNED_INTEGER, &bignumbin, sizeof(bignumbin), 0 },
+    { "p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4), 0 },
+    { "p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5), 0 },
+    /* sizeof(app_p6_init), because we know that's what we're using */
+    { "p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init), 0 },
+    { "foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), 0 },
+    { NULL, 0, NULL, 0, 0 }
 };
 
 /* The same array of OSSL_PARAM, specified with the macros from params.h */
-static const OSSL_PARAM static_api_params[] = {
+static OSSL_PARAM static_api_params[] = {
     OSSL_PARAM_int("p1", &app_p1),
-    OSSL_PARAM_SIZED_BN("p3", &bignumbin, sizeof(bignumbin), bignumbin_l),
-    OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING,
-                    &app_p4, sizeof(app_p4), &app_p4_l),
-    OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING,
-                    &app_p5, sizeof(app_p5), &app_p5_l),
-    OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR,
-                    &app_p6, sizeof(app_p6), &app_p6_l),
-    OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo), &foo_l),
+    OSSL_PARAM_BN("p3", &bignumbin, sizeof(bignumbin)),
+    OSSL_PARAM_DEFN("p4", OSSL_PARAM_UTF8_STRING, &app_p4, sizeof(app_p4)),
+    OSSL_PARAM_DEFN("p5", OSSL_PARAM_UTF8_STRING, &app_p5, sizeof(app_p5)),
+    /* sizeof(app_p6_init), because we know that's what we're using */
+    OSSL_PARAM_DEFN("p6", OSSL_PARAM_UTF8_PTR, &app_p6, sizeof(app_p6_init)),
+    OSSL_PARAM_DEFN("foo", OSSL_PARAM_OCTET_STRING, &foo, sizeof(foo)),
     OSSL_PARAM_END
 };
 
@@ -391,26 +379,24 @@ static OSSL_PARAM *construct_api_params(void)
 {
     size_t n = 0;
     static OSSL_PARAM params[10];
-    OSSL_PARAM param_end = OSSL_PARAM_END;
 
-    params[n++] = OSSL_PARAM_construct_int("p1", &app_p1, NULL);
-    params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin),
-                                          &bignumbin_l);
-    params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4, sizeof(app_p4),
-                                                   &app_p4_l);
+    params[n++] = OSSL_PARAM_construct_int("p1", &app_p1);
+    params[n++] = OSSL_PARAM_construct_BN("p3", bignumbin, sizeof(bignumbin));
+    params[n++] = OSSL_PARAM_construct_utf8_string("p4", app_p4,
+                                                   sizeof(app_p4));
     params[n++] = OSSL_PARAM_construct_utf8_string("p5", app_p5,
-                                                   sizeof(app_p5), &app_p5_l);
+                                                   sizeof(app_p5));
+    /* sizeof(app_p6_init), because we know that's what we're using */
     params[n++] = OSSL_PARAM_construct_utf8_ptr("p6", (char **)&app_p6,
-                                                &app_p6_l);
-    params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo),
-                                                    &foo_l);
-    params[n++] = param_end;
+                                                sizeof(app_p6_init));
+    params[n++] = OSSL_PARAM_construct_octet_string("foo", &foo, sizeof(foo));
+    params[n++] = OSSL_PARAM_construct_end();
 
     return params;
 }
 
 struct param_owner_st {
-    const OSSL_PARAM *static_params;
+    OSSL_PARAM *static_params;
     OSSL_PARAM *(*constructed_params)(void);
 };
 
@@ -445,12 +431,12 @@ static struct {
 };
 
 /* Generic tester of combinations of "providers" and params */
-static int test_case_variant(const OSSL_PARAM *params,
-                             const struct provider_dispatch_st *prov)
+static int test_case_variant(OSSL_PARAM *params, const struct provider_dispatch_st *prov)
 {
     BIGNUM *verify_p3 = NULL;
     void *obj = NULL;
     int errcnt = 0;
+    OSSL_PARAM *p;
 
     /*
      * Initialize
@@ -469,14 +455,20 @@ static int test_case_variant(const OSSL_PARAM *params,
 
     if (!TEST_true(prov->get_params(obj, params))
         || !TEST_int_eq(app_p1, p1_init)        /* "provider" value */
-        || !TEST_ulong_eq(app_p2, app_p2_init)  /* Should remain untouched */
-        || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
+        || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
+        || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
         || !TEST_BN_eq(app_p3, verify_p3)       /* "provider" value */
         || !TEST_str_eq(app_p4, p4_init)        /* "provider" value */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
+        || !TEST_size_t_eq(p->return_size, sizeof(p5_init)) /* "provider" value */
         || !TEST_str_eq(app_p5, p5_init)        /* "provider" value */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
+        || !TEST_size_t_eq(p->return_size, sizeof(p6_init)) /* "provider" value */
         || !TEST_str_eq(app_p6, p6_init)        /* "provider" value */
         || !TEST_char_eq(foo[0], app_foo_init)  /* Should remain untouched */
-        || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo"))
+        || !TEST_int_eq(p->return_size, 0))
         errcnt++;
 
     /*
@@ -491,10 +483,11 @@ static int test_case_variant(const OSSL_PARAM *params,
         struct object_st *sneakpeek = obj;
 
         if (!TEST_int_eq(sneakpeek->p1, app_p1)         /* app value set */
-            || !TEST_ulong_eq(sneakpeek->p2, p2_init) /* Should remain untouched */
+            || !TEST_double_eq(sneakpeek->p2, p2_init)  /* Should remain untouched */
             || !TEST_BN_eq(sneakpeek->p3, app_p3)       /* app value set */
             || !TEST_str_eq(sneakpeek->p4, app_p4)      /* app value set */
-            || !TEST_str_eq(sneakpeek->p5, app_p5))     /* app value set */
+            || !TEST_str_eq(sneakpeek->p5, app_p5)      /* app value set */
+            || !TEST_str_eq(sneakpeek->p6, app_p6))     /* app value set */
             errcnt++;
     }
 
@@ -512,14 +505,22 @@ static int test_case_variant(const OSSL_PARAM *params,
 
     if (!TEST_true(prov->get_params(obj, params))
         || !TEST_int_eq(app_p1, app_p1_init)    /* app value */
-        || !TEST_ulong_eq(app_p2, app_p2_init)  /* Should remain untouched */
-        || !TEST_ptr(BN_native2bn(bignumbin, bignumbin_l, app_p3))
+        || !TEST_double_eq(app_p2, app_p2_init) /* Should remain untouched */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "p3"))
+        || !TEST_ptr(BN_native2bn(bignumbin, p->return_size, app_p3))
         || !TEST_BN_eq(app_p3, verify_p3)       /* app value */
         || !TEST_str_eq(app_p4, app_p4_init)    /* app value */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "p5"))
+        || !TEST_size_t_eq(p->return_size,
+                           sizeof(app_p5_init)) /* app value */
         || !TEST_str_eq(app_p5, app_p5_init)    /* app value */
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "p6"))
+        || !TEST_size_t_eq(p->return_size,
+                           sizeof(app_p6_init)) /* app value */
         || !TEST_str_eq(app_p6, app_p6_init)    /* app value */
         || !TEST_char_eq(foo[0], app_foo_init)  /* Should remain untouched */
-        || !TEST_int_eq(foo_l, sizeof(app_foo_init)))
+        || !TEST_ptr(p = OSSL_PARAM_locate(params, "foo"))
+        || !TEST_int_eq(p->return_size, 0))
         errcnt++;
 
  fin: