property_test.c 12 KB

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