blob: 514c7eaa72c36e4fd32f3c2864d1b6b12c8ab66f [file] [log] [blame]
Philip Triccabff72122017-02-04 07:53:07 -08001#include <stdlib.h>
2#include <stdio.h>
3
4#include <setjmp.h>
5#include <cmocka.h>
6
7#include "marshal/base-types.h"
8#include "sapi/marshal.h"
9
10/*
11 * Test case for successful UINT32 marshaling with NULL offset.
12 */
13void
14UINT32_marshal_success (void **state)
15{
Philip Triccab37fa662017-03-09 08:02:55 -080016 UINT32 src = 0xdeadbeef, tmp = 0;
Philip Triccabff72122017-02-04 07:53:07 -080017 uint8_t buffer [4] = { 0 };
18 size_t buffer_size = sizeof (buffer);
19 TSS2_RC rc;
20
Philip Tricca42039cc2017-03-08 15:53:18 -080021 rc = UINT32_Marshal (src, buffer, buffer_size, NULL);
Philip Triccabff72122017-02-04 07:53:07 -080022
Philip Triccab37fa662017-03-09 08:02:55 -080023 tmp = HOST_TO_BE_32 (src);
Philip Triccabff72122017-02-04 07:53:07 -080024 assert_int_equal (rc, TSS2_RC_SUCCESS);
Philip Triccab37fa662017-03-09 08:02:55 -080025 assert_memory_equal (&tmp, buffer, sizeof (tmp));
Philip Triccabff72122017-02-04 07:53:07 -080026}
27/*
28 * Test case for successful UINT32 marshaling with offset.
29 */
30void
31UINT32_marshal_success_offset (void **state)
32{
Philip Triccab37fa662017-03-09 08:02:55 -080033 UINT32 src = 0xdeadbeef, tmp = 0;
Philip Triccabff72122017-02-04 07:53:07 -080034 uint8_t buffer [5] = { 0 };
35 size_t buffer_size = sizeof (buffer);
36 size_t offset = 1;
37 TSS2_RC rc;
38
Philip Tricca42039cc2017-03-08 15:53:18 -080039 rc = UINT32_Marshal (src, buffer, buffer_size, &offset);
Philip Triccab37fa662017-03-09 08:02:55 -080040 tmp = HOST_TO_BE_32 (src);
Philip Triccabff72122017-02-04 07:53:07 -080041
42 assert_int_equal (rc, TSS2_RC_SUCCESS);
Philip Triccab37fa662017-03-09 08:02:55 -080043 assert_memory_equal (&tmp, &buffer [1], sizeof (tmp));
Philip Triccabff72122017-02-04 07:53:07 -080044 assert_int_equal (offset, sizeof (buffer));
45}
46/*
Philip Triccabff72122017-02-04 07:53:07 -080047 * Test case passing NULL buffer and non-NULL offset. Test to be sure offset
48 * is updated to the size of the src parameter.
49 */
50void
51UINT32_marshal_buffer_null_with_offset (void **state)
52{
53 UINT32 src = 0xdeadbeef;
54 size_t offset = 100;
55 TSS2_RC rc;
56
Philip Tricca42039cc2017-03-08 15:53:18 -080057 rc = UINT32_Marshal (src, NULL, 2, &offset);
Philip Triccabff72122017-02-04 07:53:07 -080058
59 assert_int_equal (rc, TSS2_RC_SUCCESS);
60 assert_int_equal (offset, 100 + sizeof (src));
61}
62/*
63 * Test case passing NULL buffer and NULL offset.
64 */
65void
66UINT32_marshal_buffer_null_offset_null (void **state)
67{
68 UINT32 src = 0xdeadbeef;
69 TSS2_RC rc;
70
Philip Tricca42039cc2017-03-08 15:53:18 -080071 rc = UINT32_Marshal (src, NULL, sizeof (src), NULL);
Philip Triccabff72122017-02-04 07:53:07 -080072
73 assert_int_equal (rc, TSS2_TYPES_RC_BAD_REFERENCE);
74}
75/*
76 * Test failing case where buffer_size - offset (size of available space
77 * in buffer) is less than sizeof (UINT32). Also check offset is unchanged.
78 */
79void
80UINT32_marshal_buffer_size_lt_data (void **state)
81{
82 UINT32 src = 0xdeadbeef;
83 uint8_t buffer [4] = { 0 };
84 size_t offset = 2;
85 TSS2_RC rc;
86
Philip Tricca42039cc2017-03-08 15:53:18 -080087 rc = UINT32_Marshal (src, buffer, sizeof (src), &offset);
Philip Triccabff72122017-02-04 07:53:07 -080088
89 assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
90 assert_int_equal (offset, 2);
91}
92/*
93 * Test failing case where buffer_size is less than the offset value.
94 * This should return INSUFFICIENT_BUFFER and the offset should be unchanged.
95 */
96void
97UINT32_marshal_buffer_size_lt_offset (void **state)
98{
99 UINT32 src = 0xdeadbeef;
100 uint8_t buffer [4] = { 0 };
101 size_t buffer_size = sizeof (buffer);
102 size_t offset = sizeof (buffer) + 1;
103 TSS2_RC rc;
104
Philip Tricca42039cc2017-03-08 15:53:18 -0800105 rc = UINT32_Marshal (src, buffer, buffer_size, &offset);
Philip Triccabff72122017-02-04 07:53:07 -0800106
107 assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
108 assert_int_equal (offset, sizeof (buffer) + 1);
109}
110/*
111 * Test case for successful UINT32 unmarshaling.
112 */
113void
114UINT32_unmarshal_success (void **state)
115{
116 uint8_t buffer [4] = { 0xde, 0xad, 0xbe, 0xef };
117 uint8_t buffer_size = sizeof (buffer);
Philip Triccab37fa662017-03-09 08:02:55 -0800118 UINT32 dest = 0, tmp = 0;
Philip Triccabff72122017-02-04 07:53:07 -0800119 TSS2_RC rc;
120
121 rc = UINT32_Unmarshal (buffer, buffer_size, NULL, &dest);
Philip Triccab37fa662017-03-09 08:02:55 -0800122 tmp = HOST_TO_BE_32 (dest);
Philip Triccabff72122017-02-04 07:53:07 -0800123
124 assert_int_equal (rc, TSS2_RC_SUCCESS);
Philip Triccab37fa662017-03-09 08:02:55 -0800125 assert_memory_equal (&tmp, buffer, sizeof (tmp));
Philip Triccabff72122017-02-04 07:53:07 -0800126}
127/*
128 * Test case for successful UINT32 unmarshaling with offset.
129 */
130void
131UINT32_unmarshal_success_offset (void **state)
132{
Philip Triccab37fa662017-03-09 08:02:55 -0800133 UINT32 dest = 0, tmp = 0;
Philip Triccabff72122017-02-04 07:53:07 -0800134 uint8_t buffer [5] = { 0xff, 0xde, 0xad, 0xbe, 0xef };
135 size_t buffer_size = sizeof (buffer);
136 size_t offset = 1;
137 TSS2_RC rc;
138
139 rc = UINT32_Unmarshal (buffer, buffer_size, &offset, &dest);
Philip Triccab37fa662017-03-09 08:02:55 -0800140 tmp = HOST_TO_BE_32 (dest);
Philip Triccabff72122017-02-04 07:53:07 -0800141
142 assert_int_equal (rc, TSS2_RC_SUCCESS);
Philip Triccab37fa662017-03-09 08:02:55 -0800143 assert_memory_equal (&tmp, &buffer [1], sizeof (tmp));
Philip Triccabff72122017-02-04 07:53:07 -0800144 assert_int_equal (offset, 5);
145}
146/*
147 * Test case ensures a NULL buffer parameter produces a BAD_REFERENCE RC.
148 */
149void
150UINT32_unmarshal_buffer_null (void **state)
151{
152 TSS2_RC rc;
153
154 rc = UINT32_Unmarshal (NULL, 1, NULL, NULL);
155
156 assert_int_equal (rc, TSS2_TYPES_RC_BAD_REFERENCE);
157}
158/*
159 * Test case ensures a NULL dest and offset parameters produce an
160 * INSUFFICIENT_BUFFER RC.
161 */
162void
163UINT32_unmarshal_dest_null (void **state)
164{
165 uint8_t buffer [1];
166 TSS2_RC rc;
167
168 rc = UINT32_Unmarshal (buffer, sizeof (buffer), NULL, NULL);
169
170 assert_int_equal (rc, TSS2_TYPES_RC_BAD_REFERENCE);
171}
172/*
173 * Test case ensures that INSUFFICIENT_BUFFER is returned when buffer_size
174 * is less than the provided offset.
175 */
176void
177UINT32_unmarshal_buffer_size_lt_offset (void **state)
178{
179 UINT32 dest = 0;
180 uint8_t buffer [1];
181 size_t offset = sizeof (buffer) + 1;
182 TSS2_RC rc;
183
184 rc = UINT32_Unmarshal (buffer, sizeof (buffer), &offset, &dest);
185
186 assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
187 assert_int_equal (offset, sizeof (buffer) + 1);
188 assert_int_equal (dest, 0);
189}
190/*
191 * Test case ensures that INSUFFICIENT_BUFFER is returned when buffer_size -
192 * local_offset is less than dest (the destination type).
193 */
194void
195UINT32_unmarshal_buffer_size_lt_dest (void **state)
196{
197 UINT32 dest = 0;
198 uint8_t buffer [3];
199 size_t offset = sizeof (buffer);
200 TSS2_RC rc;
201
202 rc = UINT32_Unmarshal (buffer, sizeof (buffer), &offset, &dest);
203
204 assert_int_equal (rc, TSS2_TYPES_RC_INSUFFICIENT_BUFFER);
205 assert_int_equal (offset, sizeof (buffer));
206 assert_int_equal (dest, 0);
207}
208int
209main (void)
210{
211 const UnitTest tests [] = {
212 unit_test (UINT32_marshal_success),
213 unit_test (UINT32_marshal_success_offset),
Philip Triccabff72122017-02-04 07:53:07 -0800214 unit_test (UINT32_marshal_buffer_null_with_offset),
215 unit_test (UINT32_marshal_buffer_null_offset_null),
216 unit_test (UINT32_marshal_buffer_size_lt_data),
217 unit_test (UINT32_marshal_buffer_size_lt_offset),
218 unit_test (UINT32_unmarshal_success),
219 unit_test (UINT32_unmarshal_success_offset),
220 unit_test (UINT32_unmarshal_buffer_null),
221 unit_test (UINT32_unmarshal_dest_null),
222 unit_test (UINT32_unmarshal_buffer_size_lt_offset),
223 unit_test (UINT32_unmarshal_buffer_size_lt_dest),
224 };
225 return run_tests (tests);
226}