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