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_mu.h"
18 #include "util/tss2_endian.h"
19
20 /*
21 * Test case for successful UINT64 marshaling with NULL offset.
22 */
23 void
UINT64_marshal_success(void ** state)24 UINT64_marshal_success (void **state)
25 {
26 UINT64 src = 0xdeadbeefdeadbeef, tmp;
27 uint8_t buffer [8] = { 0 };
28 size_t buffer_size = sizeof (buffer);
29 TSS2_RC rc;
30
31 rc = Tss2_MU_UINT64_Marshal (src, buffer, buffer_size, NULL);
32 tmp = HOST_TO_BE_64 (src);
33
34 assert_int_equal (rc, TSS2_RC_SUCCESS);
35 assert_memory_equal (&tmp, &buffer [0], sizeof (tmp));
36 }
37 /*
38 * Test case for successful UINT64 marshaling with offset.
39 */
40 void
UINT64_marshal_success_offset(void ** state)41 UINT64_marshal_success_offset (void **state)
42 {
43 UINT64 src = 0xdeadbeefdeadbeef, tmp = 0;
44 uint8_t buffer [9] = { 0 };
45 size_t buffer_size = sizeof (buffer);
46 size_t offset = 1;
47 TSS2_RC rc;
48
49 rc = Tss2_MU_UINT64_Marshal (src, buffer, buffer_size, &offset);
50 tmp = HOST_TO_BE_64 (src);
51
52 assert_int_equal (rc, TSS2_RC_SUCCESS);
53 assert_memory_equal (&tmp, &buffer [1], sizeof (tmp));
54 assert_int_equal (offset, sizeof (buffer));
55 }
56 /*
57 * Test case passing NULL buffer and non-NULL offset. Test to be sure offset
58 * is updated to the size of the src parameter.
59 */
60 void
UINT64_marshal_buffer_null_with_offset(void ** state)61 UINT64_marshal_buffer_null_with_offset (void **state)
62 {
63 UINT64 src = 0xdeadbeefdeadbeef;
64 size_t offset = 100;
65 TSS2_RC rc;
66
67 rc = Tss2_MU_UINT64_Marshal (src, NULL, 2, &offset);
68
69 assert_int_equal (rc, TSS2_RC_SUCCESS);
70 assert_int_equal (offset, 100 + sizeof (src));
71 }
72 /*
73 * Test case passing NULL buffer and NULL offset.
74 */
75 void
UINT64_marshal_buffer_null_offset_null(void ** state)76 UINT64_marshal_buffer_null_offset_null (void **state)
77 {
78 UINT64 src = 0xdeadbeefdeadbeef;
79 TSS2_RC rc;
80
81 rc = Tss2_MU_UINT64_Marshal (src, NULL, sizeof (src), NULL);
82
83 assert_int_equal (rc, TSS2_MU_RC_BAD_REFERENCE);
84 }
85 /*
86 * Test failing case where buffer_size - offset (size of available space
87 * in buffer) is less than sizeof (UINT64). Also check offset is unchanged.
88 */
89 void
UINT64_marshal_buffer_size_lt_data(void ** state)90 UINT64_marshal_buffer_size_lt_data (void **state)
91 {
92 UINT64 src = 0xdeadbeefdeadbeef;
93 uint8_t buffer [8] = { 0 };
94 size_t offset = 2;
95 TSS2_RC rc;
96
97 rc = Tss2_MU_UINT64_Marshal (src, buffer, sizeof (src), &offset);
98
99 assert_int_equal (rc, TSS2_MU_RC_INSUFFICIENT_BUFFER);
100 assert_int_equal (offset, 2);
101 }
102 /*
103 * Test failing case where buffer_size is less than the offset value.
104 * This should return INSUFFICIENT_BUFFER and the offset should be unchanged.
105 */
106 void
UINT64_marshal_buffer_size_lt_offset(void ** state)107 UINT64_marshal_buffer_size_lt_offset (void **state)
108 {
109 UINT64 src = 0xdeadbeefdeadbeef;
110 uint8_t buffer [8] = { 0 };
111 size_t buffer_size = sizeof (buffer);
112 size_t offset = sizeof (buffer) + 1;
113 TSS2_RC rc;
114
115 rc = Tss2_MU_UINT64_Marshal (src, buffer, buffer_size, &offset);
116
117 assert_int_equal (rc, TSS2_MU_RC_INSUFFICIENT_BUFFER);
118 assert_int_equal (offset, sizeof (buffer) + 1);
119 }
120 /*
121 * Test case for successful UINT64 unmarshaling.
122 */
123 void
UINT64_unmarshal_success(void ** state)124 UINT64_unmarshal_success (void **state)
125 {
126 uint8_t buffer [8] = { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef };
127 uint8_t buffer_size = sizeof (buffer);
128 UINT64 dest = 0, tmp = 0;
129 TSS2_RC rc;
130
131 rc = Tss2_MU_UINT64_Unmarshal (buffer, buffer_size, NULL, &dest);
132 tmp = HOST_TO_BE_64 (dest);
133
134 assert_int_equal (rc, TSS2_RC_SUCCESS);
135 assert_memory_equal (&tmp, buffer, sizeof (tmp));
136 }
137 /*
138 * Test case for successful UINT64 unmarshaling with offset.
139 */
140 void
UINT64_unmarshal_success_offset(void ** state)141 UINT64_unmarshal_success_offset (void **state)
142 {
143 UINT64 dest = 0, tmp = 0;
144 uint8_t buffer [9] = { 0xff, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef };
145 size_t buffer_size = sizeof (buffer);
146 size_t offset = 1;
147 TSS2_RC rc;
148
149 rc = Tss2_MU_UINT64_Unmarshal (buffer, buffer_size, &offset, &dest);
150 tmp = HOST_TO_BE_64 (dest);
151
152 assert_int_equal (rc, TSS2_RC_SUCCESS);
153 assert_memory_equal (&tmp, &buffer [1], sizeof (tmp));
154 assert_int_equal (offset, 9);
155 }
156 /*
157 * Test case ensures a NULL buffer parameter produces a BAD_REFERENCE RC.
158 */
159 void
UINT64_unmarshal_buffer_null(void ** state)160 UINT64_unmarshal_buffer_null (void **state)
161 {
162 TSS2_RC rc;
163
164 rc = Tss2_MU_UINT64_Unmarshal (NULL, 1, NULL, NULL);
165
166 assert_int_equal (rc, TSS2_MU_RC_BAD_REFERENCE);
167 }
168 /*
169 * Test case ensures a NULL dest and offset parameters produce an
170 * INSUFFICIENT_BUFFER RC.
171 */
172 void
UINT64_unmarshal_dest_null(void ** state)173 UINT64_unmarshal_dest_null (void **state)
174 {
175 uint8_t buffer [1];
176 TSS2_RC rc;
177
178 rc = Tss2_MU_UINT64_Unmarshal (buffer, sizeof (buffer), NULL, NULL);
179
180 assert_int_equal (rc, TSS2_MU_RC_BAD_REFERENCE);
181 }
182 /*
183 * Test case ensures that INSUFFICIENT_BUFFER is returned when buffer_size
184 * is less than the provided offset.
185 */
186 void
UINT64_unmarshal_buffer_size_lt_offset(void ** state)187 UINT64_unmarshal_buffer_size_lt_offset (void **state)
188 {
189 UINT64 dest = 0;
190 uint8_t buffer [1];
191 size_t offset = sizeof (buffer) + 1;
192 TSS2_RC rc;
193
194 rc = Tss2_MU_UINT64_Unmarshal (buffer, sizeof (buffer), &offset, &dest);
195
196 assert_int_equal (rc, TSS2_MU_RC_INSUFFICIENT_BUFFER);
197 assert_int_equal (offset, sizeof (buffer) + 1);
198 assert_int_equal (dest, 0);
199 }
200 /*
201 * Test case ensures that INSUFFICIENT_BUFFER is returned when buffer_size -
202 * local_offset is less than dest (the destination type).
203 */
204 void
UINT64_unmarshal_buffer_size_lt_dest(void ** state)205 UINT64_unmarshal_buffer_size_lt_dest (void **state)
206 {
207 UINT64 dest = 0;
208 uint8_t buffer [3];
209 size_t offset = sizeof (buffer);
210 TSS2_RC rc;
211
212 rc = Tss2_MU_UINT64_Unmarshal (buffer, sizeof (buffer), &offset, &dest);
213
214 assert_int_equal (rc, TSS2_MU_RC_INSUFFICIENT_BUFFER);
215 assert_int_equal (offset, sizeof (buffer));
216 assert_int_equal (dest, 0);
217 }
218 int
main(void)219 main (void)
220 {
221 const struct CMUnitTest tests [] = {
222 cmocka_unit_test (UINT64_marshal_success),
223 cmocka_unit_test (UINT64_marshal_success_offset),
224 cmocka_unit_test (UINT64_marshal_buffer_null_with_offset),
225 cmocka_unit_test (UINT64_marshal_buffer_null_offset_null),
226 cmocka_unit_test (UINT64_marshal_buffer_size_lt_data),
227 cmocka_unit_test (UINT64_marshal_buffer_size_lt_offset),
228 cmocka_unit_test (UINT64_unmarshal_success),
229 cmocka_unit_test (UINT64_unmarshal_success_offset),
230 cmocka_unit_test (UINT64_unmarshal_buffer_null),
231 cmocka_unit_test (UINT64_unmarshal_dest_null),
232 cmocka_unit_test (UINT64_unmarshal_buffer_size_lt_offset),
233 cmocka_unit_test (UINT64_unmarshal_buffer_size_lt_dest),
234 };
235 return cmocka_run_group_tests (tests, NULL, NULL);
236 }
237