property_test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright 2019-2021 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. { 1, "a=2, n=012345678" }, /* Bad octal digit */
  184. { 0, "n=0x28FG, a=3" }, /* Bad hex digit */
  185. { 0, "n=145d, a=2" }, /* Bad decimal digit */
  186. { 1, "@='hello'" }, /* Invalid name */
  187. { 1, "n0123456789012345678901234567890123456789"
  188. "0123456789012345678901234567890123456789"
  189. "0123456789012345678901234567890123456789"
  190. "0123456789012345678901234567890123456789=yes" }, /* Name too long */
  191. { 0, ".n=3" }, /* Invalid name */
  192. { 1, "fnord.fnord.=3" } /* Invalid name */
  193. };
  194. static int test_property_parse_error(int n)
  195. {
  196. OSSL_METHOD_STORE *store;
  197. OSSL_PROPERTY_LIST *p = NULL;
  198. int r = 0;
  199. const char *ps;
  200. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  201. || !add_property_names("a", "n", NULL))
  202. goto err;
  203. ps = parse_error_tests[n].ps;
  204. if (parse_error_tests[n].query) {
  205. if (!TEST_ptr_null(p = ossl_parse_query(NULL, ps, 1)))
  206. goto err;
  207. } else if (!TEST_ptr_null(p = ossl_parse_property(NULL, ps))) {
  208. goto err;
  209. }
  210. r = 1;
  211. err:
  212. ossl_property_free(p);
  213. ossl_method_store_free(store);
  214. return r;
  215. }
  216. static const struct {
  217. const char *q_global;
  218. const char *q_local;
  219. const char *prop;
  220. } merge_tests[] = {
  221. { "", "colour=blue", "colour=blue" },
  222. { "colour=blue", "", "colour=blue" },
  223. { "colour=red", "colour=blue", "colour=blue" },
  224. { "clouds=pink, urn=red", "urn=blue, colour=green",
  225. "urn=blue, colour=green, clouds=pink" },
  226. { "pot=gold", "urn=blue", "pot=gold, urn=blue" },
  227. { "night", "day", "day=yes, night=yes" },
  228. { "day", "night", "day=yes, night=yes" },
  229. { "", "", "" },
  230. /*
  231. * The following four leave 'day' unspecified in the query, and will match
  232. * any definition
  233. */
  234. { "day=yes", "-day", "day=no" },
  235. { "day=yes", "-day", "day=yes" },
  236. { "day=yes", "-day", "day=arglebargle" },
  237. { "day=yes", "-day", "pot=sesquioxidizing" },
  238. { "day, night", "-night, day", "day=yes, night=no" },
  239. { "-day", "day=yes", "day=yes" },
  240. };
  241. static int test_property_merge(int n)
  242. {
  243. OSSL_METHOD_STORE *store;
  244. OSSL_PROPERTY_LIST *q_global = NULL, *q_local = NULL;
  245. OSSL_PROPERTY_LIST *q_combined = NULL, *prop = NULL;
  246. int r = 0;
  247. if (TEST_ptr(store = ossl_method_store_new(NULL))
  248. && add_property_names("colour", "urn", "clouds", "pot", "day", "night",
  249. NULL)
  250. && TEST_ptr(prop = ossl_parse_property(NULL, merge_tests[n].prop))
  251. && TEST_ptr(q_global = ossl_parse_query(NULL, merge_tests[n].q_global,
  252. 0))
  253. && TEST_ptr(q_local = ossl_parse_query(NULL, merge_tests[n].q_local, 0))
  254. && TEST_ptr(q_combined = ossl_property_merge(q_local, q_global))
  255. && TEST_int_ge(ossl_property_match_count(q_combined, prop), 0))
  256. r = 1;
  257. ossl_property_free(q_global);
  258. ossl_property_free(q_local);
  259. ossl_property_free(q_combined);
  260. ossl_property_free(prop);
  261. ossl_method_store_free(store);
  262. return r;
  263. }
  264. static int test_property_defn_cache(void)
  265. {
  266. OSSL_METHOD_STORE *store;
  267. OSSL_PROPERTY_LIST *red, *blue;
  268. int r = 0;
  269. if (TEST_ptr(store = ossl_method_store_new(NULL))
  270. && add_property_names("red", "blue", NULL)
  271. && TEST_ptr(red = ossl_parse_property(NULL, "red"))
  272. && TEST_ptr(blue = ossl_parse_property(NULL, "blue"))
  273. && TEST_ptr_ne(red, blue)
  274. && TEST_true(ossl_prop_defn_set(NULL, "red", red))
  275. && TEST_true(ossl_prop_defn_set(NULL, "blue", blue))
  276. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
  277. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue))
  278. r = 1;
  279. ossl_method_store_free(store);
  280. return r;
  281. }
  282. static const struct {
  283. const char *defn;
  284. const char *query;
  285. int e;
  286. } definition_tests[] = {
  287. { "alpha", "alpha=yes", 1 },
  288. { "alpha=no", "alpha", -1 },
  289. { "alpha=1", "alpha=1", 1 },
  290. { "alpha=2", "alpha=1",-1 },
  291. { "alpha", "omega", -1 },
  292. { "alpha", "?omega", 0 },
  293. { "alpha", "?omega=1", 0 },
  294. { "alpha", "?omega=no", 1 },
  295. { "alpha", "?omega=yes", 0 },
  296. { "alpha, omega", "?omega=yes", 1 },
  297. { "alpha, omega", "?omega=no", 0 }
  298. };
  299. static int test_definition_compares(int n)
  300. {
  301. OSSL_METHOD_STORE *store;
  302. OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
  303. int r;
  304. r = TEST_ptr(store = ossl_method_store_new(NULL))
  305. && add_property_names("alpha", "omega", NULL)
  306. && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
  307. && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0))
  308. && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
  309. ossl_property_free(d);
  310. ossl_property_free(q);
  311. ossl_method_store_free(store);
  312. return r;
  313. }
  314. static int test_register_deregister(void)
  315. {
  316. static const struct {
  317. int nid;
  318. const char *prop;
  319. char *impl;
  320. } impls[] = {
  321. { 6, "position=1", "a" },
  322. { 6, "position=2", "b" },
  323. { 6, "position=3", "c" },
  324. { 6, "position=4", "d" },
  325. };
  326. size_t i;
  327. int ret = 0;
  328. OSSL_METHOD_STORE *store;
  329. OSSL_PROVIDER prov = { 1 };
  330. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  331. || !add_property_names("position", NULL))
  332. goto err;
  333. for (i = 0; i < OSSL_NELEM(impls); i++)
  334. if (!TEST_true(ossl_method_store_add(store, &prov, impls[i].nid,
  335. impls[i].prop, impls[i].impl,
  336. &up_ref, &down_ref))) {
  337. TEST_note("iteration %zd", i + 1);
  338. goto err;
  339. }
  340. /* Deregister in a different order to registration */
  341. for (i = 0; i < OSSL_NELEM(impls); i++) {
  342. const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
  343. int nid = impls[j].nid;
  344. void *impl = impls[j].impl;
  345. if (!TEST_true(ossl_method_store_remove(store, nid, impl))
  346. || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
  347. TEST_note("iteration %zd, position %zd", i + 1, j + 1);
  348. goto err;
  349. }
  350. }
  351. if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
  352. ret = 1;
  353. err:
  354. ossl_method_store_free(store);
  355. return ret;
  356. }
  357. static int test_property(void)
  358. {
  359. static OSSL_PROVIDER fake_provider1 = { 1 };
  360. static OSSL_PROVIDER fake_provider2 = { 2 };
  361. static const OSSL_PROVIDER *fake_prov1 = &fake_provider1;
  362. static const OSSL_PROVIDER *fake_prov2 = &fake_provider2;
  363. static const struct {
  364. const OSSL_PROVIDER **prov;
  365. int nid;
  366. const char *prop;
  367. char *impl;
  368. } impls[] = {
  369. { &fake_prov1, 1, "fast=no, colour=green", "a" },
  370. { &fake_prov1, 1, "fast, colour=blue", "b" },
  371. { &fake_prov1, 1, "", "-" },
  372. { &fake_prov2, 9, "sky=blue, furry", "c" },
  373. { &fake_prov2, 3, NULL, "d" },
  374. { &fake_prov2, 6, "sky.colour=blue, sky=green, old.data", "e" },
  375. };
  376. static struct {
  377. const OSSL_PROVIDER **prov;
  378. int nid;
  379. const char *prop;
  380. char *expected;
  381. } queries[] = {
  382. { &fake_prov1, 1, "fast", "b" },
  383. { &fake_prov1, 1, "fast=yes", "b" },
  384. { &fake_prov1, 1, "fast=no, colour=green", "a" },
  385. { &fake_prov1, 1, "colour=blue, fast", "b" },
  386. { &fake_prov1, 1, "colour=blue", "b" },
  387. { &fake_prov2, 9, "furry", "c" },
  388. { &fake_prov2, 6, "sky.colour=blue", "e" },
  389. { &fake_prov2, 6, "old.data", "e" },
  390. { &fake_prov2, 9, "furry=yes, sky=blue", "c" },
  391. { &fake_prov1, 1, "", "a" },
  392. { &fake_prov2, 3, "", "d" },
  393. };
  394. OSSL_METHOD_STORE *store;
  395. size_t i;
  396. int ret = 0;
  397. void *result;
  398. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  399. || !add_property_names("fast", "colour", "sky", "furry", NULL))
  400. goto err;
  401. for (i = 0; i < OSSL_NELEM(impls); i++)
  402. if (!TEST_true(ossl_method_store_add(store, *impls[i].prov,
  403. impls[i].nid, impls[i].prop,
  404. impls[i].impl,
  405. &up_ref, &down_ref))) {
  406. TEST_note("iteration %zd", i + 1);
  407. goto err;
  408. }
  409. /*
  410. * The first check of queries is with NULL given as provider. All
  411. * queries are expected to succeed.
  412. */
  413. for (i = 0; i < OSSL_NELEM(queries); i++) {
  414. const OSSL_PROVIDER *nullprov = NULL;
  415. OSSL_PROPERTY_LIST *pq = NULL;
  416. if (!TEST_true(ossl_method_store_fetch(store,
  417. queries[i].nid, queries[i].prop,
  418. &nullprov, &result))
  419. || !TEST_str_eq((char *)result, queries[i].expected)) {
  420. TEST_note("iteration %zd", i + 1);
  421. ossl_property_free(pq);
  422. goto err;
  423. }
  424. ossl_property_free(pq);
  425. }
  426. /*
  427. * The second check of queries is with &address1 given as provider.
  428. */
  429. for (i = 0; i < OSSL_NELEM(queries); i++) {
  430. OSSL_PROPERTY_LIST *pq = NULL;
  431. result = NULL;
  432. if (queries[i].prov == &fake_prov1) {
  433. if (!TEST_true(ossl_method_store_fetch(store,
  434. queries[i].nid,
  435. queries[i].prop,
  436. &fake_prov1, &result))
  437. || !TEST_ptr_eq(fake_prov1, &fake_provider1)
  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. } else {
  444. if (!TEST_false(ossl_method_store_fetch(store,
  445. queries[i].nid,
  446. queries[i].prop,
  447. &fake_prov1, &result))
  448. || !TEST_ptr_eq(fake_prov1, &fake_provider1)
  449. || !TEST_ptr_null(result)) {
  450. TEST_note("iteration %zd", i + 1);
  451. ossl_property_free(pq);
  452. goto err;
  453. }
  454. }
  455. ossl_property_free(pq);
  456. }
  457. /*
  458. * The third check of queries is with &address2 given as provider.
  459. */
  460. for (i = 0; i < OSSL_NELEM(queries); i++) {
  461. OSSL_PROPERTY_LIST *pq = NULL;
  462. result = NULL;
  463. if (queries[i].prov == &fake_prov2) {
  464. if (!TEST_true(ossl_method_store_fetch(store,
  465. queries[i].nid,
  466. queries[i].prop,
  467. &fake_prov2, &result))
  468. || !TEST_ptr_eq(fake_prov2, &fake_provider2)
  469. || !TEST_str_eq((char *)result, queries[i].expected)) {
  470. TEST_note("iteration %zd", i + 1);
  471. ossl_property_free(pq);
  472. goto err;
  473. }
  474. } else {
  475. if (!TEST_false(ossl_method_store_fetch(store,
  476. queries[i].nid,
  477. queries[i].prop,
  478. &fake_prov2, &result))
  479. || !TEST_ptr_eq(fake_prov2, &fake_provider2)
  480. || !TEST_ptr_null(result)) {
  481. TEST_note("iteration %zd", i + 1);
  482. ossl_property_free(pq);
  483. goto err;
  484. }
  485. }
  486. ossl_property_free(pq);
  487. }
  488. ret = 1;
  489. err:
  490. ossl_method_store_free(store);
  491. return ret;
  492. }
  493. static int test_query_cache_stochastic(void)
  494. {
  495. const int max = 10000, tail = 10;
  496. OSSL_METHOD_STORE *store;
  497. int i, res = 0;
  498. char buf[50];
  499. void *result;
  500. int errors = 0;
  501. int v[10001];
  502. OSSL_PROVIDER prov = { 1 };
  503. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  504. || !add_property_names("n", NULL))
  505. goto err;
  506. for (i = 1; i <= max; i++) {
  507. v[i] = 2 * i;
  508. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  509. if (!TEST_true(ossl_method_store_add(store, &prov, i, buf, "abc",
  510. &up_ref, &down_ref))
  511. || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
  512. buf, v + i,
  513. &up_ref, &down_ref))
  514. || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
  515. "n=1234", "miss",
  516. &up_ref, &down_ref))) {
  517. TEST_note("iteration %d", i);
  518. goto err;
  519. }
  520. }
  521. for (i = 1; i <= max; i++) {
  522. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  523. if (!ossl_method_store_cache_get(store, NULL, i, buf, &result)
  524. || result != v + i)
  525. errors++;
  526. }
  527. /* There is a tiny probability that this will fail when it shouldn't */
  528. res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
  529. err:
  530. ossl_method_store_free(store);
  531. return res;
  532. }
  533. static int test_fips_mode(void)
  534. {
  535. int ret = 0;
  536. OSSL_LIB_CTX *ctx = NULL;
  537. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()))
  538. goto err;
  539. ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
  540. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  541. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  542. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  543. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  544. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  545. && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
  546. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  547. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  548. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  549. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  550. && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
  551. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  552. && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
  553. && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
  554. err:
  555. OSSL_LIB_CTX_free(ctx);
  556. return ret;
  557. }
  558. static struct {
  559. const char *in;
  560. const char *out;
  561. } to_string_tests[] = {
  562. { "fips=yes", "fips=yes" },
  563. { "fips!=yes", "fips!=yes" },
  564. { "fips = yes", "fips=yes" },
  565. { "fips", "fips=yes" },
  566. { "fips=no", "fips=no" },
  567. { "-fips", "-fips" },
  568. { "?fips=yes", "?fips=yes" },
  569. { "fips=yes,provider=fips", "fips=yes,provider=fips" },
  570. { "fips = yes , provider = fips", "fips=yes,provider=fips" },
  571. { "fips=yes,provider!=fips", "fips=yes,provider!=fips" },
  572. { "fips=yes,?provider=fips", "fips=yes,?provider=fips" },
  573. { "fips=yes,-provider", "fips=yes,-provider" },
  574. /* foo is an unknown internal name */
  575. { "foo=yes,fips=yes", "fips=yes"},
  576. { "", "" },
  577. { "fips=3", "fips=3" },
  578. { "fips=-3", "fips=-3" },
  579. { NULL, "" }
  580. };
  581. static int test_property_list_to_string(int i)
  582. {
  583. OSSL_PROPERTY_LIST *pl = NULL;
  584. int ret = 0;
  585. size_t bufsize;
  586. char *buf = NULL;
  587. if (to_string_tests[i].in != NULL
  588. && !TEST_ptr(pl = ossl_parse_query(NULL, to_string_tests[i].in, 1)))
  589. goto err;
  590. bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
  591. if (!TEST_size_t_gt(bufsize, 0))
  592. goto err;
  593. buf = OPENSSL_malloc(bufsize);
  594. if (!TEST_ptr(buf)
  595. || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
  596. bufsize),
  597. bufsize)
  598. || !TEST_str_eq(to_string_tests[i].out, buf)
  599. || !TEST_size_t_eq(bufsize, strlen(to_string_tests[i].out) + 1))
  600. goto err;
  601. ret = 1;
  602. err:
  603. OPENSSL_free(buf);
  604. ossl_property_free(pl);
  605. return ret;
  606. }
  607. int setup_tests(void)
  608. {
  609. ADD_TEST(test_property_string);
  610. ADD_TEST(test_property_query_value_create);
  611. ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
  612. ADD_ALL_TESTS(test_property_parse_error, OSSL_NELEM(parse_error_tests));
  613. ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
  614. ADD_TEST(test_property_defn_cache);
  615. ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
  616. ADD_TEST(test_register_deregister);
  617. ADD_TEST(test_property);
  618. ADD_TEST(test_query_cache_stochastic);
  619. ADD_TEST(test_fips_mode);
  620. ADD_ALL_TESTS(test_property_list_to_string, OSSL_NELEM(to_string_tests));
  621. return 1;
  622. }