property_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdarg.h>
  11. #include <openssl/evp.h>
  12. #include "testutil.h"
  13. #include "internal/nelem.h"
  14. #include "internal/property.h"
  15. #include "../crypto/property/property_local.h"
  16. static int add_property_names(const char *n, ...)
  17. {
  18. va_list args;
  19. int res = 1;
  20. va_start(args, n);
  21. do {
  22. if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0))
  23. res = 0;
  24. } while ((n = va_arg(args, const char *)) != NULL);
  25. va_end(args);
  26. return res;
  27. }
  28. static int up_ref(void *p)
  29. {
  30. return 1;
  31. }
  32. static void down_ref(void *p)
  33. {
  34. }
  35. static int test_property_string(void)
  36. {
  37. OSSL_METHOD_STORE *store;
  38. int res = 0;
  39. OSSL_PROPERTY_IDX i, j;
  40. if (TEST_ptr(store = ossl_method_store_new(NULL))
  41. && TEST_int_eq(ossl_property_name(NULL, "fnord", 0), 0)
  42. && TEST_int_ne(ossl_property_name(NULL, "fnord", 1), 0)
  43. && TEST_int_ne(ossl_property_name(NULL, "name", 1), 0)
  44. /* Property value checks */
  45. && TEST_int_eq(ossl_property_value(NULL, "fnord", 0), 0)
  46. && TEST_int_ne(i = ossl_property_value(NULL, "no", 0), 0)
  47. && TEST_int_ne(j = ossl_property_value(NULL, "yes", 0), 0)
  48. && TEST_int_ne(i, j)
  49. && TEST_int_eq(ossl_property_value(NULL, "yes", 1), j)
  50. && TEST_int_eq(ossl_property_value(NULL, "no", 1), i)
  51. && TEST_int_ne(i = ossl_property_value(NULL, "illuminati", 1), 0)
  52. && TEST_int_eq(j = ossl_property_value(NULL, "fnord", 1), i + 1)
  53. && TEST_int_eq(ossl_property_value(NULL, "fnord", 1), j)
  54. /* Check name and values are distinct */
  55. && TEST_int_eq(ossl_property_value(NULL, "cold", 0), 0)
  56. && TEST_int_ne(ossl_property_name(NULL, "fnord", 0),
  57. ossl_property_value(NULL, "fnord", 0)))
  58. res = 1;
  59. ossl_method_store_free(store);
  60. return res;
  61. }
  62. static const struct {
  63. const char *defn;
  64. const char *query;
  65. int e;
  66. } parser_tests[] = {
  67. { "", "sky=blue", -1 },
  68. { "", "sky!=blue", 1 },
  69. { "groan", "", 0 },
  70. { "cold=yes", "cold=yes", 1 },
  71. { "cold=yes", "cold", 1 },
  72. { "cold=yes", "cold!=no", 1 },
  73. { "groan", "groan=yes", 1 },
  74. { "groan", "groan=no", -1 },
  75. { "groan", "groan!=yes", -1 },
  76. { "cold=no", "cold", -1 },
  77. { "cold=no", "?cold", 0 },
  78. { "cold=no", "cold=no", 1 },
  79. { "groan", "cold", -1 },
  80. { "groan", "cold=no", 1 },
  81. { "groan", "cold!=yes", 1 },
  82. { "groan=blue", "groan=yellow", -1 },
  83. { "groan=blue", "?groan=yellow", 0 },
  84. { "groan=blue", "groan!=yellow", 1 },
  85. { "groan=blue", "?groan!=yellow", 1 },
  86. { "today=monday, tomorrow=3", "today!=2", 1 },
  87. { "today=monday, tomorrow=3", "today!='monday'", -1 },
  88. { "today=monday, tomorrow=3", "tomorrow=3", 1 },
  89. { "n=0x3", "n=3", 1 },
  90. { "n=0x3", "n=-3", -1 },
  91. { "n=0x33", "n=51", 1 },
  92. { "n=033", "n=27", 1 },
  93. { "n=0", "n=00", 1 },
  94. { "n=0x0", "n=0", 1 },
  95. { "n=0, sky=blue", "?n=0, sky=blue", 2 },
  96. { "n=1, sky=blue", "?n=0, sky=blue", 1 },
  97. };
  98. static int test_property_parse(int n)
  99. {
  100. OSSL_METHOD_STORE *store;
  101. OSSL_PROPERTY_LIST *p = NULL, *q = NULL;
  102. int r = 0;
  103. if (TEST_ptr(store = ossl_method_store_new(NULL))
  104. && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
  105. NULL)
  106. && TEST_ptr(p = ossl_parse_property(NULL, parser_tests[n].defn))
  107. && TEST_ptr(q = ossl_parse_query(NULL, parser_tests[n].query))
  108. && TEST_int_eq(ossl_property_match_count(q, p), parser_tests[n].e))
  109. r = 1;
  110. ossl_property_free(p);
  111. ossl_property_free(q);
  112. ossl_method_store_free(store);
  113. return r;
  114. }
  115. static const struct {
  116. const char *q_global;
  117. const char *q_local;
  118. const char *prop;
  119. } merge_tests[] = {
  120. { "", "colour=blue", "colour=blue" },
  121. { "colour=blue", "", "colour=blue" },
  122. { "colour=red", "colour=blue", "colour=blue" },
  123. { "clouds=pink, urn=red", "urn=blue, colour=green",
  124. "urn=blue, colour=green, clouds=pink" },
  125. { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
  126. { "night", "day", "day=yes, night=yes" },
  127. { "day", "night", "day=yes, night=yes" },
  128. { "", "", "" },
  129. /*
  130. * The following four leave 'day' unspecified in the query, and will match
  131. * any definition
  132. */
  133. { "day=yes", "-day", "day=no" },
  134. { "day=yes", "-day", "day=yes" },
  135. { "day=yes", "-day", "day=arglebargle" },
  136. { "day=yes", "-day", "pot=sesquioxidizing" },
  137. { "day, night", "-night, day", "day=yes, night=no" },
  138. { "-day", "day=yes", "day=yes" },
  139. };
  140. static int test_property_merge(int n)
  141. {
  142. OSSL_METHOD_STORE *store;
  143. OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
  144. OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
  145. int r = 0;
  146. if (TEST_ptr(store = ossl_method_store_new(NULL))
  147. && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
  148. NULL)
  149. && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
  150. && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global))
  151. && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local))
  152. && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
  153. && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
  154. r = 1;
  155. ossl_property_free(q_global);
  156. ossl_property_free(q_local);
  157. ossl_property_free(q_combined);
  158. ossl_property_free(prop);
  159. ossl_method_store_free(store);
  160. return r;
  161. }
  162. static int test_property_defn_cache(void)
  163. {
  164. OSSL_METHOD_STORE *store;
  165. OSSL_PROPERTY_LIST *red, *blue;
  166. int r = 0;
  167. if (TEST_ptr(store = ossl_method_store_new(NULL))
  168. && add_property_names("red", "blue", NULL)
  169. && TEST_ptr(red = ossl_parse_property(NULL, "red"))
  170. && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
  171. && TEST_ptr_ne(red, blue)
  172. && TEST_true(ossl_prop_defn_set(NULL, "red", red))
  173. && TEST_true(ossl_prop_defn_set(NULL, "blue", blue))
  174. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
  175. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue))
  176. r = 1;
  177. ossl_method_store_free(store);
  178. return r;
  179. }
  180. static const struct {
  181. const char *defn;
  182. const char *query;
  183. int e;
  184. } definition_tests[] = {
  185. { "alpha", "alpha=yes", 1 },
  186. { "alpha=no", "alpha", -1 },
  187. { "alpha=1", "alpha=1", 1 },
  188. { "alpha=2", "alpha=1",-1 },
  189. { "alpha", "omega", -1 },
  190. { "alpha", "?omega", 0 },
  191. { "alpha", "?omega=1", 0 },
  192. { "alpha", "?omega=no", 1 },
  193. { "alpha", "?omega=yes", 0 },
  194. { "alpha, omega", "?omega=yes", 1 },
  195. { "alpha, omega", "?omega=no", 0 }
  196. };
  197. static int test_definition_compares(int n)
  198. {
  199. OSSL_METHOD_STORE *store;
  200. OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
  201. int r;
  202. r = TEST_ptr(store = ossl_method_store_new(NULL))
  203. && add_property_names("alpha", "omega", NULL)
  204. && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
  205. && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query))
  206. && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
  207. ossl_property_free(d);
  208. ossl_property_free(q);
  209. ossl_method_store_free(store);
  210. return r;
  211. }
  212. static int test_register_deregister(void)
  213. {
  214. static const struct {
  215. int nid;
  216. const char *prop;
  217. char *impl;
  218. } impls[] = {
  219. { 6, "position=1", "a" },
  220. { 6, "position=2", "b" },
  221. { 6, "position=3", "c" },
  222. { 6, "position=4", "d" },
  223. };
  224. size_t i;
  225. int ret = 0;
  226. OSSL_METHOD_STORE *store;
  227. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  228. || !add_property_names("position", NULL))
  229. goto err;
  230. for (i = 0; i < OSSL_NELEM(impls); i++)
  231. if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
  232. impls[i].prop, impls[i].impl,
  233. &up_ref, &down_ref))) {
  234. TEST_note("iteration %zd", i + 1);
  235. goto err;
  236. }
  237. /* Deregister in a different order to registration */
  238. for (i = 0; i < OSSL_NELEM(impls); i++) {
  239. const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
  240. int nid = impls[j].nid;
  241. void *impl = impls[j].impl;
  242. if (!TEST_true(ossl_method_store_remove(store, nid, impl))
  243. || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
  244. TEST_note("iteration %zd, position %zd", i + 1, j + 1);
  245. goto err;
  246. }
  247. }
  248. if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
  249. ret = 1;
  250. err:
  251. ossl_method_store_free(store);
  252. return ret;
  253. }
  254. static int test_property(void)
  255. {
  256. static const struct {
  257. int nid;
  258. const char *prop;
  259. char *impl;
  260. } impls[] = {
  261. { 1, "fast=no, colour=green", "a" },
  262. { 1, "fast, colour=blue", "b" },
  263. { 1, "", "-" },
  264. { 9, "sky=blue, furry", "c" },
  265. { 3, NULL, "d" },
  266. { 6, "sky.colour=blue, sky=green, old.data", "e" },
  267. };
  268. static struct {
  269. int nid;
  270. const char *prop;
  271. char *expected;
  272. } queries[] = {
  273. { 1, "fast", "b" },
  274. { 1, "fast=yes", "b" },
  275. { 1, "fast=no, colour=green", "a" },
  276. { 1, "colour=blue, fast", "b" },
  277. { 1, "colour=blue", "b" },
  278. { 9, "furry", "c" },
  279. { 6, "sky.colour=blue", "e" },
  280. { 6, "old.data", "e" },
  281. { 9, "furry=yes, sky=blue", "c" },
  282. { 1, "", "a" },
  283. { 3, "", "d" },
  284. };
  285. OSSL_METHOD_STORE *store;
  286. size_t i;
  287. int ret = 0;
  288. void *result;
  289. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  290. || !add_property_names("fast", "colour", "sky", "furry", NULL))
  291. goto err;
  292. for (i = 0; i < OSSL_NELEM(impls); i++)
  293. if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
  294. impls[i].prop, impls[i].impl,
  295. &up_ref, &down_ref))) {
  296. TEST_note("iteration %zd", i + 1);
  297. goto err;
  298. }
  299. for (i = 0; i < OSSL_NELEM(queries); i++) {
  300. OSSL_PROPERTY_LIST *pq = NULL;
  301. if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
  302. queries[i].prop, &result))
  303. || !TEST_str_eq((char *)result, queries[i].expected)) {
  304. TEST_note("iteration %zd", i + 1);
  305. ossl_property_free(pq);
  306. goto err;
  307. }
  308. ossl_property_free(pq);
  309. }
  310. ret = 1;
  311. err:
  312. ossl_method_store_free(store);
  313. return ret;
  314. }
  315. static int test_query_cache_stochastic(void)
  316. {
  317. const int max = 10000, tail = 10;
  318. OSSL_METHOD_STORE *store;
  319. int i, res = 0;
  320. char buf[50];
  321. void *result;
  322. int errors = 0;
  323. int v[10001];
  324. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  325. || !add_property_names("n", NULL))
  326. goto err;
  327. for (i = 1; i <= max; i++) {
  328. v[i] = 2 * i;
  329. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  330. if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
  331. &up_ref, &down_ref))
  332. || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i,
  333. &up_ref, &down_ref))
  334. || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
  335. "miss", &up_ref,
  336. &down_ref))) {
  337. TEST_note("iteration %d", i);
  338. goto err;
  339. }
  340. }
  341. for (i = 1; i <= max; i++) {
  342. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  343. if (!ossl_method_store_cache_get(store, i, buf, &result)
  344. || result != v + i)
  345. errors++;
  346. }
  347. /* There is a tiny probability that this will fail when it shouldn't */
  348. res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
  349. err:
  350. ossl_method_store_free(store);
  351. return res;
  352. }
  353. static int test_fips_mode(void)
  354. {
  355. int ret = 0;
  356. OPENSSL_CTX *ctx = NULL;
  357. if (!TEST_ptr(ctx = OPENSSL_CTX_new()))
  358. goto err;
  359. ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
  360. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  361. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  362. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  363. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  364. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  365. && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
  366. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  367. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  368. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  369. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  370. && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
  371. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  372. && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
  373. && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
  374. err:
  375. OPENSSL_CTX_free(ctx);
  376. return ret;
  377. }
  378. int setup_tests(void)
  379. {
  380. ADD_TEST(test_property_string);
  381. ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
  382. ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
  383. ADD_TEST(test_property_defn_cache);
  384. ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
  385. ADD_TEST(test_register_deregister);
  386. ADD_TEST(test_property);
  387. ADD_TEST(test_query_cache_stochastic);
  388. ADD_TEST(test_fips_mode);
  389. return 1;
  390. }