property_test.c 25 KB

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