test-fapi.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * Copyright (c) 2017-2018, Intel Corporation
  5. *
  6. * All rights reserved.
  7. ***********************************************************************/
  8. #include <assert.h>
  9. #include <json-c/json.h>
  10. #include <json-c/json_util.h>
  11. #include <string.h>
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include "tss2_fapi.h"
  15. #define EXIT_SKIP 77
  16. #define EXIT_ERROR 99
  17. #define ASSERT_SIZE 10 /* sanity check value for string outputs of Fapi commands */
  18. #define ASSERT(EXPR) \
  19. if (!(EXPR)) { \
  20. LOG_ERROR("Failed assertion: " #EXPR); \
  21. goto error; \
  22. }
  23. /*
  24. * Based on a list of keys (FIELD_LIST) a json sub object of the
  25. * json object represented by JSON_STRING will be determined.
  26. * If an json array is part of the list of sub objects an element
  27. * of the array can be selected by a value from '0' to '9'.
  28. * If SUBSTRING is "" the check is ok if the sub object was found.
  29. * Otherwise it will be checked whether SUBSTRING does occur
  30. * in the string representing the sub object.
  31. */
  32. #define CHECK_JSON_FIELDS(JSON_STRING, FIELD_LIST, SUBSTRING, LABEL) \
  33. { \
  34. json_object *jso = NULL; \
  35. json_object *jso1 = NULL; \
  36. json_object *jso2 = NULL; \
  37. size_t i, n; \
  38. n = sizeof(FIELD_LIST) / sizeof(FIELD_LIST[0]); \
  39. jso = json_tokener_parse(JSON_STRING); \
  40. if (!jso) { \
  41. LOG_ERROR("Invalid JSON"); \
  42. goto error; \
  43. } \
  44. jso1 = jso; \
  45. for (i = 0; i < n; i++) { \
  46. if (strlen(FIELD_LIST[i]) == 1 && \
  47. FIELD_LIST[i][0] >= '0' && FIELD_LIST[i][0] <= '9') { \
  48. jso2 = json_object_array_get_idx(jso1, FIELD_LIST[i][0] - '0'); \
  49. ASSERT(jso2); \
  50. } \
  51. else if (!jso1 || !json_object_object_get_ex(jso1, FIELD_LIST[i], &jso2)) { \
  52. json_object_put(jso); \
  53. LOG_ERROR("%s not found.", FIELD_LIST[i]); \
  54. goto error; \
  55. } \
  56. jso1 = jso2; \
  57. } \
  58. if (strlen(SUBSTRING) > 0 && !strstr( json_object_get_string(jso1), SUBSTRING)) { \
  59. json_object_put(jso); \
  60. LOG_ERROR("Sub string %s not found.", SUBSTRING); \
  61. goto error; \
  62. } \
  63. json_object_put(jso); \
  64. }
  65. /* It will be checked whether two json objects are equal. */
  66. #define CHECK_JSON(JSON1, JSON2, LABEL) \
  67. { \
  68. json_object *jso1 = NULL; \
  69. json_object *jso2 = NULL; \
  70. jso1 = json_tokener_parse(JSON1) ; \
  71. ASSERT(jso1) ; \
  72. if (!jso1) { \
  73. LOG_ERROR("Invalid JSON") ;\
  74. goto LABEL ;\
  75. } \
  76. jso2 = json_tokener_parse(JSON2) ;\
  77. ASSERT(jso2) ;\
  78. if (!jso2) { \
  79. LOG_ERROR("Invalid JSON") ;\
  80. goto LABEL ;\
  81. } \
  82. if (!cmp_jso(jso1, jso2)) { \
  83. json_object_put(jso1) ; \
  84. json_object_put(jso2) ; \
  85. goto LABEL; \
  86. } \
  87. json_object_put(jso1); \
  88. json_object_put(jso2); \
  89. }
  90. /* It will be checked whether a json object is included in a list
  91. of json objects. */
  92. #define CHECK_JSON_LIST(LIST, JSO_STRING, LABEL) \
  93. { \
  94. size_t i, n; \
  95. n = sizeof(LIST) / sizeof(LIST[0]); \
  96. json_object *jso1 = json_tokener_parse(JSO_STRING); \
  97. ASSERT(jso1); \
  98. json_object *jso2 = NULL; \
  99. for (i = 0; i < n; i++) { \
  100. jso2 = json_tokener_parse(LIST[i]); \
  101. ASSERT(jso2); \
  102. if (cmp_jso(jso1, jso2)) { \
  103. break; \
  104. } \
  105. json_object_put(jso2); \
  106. } \
  107. if (i >= n) { \
  108. json_object_put(jso1); \
  109. LOG_ERROR("Mismatch" ); \
  110. goto LABEL; \
  111. } \
  112. json_object_put(jso1); \
  113. json_object_put(jso2); \
  114. }
  115. #define goto_error_if_not_failed(rc,msg,label) \
  116. if (rc == TSS2_RC_SUCCESS) { \
  117. LOG_ERROR("Error %s (%x) in Line %i: \n", msg, __LINE__, rc); \
  118. goto label; }
  119. /* This variable is set to the same value in order to allow usage in if-statements etc. */
  120. extern char *fapi_profile;
  121. #define FAPI_POLICIES TOP_SOURCEDIR "/test/data/fapi"
  122. TSS2_RC
  123. pcr_reset(FAPI_CONTEXT *context, UINT32 pcr);
  124. bool cmp_strtokens(char* string1, char *string2, char *delimiter);
  125. char * normalize_string(const char *string);
  126. bool cmp_jso(json_object *jso1, json_object *jso2);
  127. /*
  128. * This is the prototype for all integration tests in the tpm2-tss
  129. * project. Integration tests are intended to exercise the combined
  130. * components in the software stack. This typically means executing some
  131. * SAPI function using the socket TCTI to communicate with a software
  132. * TPM2 simulator.
  133. * Return values:
  134. * A successful test will return 0, any other value indicates failure.
  135. */
  136. int test_invoke_fapi(FAPI_CONTEXT * fapi_context);
  137. int init_fapi(char *fapi_profile, FAPI_CONTEXT **fapi_context);