property_test.c 12 KB

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