test_tpm2_util.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <setjmp.h>
  5. #include <cmocka.h>
  6. #include "tpm2_hierarchy.h"
  7. #include "tpm2_util.h"
  8. static void test_tpm2_util_handle_from_optarg_NULL(void **state) {
  9. UNUSED(state);
  10. TPMI_RH_PROVISION h;
  11. bool result = tpm2_util_handle_from_optarg(NULL, &h,
  12. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  13. assert_false(result);
  14. }
  15. static void test_tpm2_util_handle_from_optarg_empty(void **state) {
  16. UNUSED(state);
  17. TPMI_RH_PROVISION h;
  18. bool result = tpm2_util_handle_from_optarg("", &h,
  19. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  20. assert_false(result);
  21. }
  22. static void test_tpm2_util_handle_from_optarg_invalid_id(void **state) {
  23. UNUSED(state);
  24. TPMI_RH_PROVISION h;
  25. bool result = tpm2_util_handle_from_optarg("q", &h,
  26. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  27. assert_false(result);
  28. }
  29. static void test_tpm2_util_handle_from_optarg_invalid_str(void **state) {
  30. UNUSED(state);
  31. TPMI_RH_PROVISION h;
  32. bool result = tpm2_util_handle_from_optarg("nope", &h,
  33. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  34. assert_false(result);
  35. }
  36. static void test_tpm2_util_handle_from_optarg_valid_ids(void **state) {
  37. UNUSED(state);
  38. TPMI_RH_PROVISION h;
  39. bool result = tpm2_util_handle_from_optarg("o", &h,
  40. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  41. assert_true(result);
  42. assert_int_equal(h, TPM2_RH_OWNER);
  43. result = tpm2_util_handle_from_optarg("p", &h,
  44. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  45. assert_true(result);
  46. assert_int_equal(h, TPM2_RH_PLATFORM);
  47. result = tpm2_util_handle_from_optarg("e", &h,
  48. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  49. assert_true(result);
  50. assert_int_equal(h, TPM2_RH_ENDORSEMENT);
  51. result = tpm2_util_handle_from_optarg("n", &h,
  52. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  53. assert_true(result);
  54. assert_int_equal(h, TPM2_RH_NULL);
  55. result = tpm2_util_handle_from_optarg("0x81010009", &h,
  56. TPM2_HANDLE_ALL_W_NV);
  57. assert_true(result);
  58. assert_int_equal(h, 0x81010009);
  59. }
  60. static void test_tpm2_util_handle_from_optarg_valid_ids_disabled(void **state) {
  61. UNUSED(state);
  62. TPMI_RH_PROVISION h;
  63. bool result = tpm2_util_handle_from_optarg("o", &h, TPM2_HANDLE_FLAGS_N);
  64. assert_false(result);
  65. result = tpm2_util_handle_from_optarg("p", &h, TPM2_HANDLE_FLAGS_O);
  66. assert_false(result);
  67. result = tpm2_util_handle_from_optarg("e", &h, TPM2_HANDLE_FLAGS_P);
  68. assert_false(result);
  69. result = tpm2_util_handle_from_optarg("n", &h, TPM2_HANDLE_FLAGS_E);
  70. assert_false(result);
  71. result = tpm2_util_handle_from_optarg("0x81010009", &h,
  72. TPM2_HANDLE_FLAGS_ALL_HIERACHIES);
  73. assert_false(result);
  74. }
  75. static void test_tpm2_util_handle_from_optarg_valid_ids_enabled(void **state) {
  76. UNUSED(state);
  77. TPMI_RH_PROVISION h;
  78. bool result = tpm2_util_handle_from_optarg("o", &h, TPM2_HANDLE_FLAGS_O);
  79. assert_true(result);
  80. assert_int_equal(h, TPM2_RH_OWNER);
  81. result = tpm2_util_handle_from_optarg("p", &h, TPM2_HANDLE_FLAGS_P);
  82. assert_true(result);
  83. assert_int_equal(h, TPM2_RH_PLATFORM);
  84. result = tpm2_util_handle_from_optarg("e", &h, TPM2_HANDLE_FLAGS_E);
  85. assert_true(result);
  86. assert_int_equal(h, TPM2_RH_ENDORSEMENT);
  87. result = tpm2_util_handle_from_optarg("n", &h, TPM2_HANDLE_FLAGS_N);
  88. assert_true(result);
  89. assert_int_equal(h, TPM2_RH_NULL);
  90. }
  91. static void test_tpm2_util_handle_from_optarg_nv_valid_range(void **state) {
  92. UNUSED(state);
  93. TPMI_RH_PROVISION h;
  94. /*
  95. * NV index specified as NV:offset
  96. */
  97. bool result = tpm2_util_handle_from_optarg("1", &h, TPM2_HANDLE_FLAGS_NV);
  98. assert_true(result);
  99. assert_int_equal(h, 0x01000001);
  100. /*
  101. * NV index specified as full raw handle
  102. */
  103. result = tpm2_util_handle_from_optarg("0x01000002", &h,
  104. TPM2_HANDLE_FLAGS_NV);
  105. assert_true(result);
  106. assert_int_equal(h, 0x01000002);
  107. }
  108. static void test_tpm2_util_handle_from_optarg_nv_invalid_offset(void **state) {
  109. UNUSED(state);
  110. TPMI_RH_PROVISION h;
  111. /*
  112. * No offset specified
  113. */
  114. bool result = tpm2_util_handle_from_optarg("", &h, TPM2_HANDLE_FLAGS_NV);
  115. assert_false(result);
  116. /*
  117. * Offset is non hex string
  118. */
  119. result = tpm2_util_handle_from_optarg("random", &h, TPM2_HANDLE_FLAGS_NV);
  120. assert_false(result);
  121. /*
  122. * Offset is larger than TPM2_HR_HANDLE_MASK
  123. */
  124. result = tpm2_util_handle_from_optarg("0x12345678", &h,
  125. TPM2_HANDLE_FLAGS_NV);
  126. assert_false(result);
  127. /*
  128. * Wrongly specify NV index as raw handle and disable NV in flags
  129. */
  130. result = tpm2_util_handle_from_optarg("0x01000001", &h,
  131. TPM2_HANDLE_FLAGS_O);
  132. assert_false(result);
  133. }
  134. /* link required symbol, but tpm2_tool.c declares it AND main, which
  135. * we have a main below for cmocka tests.
  136. */
  137. bool output_enabled = true;
  138. int main(int argc, char* argv[]) {
  139. (void) argc;
  140. (void) argv;
  141. const struct CMUnitTest tests[] = {
  142. cmocka_unit_test(test_tpm2_util_handle_from_optarg_NULL),
  143. cmocka_unit_test(test_tpm2_util_handle_from_optarg_empty),
  144. cmocka_unit_test(test_tpm2_util_handle_from_optarg_invalid_id),
  145. cmocka_unit_test(test_tpm2_util_handle_from_optarg_invalid_str),
  146. cmocka_unit_test(test_tpm2_util_handle_from_optarg_valid_ids),
  147. cmocka_unit_test(test_tpm2_util_handle_from_optarg_valid_ids_disabled),
  148. cmocka_unit_test(test_tpm2_util_handle_from_optarg_valid_ids_enabled),
  149. cmocka_unit_test(test_tpm2_util_handle_from_optarg_nv_valid_range),
  150. cmocka_unit_test(test_tpm2_util_handle_from_optarg_nv_invalid_offset),
  151. };
  152. return cmocka_run_group_tests(tests, NULL, NULL);
  153. }