CopyCommandHeader.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <arpa/inet.h>
  13. #include <setjmp.h>
  14. #include <cmocka.h>
  15. #include "tss2_sys.h"
  16. #include "sysapi_util.h"
  17. #define MAX_SIZE_CTX 4096
  18. /**
  19. *
  20. */
  21. static int
  22. CopyCommandHeader_sys_setup (void **state)
  23. {
  24. _TSS2_SYS_CONTEXT_BLOB *sys_ctx;
  25. UINT32 size_ctx;
  26. size_ctx = Tss2_Sys_GetContextSize (MAX_SIZE_CTX);
  27. sys_ctx = calloc (1, size_ctx);
  28. assert_non_null (sys_ctx);
  29. /**
  30. * This is the important part: the CopyCommandHeader function builds up
  31. * the command buffer in the memory pointed to by tpmInitBuffPtr. This
  32. * must point to the data after the context structure.
  33. */
  34. sys_ctx->cmdBuffer = (UINT8*) (sys_ctx + sizeof (_TSS2_SYS_CONTEXT_BLOB));
  35. InitSysContextFields (sys_ctx);
  36. InitSysContextPtrs (sys_ctx, size_ctx);
  37. *state = sys_ctx;
  38. return 0;
  39. }
  40. static int
  41. CopyCommandHeader_sys_teardown (void **state)
  42. {
  43. TSS2_SYS_CONTEXT *sys_ctx = (TSS2_SYS_CONTEXT*)*state;
  44. if (sys_ctx)
  45. free (sys_ctx);
  46. return 0;
  47. }
  48. /**
  49. * CopyCommandHeader creates the standard TPM command header (tag, size,
  50. * command_code) to the data buffer in the context structure. It also
  51. * advances the 'nextData' pointer to the address after the header. This
  52. * test will fail if the nextData pointer isn't set as expected
  53. */
  54. static void
  55. CopyCommandHeader_nextData_unit (void **state)
  56. {
  57. _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB *)*state;
  58. TPM2_CC cc = TPM2_CC_GetCapability;
  59. CopyCommandHeader (sys_ctx, cc);
  60. assert_int_equal (sys_ctx->nextData, sizeof (TPM20_Header_In));
  61. }
  62. /**
  63. * After a call to CopyCommandHeader the tag in the TPM20_Header_In portion of
  64. * the cmdBuffer member of the sys context should be TPM2_ST_NO_SESSIONS
  65. * transformed into network byte order.
  66. */
  67. static void
  68. CopyCommandHeader_tag_unit (void **state)
  69. {
  70. _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
  71. TPM2_CC cc = TPM2_CC_GetCapability;
  72. TPM20_Header_In *header = (TPM20_Header_In*)sys_ctx->cmdBuffer;
  73. /* The TSS code uses a custom function to convert stuff to network byte
  74. * order but we can just use htons. Not sure why we don't use htons/l
  75. * everywhere.
  76. */
  77. TPMI_ST_COMMAND_TAG tag_net = htons (TPM2_ST_NO_SESSIONS);
  78. CopyCommandHeader (sys_ctx, cc);
  79. assert_int_equal (tag_net, header->tag);
  80. }
  81. /**
  82. * After a call to CopyCommandHeader the commandCode in the TPM20_Header_In
  83. * portion of the cmdBuffer member of the sys context should be the command
  84. * code parameter in network byte order.
  85. */
  86. static void
  87. CopyCommandHeader_commandcode_unit (void **state)
  88. {
  89. _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
  90. TPM2_CC cc = TPM2_CC_GetCapability;
  91. TPM2_CC cc_net = htonl (cc);
  92. TPM20_Header_In *header = (TPM20_Header_In*)sys_ctx->cmdBuffer;
  93. CopyCommandHeader (sys_ctx, cc);
  94. assert_int_equal (cc_net, header->commandCode);
  95. }
  96. int
  97. main (int argc, char* argv[])
  98. {
  99. const struct CMUnitTest tests[] = {
  100. cmocka_unit_test_setup_teardown (CopyCommandHeader_nextData_unit,
  101. CopyCommandHeader_sys_setup,
  102. CopyCommandHeader_sys_teardown),
  103. cmocka_unit_test_setup_teardown (CopyCommandHeader_tag_unit,
  104. CopyCommandHeader_sys_setup,
  105. CopyCommandHeader_sys_teardown),
  106. cmocka_unit_test_setup_teardown (CopyCommandHeader_commandcode_unit,
  107. CopyCommandHeader_sys_setup,
  108. CopyCommandHeader_sys_teardown),
  109. };
  110. return cmocka_run_group_tests (tests, NULL, NULL);
  111. }