CommonPreparePrologue.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <setjmp.h>
  13. #include <cmocka.h>
  14. #include "tss2_sys.h"
  15. #include "sysapi_util.h"
  16. #define MAX_SIZE_CTX 4096
  17. /**
  18. * Pass CommonPreparePrologue a NULL TSS2_SYS_CONTEXT.
  19. */
  20. static void
  21. CommonPreparePrologue_null_sys_context_unit (void **state)
  22. {
  23. TSS2_RC rc;
  24. rc = CommonPreparePrologue (NULL, 0);
  25. assert_int_equal (rc, TSS2_SYS_RC_BAD_REFERENCE);
  26. }
  27. /**
  28. * Accessing the _TSS2_SYS_CONTEXT_BLOB directly like this isn't allowed
  29. * in normal code. Use the opaque TSS2_SYS_CONTEXT in user space
  30. * applications. In the test cases we do this to induce error conditions.
  31. */
  32. static int
  33. CommonPreparePrologue_sys_setup (void **state)
  34. {
  35. _TSS2_SYS_CONTEXT_BLOB *sys_ctx;
  36. UINT32 size_ctx;
  37. size_ctx = Tss2_Sys_GetContextSize (MAX_SIZE_CTX);
  38. sys_ctx = calloc (1, size_ctx);
  39. assert_non_null (sys_ctx);
  40. *state = sys_ctx;
  41. return 0;
  42. }
  43. static int
  44. CommonPreparePrologue_sys_teardown (void **state)
  45. {
  46. _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
  47. if (sys_ctx)
  48. free (sys_ctx);
  49. return 0;
  50. }
  51. /**
  52. * CommonPrepareProlog must be passed a sys context with previousStage
  53. * set to either CMD_STAGE_INITIALIZE, CMD_STAGE_RECEIVE_RESPONSE or
  54. * CMD_STAGE_PREPARE.
  55. */
  56. static void
  57. CommonPreparePrologue_previous_stage_initialize (void **state)
  58. {
  59. _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
  60. TSS2_RC rc;
  61. sys_ctx->previousStage |= ~CMD_STAGE_INITIALIZE;
  62. rc = CommonPreparePrologue (sys_ctx, 0);
  63. assert_int_equal (rc, TSS2_SYS_RC_BAD_SEQUENCE);
  64. }
  65. static void
  66. CommonPreparePrologue_previous_stage_prepare (void **state)
  67. {
  68. _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
  69. TSS2_RC rc;
  70. sys_ctx->previousStage |= ~CMD_STAGE_RECEIVE_RESPONSE;
  71. rc = CommonPreparePrologue (sys_ctx, 0);
  72. assert_int_equal (rc, TSS2_SYS_RC_BAD_SEQUENCE);
  73. }
  74. static void
  75. CommonPreparePrologue_previous_stage_response (void **state)
  76. {
  77. _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
  78. TSS2_RC rc;
  79. sys_ctx->previousStage |= ~CMD_STAGE_PREPARE;
  80. rc = CommonPreparePrologue (sys_ctx, 0);
  81. assert_int_equal (rc, TSS2_SYS_RC_BAD_SEQUENCE);
  82. }
  83. int
  84. main (int argc, char* arvg[])
  85. {
  86. const struct CMUnitTest tests[] = {
  87. cmocka_unit_test(CommonPreparePrologue_null_sys_context_unit),
  88. cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_initialize,
  89. CommonPreparePrologue_sys_setup,
  90. CommonPreparePrologue_sys_teardown),
  91. cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_prepare,
  92. CommonPreparePrologue_sys_setup,
  93. CommonPreparePrologue_sys_teardown),
  94. cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_response,
  95. CommonPreparePrologue_sys_setup,
  96. CommonPreparePrologue_sys_teardown),
  97. };
  98. return cmocka_run_group_tests (tests, NULL, NULL);
  99. }