fapi-config.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * All rights reserved.
  5. ******************************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <stdarg.h>
  10. #include <inttypes.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <stdio.h>
  15. #include <json-c/json_object.h>
  16. #include <json-c/json_util.h>
  17. #include <json-c/json_tokener.h>
  18. #include <setjmp.h>
  19. #include <cmocka.h>
  20. #include <errno.h>
  21. #include "ifapi_io.h"
  22. #include "ifapi_config.h"
  23. #include "util/aux_util.h"
  24. #define LOGMODULE tests
  25. #include "util/log.h"
  26. /*
  27. * The unit tests will test deserialization of FAPI config files. It will be
  28. * checked whether the correct return codes are returned if optional and
  29. * mandatory fields are removed from the configuration.
  30. * Also the expansion of abbreviations for the home directory will be
  31. * tested.
  32. */
  33. /* Config file which will be used for the test. */
  34. char *wrap_config_file_content;
  35. /* JSON field which will be removed for the test. */
  36. char *wrap_remove_field;
  37. static char* config_tilde =
  38. "{" \
  39. " \"profile_name\": \"P_ECCP256SHA256\"," \
  40. " \"profile_dir\": \"~/profile\"," \
  41. " \"user_dir\": \"~/user_dir\"," \
  42. " \"system_dir\": \"~/system_dir\"," \
  43. " \"log_dir\": \"~/log_dir\"," \
  44. " \"tcti\": \"\"," \
  45. " \"system_pcrs\" : []" \
  46. "}";
  47. static char* config_home =
  48. "{" \
  49. " \"profile_name\": \"P_ECCP256SHA256\"," \
  50. " \"profile_dir\": \"$HOME/profile\"," \
  51. " \"user_dir\": \"$HOME/user_dir\"," \
  52. " \"system_dir\": \"$HOME/system_dir\"," \
  53. " \"log_dir\": \"$HOME/log_dir\"," \
  54. " \"tcti\": \"\"," \
  55. " \"system_pcrs\" : []" \
  56. "}";
  57. /*
  58. * Wrappers for reading the JSON profile.
  59. */
  60. TSS2_RC
  61. __wrap_ifapi_io_read_finish(
  62. struct IFAPI_IO *io,
  63. uint8_t **buffer,
  64. size_t *length, ...);
  65. TSS2_RC
  66. __wrap_ifapi_io_read_finish(
  67. struct IFAPI_IO *io,
  68. uint8_t **buffer,
  69. size_t *length, ...)
  70. {
  71. json_object *jso = NULL;
  72. const char *jso_string = NULL;
  73. jso = json_tokener_parse(wrap_config_file_content);
  74. assert_ptr_not_equal(jso, NULL);
  75. json_object_object_del(jso, wrap_remove_field);
  76. jso_string = json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY);
  77. assert_ptr_not_equal(jso_string, NULL);
  78. *buffer = (uint8_t *)strdup(jso_string);
  79. *length = strlen(jso_string);
  80. json_object_put(jso);
  81. assert_ptr_not_equal(*buffer, NULL);
  82. return TSS2_RC_SUCCESS;
  83. }
  84. /* Function to remove the field and check the initialization of the configuration. */
  85. void check_remove_field(char *file_content, char* fname, TSS2_RC rc)
  86. {
  87. IFAPI_IO io;
  88. IFAPI_CONFIG config;
  89. TSS2_RC r;
  90. char *home_dir = getenv("HOME");
  91. assert_ptr_not_equal(home_dir, NULL);
  92. wrap_config_file_content = file_content;
  93. wrap_remove_field = fname;
  94. r = ifapi_config_initialize_finish(&io, &config);
  95. assert_int_equal(r, rc);
  96. if (r == TSS2_RC_SUCCESS) {
  97. LOG_WARNING("TEST OUTPUT: %s", config.profile_dir);
  98. assert_true(strncmp(config.profile_dir, home_dir, strlen(home_dir)) == 0);
  99. SAFE_FREE(config.profile_dir);
  100. assert_true(strncmp(config.user_dir, home_dir, strlen(home_dir)) == 0);
  101. SAFE_FREE(config.user_dir);
  102. assert_true(strncmp(config.keystore_dir, home_dir, strlen(home_dir)) == 0);
  103. SAFE_FREE(config.keystore_dir);
  104. SAFE_FREE(config.log_dir);
  105. SAFE_FREE(config.profile_name);
  106. SAFE_FREE(config.tcti);
  107. SAFE_FREE(config.ek_cert_file);
  108. SAFE_FREE(config.intel_cert_service)
  109. }
  110. }
  111. /* Function to remove the field and check the initialization of the configuration. */
  112. static void
  113. check_config_json_remove_field_allowed(void **state) {
  114. check_remove_field(config_home, "log_dir", TSS2_RC_SUCCESS);
  115. check_remove_field(config_tilde, "log_dir", TSS2_RC_SUCCESS);
  116. }
  117. /* Check removing of the mandatory fields. */
  118. static void
  119. check_config_json_remove_field_not_allowed(void **state) {
  120. check_remove_field(config_tilde, "profile_dir", TSS2_FAPI_RC_BAD_VALUE);
  121. check_remove_field(config_tilde, "system_dir", TSS2_FAPI_RC_BAD_VALUE);
  122. check_remove_field(config_tilde, "user_dir", TSS2_FAPI_RC_BAD_VALUE);
  123. check_remove_field(config_tilde, "profile_name", TSS2_FAPI_RC_BAD_VALUE);
  124. check_remove_field(config_tilde, "tcti", TSS2_FAPI_RC_BAD_VALUE);
  125. check_remove_field(config_tilde, "system_pcrs", TSS2_FAPI_RC_BAD_VALUE);
  126. }
  127. int
  128. main(int argc, char *argv[])
  129. {
  130. const struct CMUnitTest tests[] = {
  131. cmocka_unit_test(check_config_json_remove_field_allowed),
  132. cmocka_unit_test(check_config_json_remove_field_not_allowed),
  133. };
  134. return cmocka_run_group_tests(tests, NULL, NULL);
  135. }