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
14 #include <setjmp.h>
15 #include <cmocka.h>
16
17 #include "tss2_sys.h"
18 #include "sysapi_util.h"
19
20 #define MAX_SIZE_CTX 4096
21
22 /**
23 * Pass CommonPreparePrologue a NULL TSS2_SYS_CONTEXT.
24 */
25 static void
CommonPreparePrologue_null_sys_context_unit(void ** state)26 CommonPreparePrologue_null_sys_context_unit (void **state)
27 {
28 TSS2_RC rc;
29
30 rc = CommonPreparePrologue (NULL, 0);
31 assert_int_equal (rc, TSS2_SYS_RC_BAD_REFERENCE);
32 }
33
34 /**
35 * Accessing the _TSS2_SYS_CONTEXT_BLOB directly like this isn't allowed
36 * in normal code. Use the opaque TSS2_SYS_CONTEXT in user space
37 * applications. In the test cases we do this to induce error conditions.
38 */
39 static int
CommonPreparePrologue_sys_setup(void ** state)40 CommonPreparePrologue_sys_setup (void **state)
41 {
42 _TSS2_SYS_CONTEXT_BLOB *sys_ctx;
43 UINT32 size_ctx;
44
45 size_ctx = Tss2_Sys_GetContextSize (MAX_SIZE_CTX);
46 sys_ctx = calloc (1, size_ctx);
47 assert_non_null (sys_ctx);
48
49 *state = sys_ctx;
50 return 0;
51 }
52
53 static int
CommonPreparePrologue_sys_teardown(void ** state)54 CommonPreparePrologue_sys_teardown (void **state)
55 {
56 _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
57
58 if (sys_ctx)
59 free (sys_ctx);
60
61 return 0;
62 }
63
64 /**
65 * CommonPrepareProlog must be passed a sys context with previousStage
66 * set to either CMD_STAGE_INITIALIZE, CMD_STAGE_RECEIVE_RESPONSE or
67 * CMD_STAGE_PREPARE.
68 */
69 static void
CommonPreparePrologue_previous_stage_initialize(void ** state)70 CommonPreparePrologue_previous_stage_initialize (void **state)
71 {
72 _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
73 TSS2_RC rc;
74
75 sys_ctx->previousStage |= ~CMD_STAGE_INITIALIZE;
76 rc = CommonPreparePrologue (sys_ctx, 0);
77 assert_int_equal (rc, TSS2_SYS_RC_BAD_SEQUENCE);
78 }
79 static void
CommonPreparePrologue_previous_stage_prepare(void ** state)80 CommonPreparePrologue_previous_stage_prepare (void **state)
81 {
82 _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
83 TSS2_RC rc;
84
85 sys_ctx->previousStage |= ~CMD_STAGE_RECEIVE_RESPONSE;
86 rc = CommonPreparePrologue (sys_ctx, 0);
87 assert_int_equal (rc, TSS2_SYS_RC_BAD_SEQUENCE);
88 }
89 static void
CommonPreparePrologue_previous_stage_response(void ** state)90 CommonPreparePrologue_previous_stage_response (void **state)
91 {
92 _TSS2_SYS_CONTEXT_BLOB *sys_ctx = (_TSS2_SYS_CONTEXT_BLOB*)*state;
93 TSS2_RC rc;
94
95 sys_ctx->previousStage |= ~CMD_STAGE_PREPARE;
96 rc = CommonPreparePrologue (sys_ctx, 0);
97 assert_int_equal (rc, TSS2_SYS_RC_BAD_SEQUENCE);
98 }
99 int
main(int argc,char * arvg[])100 main (int argc, char* arvg[])
101 {
102 const struct CMUnitTest tests[] = {
103 cmocka_unit_test(CommonPreparePrologue_null_sys_context_unit),
104 cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_initialize,
105 CommonPreparePrologue_sys_setup,
106 CommonPreparePrologue_sys_teardown),
107 cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_prepare,
108 CommonPreparePrologue_sys_setup,
109 CommonPreparePrologue_sys_teardown),
110 cmocka_unit_test_setup_teardown (CommonPreparePrologue_previous_stage_response,
111 CommonPreparePrologue_sys_setup,
112 CommonPreparePrologue_sys_teardown),
113 };
114 return cmocka_run_group_tests (tests, NULL, NULL);
115 }
116