property_test.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright 2019-2023 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. /*
  17. * We make our OSSL_PROVIDER for testing purposes. All we really need is
  18. * a pointer. We know that as long as we don't try to use the method
  19. * cache flush functions, the provider pointer is merely a pointer being
  20. * passed around, and used as a tag of sorts.
  21. */
  22. struct ossl_provider_st {
  23. int x;
  24. };
  25. static int add_property_names(const char *n, ...)
  26. {
  27. va_list args;
  28. int res = 1;
  29. va_start(args, n);
  30. do {
  31. if (!TEST_int_ne(ossl_property_name(NULL, n, 1), 0))
  32. res = 0;
  33. } while ((n = va_arg(args, const char *)) != NULL);
  34. va_end(args);
  35. return res;
  36. }
  37. static int up_ref(void *p)
  38. {
  39. return 1;
  40. }
  41. static void down_ref(void *p)
  42. {
  43. }
  44. static int test_property_string(void)
  45. {
  46. OSSL_LIB_CTX *ctx;
  47. OSSL_METHOD_STORE *store = NULL;
  48. int res = 0;
  49. OSSL_PROPERTY_IDX i, j;
  50. /*-
  51. * Use our own library context because we depend on ordering from a
  52. * pristine state.
  53. */
  54. if (TEST_ptr(ctx = OSSL_LIB_CTX_new())
  55. && TEST_ptr(store = ossl_method_store_new(ctx))
  56. && TEST_int_eq(ossl_property_name(ctx, "fnord", 0), 0)
  57. && TEST_int_ne(ossl_property_name(ctx, "fnord", 1), 0)
  58. && TEST_int_ne(ossl_property_name(ctx, "name", 1), 0)
  59. /* Pre loaded names */
  60. && TEST_str_eq(ossl_property_name_str(ctx, 1), "provider")
  61. && TEST_str_eq(ossl_property_name_str(ctx, 2), "version")
  62. && TEST_str_eq(ossl_property_name_str(ctx, 3), "fips")
  63. && TEST_str_eq(ossl_property_name_str(ctx, 4), "output")
  64. && TEST_str_eq(ossl_property_name_str(ctx, 5), "input")
  65. && TEST_str_eq(ossl_property_name_str(ctx, 6), "structure")
  66. /* The names we added */
  67. && TEST_str_eq(ossl_property_name_str(ctx, 7), "fnord")
  68. && TEST_str_eq(ossl_property_name_str(ctx, 8), "name")
  69. /* Out of range */
  70. && TEST_ptr_null(ossl_property_name_str(ctx, 0))
  71. && TEST_ptr_null(ossl_property_name_str(ctx, 9))
  72. /* Property value checks */
  73. && TEST_int_eq(ossl_property_value(ctx, "fnord", 0), 0)
  74. && TEST_int_ne(i = ossl_property_value(ctx, "no", 0), 0)
  75. && TEST_int_ne(j = ossl_property_value(ctx, "yes", 0), 0)
  76. && TEST_int_ne(i, j)
  77. && TEST_int_eq(ossl_property_value(ctx, "yes", 1), j)
  78. && TEST_int_eq(ossl_property_value(ctx, "no", 1), i)
  79. && TEST_int_ne(i = ossl_property_value(ctx, "illuminati", 1), 0)
  80. && TEST_int_eq(j = ossl_property_value(ctx, "fnord", 1), i + 1)
  81. && TEST_int_eq(ossl_property_value(ctx, "fnord", 1), j)
  82. /* Pre loaded values */
  83. && TEST_str_eq(ossl_property_value_str(ctx, 1), "yes")
  84. && TEST_str_eq(ossl_property_value_str(ctx, 2), "no")
  85. /* The value we added */
  86. && TEST_str_eq(ossl_property_value_str(ctx, 3), "illuminati")
  87. && TEST_str_eq(ossl_property_value_str(ctx, 4), "fnord")
  88. /* Out of range */
  89. && TEST_ptr_null(ossl_property_value_str(ctx, 0))
  90. && TEST_ptr_null(ossl_property_value_str(ctx, 5))
  91. /* Check name and values are distinct */
  92. && TEST_int_eq(ossl_property_value(ctx, "cold", 0), 0)
  93. && TEST_int_ne(ossl_property_name(ctx, "fnord", 0),
  94. ossl_property_value(ctx, "fnord", 0)))
  95. res = 1;
  96. ossl_method_store_free(store);
  97. OSSL_LIB_CTX_free(ctx);
  98. return res;
  99. }
  100. static const struct {
  101. const char *defn;
  102. const char *query;
  103. int e;
  104. } parser_tests[] = {
  105. { "", "sky=blue", -1 },
  106. { "", "sky!=blue", 1 },
  107. { "groan", "", 0 },
  108. { "cold=yes", "cold=yes", 1 },
  109. { "cold=yes", "cold", 1 },
  110. { "cold=yes", "cold!=no", 1 },
  111. { "groan", "groan=yes", 1 },
  112. { "groan", "groan=no", -1 },
  113. { "groan", "groan!=yes", -1 },
  114. { "cold=no", "cold", -1 },
  115. { "cold=no", "?cold", 0 },
  116. { "cold=no", "cold=no", 1 },
  117. { "groan", "cold", -1 },
  118. { "groan", "cold=no", 1 },
  119. { "groan", "cold!=yes", 1 },
  120. { "groan=blue", "groan=yellow", -1 },
  121. { "groan=blue", "?groan=yellow", 0 },
  122. { "groan=blue", "groan!=yellow", 1 },
  123. { "groan=blue", "?groan!=yellow", 1 },
  124. { "today=monday, tomorrow=3", "today!=2", 1 },
  125. { "today=monday, tomorrow=3", "today!='monday'", -1 },
  126. { "today=monday, tomorrow=3", "tomorrow=3", 1 },
  127. { "n=0x3", "n=3", 1 },
  128. { "n=0x3", "n=-3", -1 },
  129. { "n=0x33", "n=51", 1 },
  130. { "n=033", "n=27", 1 },
  131. { "n=0", "n=00", 1 },
  132. { "n=0x0", "n=0", 1 },
  133. { "n=0, sky=blue", "?n=0, sky=blue", 2 },
  134. { "n=1, sky=blue", "?n=0, sky=blue", 1 },
  135. };
  136. static int test_property_parse(int n)
  137. {
  138. OSSL_METHOD_STORE *store;
  139. OSSL_PROPERTY_LIST *p = NULL, *q = NULL;
  140. int r = 0;
  141. if (TEST_ptr(store = ossl_method_store_new(NULL))
  142. && add_property_names("sky", "groan", "cold", "today", "tomorrow", "n",
  143. NULL)
  144. && TEST_ptr(p = ossl_parse_property(NULL, parser_tests[n].defn))
  145. && TEST_ptr(q = ossl_parse_query(NULL, parser_tests[n].query, 0))
  146. && TEST_int_eq(ossl_property_match_count(q, p), parser_tests[n].e))
  147. r = 1;
  148. ossl_property_free(p);
  149. ossl_property_free(q);
  150. ossl_method_store_free(store);
  151. return r;
  152. }
  153. static int test_property_query_value_create(void)
  154. {
  155. OSSL_METHOD_STORE *store;
  156. OSSL_PROPERTY_LIST *p = NULL, *q = NULL, *o = NULL;
  157. int r = 0;
  158. /* The property value used here must not be used in other test cases */
  159. if (TEST_ptr(store = ossl_method_store_new(NULL))
  160. && add_property_names("wood", NULL)
  161. && TEST_ptr(p = ossl_parse_query(NULL, "wood=oak", 0)) /* undefined */
  162. && TEST_ptr(q = ossl_parse_query(NULL, "wood=oak", 1)) /* creates */
  163. && TEST_ptr(o = ossl_parse_query(NULL, "wood=oak", 0)) /* defined */
  164. && TEST_int_eq(ossl_property_match_count(q, p), -1)
  165. && TEST_int_eq(ossl_property_match_count(q, o), 1))
  166. r = 1;
  167. ossl_property_free(o);
  168. ossl_property_free(p);
  169. ossl_property_free(q);
  170. ossl_method_store_free(store);
  171. return r;
  172. }
  173. static const struct {
  174. int query;
  175. const char *ps;
  176. } parse_error_tests[] = {
  177. { 0, "n=1, n=1" }, /* duplicate name */
  178. { 0, "n=1, a=hi, n=1" }, /* duplicate name */
  179. { 1, "n=1, a=bye, ?n=0" }, /* duplicate name */
  180. { 0, "a=abc,#@!, n=1" }, /* non-ASCII character located */
  181. { 1, "a='Hello" }, /* Unterminated string */
  182. { 0, "a=\"World" }, /* Unterminated string */
  183. { 0, "a=_abd_" }, /* Unquoted string not starting with alphabetic */
  184. { 1, "a=2, n=012345678" }, /* Bad octal digit */
  185. { 0, "n=0x28FG, a=3" }, /* Bad hex digit */
  186. { 0, "n=145d, a=2" }, /* Bad decimal digit */
  187. { 1, "@='hello'" }, /* Invalid name */
  188. { 1, "n0123456789012345678901234567890123456789"
  189. "0123456789012345678901234567890123456789"
  190. "0123456789012345678901234567890123456789"
  191. "0123456789012345678901234567890123456789=yes" }, /* Name too long */
  192. { 0, ".n=3" }, /* Invalid name */
  193. { 1, "fnord.fnord.=3" } /* Invalid name */
  194. };
  195. static int test_property_parse_error(int n)
  196. {
  197. OSSL_METHOD_STORE *store;
  198. OSSL_PROPERTY_LIST *p = NULL;
  199. int r = 0;
  200. const char *ps;
  201. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  202. || !add_property_names("a", "n", NULL))
  203. goto err;
  204. ps = parse_error_tests[n].ps;
  205. if (parse_error_tests[n].query) {
  206. if (!TEST_ptr_null(p = ossl_parse_query(NULL, ps, 1)))
  207. goto err;
  208. } else if (!TEST_ptr_null(p = ossl_parse_property(NULL, ps))) {
  209. goto err;
  210. }
  211. r = 1;
  212. err:
  213. ossl_property_free(p);
  214. ossl_method_store_free(store);
  215. return r;
  216. }
  217. static const struct {
  218. const char *q_global;
  219. const char *q_local;
  220. const char *prop;
  221. } merge_tests[] = {
  222. { "", "colour=blue", "colour=blue" },
  223. { "colour=blue", "", "colour=blue" },
  224. { "colour=red", "colour=blue", "colour=blue" },
  225. { "clouds=pink, urn=red", "urn=blue, colour=green",
  226. "urn=blue, colour=green, clouds=pink" },
  227. { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
  228. { "night", "day", "day=yes, night=yes" },
  229. { "day", "night", "day=yes, night=yes" },
  230. { "", "", "" },
  231. /*
  232. * The following four leave 'day' unspecified in the query, and will match
  233. * any definition
  234. */
  235. { "day=yes", "-day", "day=no" },
  236. { "day=yes", "-day", "day=yes" },
  237. { "day=yes", "-day", "day=arglebargle" },
  238. { "day=yes", "-day", "pot=sesquioxidizing" },
  239. { "day, night", "-night, day", "day=yes, night=no" },
  240. { "-day", "day=yes", "day=yes" },
  241. };
  242. static int test_property_merge(int n)
  243. {
  244. OSSL_METHOD_STORE *store;
  245. OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
  246. OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
  247. int r = 0;
  248. if (TEST_ptr(store = ossl_method_store_new(NULL))
  249. && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
  250. NULL)
  251. && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
  252. && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global,
  253. 0))
  254. && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local, 0))
  255. && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
  256. && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
  257. r = 1;
  258. ossl_property_free(q_global);
  259. ossl_property_free(q_local);
  260. ossl_property_free(q_combined);
  261. ossl_property_free(prop);
  262. ossl_method_store_free(store);
  263. return r;
  264. }
  265. static int test_property_defn_cache(void)
  266. {
  267. OSSL_METHOD_STORE *store;
  268. OSSL_PROPERTY_LIST *red = NULL, *blue = NULL, *blue2 = NULL;
  269. int r;
  270. r = TEST_ptr(store = ossl_method_store_new(NULL))
  271. && add_property_names("red", "blue", NULL)
  272. && TEST_ptr(red = ossl_parse_property(NULL, "red"))
  273. && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
  274. && TEST_ptr_ne(red, blue)
  275. && TEST_true(ossl_prop_defn_set(NULL, "red", &red));
  276. if (!r) {
  277. ossl_property_free(red);
  278. red = NULL;
  279. ossl_property_free(blue);
  280. blue = NULL;
  281. }
  282. r = r && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue));
  283. if (!r) {
  284. ossl_property_free(blue);
  285. blue = NULL;
  286. }
  287. r = r && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
  288. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue)
  289. && TEST_ptr(blue2 = ossl_parse_property(NULL, "blue"))
  290. && TEST_ptr_ne(blue2, blue)
  291. && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue2));
  292. if (!r) {
  293. ossl_property_free(blue2);
  294. blue2 = NULL;
  295. }
  296. r = r && TEST_ptr_eq(blue2, blue)
  297. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue);
  298. ossl_method_store_free(store);
  299. return r;
  300. }
  301. static const struct {
  302. const char *defn;
  303. const char *query;
  304. int e;
  305. } definition_tests[] = {
  306. { "alpha", "alpha=yes", 1 },
  307. { "alpha=no", "alpha", -1 },
  308. { "alpha=1", "alpha=1", 1 },
  309. { "alpha=2", "alpha=1",-1 },
  310. { "alpha", "omega", -1 },
  311. { "alpha", "?omega", 0 },
  312. { "alpha", "?omega=1", 0 },
  313. { "alpha", "?omega=no", 1 },
  314. { "alpha", "?omega=yes", 0 },
  315. { "alpha, omega", "?omega=yes", 1 },
  316. { "alpha, omega", "?omega=no", 0 }
  317. };
  318. static int test_definition_compares(int n)
  319. {
  320. OSSL_METHOD_STORE *store;
  321. OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
  322. int r;
  323. r = TEST_ptr(store = ossl_method_store_new(NULL))
  324. && add_property_names("alpha", "omega", NULL)
  325. && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
  326. && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0))
  327. && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
  328. ossl_property_free(d);
  329. ossl_property_free(q);
  330. ossl_method_store_free(store);
  331. return r;
  332. }
  333. static int test_register_deregister(void)
  334. {
  335. static const struct {
  336. int nid;
  337. const char *prop;
  338. char *impl;
  339. } impls[] = {
  340. { 6, "position=1", "a" },
  341. { 6, "position=2", "b" },
  342. { 6, "position=3", "c" },
  343. { 6, "position=4", "d" },
  344. };
  345. size_t i;
  346. int ret = 0;
  347. OSSL_METHOD_STORE *store;
  348. OSSL_PROVIDER prov = { 1 };
  349. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  350. || !add_property_names("position", NULL))
  351. goto err;
  352. for (i = 0; i < OSSL_NELEM(impls); i++)
  353. if (!TEST_true(ossl_method_store_add(store, &prov, impls[i].nid,
  354. impls[i].prop, impls[i].impl,
  355. &up_ref, &down_ref))) {
  356. TEST_note("iteration %zd", i + 1);
  357. goto err;
  358. }
  359. /* Deregister in a different order to registration */
  360. for (i = 0; i < OSSL_NELEM(impls); i++) {
  361. const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
  362. int nid = impls[j].nid;
  363. void *impl = impls[j].impl;
  364. if (!TEST_true(ossl_method_store_remove(store, nid, impl))
  365. || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
  366. TEST_note("iteration %zd, position %zd", i + 1, j + 1);
  367. goto err;
  368. }
  369. }
  370. if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
  371. ret = 1;
  372. err:
  373. ossl_method_store_free(store);
  374. return ret;
  375. }
  376. static int test_property(void)
  377. {
  378. static OSSL_PROVIDER fake_provider1 = { 1 };
  379. static OSSL_PROVIDER fake_provider2 = { 2 };
  380. static const OSSL_PROVIDER *fake_prov1 = &fake_provider1;
  381. static const OSSL_PROVIDER *fake_prov2 = &fake_provider2;
  382. static const struct {
  383. const OSSL_PROVIDER **prov;
  384. int nid;
  385. const char *prop;
  386. char *impl;
  387. } impls[] = {
  388. { &fake_prov1, 1, "fast=no, colour=green", "a" },
  389. { &fake_prov1, 1, "fast, colour=blue", "b" },
  390. { &fake_prov1, 1, "", "-" },
  391. { &fake_prov2, 9, "sky=blue, furry", "c" },
  392. { &fake_prov2, 3, NULL, "d" },
  393. { &fake_prov2, 6, "sky.colour=blue, sky=green, old.data", "e" },
  394. };
  395. static struct {
  396. const OSSL_PROVIDER **prov;
  397. int nid;
  398. const char *prop;
  399. char *expected;
  400. } queries[] = {
  401. { &fake_prov1, 1, "fast", "b" },
  402. { &fake_prov1, 1, "fast=yes", "b" },
  403. { &fake_prov1, 1, "fast=no, colour=green", "a" },
  404. { &fake_prov1, 1, "colour=blue, fast", "b" },
  405. { &fake_prov1, 1, "colour=blue", "b" },
  406. { &fake_prov2, 9, "furry", "c" },
  407. { &fake_prov2, 6, "sky.colour=blue", "e" },
  408. { &fake_prov2, 6, "old.data", "e" },
  409. { &fake_prov2, 9, "furry=yes, sky=blue", "c" },
  410. { &fake_prov1, 1, "", "a" },
  411. { &fake_prov2, 3, "", "d" },
  412. };
  413. OSSL_METHOD_STORE *store;
  414. size_t i;
  415. int ret = 0;
  416. void *result;
  417. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  418. || !add_property_names("fast", "colour", "sky", "furry", NULL))
  419. goto err;
  420. for (i = 0; i < OSSL_NELEM(impls); i++)
  421. if (!TEST_true(ossl_method_store_add(store, *impls[i].prov,
  422. impls[i].nid, impls[i].prop,
  423. impls[i].impl,
  424. &up_ref, &down_ref))) {
  425. TEST_note("iteration %zd", i + 1);
  426. goto err;
  427. }
  428. /*
  429. * The first check of queries is with NULL given as provider. All
  430. * queries are expected to succeed.
  431. */
  432. for (i = 0; i < OSSL_NELEM(queries); i++) {
  433. const OSSL_PROVIDER *nullprov = NULL;
  434. OSSL_PROPERTY_LIST *pq = NULL;
  435. if (!TEST_true(ossl_method_store_fetch(store,
  436. queries[i].nid, queries[i].prop,
  437. &nullprov, &result))
  438. || !TEST_str_eq((char *)result, queries[i].expected)) {
  439. TEST_note("iteration %zd", i + 1);
  440. ossl_property_free(pq);
  441. goto err;
  442. }
  443. ossl_property_free(pq);
  444. }
  445. /*
  446. * The second check of queries is with &address1 given as provider.
  447. */
  448. for (i = 0; i < OSSL_NELEM(queries); i++) {
  449. OSSL_PROPERTY_LIST *pq = NULL;
  450. result = NULL;
  451. if (queries[i].prov == &fake_prov1) {
  452. if (!TEST_true(ossl_method_store_fetch(store,
  453. queries[i].nid,
  454. queries[i].prop,
  455. &fake_prov1, &result))
  456. || !TEST_ptr_eq(fake_prov1, &fake_provider1)
  457. || !TEST_str_eq((char *)result, queries[i].expected)) {
  458. TEST_note("iteration %zd", i + 1);
  459. ossl_property_free(pq);
  460. goto err;
  461. }
  462. } else {
  463. if (!TEST_false(ossl_method_store_fetch(store,
  464. queries[i].nid,
  465. queries[i].prop,
  466. &fake_prov1, &result))
  467. || !TEST_ptr_eq(fake_prov1, &fake_provider1)
  468. || !TEST_ptr_null(result)) {
  469. TEST_note("iteration %zd", i + 1);
  470. ossl_property_free(pq);
  471. goto err;
  472. }
  473. }
  474. ossl_property_free(pq);
  475. }
  476. /*
  477. * The third check of queries is with &address2 given as provider.
  478. */
  479. for (i = 0; i < OSSL_NELEM(queries); i++) {
  480. OSSL_PROPERTY_LIST *pq = NULL;
  481. result = NULL;
  482. if (queries[i].prov == &fake_prov2) {
  483. if (!TEST_true(ossl_method_store_fetch(store,
  484. queries[i].nid,
  485. queries[i].prop,
  486. &fake_prov2, &result))
  487. || !TEST_ptr_eq(fake_prov2, &fake_provider2)
  488. || !TEST_str_eq((char *)result, queries[i].expected)) {
  489. TEST_note("iteration %zd", i + 1);
  490. ossl_property_free(pq);
  491. goto err;
  492. }
  493. } else {
  494. if (!TEST_false(ossl_method_store_fetch(store,
  495. queries[i].nid,
  496. queries[i].prop,
  497. &fake_prov2, &result))
  498. || !TEST_ptr_eq(fake_prov2, &fake_provider2)
  499. || !TEST_ptr_null(result)) {
  500. TEST_note("iteration %zd", i + 1);
  501. ossl_property_free(pq);
  502. goto err;
  503. }
  504. }
  505. ossl_property_free(pq);
  506. }
  507. ret = 1;
  508. err:
  509. ossl_method_store_free(store);
  510. return ret;
  511. }
  512. static int test_query_cache_stochastic(void)
  513. {
  514. const int max = 10000, tail = 10;
  515. OSSL_METHOD_STORE *store;
  516. int i, res = 0;
  517. char buf[50];
  518. void *result;
  519. int errors = 0;
  520. int v[10001];
  521. OSSL_PROVIDER prov = { 1 };
  522. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  523. || !add_property_names("n", NULL))
  524. goto err;
  525. for (i = 1; i <= max; i++) {
  526. v[i] = 2 * i;
  527. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  528. if (!TEST_true(ossl_method_store_add(store, &prov, i, buf, "abc",
  529. &up_ref, &down_ref))
  530. || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
  531. buf, v + i,
  532. &up_ref, &down_ref))
  533. || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
  534. "n=1234", "miss",
  535. &up_ref, &down_ref))) {
  536. TEST_note("iteration %d", i);
  537. goto err;
  538. }
  539. }
  540. for (i = 1; i <= max; i++) {
  541. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  542. if (!ossl_method_store_cache_get(store, NULL, i, buf, &result)
  543. || result != v + i)
  544. errors++;
  545. }
  546. /* There is a tiny probability that this will fail when it shouldn't */
  547. res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
  548. err:
  549. ossl_method_store_free(store);
  550. return res;
  551. }
  552. static int test_fips_mode(void)
  553. {
  554. int ret = 0;
  555. OSSL_LIB_CTX *ctx = NULL;
  556. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()))
  557. goto err;
  558. ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
  559. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  560. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  561. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  562. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  563. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  564. && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
  565. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  566. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  567. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  568. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  569. && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
  570. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  571. && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
  572. && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
  573. err:
  574. OSSL_LIB_CTX_free(ctx);
  575. return ret;
  576. }
  577. static struct {
  578. const char *in;
  579. const char *out;
  580. } to_string_tests[] = {
  581. { "fips=yes", "fips=yes" },
  582. { "fips!=yes", "fips!=yes" },
  583. { "fips = yes", "fips=yes" },
  584. { "fips", "fips=yes" },
  585. { "fips=no", "fips=no" },
  586. { "-fips", "-fips" },
  587. { "?fips=yes", "?fips=yes" },
  588. { "fips=yes,provider=fips", "fips=yes,provider=fips" },
  589. { "fips = yes , provider = fips", "fips=yes,provider=fips" },
  590. { "fips=yes,provider!=fips", "fips=yes,provider!=fips" },
  591. { "fips=yes,?provider=fips", "fips=yes,?provider=fips" },
  592. { "fips=yes,-provider", "fips=yes,-provider" },
  593. /* foo is an unknown internal name */
  594. { "foo=yes,fips=yes", "fips=yes"},
  595. { "", "" },
  596. { "fips=3", "fips=3" },
  597. { "fips=-3", "fips=-3" },
  598. { "provider='foo bar'", "provider='foo bar'" },
  599. { "provider=\"foo bar'\"", "provider=\"foo bar'\"" },
  600. { "provider=abc***", "provider='abc***'" },
  601. { NULL, "" }
  602. };
  603. static int test_property_list_to_string(int i)
  604. {
  605. OSSL_PROPERTY_LIST *pl = NULL;
  606. int ret = 0;
  607. size_t bufsize;
  608. char *buf = NULL;
  609. if (to_string_tests[i].in != NULL
  610. && !TEST_ptr(pl = ossl_parse_query(NULL, to_string_tests[i].in, 1)))
  611. goto err;
  612. bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
  613. if (!TEST_size_t_gt(bufsize, 0))
  614. goto err;
  615. buf = OPENSSL_malloc(bufsize);
  616. if (!TEST_ptr(buf)
  617. || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
  618. bufsize),
  619. bufsize)
  620. || !TEST_str_eq(to_string_tests[i].out, buf)
  621. || !TEST_size_t_eq(bufsize, strlen(to_string_tests[i].out) + 1))
  622. goto err;
  623. ret = 1;
  624. err:
  625. OPENSSL_free(buf);
  626. ossl_property_free(pl);
  627. return ret;
  628. }
  629. int setup_tests(void)
  630. {
  631. ADD_TEST(test_property_string);
  632. ADD_TEST(test_property_query_value_create);
  633. ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
  634. ADD_ALL_TESTS(test_property_parse_error, OSSL_NELEM(parse_error_tests));
  635. ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
  636. ADD_TEST(test_property_defn_cache);
  637. ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
  638. ADD_TEST(test_register_deregister);
  639. ADD_TEST(test_property);
  640. ADD_TEST(test_query_cache_stochastic);
  641. ADD_TEST(test_fips_mode);
  642. ADD_ALL_TESTS(test_property_list_to_string, OSSL_NELEM(to_string_tests));
  643. return 1;
  644. }