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