Add a test for converting a property list to a string
authorMatt Caswell <matt@openssl.org>
Fri, 7 May 2021 16:13:05 +0000 (17:13 +0100)
committerMatt Caswell <matt@openssl.org>
Thu, 20 May 2021 08:29:30 +0000 (09:29 +0100)
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15242)

test/property_test.c

index 3682474bd216466d06a4937174bdf9fc5db792f0..94540bc7769d1da8ed8af6bdaed02c130a3bafe6 100644 (file)
@@ -435,6 +435,61 @@ err:
     return ret;
 }
 
+static int test_property_list_to_string(void)
+{
+    OSSL_PROPERTY_LIST *pl = NULL;
+    int ret = 0;
+    struct props_list_str {
+        const char *in;
+        const char *out;
+    } props[] = {
+        { "fips=yes", "fips=yes" },
+        { "fips!=yes", "fips!=yes" },
+        { "fips = yes", "fips=yes" },
+        { "fips", "fips=yes" },
+        { "fips=no", "fips=no" },
+        { "-fips", "-fips" },
+        { "?fips=yes", "?fips=yes" },
+        { "fips=yes,provider=fips", "fips=yes,provider=fips" },
+        { "fips = yes , provider = fips", "fips=yes,provider=fips" },
+        { "fips=yes,provider!=fips", "fips=yes,provider!=fips" },
+        { "fips=yes,?provider=fips", "fips=yes,?provider=fips" },
+        { "fips=yes,-provider", "fips=yes,-provider" },
+          /* foo is an unknown internal name */
+        { "foo=yes,fips=yes", "fips=yes"},
+        { "", "" },
+        { NULL, "" }
+    };
+    size_t i, bufsize;
+    char *buf = NULL;
+
+    for (i = 0; i < OSSL_NELEM(props); i++) {
+        if (props[i].in != NULL
+                && !TEST_ptr(pl = ossl_parse_query(NULL, props[i].in, 1)))
+            goto err;
+        bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
+        if (!TEST_size_t_gt(bufsize, 0))
+            goto err;
+        buf = OPENSSL_malloc(bufsize);
+        if (!TEST_ptr(buf)
+                || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
+                                                                bufsize),
+                                   bufsize)
+                || !TEST_str_eq(props[i].out, buf)
+                || !TEST_size_t_eq(bufsize, strlen(props[i].out) + 1))
+            goto err;
+        OPENSSL_free(buf);
+        buf = NULL;
+        ossl_property_free(pl);
+        pl = NULL;
+    }
+
+    ret = 1;
+ err:
+    OPENSSL_free(buf);
+    ossl_property_free(pl);
+    return ret;
+}
 
 int setup_tests(void)
 {
@@ -448,5 +503,6 @@ int setup_tests(void)
     ADD_TEST(test_property);
     ADD_TEST(test_query_cache_stochastic);
     ADD_TEST(test_fips_mode);
+    ADD_TEST(test_property_list_to_string);
     return 1;
 }