property_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_local.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, NULL, impls[i].nid,
  224. impls[i].prop, impls[i].impl,
  225. NULL, NULL))) {
  226. TEST_note("iteration %zd", i + 1);
  227. goto err;
  228. }
  229. /* Deregister in a different order to registration */
  230. for (i = 0; i < OSSL_NELEM(impls); i++) {
  231. const size_t j = (1 + i * 3) % OSSL_NELEM(impls);
  232. int nid = impls[j].nid;
  233. void *impl = impls[j].impl;
  234. if (!TEST_true(ossl_method_store_remove(store, nid, impl))
  235. || !TEST_false(ossl_method_store_remove(store, nid, impl))) {
  236. TEST_note("iteration %zd, position %zd", i + 1, j + 1);
  237. goto err;
  238. }
  239. }
  240. if (TEST_false(ossl_method_store_remove(store, impls[0].nid, impls[0].impl)))
  241. ret = 1;
  242. err:
  243. ossl_method_store_free(store);
  244. return ret;
  245. }
  246. static int test_property(void)
  247. {
  248. static const struct {
  249. int nid;
  250. const char *prop;
  251. char *impl;
  252. } impls[] = {
  253. { 1, "fast=no, colour=green", "a" },
  254. { 1, "fast, colour=blue", "b" },
  255. { 1, "", "-" },
  256. { 9, "sky=blue, furry", "c" },
  257. { 3, NULL, "d" },
  258. { 6, "sky.colour=blue, sky=green, old.data", "e" },
  259. };
  260. static struct {
  261. int nid;
  262. const char *prop;
  263. char *expected;
  264. } queries[] = {
  265. { 1, "fast", "b" },
  266. { 1, "fast=yes", "b" },
  267. { 1, "fast=no, colour=green", "a" },
  268. { 1, "colour=blue, fast", "b" },
  269. { 1, "colour=blue", "b" },
  270. { 9, "furry", "c" },
  271. { 6, "sky.colour=blue", "e" },
  272. { 6, "old.data", "e" },
  273. { 9, "furry=yes, sky=blue", "c" },
  274. { 1, "", "a" },
  275. { 3, "", "d" },
  276. };
  277. OSSL_METHOD_STORE *store;
  278. size_t i;
  279. int ret = 0;
  280. void *result;
  281. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  282. || !add_property_names("fast", "colour", "sky", "furry", NULL))
  283. goto err;
  284. for (i = 0; i < OSSL_NELEM(impls); i++)
  285. if (!TEST_true(ossl_method_store_add(store, NULL, impls[i].nid,
  286. impls[i].prop, impls[i].impl,
  287. NULL, NULL))) {
  288. TEST_note("iteration %zd", i + 1);
  289. goto err;
  290. }
  291. for (i = 0; i < OSSL_NELEM(queries); i++) {
  292. OSSL_PROPERTY_LIST *pq = NULL;
  293. if (!TEST_true(ossl_method_store_fetch(store, queries[i].nid,
  294. queries[i].prop, &result))
  295. || !TEST_str_eq((char *)result, queries[i].expected)) {
  296. TEST_note("iteration %zd", i + 1);
  297. ossl_property_free(pq);
  298. goto err;
  299. }
  300. ossl_property_free(pq);
  301. }
  302. ret = 1;
  303. err:
  304. ossl_method_store_free(store);
  305. return ret;
  306. }
  307. static int test_query_cache_stochastic(void)
  308. {
  309. const int max = 10000, tail = 10;
  310. OSSL_METHOD_STORE *store;
  311. int i, res = 0;
  312. char buf[50];
  313. void *result;
  314. int errors = 0;
  315. int v[10001];
  316. if (!TEST_ptr(store = ossl_method_store_new(NULL))
  317. || !add_property_names("n", NULL))
  318. goto err;
  319. for (i = 1; i <= max; i++) {
  320. v[i] = 2 * i;
  321. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  322. if (!TEST_true(ossl_method_store_add(store, NULL, i, buf, "abc",
  323. NULL, NULL))
  324. || !TEST_true(ossl_method_store_cache_set(store, i, buf, v + i))
  325. || !TEST_true(ossl_method_store_cache_set(store, i, "n=1234",
  326. "miss"))) {
  327. TEST_note("iteration %d", i);
  328. goto err;
  329. }
  330. }
  331. for (i = 1; i <= max; i++) {
  332. BIO_snprintf(buf, sizeof(buf), "n=%d\n", i);
  333. if (!ossl_method_store_cache_get(store, i, buf, &result)
  334. || result != v + i)
  335. errors++;
  336. }
  337. /* There is a tiny probability that this will fail when it shouldn't */
  338. res = TEST_int_gt(errors, tail) && TEST_int_lt(errors, max - tail);
  339. err:
  340. ossl_method_store_free(store);
  341. return res;
  342. }
  343. int setup_tests(void)
  344. {
  345. ADD_TEST(test_property_string);
  346. ADD_ALL_TESTS(test_property_parse, OSSL_NELEM(parser_tests));
  347. ADD_ALL_TESTS(test_property_merge, OSSL_NELEM(merge_tests));
  348. ADD_TEST(test_property_defn_cache);
  349. ADD_ALL_TESTS(test_definition_compares, OSSL_NELEM(definition_tests));
  350. ADD_TEST(test_register_deregister);
  351. ADD_TEST(test_property);
  352. ADD_TEST(test_query_cache_stochastic);
  353. return 1;
  354. }