property_test.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 = NULL, *blue = NULL, *blue2 = NULL;
  268. int r;
  269. r = 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. if (!r) {
  276. ossl_property_free(red);
  277. red = NULL;
  278. ossl_property_free(blue);
  279. blue = NULL;
  280. }
  281. r = r && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue));
  282. if (!r) {
  283. ossl_property_free(blue);
  284. blue = NULL;
  285. }
  286. r = r && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red)
  287. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue)
  288. && TEST_ptr(blue2 = ossl_parse_property(NULL, "blue"))
  289. && TEST_ptr_ne(blue2, blue)
  290. && TEST_true(ossl_prop_defn_set(NULL, "blue", &blue2));
  291. if (!r) {
  292. ossl_property_free(blue2);
  293. blue2 = NULL;
  294. }
  295. r = r && TEST_ptr_eq(blue2, blue)
  296. && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue);
  297. ossl_method_store_free(store);
  298. return r;
  299. }
  300. static const struct {
  301. const char *defn;
  302. const char *query;
  303. int e;
  304. } definition_tests[] = {
  305. { "alpha", "alpha=yes", 1 },
  306. { "alpha=no", "alpha", -1 },
  307. { "alpha=1", "alpha=1", 1 },
  308. { "alpha=2", "alpha=1",-1 },
  309. { "alpha", "omega", -1 },
  310. { "alpha", "?omega", 0 },
  311. { "alpha", "?omega=1", 0 },
  312. { "alpha", "?omega=no", 1 },
  313. { "alpha", "?omega=yes", 0 },
  314. { "alpha, omega", "?omega=yes", 1 },
  315. { "alpha, omega", "?omega=no", 0 }
  316. };
  317. static int test_definition_compares(int n)
  318. {
  319. OSSL_METHOD_STORE *store;
  320. OSSL_PROPERTY_LIST *d = NULL, *q = NULL;
  321. int r;
  322. r = TEST_ptr(store = ossl_method_store_new(NULL))
  323. && add_property_names("alpha", "omega", NULL)
  324. && TEST_ptr(d = ossl_parse_property(NULL, definition_tests[n].defn))
  325. && TEST_ptr(q = ossl_parse_query(NULL, definition_tests[n].query, 0))
  326. && TEST_int_eq(ossl_property_match_count(q, d), definition_tests[n].e);
  327. ossl_property_free(d);
  328. ossl_property_free(q);
  329. ossl_method_store_free(store);
  330. return r;
  331. }
  332. static int test_register_deregister(void)
  333. {
  334. static const struct {
  335. int nid;
  336. const char *prop;
  337. char *impl;
  338. } impls[] = {
  339. { 6, "position=1", "a" },
  340. { 6, "position=2", "b" },
  341. { 6, "position=3", "c" },
  342. { 6, "position=4", "d" },
  343. };
  344. size_t i;
  345. int ret = 0;
  346. OSSL_METHOD_STORE *store;
  347. OSSL_PROVIDER prov = { 1 };
  348. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  349. || !add_property_names("position", NULL))
  350. goto err;
  351. for (i = 0; i < OSSL_NELEM(impls); i++)
  352. if (!TEST_true(ossl_method_store_add(store, &prov, impls[i].nid,
  353. impls[i].prop, impls[i].impl,
  354. &up_ref, &down_ref))) {
  355. TEST_note("iteration %zd", i + 1);
  356. goto err;
  357. }
  358. /* Deregister in a different order to registration */
  359. for (i = 0; i < OSSL_NELEM(impls); i++) {
  360. const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
  361. int nid = impls[j].nid;
  362. void *impl = impls[j].impl;
  363. if (!TEST_true(ossl_method_store_remove(store, nid, impl))
  364. || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
  365. TEST_note("iteration %zd, position %zd", i + 1, j + 1);
  366. goto err;
  367. }
  368. }
  369. if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
  370. ret = 1;
  371. err:
  372. ossl_method_store_free(store);
  373. return ret;
  374. }
  375. static int test_property(void)
  376. {
  377. static OSSL_PROVIDER fake_provider1 = { 1 };
  378. static OSSL_PROVIDER fake_provider2 = { 2 };
  379. static const OSSL_PROVIDER *fake_prov1 = &fake_provider1;
  380. static const OSSL_PROVIDER *fake_prov2 = &fake_provider2;
  381. static const struct {
  382. const OSSL_PROVIDER **prov;
  383. int nid;
  384. const char *prop;
  385. char *impl;
  386. } impls[] = {
  387. { &fake_prov1, 1, "fast=no, colour=green", "a" },
  388. { &fake_prov1, 1, "fast, colour=blue", "b" },
  389. { &fake_prov1, 1, "", "-" },
  390. { &fake_prov2, 9, "sky=blue, furry", "c" },
  391. { &fake_prov2, 3, NULL, "d" },
  392. { &fake_prov2, 6, "sky.colour=blue, sky=green, old.data", "e" },
  393. };
  394. static struct {
  395. const OSSL_PROVIDER **prov;
  396. int nid;
  397. const char *prop;
  398. char *expected;
  399. } queries[] = {
  400. { &fake_prov1, 1, "fast", "b" },
  401. { &fake_prov1, 1, "fast=yes", "b" },
  402. { &fake_prov1, 1, "fast=no, colour=green", "a" },
  403. { &fake_prov1, 1, "colour=blue, fast", "b" },
  404. { &fake_prov1, 1, "colour=blue", "b" },
  405. { &fake_prov2, 9, "furry", "c" },
  406. { &fake_prov2, 6, "sky.colour=blue", "e" },
  407. { &fake_prov2, 6, "old.data", "e" },
  408. { &fake_prov2, 9, "furry=yes, sky=blue", "c" },
  409. { &fake_prov1, 1, "", "a" },
  410. { &fake_prov2, 3, "", "d" },
  411. };
  412. OSSL_METHOD_STORE *store;
  413. size_t i;
  414. int ret = 0;
  415. void *result;
  416. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  417. || !add_property_names("fast", "colour", "sky", "furry", NULL))
  418. goto err;
  419. for (i = 0; i < OSSL_NELEM(impls); i++)
  420. if (!TEST_true(ossl_method_store_add(store, *impls[i].prov,
  421. impls[i].nid, impls[i].prop,
  422. impls[i].impl,
  423. &up_ref, &down_ref))) {
  424. TEST_note("iteration %zd", i + 1);
  425. goto err;
  426. }
  427. /*
  428. * The first check of queries is with NULL given as provider. All
  429. * queries are expected to succeed.
  430. */
  431. for (i = 0; i < OSSL_NELEM(queries); i++) {
  432. const OSSL_PROVIDER *nullprov = NULL;
  433. OSSL_PROPERTY_LIST *pq = NULL;
  434. if (!TEST_true(ossl_method_store_fetch(store,
  435. queries[i].nid, queries[i].prop,
  436. &nullprov, &result))
  437. || !TEST_str_eq((char *)result, queries[i].expected)) {
  438. TEST_note("iteration %zd", i + 1);
  439. ossl_property_free(pq);
  440. goto err;
  441. }
  442. ossl_property_free(pq);
  443. }
  444. /*
  445. * The second check of queries is with &address1 given as provider.
  446. */
  447. for (i = 0; i < OSSL_NELEM(queries); i++) {
  448. OSSL_PROPERTY_LIST *pq = NULL;
  449. result = NULL;
  450. if (queries[i].prov == &fake_prov1) {
  451. if (!TEST_true(ossl_method_store_fetch(store,
  452. queries[i].nid,
  453. queries[i].prop,
  454. &fake_prov1, &result))
  455. || !TEST_ptr_eq(fake_prov1, &fake_provider1)
  456. || !TEST_str_eq((char *)result, queries[i].expected)) {
  457. TEST_note("iteration %zd", i + 1);
  458. ossl_property_free(pq);
  459. goto err;
  460. }
  461. } else {
  462. if (!TEST_false(ossl_method_store_fetch(store,
  463. queries[i].nid,
  464. queries[i].prop,
  465. &fake_prov1, &result))
  466. || !TEST_ptr_eq(fake_prov1, &fake_provider1)
  467. || !TEST_ptr_null(result)) {
  468. TEST_note("iteration %zd", i + 1);
  469. ossl_property_free(pq);
  470. goto err;
  471. }
  472. }
  473. ossl_property_free(pq);
  474. }
  475. /*
  476. * The third check of queries is with &address2 given as provider.
  477. */
  478. for (i = 0; i < OSSL_NELEM(queries); i++) {
  479. OSSL_PROPERTY_LIST *pq = NULL;
  480. result = NULL;
  481. if (queries[i].prov == &fake_prov2) {
  482. if (!TEST_true(ossl_method_store_fetch(store,
  483. queries[i].nid,
  484. queries[i].prop,
  485. &fake_prov2, &result))
  486. || !TEST_ptr_eq(fake_prov2, &fake_provider2)
  487. || !TEST_str_eq((char *)result, queries[i].expected)) {
  488. TEST_note("iteration %zd", i + 1);
  489. ossl_property_free(pq);
  490. goto err;
  491. }
  492. } else {
  493. if (!TEST_false(ossl_method_store_fetch(store,
  494. queries[i].nid,
  495. queries[i].prop,
  496. &fake_prov2, &result))
  497. || !TEST_ptr_eq(fake_prov2, &fake_provider2)
  498. || !TEST_ptr_null(result)) {
  499. TEST_note("iteration %zd", i + 1);
  500. ossl_property_free(pq);
  501. goto err;
  502. }
  503. }
  504. ossl_property_free(pq);
  505. }
  506. ret = 1;
  507. err:
  508. ossl_method_store_free(store);
  509. return ret;
  510. }
  511. static int test_query_cache_stochastic(void)
  512. {
  513. const int max = 10000, tail = 10;
  514. OSSL_METHOD_STORE *store;
  515. int i, res = 0;
  516. char buf[50];
  517. void *result;
  518. int errors = 0;
  519. int v[10001];
  520. OSSL_PROVIDER prov = { 1 };
  521. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  522. || !add_property_names("n", NULL))
  523. goto err;
  524. for (i = 1; i <= max; i++) {
  525. v[i] = 2 * i;
  526. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  527. if (!TEST_true(ossl_method_store_add(store, &prov, i, buf, "abc",
  528. &up_ref, &down_ref))
  529. || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
  530. buf, v + i,
  531. &up_ref, &down_ref))
  532. || !TEST_true(ossl_method_store_cache_set(store, &prov, i,
  533. "n=1234", "miss",
  534. &up_ref, &down_ref))) {
  535. TEST_note("iteration %d", i);
  536. goto err;
  537. }
  538. }
  539. for (i = 1; i <= max; i++) {
  540. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  541. if (!ossl_method_store_cache_get(store, NULL, i, buf, &result)
  542. || result != v + i)
  543. errors++;
  544. }
  545. /* There is a tiny probability that this will fail when it shouldn't */
  546. res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
  547. err:
  548. ossl_method_store_free(store);
  549. return res;
  550. }
  551. static int test_fips_mode(void)
  552. {
  553. int ret = 0;
  554. OSSL_LIB_CTX *ctx = NULL;
  555. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new()))
  556. goto err;
  557. ret = TEST_true(EVP_set_default_properties(ctx, "default=yes,fips=yes"))
  558. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  559. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  560. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  561. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  562. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  563. && TEST_true(EVP_set_default_properties(ctx, "fips!=no"))
  564. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  565. && TEST_true(EVP_set_default_properties(ctx, "fips=no"))
  566. && TEST_false(EVP_default_properties_is_fips_enabled(ctx))
  567. && TEST_true(EVP_set_default_properties(ctx, "fips=no,default=yes"))
  568. && TEST_true(EVP_default_properties_enable_fips(ctx, 1))
  569. && TEST_true(EVP_default_properties_is_fips_enabled(ctx))
  570. && TEST_true(EVP_default_properties_enable_fips(ctx, 0))
  571. && TEST_false(EVP_default_properties_is_fips_enabled(ctx));
  572. err:
  573. OSSL_LIB_CTX_free(ctx);
  574. return ret;
  575. }
  576. static struct {
  577. const char *in;
  578. const char *out;
  579. } to_string_tests[] = {
  580. { "fips=yes", "fips=yes" },
  581. { "fips!=yes", "fips!=yes" },
  582. { "fips = yes", "fips=yes" },
  583. { "fips", "fips=yes" },
  584. { "fips=no", "fips=no" },
  585. { "-fips", "-fips" },
  586. { "?fips=yes", "?fips=yes" },
  587. { "fips=yes,provider=fips", "fips=yes,provider=fips" },
  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=yes,-provider" },
  592. /* foo is an unknown internal name */
  593. { "foo=yes,fips=yes", "fips=yes"},
  594. { "", "" },
  595. { "fips=3", "fips=3" },
  596. { "fips=-3", "fips=-3" },
  597. { NULL, "" }
  598. };
  599. static int test_property_list_to_string(int i)
  600. {
  601. OSSL_PROPERTY_LIST *pl = NULL;
  602. int ret = 0;
  603. size_t bufsize;
  604. char *buf = NULL;
  605. if (to_string_tests[i].in != NULL
  606. && !TEST_ptr(pl = ossl_parse_query(NULL, to_string_tests[i].in, 1)))
  607. goto err;
  608. bufsize = ossl_property_list_to_string(NULL, pl, NULL, 0);
  609. if (!TEST_size_t_gt(bufsize, 0))
  610. goto err;
  611. buf = OPENSSL_malloc(bufsize);
  612. if (!TEST_ptr(buf)
  613. || !TEST_size_t_eq(ossl_property_list_to_string(NULL, pl, buf,
  614. bufsize),
  615. bufsize)
  616. || !TEST_str_eq(to_string_tests[i].out, buf)
  617. || !TEST_size_t_eq(bufsize, strlen(to_string_tests[i].out) + 1))
  618. goto err;
  619. ret = 1;
  620. err:
  621. OPENSSL_free(buf);
  622. ossl_property_free(pl);
  623. return ret;
  624. }
  625. int setup_tests(void)
  626. {
  627. ADD_TEST(test_property_string);
  628. ADD_TEST(test_property_query_value_create);
  629. ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
  630. ADD_ALL_TESTS(test_property_parse_error, OSSL_NELEM(parse_error_tests));
  631. ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
  632. ADD_TEST(test_property_defn_cache);
  633. ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
  634. ADD_TEST(test_register_deregister);
  635. ADD_TEST(test_property);
  636. ADD_TEST(test_query_cache_stochastic);
  637. ADD_TEST(test_fips_mode);
  638. ADD_ALL_TESTS(test_property_list_to_string, OSSL_NELEM(to_string_tests));
  639. return 1;
  640. }