test_options.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdarg.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <setjmp.h>
  6. #include <cmocka.h>
  7. #include "tpm2_options.h"
  8. #include "tpm2_util.h"
  9. typedef struct test_pair test_pair;
  10. struct test_pair {
  11. union {
  12. const char *cn;
  13. void *v;
  14. } input[2];
  15. union {
  16. char *s;
  17. void *v;
  18. int i;
  19. } output;
  20. };
  21. static void set_getenv(const char *name, char *ret) {
  22. test_pair *e = calloc(1, sizeof(test_pair));
  23. assert_non_null(e);
  24. e->input[0].cn = name;
  25. e->output.s = ret;
  26. will_return(__wrap_tpm2_util_getenv, e);
  27. }
  28. #define HANDLE_SKIP_CHECK ((void *) -1)
  29. char *__wrap_tpm2_util_getenv(const char *name) {
  30. test_pair *x = mock_ptr_type(test_pair *);
  31. const char *expected_name = x->input[0].cn;
  32. char *ret = x->output.s;
  33. free(x);
  34. assert_string_equal(name, expected_name);
  35. return ret;
  36. }
  37. TSS2_RC __wrap_Tss2_TctiLdr_Initialize(const char *nameConf,
  38. TSS2_TCTI_CONTEXT **context) {
  39. UNUSED(nameConf);
  40. printf("fml\n");
  41. TSS2_RC rc = mock_type(TSS2_RC);
  42. if (rc == TSS2_RC_SUCCESS) {
  43. *context = mock_type(TSS2_TCTI_CONTEXT*);
  44. }
  45. return rc;
  46. }
  47. static TSS2_TCTI_CONTEXT_COMMON_V2 tcti_instance;
  48. static void common_prelude(void) {
  49. /*
  50. * NULL getenv for the tcti config results
  51. * in a default TCTI based on a probe using dlopen().
  52. *
  53. * If we find that tcti, we return the tcti conf, which
  54. * then results in us following the normal tcti loading logic.
  55. */
  56. set_getenv(TPM2TOOLS_ENV_TCTI, NULL);
  57. /* mock Tss2_TctiLdr_Initialize */
  58. will_return (__wrap_Tss2_TctiLdr_Initialize, TSS2_RC_SUCCESS);
  59. will_return (__wrap_Tss2_TctiLdr_Initialize, &tcti_instance);
  60. }
  61. static void test_null_tcti_getenv_no_errata(void **state) {
  62. UNUSED(state);
  63. char *argv[] = {
  64. "program",
  65. "-Z" // Disable errata getenv call
  66. };
  67. int argc = ARRAY_LEN(argv);
  68. tpm2_options *tool_opts = NULL;
  69. tpm2_option_flags flags = { .all = 0 };
  70. TSS2_TCTI_CONTEXT *tcti = NULL;
  71. common_prelude();
  72. tpm2_option_code oc = tpm2_handle_options(argc, argv, tool_opts, &flags,
  73. &tcti);
  74. assert_non_null(tcti);
  75. assert_int_equal(oc, tpm2_option_code_continue);
  76. }
  77. static void test_null_tcti_getenv_with_errata(void **state) {
  78. UNUSED(state);
  79. char *argv[] = {
  80. "program",
  81. // Enable errata getenv call via no -Z
  82. };
  83. int argc = ARRAY_LEN(argv);
  84. tpm2_options *tool_opts = NULL;
  85. tpm2_option_flags flags = { .all = 0 };
  86. TSS2_TCTI_CONTEXT *tcti = NULL;
  87. common_prelude();
  88. set_getenv(TPM2TOOLS_ENV_ENABLE_ERRATA, NULL);
  89. tpm2_option_code oc = tpm2_handle_options(argc, argv, tool_opts, &flags,
  90. &tcti);
  91. assert_non_null(tcti);
  92. assert_int_equal(oc, tpm2_option_code_continue);
  93. }
  94. static void test_tcti_short_option_no_errata(void **state) {
  95. UNUSED(state);
  96. char *argv[] = {
  97. "program",
  98. "-T", // Set TCTI to something specific
  99. "tctifake",
  100. "-Z" // Disable errata getenv call
  101. };
  102. int argc = ARRAY_LEN(argv);
  103. tpm2_options *tool_opts = NULL;
  104. tpm2_option_flags flags = { .all = 0 };
  105. TSS2_TCTI_CONTEXT *tcti = NULL;
  106. /* we never call getenv() because we use -T */
  107. /* we never probe for a tcti */
  108. /* we just use what is given, in this case, return a mocked instance */
  109. will_return(__wrap_Tss2_TctiLdr_Initialize, TSS2_RC_SUCCESS);
  110. will_return(__wrap_Tss2_TctiLdr_Initialize, &tcti_instance);
  111. tpm2_option_code oc = tpm2_handle_options(argc, argv, tool_opts, &flags,
  112. &tcti);
  113. assert_non_null(tcti);
  114. assert_int_equal(oc, tpm2_option_code_continue);
  115. }
  116. static void test_tcti_long_option_with_equals_no_errata(void **state) {
  117. UNUSED(state);
  118. char *argv[] = {
  119. "program",
  120. "--tcti=tctifake", // Set TCTI to something specific
  121. "-Z" // Disable errata getenv call
  122. };
  123. int argc = ARRAY_LEN(argv);
  124. tpm2_options *tool_opts = NULL;
  125. tpm2_option_flags flags = { .all = 0 };
  126. TSS2_TCTI_CONTEXT *tcti = NULL;
  127. /* we never call getenv() because we use -T */
  128. /* we never probe for a tcti */
  129. /* we just use what is given, in this case, return a mocked instance */
  130. will_return(__wrap_Tss2_TctiLdr_Initialize, TSS2_RC_SUCCESS);
  131. will_return(__wrap_Tss2_TctiLdr_Initialize, &tcti_instance);
  132. tpm2_option_code oc = tpm2_handle_options(argc, argv, tool_opts, &flags,
  133. &tcti);
  134. assert_non_null(tcti);
  135. assert_int_equal(oc, tpm2_option_code_continue);
  136. }
  137. static void test_tcti_long_option_no_equals_no_errata(void **state) {
  138. UNUSED(state);
  139. char *argv[] = {
  140. "program",
  141. "--tcti", // Set TCTI to something specific
  142. "tctifake",
  143. "-Z" // Disable errata getenv call
  144. };
  145. int argc = ARRAY_LEN(argv);
  146. tpm2_options *tool_opts = NULL;
  147. tpm2_option_flags flags = { .all = 0 };
  148. TSS2_TCTI_CONTEXT *tcti = NULL;
  149. /* we never call getenv() because we use -T */
  150. /* we never probe for a tcti */
  151. /* we just use what is given, in this case, return a mocked instance */
  152. will_return(__wrap_Tss2_TctiLdr_Initialize, TSS2_RC_SUCCESS);
  153. will_return(__wrap_Tss2_TctiLdr_Initialize, &tcti_instance);
  154. tpm2_option_code oc = tpm2_handle_options(argc, argv, tool_opts, &flags,
  155. &tcti);
  156. assert_non_null(tcti);
  157. assert_int_equal(oc, tpm2_option_code_continue);
  158. }
  159. static void test_invalid_tcti_no_errata(void **state) {
  160. UNUSED(state);
  161. char *argv[] = {
  162. "program",
  163. "-T", // Set TCTI to something specific
  164. "tctiinvalid",
  165. "-Z" // Disable errata getenv call
  166. };
  167. int argc = ARRAY_LEN(argv);
  168. tpm2_options *tool_opts = NULL;
  169. tpm2_option_flags flags = { .all = 0 };
  170. TSS2_TCTI_CONTEXT *tcti = NULL;
  171. will_return(__wrap_Tss2_TctiLdr_Initialize, TSS2_TCTI_RC_NOT_SUPPORTED);
  172. tpm2_option_code oc = tpm2_handle_options(argc, argv, tool_opts, &flags,
  173. &tcti);
  174. assert_int_equal(oc, tpm2_option_code_err);
  175. }
  176. /*
  177. * link required symbol, but tpm2_tool.c declares it AND main, which
  178. * we have a main below for cmocka tests.
  179. */
  180. bool output_enabled = true;
  181. int main(int argc, char *argv[]) {
  182. UNUSED(argc);
  183. UNUSED(argv);
  184. const struct CMUnitTest tests[] = {
  185. cmocka_unit_test(test_null_tcti_getenv_no_errata),
  186. cmocka_unit_test(test_null_tcti_getenv_with_errata),
  187. cmocka_unit_test(test_tcti_short_option_no_errata),
  188. cmocka_unit_test(test_tcti_long_option_no_equals_no_errata),
  189. cmocka_unit_test(test_tcti_long_option_with_equals_no_errata),
  190. cmocka_unit_test(test_invalid_tcti_no_errata),
  191. };
  192. return cmocka_run_group_tests(tests, NULL, NULL);
  193. }