key-value-parse.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <setjmp.h>
  15. #include <cmocka.h>
  16. #include "util/key-value-parse.h"
  17. /*
  18. * Ensure that a simple key / value string is parsed into its component parts.
  19. */
  20. static void
  21. parse_key_value_simple_test (void **state)
  22. {
  23. bool ret;
  24. char test_str[] = "key=value";
  25. key_value_t key_value = KEY_VALUE_INIT;
  26. ret = parse_key_value (test_str, &key_value);
  27. assert_true (ret);
  28. assert_string_equal (key_value.key, "key");
  29. assert_string_equal (key_value.value, "value");
  30. }
  31. /*
  32. * Ensure that a NULL key/value string causes parse_key_value to return false.
  33. */
  34. static void
  35. parse_key_value_NULL_string_test (void **state)
  36. {
  37. bool ret;
  38. key_value_t key_value = KEY_VALUE_INIT;
  39. ret = parse_key_value (NULL, &key_value);
  40. assert_false (ret);
  41. }
  42. /*
  43. * Ensure that a NULL key_value_t parameter causes parse_key_value to return
  44. * false.
  45. */
  46. static void
  47. parse_key_value_NULL_key_value_test (void **state)
  48. {
  49. bool ret;
  50. char test_str[] = "key=value";
  51. ret = parse_key_value (test_str, NULL);
  52. assert_false (ret);
  53. }
  54. /*
  55. * Ensure that an incomplete key/value string with only the "key=" returns
  56. * false.
  57. */
  58. static void
  59. parse_key_value_no_value_test (void **state)
  60. {
  61. bool ret;
  62. char test_str[] = "key=";
  63. key_value_t key_value = KEY_VALUE_INIT;
  64. ret = parse_key_value (test_str, &key_value);
  65. assert_false (ret);
  66. }
  67. /*
  68. * Ensure that a key/value string with only the "=value" part returns false.
  69. */
  70. static void
  71. parse_key_value_no_key_test (void **state)
  72. {
  73. bool ret;
  74. char test_str[] = "=value";
  75. key_value_t key_value = KEY_VALUE_INIT;
  76. ret = parse_key_value (test_str, &key_value);
  77. assert_false (ret);
  78. }
  79. /*
  80. * Ensure that a key/value string with the separators in the wrong place
  81. * returns false.
  82. */
  83. static void
  84. parse_key_value_two_seps_test (void **state)
  85. {
  86. bool ret;
  87. char test_str[] = "=foo=";
  88. key_value_t key_value = KEY_VALUE_INIT;
  89. ret = parse_key_value (test_str, &key_value);
  90. assert_false (ret);
  91. }
  92. /*
  93. * Ensure that a key/value string with all separators returns false.
  94. */
  95. static void
  96. parse_key_value_all_seps_test (void **state)
  97. {
  98. bool ret;
  99. char test_str[] = "====";
  100. key_value_t key_value = KEY_VALUE_INIT;
  101. ret = parse_key_value (test_str, &key_value);
  102. assert_false (ret);
  103. }
  104. /*
  105. * Ensure that a key/value string that alternates strings and separators
  106. * will parse the first two and ignore the rest.
  107. */
  108. static void
  109. parse_key_value_alt_seps_test (void **state)
  110. {
  111. bool ret;
  112. char test_str[] = "key=value=key=value";
  113. key_value_t key_value = KEY_VALUE_INIT;
  114. ret = parse_key_value (test_str, &key_value);
  115. assert_true (ret);
  116. assert_string_equal (key_value.key, "key");
  117. assert_string_equal (key_value.value, "value");
  118. }
  119. /*
  120. * This is a simple data structure used to hold values parsed from a string
  121. * of key/value pairs.
  122. */
  123. #define TEST_DATA_INIT { \
  124. .value0 = NULL, \
  125. .value1 = NULL, \
  126. }
  127. typedef struct {
  128. char *value0;
  129. char *value1;
  130. } test_data_t;
  131. /*
  132. * This is a callback function used to handle extracted key / value pairs.
  133. */
  134. TSS2_RC
  135. key_value_callback (const key_value_t *key_value,
  136. void *user_data)
  137. {
  138. test_data_t *test_data = (test_data_t*)user_data;
  139. if (strcmp ("key0", key_value->key) == 0) {
  140. test_data->value0 = key_value->value;
  141. return TSS2_RC_SUCCESS;
  142. } else if (strcmp ("key1", key_value->key) == 0) {
  143. test_data->value1 = key_value->value;
  144. return TSS2_RC_SUCCESS;
  145. } else {
  146. return 1;
  147. }
  148. }
  149. /*
  150. * This tests the typical case for the parsing of a string of key / value
  151. * pairs.
  152. */
  153. static void
  154. parse_key_value_string_good_test (void **state)
  155. {
  156. TSS2_RC rc;
  157. char test_str[] = "key0=value0,key1=value1";
  158. test_data_t test_data = TEST_DATA_INIT;
  159. rc = parse_key_value_string (test_str, key_value_callback, &test_data);
  160. assert_int_equal (rc, TSS2_RC_SUCCESS);
  161. assert_string_equal (test_data.value0, "value0");
  162. assert_string_equal (test_data.value1, "value1");
  163. }
  164. /*
  165. * This test ensures that he parse_key_value_string function handles a failed
  166. * call to parse a key/value pair properly.
  167. */
  168. static void
  169. parse_key_value_string_no_value_test (void **state)
  170. {
  171. TSS2_RC rc;
  172. char test_str[] = "key0=,key1=value1";
  173. test_data_t test_data = TEST_DATA_INIT;
  174. rc = parse_key_value_string (test_str, key_value_callback, &test_data);
  175. assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
  176. }
  177. /*
  178. * This test ensures that the parse_key_value_string function handles a failed
  179. * call to the user provided callback properly. The return value we get from
  180. * the parse_key_value_string function is the same value returned by our
  181. * callback.
  182. */
  183. static void
  184. parse_key_value_string_unknown_key_test (void **state)
  185. {
  186. TSS2_RC rc;
  187. char test_str[] = "key0=foo=bar,baz=qux";
  188. test_data_t test_data = TEST_DATA_INIT;
  189. rc = parse_key_value_string (test_str, key_value_callback, &test_data);
  190. assert_int_equal (rc, 1);
  191. }
  192. /*
  193. * The following 3 tests ensures that NULL parameters produce an error.
  194. */
  195. static void
  196. parse_key_value_string_NULL_kv_string_test (void **state)
  197. {
  198. TSS2_RC rc;
  199. test_data_t test_data = TEST_DATA_INIT;
  200. rc = parse_key_value_string (NULL, key_value_callback, &test_data);
  201. assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
  202. }
  203. static void
  204. parse_key_value_string_NULL_callback_test (void **state)
  205. {
  206. TSS2_RC rc;
  207. char test_str[] = "key0=foo=bar,baz=qux";
  208. test_data_t test_data = TEST_DATA_INIT;
  209. rc = parse_key_value_string (test_str, NULL, &test_data);
  210. assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
  211. }
  212. static void
  213. parse_key_value_string_NULL_user_data_test (void **state)
  214. {
  215. TSS2_RC rc;
  216. char test_str[] = "key0=foo=bar,baz=qux";
  217. rc = parse_key_value_string (test_str, key_value_callback, NULL);
  218. assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
  219. }
  220. int
  221. main(int argc, char* argv[])
  222. {
  223. const struct CMUnitTest tests[] = {
  224. cmocka_unit_test (parse_key_value_simple_test),
  225. cmocka_unit_test (parse_key_value_NULL_string_test),
  226. cmocka_unit_test (parse_key_value_NULL_key_value_test),
  227. cmocka_unit_test (parse_key_value_no_value_test),
  228. cmocka_unit_test (parse_key_value_no_key_test),
  229. cmocka_unit_test (parse_key_value_two_seps_test),
  230. cmocka_unit_test (parse_key_value_all_seps_test),
  231. cmocka_unit_test (parse_key_value_alt_seps_test),
  232. cmocka_unit_test (parse_key_value_string_good_test),
  233. cmocka_unit_test (parse_key_value_string_no_value_test),
  234. cmocka_unit_test (parse_key_value_string_unknown_key_test),
  235. cmocka_unit_test (parse_key_value_string_NULL_kv_string_test),
  236. cmocka_unit_test (parse_key_value_string_NULL_callback_test),
  237. cmocka_unit_test (parse_key_value_string_NULL_user_data_test),
  238. };
  239. return cmocka_run_group_tests (tests, NULL, NULL);
  240. }