1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /* A collection of unit tests for blob.c */
25
26 #include <inttypes.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <string.h>
31
32 #include "util/ralloc.h"
33 #include "blob.h"
34
35 #define bytes_test_str "bytes_test"
36 #define reserve_test_str "reserve_test"
37
38 /* This placeholder must be the same length as the next overwrite_test_str */
39 #define placeholder_str "XXXXXXXXXXXXXX"
40 #define overwrite_test_str "overwrite_test"
41 #define uint32_test 0x12345678
42 #define uint32_placeholder 0xDEADBEEF
43 #define uint32_overwrite 0xA1B2C3D4
44 #define uint64_test 0x1234567890ABCDEF
45 #define string_test_str "string_test"
46
47 bool error = false;
48
49 static void
expect_equal(uint64_t expected,uint64_t actual,const char * test)50 expect_equal(uint64_t expected, uint64_t actual, const char *test)
51 {
52 if (actual != expected) {
53 fprintf(stderr,
54 "Error: Test '%s' failed: "
55 "Expected=%" PRIu64 ", "
56 "Actual=%" PRIu64 "\n",
57 test, expected, actual);
58 error = true;
59 }
60 }
61
62 static void
expect_unequal(uint64_t expected,uint64_t actual,const char * test)63 expect_unequal(uint64_t expected, uint64_t actual, const char *test)
64 {
65 if (actual == expected) {
66 fprintf(stderr,
67 "Error: Test '%s' failed: Result=%" PRIu64 ", "
68 "but expected something different.\n",
69 test, actual);
70 error = true;
71 }
72 }
73
74 static void
expect_equal_str(const char * expected,const char * actual,const char * test)75 expect_equal_str(const char *expected, const char *actual, const char *test)
76 {
77 if (strcmp(expected, actual)) {
78 fprintf (stderr, "Error: Test '%s' failed:\n\t"
79 "Expected=\"%s\", Actual=\"%s\"\n",
80 test, expected, actual);
81 error = true;
82 }
83 }
84
85 static void
expect_equal_bytes(uint8_t * expected,uint8_t * actual,size_t num_bytes,const char * test)86 expect_equal_bytes(uint8_t *expected, uint8_t *actual,
87 size_t num_bytes, const char *test)
88 {
89 size_t i;
90
91 if (memcmp(expected, actual, num_bytes)) {
92 fprintf (stderr, "Error: Test '%s' failed:\n\t", test);
93
94 fprintf (stderr, "Expected=[");
95 for (i = 0; i < num_bytes; i++) {
96 if (i != 0)
97 fprintf(stderr, ", ");
98 fprintf(stderr, "0x%02x", expected[i]);
99 }
100 fprintf (stderr, "]");
101
102 fprintf (stderr, "Actual=[");
103 for (i = 0; i < num_bytes; i++) {
104 if (i != 0)
105 fprintf(stderr, ", ");
106 fprintf(stderr, "0x%02x", actual[i]);
107 }
108 fprintf (stderr, "]\n");
109
110 error = true;
111 }
112 }
113
114 /* Test at least one call of each blob_write_foo and blob_read_foo function,
115 * verifying that we read out everything we wrote, that every bytes is
116 * consumed, and that the overrun bit is not set.
117 */
118 static void
test_write_and_read_functions(void)119 test_write_and_read_functions (void)
120 {
121 void *ctx = ralloc_context(NULL);
122 struct blob *blob;
123 struct blob_reader reader;
124 uint8_t *reserved;
125 size_t str_offset, uint_offset;
126 uint8_t reserve_buf[sizeof(reserve_test_str)];
127
128 blob = blob_create(ctx);
129
130 /*** Test blob by writing one of every possible kind of value. */
131
132 blob_write_bytes(blob, bytes_test_str, sizeof(bytes_test_str));
133
134 reserved = blob_reserve_bytes(blob, sizeof(reserve_test_str));
135 memcpy(reserved, reserve_test_str, sizeof(reserve_test_str));
136
137 /* Write a placeholder, (to be replaced later via overwrite_bytes) */
138 str_offset = blob->size;
139 blob_write_bytes(blob, placeholder_str, sizeof(placeholder_str));
140
141 blob_write_uint32(blob, uint32_test);
142
143 /* Write a placeholder, (to be replaced later via overwrite_uint32) */
144 uint_offset = blob->size;
145 blob_write_uint32(blob, uint32_placeholder);
146
147 blob_write_uint64(blob, uint64_test);
148
149 blob_write_intptr(blob, (intptr_t) blob);
150
151 blob_write_string(blob, string_test_str);
152
153 /* Finally, overwrite our placeholders. */
154 blob_overwrite_bytes(blob, str_offset, overwrite_test_str,
155 sizeof(overwrite_test_str));
156 blob_overwrite_uint32(blob, uint_offset, uint32_overwrite);
157
158 /*** Now read each value and verify. */
159 blob_reader_init(&reader, blob->data, blob->size);
160
161 expect_equal_str(bytes_test_str,
162 blob_read_bytes(&reader, sizeof(bytes_test_str)),
163 "blob_write/read_bytes");
164
165 blob_copy_bytes(&reader, reserve_buf, sizeof(reserve_buf));
166 expect_equal_str(reserve_test_str, (char *) reserve_buf,
167 "blob_reserve_bytes/blob_copy_bytes");
168
169 expect_equal_str(overwrite_test_str,
170 blob_read_bytes(&reader, sizeof(overwrite_test_str)),
171 "blob_overwrite_bytes");
172
173 expect_equal(uint32_test, blob_read_uint32(&reader),
174 "blob_write/read_uint32");
175 expect_equal(uint32_overwrite, blob_read_uint32(&reader),
176 "blob_overwrite_uint32");
177 expect_equal(uint64_test, blob_read_uint64(&reader),
178 "blob_write/read_uint64");
179 expect_equal((intptr_t) blob, blob_read_intptr(&reader),
180 "blob_write/read_intptr");
181 expect_equal_str(string_test_str, blob_read_string(&reader),
182 "blob_write/read_string");
183
184 expect_equal(reader.end - reader.data, reader.current - reader.data,
185 "read_consumes_all_bytes");
186 expect_equal(false, reader.overrun, "read_does_not_overrun");
187
188 ralloc_free(ctx);
189 }
190
191 /* Test that data values are written and read with proper alignment. */
192 static void
test_alignment(void)193 test_alignment(void)
194 {
195 void *ctx = ralloc_context(NULL);
196 struct blob *blob;
197 struct blob_reader reader;
198 uint8_t bytes[] = "ABCDEFGHIJKLMNOP";
199 size_t delta, last, num_bytes;
200
201 blob = blob_create(ctx);
202
203 /* First, write an intptr value to the blob and capture that size. This is
204 * the expected offset between any pair of intptr values (if written with
205 * alignment).
206 */
207 blob_write_intptr(blob, (intptr_t) blob);
208
209 delta = blob->size;
210 last = blob->size;
211
212 /* Then loop doing the following:
213 *
214 * 1. Write an unaligned number of bytes
215 * 2. Verify that write results in an unaligned size
216 * 3. Write an intptr_t value
217 * 2. Verify that that write results in an aligned size
218 */
219 for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) {
220 blob_write_bytes(blob, bytes, num_bytes);
221
222 expect_unequal(delta, blob->size - last, "unaligned write of bytes");
223
224 blob_write_intptr(blob, (intptr_t) blob);
225
226 expect_equal(2 * delta, blob->size - last, "aligned write of intptr");
227
228 last = blob->size;
229 }
230
231 /* Finally, test that reading also does proper alignment. Since we know
232 * that values were written with all the right alignment, all we have to do
233 * here is verify that correct values are read.
234 */
235 blob_reader_init(&reader, blob->data, blob->size);
236
237 expect_equal((intptr_t) blob, blob_read_intptr(&reader),
238 "read of initial, aligned intptr_t");
239
240 for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) {
241 expect_equal_bytes(bytes, blob_read_bytes(&reader, num_bytes),
242 num_bytes, "unaligned read of bytes");
243 expect_equal((intptr_t) blob, blob_read_intptr(&reader),
244 "aligned read of intptr_t");
245 }
246
247 ralloc_free(ctx);
248 }
249
250 /* Test that we detect overrun. */
251 static void
test_overrun(void)252 test_overrun(void)
253 {
254 void *ctx =ralloc_context(NULL);
255 struct blob *blob;
256 struct blob_reader reader;
257 uint32_t value = 0xdeadbeef;
258
259 blob = blob_create(ctx);
260
261 blob_write_uint32(blob, value);
262
263 blob_reader_init(&reader, blob->data, blob->size);
264
265 expect_equal(value, blob_read_uint32(&reader), "read before overrun");
266 expect_equal(false, reader.overrun, "overrun flag not set");
267 expect_equal(0, blob_read_uint32(&reader), "read at overrun");
268 expect_equal(true, reader.overrun, "overrun flag set");
269
270 ralloc_free(ctx);
271 }
272
273 /* Test that we can read and write some large objects, (exercising the code in
274 * the blob_write functions to realloc blob->data.
275 */
276 static void
test_big_objects(void)277 test_big_objects(void)
278 {
279 void *ctx = ralloc_context(NULL);
280 struct blob *blob;
281 struct blob_reader reader;
282 int size = 1000;
283 int count = 1000;
284 size_t i;
285 char *buf;
286
287 blob = blob_create(ctx);
288
289 /* Initialize our buffer. */
290 buf = ralloc_size(ctx, size);
291 for (i = 0; i < size; i++) {
292 buf[i] = i % 256;
293 }
294
295 /* Write it many times. */
296 for (i = 0; i < count; i++) {
297 blob_write_bytes(blob, buf, size);
298 }
299
300 blob_reader_init(&reader, blob->data, blob->size);
301
302 /* Read and verify it many times. */
303 for (i = 0; i < count; i++) {
304 expect_equal_bytes((uint8_t *) buf, blob_read_bytes(&reader, size), size,
305 "read of large objects");
306 }
307
308 expect_equal(reader.end - reader.data, reader.current - reader.data,
309 "number of bytes read reading large objects");
310
311 expect_equal(false, reader.overrun,
312 "overrun flag not set reading large objects");
313
314 ralloc_free(ctx);
315 }
316
317 int
main(void)318 main (void)
319 {
320 test_write_and_read_functions ();
321 test_alignment ();
322 test_overrun ();
323 test_big_objects ();
324
325 return error ? 1 : 0;
326 }
327