fapi-profiles.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_profiles.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 profiles. It will be
  28. * checked whether the correct return codes are returned if optional and
  29. * mandatory fields are removed from the profile.
  30. */
  31. /* Profile file which will be used for the test. */
  32. char *wrap_profile_file;
  33. /* JSON field which will be removed for the test. */
  34. char *wrap_remove_field;
  35. json_object *
  36. read_json(char *file_name)
  37. {
  38. FILE *stream = NULL;
  39. long file_size;
  40. char *json_string = NULL;
  41. json_object *jso = NULL;
  42. char file[1024];
  43. if (snprintf(&file[0], 1023, TOP_SOURCEDIR "/%s", file_name) < 0)
  44. return NULL;
  45. stream = fopen(file, "r");
  46. if (!stream) {
  47. LOG_ERROR("File %s does not exist", file);
  48. return NULL;
  49. }
  50. fseek(stream, 0L, SEEK_END);
  51. file_size = ftell(stream);
  52. fclose(stream);
  53. json_string = malloc(file_size + 1);
  54. stream = fopen(file, "r");
  55. ssize_t ret = read(fileno(stream), json_string, file_size);
  56. if (ret != file_size) {
  57. LOG_ERROR("IO error %s.",file);
  58. return NULL;
  59. }
  60. json_string[file_size] = '\0';
  61. jso = json_tokener_parse(json_string);
  62. SAFE_FREE(json_string);
  63. return jso;
  64. }
  65. /*
  66. * Wrappers for reading the JSON profile.
  67. */
  68. TSS2_RC
  69. __wrap_ifapi_io_read_finish(
  70. struct IFAPI_IO *io,
  71. uint8_t **buffer,
  72. size_t *length, ...);
  73. TSS2_RC
  74. __wrap_ifapi_io_read_finish(
  75. struct IFAPI_IO *io,
  76. uint8_t **buffer,
  77. size_t *length, ...)
  78. {
  79. json_object *jso = NULL;
  80. const char *jso_string = NULL;
  81. jso = read_json(wrap_profile_file);
  82. assert_ptr_not_equal(jso, NULL);
  83. json_object_object_del(jso, wrap_remove_field);
  84. jso_string = json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY);
  85. assert_ptr_not_equal(jso_string, NULL);
  86. *buffer = (uint8_t *)strdup(jso_string);
  87. json_object_put(jso);
  88. assert_ptr_not_equal(*buffer, NULL);
  89. return TSS2_RC_SUCCESS;
  90. }
  91. /* Function to remove the field and check the profile initialization. */
  92. void check_remove_field(char *file, char* fname, TSS2_RC rc)
  93. {
  94. IFAPI_IO io;
  95. IFAPI_PROFILES profiles;
  96. TSS2_RC r;
  97. profiles.num_profiles = 1;
  98. profiles.profiles_idx = 0;
  99. profiles.default_name = strdup("dmy_name");
  100. profiles.filenames = calloc(1 ,sizeof(profiles.filenames[0]));
  101. assert_ptr_not_equal(profiles.filenames, NULL);
  102. profiles.profiles = calloc(profiles.num_profiles, sizeof(profiles.profiles[0]));
  103. profiles.profiles[0].name = strdup("dmy_name");
  104. assert_ptr_not_equal(profiles.profiles[0].name, NULL);
  105. profiles.filenames[0] = strdup("dmy_name");
  106. assert_ptr_not_equal( profiles.filenames[0], NULL);
  107. wrap_profile_file = file;
  108. wrap_remove_field = fname;
  109. r = ifapi_profiles_initialize_finish(&profiles, &io);
  110. assert_int_equal(r, rc);
  111. ifapi_profiles_finalize(&profiles);
  112. }
  113. /* Check removing the optional fields. */
  114. static void
  115. check_profile_json_remove_field_allowed(void **state) {
  116. check_remove_field("test/data/fapi/P_RSA.json", "srk_description", TSS2_RC_SUCCESS);
  117. check_remove_field("test/data/fapi/P_RSA.json", "ekk_description", TSS2_RC_SUCCESS);
  118. }
  119. /* Check removing of the mandatory fields. */
  120. static void
  121. check_profile_json_remove_field_not_allowed(void **state) {
  122. check_remove_field("test/data/fapi/P_ECC.json", "type", TSS2_FAPI_RC_BAD_VALUE);
  123. check_remove_field("test/data/fapi/P_ECC.json", "curveID", TSS2_FAPI_RC_BAD_VALUE);
  124. check_remove_field("test/data/fapi/P_RSA.json", "keyBits", TSS2_FAPI_RC_BAD_VALUE);
  125. check_remove_field("test/data/fapi/P_RSA.json", "exponent", TSS2_FAPI_RC_BAD_VALUE);
  126. check_remove_field("test/data/fapi/P_RSA.json", "nameAlg", TSS2_FAPI_RC_BAD_VALUE);
  127. check_remove_field("test/data/fapi/P_RSA.json", "pcr_selection", TSS2_FAPI_RC_BAD_VALUE);
  128. check_remove_field("test/data/fapi/P_RSA.json", "pcr_selection", TSS2_FAPI_RC_BAD_VALUE);
  129. check_remove_field("test/data/fapi/P_RSA.json", "sym_block_size", TSS2_FAPI_RC_BAD_VALUE);
  130. check_remove_field("test/data/fapi/P_RSA.json", "sym_parameters", TSS2_FAPI_RC_BAD_VALUE);
  131. check_remove_field("test/data/fapi/P_RSA.json", "sym_mode", TSS2_FAPI_RC_BAD_VALUE);
  132. check_remove_field("test/data/fapi/P_RSA.json", "ek_template", TSS2_FAPI_RC_BAD_VALUE);
  133. check_remove_field("test/data/fapi/P_RSA.json", "srk_template", TSS2_FAPI_RC_BAD_VALUE);
  134. }
  135. int
  136. main(int argc, char *argv[])
  137. {
  138. const struct CMUnitTest tests[] = {
  139. cmocka_unit_test(check_profile_json_remove_field_allowed),
  140. cmocka_unit_test(check_profile_json_remove_field_not_allowed),
  141. };
  142. return cmocka_run_group_tests(tests, NULL, NULL);
  143. }