blob: 21b8b1efdc186c9f16b0787e6054bda507311b61 [file] [log] [blame]
Carl Worth977ddec2014-12-15 15:58:34 -08001/*
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
Aaron Watry60c3a0a2016-11-22 11:18:11 -060026#include <inttypes.h>
Carl Worth977ddec2014-12-15 15:58:34 -080027#include <stdio.h>
28#include <stdlib.h>
29#include <stdbool.h>
30#include <string.h>
Dylan Baker5eb0f332018-05-22 15:32:22 -070031#ifdef _MSC_VER
32#include <BaseTsd.h>
33typedef SSIZE_T ssize_t;
34#endif
Carl Worth977ddec2014-12-15 15:58:34 -080035
36#include "util/ralloc.h"
37#include "blob.h"
38
39#define bytes_test_str "bytes_test"
40#define reserve_test_str "reserve_test"
41
42/* This placeholder must be the same length as the next overwrite_test_str */
43#define placeholder_str "XXXXXXXXXXXXXX"
44#define overwrite_test_str "overwrite_test"
45#define uint32_test 0x12345678
46#define uint32_placeholder 0xDEADBEEF
47#define uint32_overwrite 0xA1B2C3D4
48#define uint64_test 0x1234567890ABCDEF
49#define string_test_str "string_test"
50
51bool error = false;
52
53static void
54expect_equal(uint64_t expected, uint64_t actual, const char *test)
55{
56 if (actual != expected) {
Aaron Watry60c3a0a2016-11-22 11:18:11 -060057 fprintf(stderr,
58 "Error: Test '%s' failed: "
59 "Expected=%" PRIu64 ", "
60 "Actual=%" PRIu64 "\n",
Carl Worth977ddec2014-12-15 15:58:34 -080061 test, expected, actual);
62 error = true;
63 }
64}
65
66static void
67expect_unequal(uint64_t expected, uint64_t actual, const char *test)
68{
69 if (actual == expected) {
Aaron Watry60c3a0a2016-11-22 11:18:11 -060070 fprintf(stderr,
71 "Error: Test '%s' failed: Result=%" PRIu64 ", "
72 "but expected something different.\n",
Carl Worth977ddec2014-12-15 15:58:34 -080073 test, actual);
74 error = true;
75 }
76}
77
78static void
79expect_equal_str(const char *expected, const char *actual, const char *test)
80{
81 if (strcmp(expected, actual)) {
82 fprintf (stderr, "Error: Test '%s' failed:\n\t"
83 "Expected=\"%s\", Actual=\"%s\"\n",
84 test, expected, actual);
85 error = true;
86 }
87}
88
89static void
Jason Ekstrand4d56ff02017-10-11 12:09:02 -070090expect_equal_bytes(uint8_t *expected, const uint8_t *actual,
Carl Worth977ddec2014-12-15 15:58:34 -080091 size_t num_bytes, const char *test)
92{
93 size_t i;
94
95 if (memcmp(expected, actual, num_bytes)) {
96 fprintf (stderr, "Error: Test '%s' failed:\n\t", test);
97
98 fprintf (stderr, "Expected=[");
99 for (i = 0; i < num_bytes; i++) {
100 if (i != 0)
101 fprintf(stderr, ", ");
102 fprintf(stderr, "0x%02x", expected[i]);
103 }
104 fprintf (stderr, "]");
105
106 fprintf (stderr, "Actual=[");
107 for (i = 0; i < num_bytes; i++) {
108 if (i != 0)
109 fprintf(stderr, ", ");
110 fprintf(stderr, "0x%02x", actual[i]);
111 }
112 fprintf (stderr, "]\n");
113
114 error = true;
115 }
116}
117
118/* Test at least one call of each blob_write_foo and blob_read_foo function,
119 * verifying that we read out everything we wrote, that every bytes is
120 * consumed, and that the overrun bit is not set.
121 */
122static void
123test_write_and_read_functions (void)
124{
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700125 struct blob blob;
Carl Worth977ddec2014-12-15 15:58:34 -0800126 struct blob_reader reader;
Connor Abbott69354402017-09-15 00:29:46 -0400127 ssize_t reserved;
Carl Worth977ddec2014-12-15 15:58:34 -0800128 size_t str_offset, uint_offset;
129 uint8_t reserve_buf[sizeof(reserve_test_str)];
130
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700131 blob_init(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800132
133 /*** Test blob by writing one of every possible kind of value. */
134
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700135 blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str));
Carl Worth977ddec2014-12-15 15:58:34 -0800136
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700137 reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str));
Connor Abbott69354402017-09-15 00:29:46 -0400138 blob_overwrite_bytes(&blob, reserved, reserve_test_str, sizeof(reserve_test_str));
Carl Worth977ddec2014-12-15 15:58:34 -0800139
140 /* Write a placeholder, (to be replaced later via overwrite_bytes) */
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700141 str_offset = blob.size;
142 blob_write_bytes(&blob, placeholder_str, sizeof(placeholder_str));
Carl Worth977ddec2014-12-15 15:58:34 -0800143
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700144 blob_write_uint32(&blob, uint32_test);
Carl Worth977ddec2014-12-15 15:58:34 -0800145
146 /* Write a placeholder, (to be replaced later via overwrite_uint32) */
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700147 uint_offset = blob.size;
148 blob_write_uint32(&blob, uint32_placeholder);
Carl Worth977ddec2014-12-15 15:58:34 -0800149
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700150 blob_write_uint64(&blob, uint64_test);
Carl Worth977ddec2014-12-15 15:58:34 -0800151
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700152 blob_write_intptr(&blob, (intptr_t) &blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800153
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700154 blob_write_string(&blob, string_test_str);
Carl Worth977ddec2014-12-15 15:58:34 -0800155
156 /* Finally, overwrite our placeholders. */
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700157 blob_overwrite_bytes(&blob, str_offset, overwrite_test_str,
Carl Worth977ddec2014-12-15 15:58:34 -0800158 sizeof(overwrite_test_str));
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700159 blob_overwrite_uint32(&blob, uint_offset, uint32_overwrite);
Carl Worth977ddec2014-12-15 15:58:34 -0800160
161 /*** Now read each value and verify. */
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700162 blob_reader_init(&reader, blob.data, blob.size);
Carl Worth977ddec2014-12-15 15:58:34 -0800163
164 expect_equal_str(bytes_test_str,
165 blob_read_bytes(&reader, sizeof(bytes_test_str)),
166 "blob_write/read_bytes");
167
168 blob_copy_bytes(&reader, reserve_buf, sizeof(reserve_buf));
169 expect_equal_str(reserve_test_str, (char *) reserve_buf,
170 "blob_reserve_bytes/blob_copy_bytes");
171
172 expect_equal_str(overwrite_test_str,
173 blob_read_bytes(&reader, sizeof(overwrite_test_str)),
174 "blob_overwrite_bytes");
175
176 expect_equal(uint32_test, blob_read_uint32(&reader),
177 "blob_write/read_uint32");
178 expect_equal(uint32_overwrite, blob_read_uint32(&reader),
179 "blob_overwrite_uint32");
180 expect_equal(uint64_test, blob_read_uint64(&reader),
181 "blob_write/read_uint64");
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700182 expect_equal((intptr_t) &blob, blob_read_intptr(&reader),
Carl Worth977ddec2014-12-15 15:58:34 -0800183 "blob_write/read_intptr");
184 expect_equal_str(string_test_str, blob_read_string(&reader),
185 "blob_write/read_string");
186
187 expect_equal(reader.end - reader.data, reader.current - reader.data,
188 "read_consumes_all_bytes");
189 expect_equal(false, reader.overrun, "read_does_not_overrun");
190
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700191 blob_finish(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800192}
193
194/* Test that data values are written and read with proper alignment. */
195static void
196test_alignment(void)
197{
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700198 struct blob blob;
Carl Worth977ddec2014-12-15 15:58:34 -0800199 struct blob_reader reader;
200 uint8_t bytes[] = "ABCDEFGHIJKLMNOP";
201 size_t delta, last, num_bytes;
202
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700203 blob_init(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800204
205 /* First, write an intptr value to the blob and capture that size. This is
206 * the expected offset between any pair of intptr values (if written with
207 * alignment).
208 */
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700209 blob_write_intptr(&blob, (intptr_t) &blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800210
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700211 delta = blob.size;
212 last = blob.size;
Carl Worth977ddec2014-12-15 15:58:34 -0800213
214 /* Then loop doing the following:
215 *
216 * 1. Write an unaligned number of bytes
217 * 2. Verify that write results in an unaligned size
218 * 3. Write an intptr_t value
219 * 2. Verify that that write results in an aligned size
220 */
221 for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) {
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700222 blob_write_bytes(&blob, bytes, num_bytes);
Carl Worth977ddec2014-12-15 15:58:34 -0800223
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700224 expect_unequal(delta, blob.size - last, "unaligned write of bytes");
Carl Worth977ddec2014-12-15 15:58:34 -0800225
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700226 blob_write_intptr(&blob, (intptr_t) &blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800227
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700228 expect_equal(2 * delta, blob.size - last, "aligned write of intptr");
Carl Worth977ddec2014-12-15 15:58:34 -0800229
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700230 last = blob.size;
Carl Worth977ddec2014-12-15 15:58:34 -0800231 }
232
233 /* Finally, test that reading also does proper alignment. Since we know
234 * that values were written with all the right alignment, all we have to do
235 * here is verify that correct values are read.
236 */
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700237 blob_reader_init(&reader, blob.data, blob.size);
Carl Worth977ddec2014-12-15 15:58:34 -0800238
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700239 expect_equal((intptr_t) &blob, blob_read_intptr(&reader),
Carl Worth977ddec2014-12-15 15:58:34 -0800240 "read of initial, aligned intptr_t");
241
242 for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) {
243 expect_equal_bytes(bytes, blob_read_bytes(&reader, num_bytes),
244 num_bytes, "unaligned read of bytes");
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700245 expect_equal((intptr_t) &blob, blob_read_intptr(&reader),
Carl Worth977ddec2014-12-15 15:58:34 -0800246 "aligned read of intptr_t");
247 }
248
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700249 blob_finish(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800250}
251
252/* Test that we detect overrun. */
253static void
254test_overrun(void)
255{
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700256 struct blob blob;
Carl Worth977ddec2014-12-15 15:58:34 -0800257 struct blob_reader reader;
258 uint32_t value = 0xdeadbeef;
259
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700260 blob_init(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800261
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700262 blob_write_uint32(&blob, value);
Carl Worth977ddec2014-12-15 15:58:34 -0800263
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700264 blob_reader_init(&reader, blob.data, blob.size);
Carl Worth977ddec2014-12-15 15:58:34 -0800265
266 expect_equal(value, blob_read_uint32(&reader), "read before overrun");
267 expect_equal(false, reader.overrun, "overrun flag not set");
268 expect_equal(0, blob_read_uint32(&reader), "read at overrun");
269 expect_equal(true, reader.overrun, "overrun flag set");
270
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700271 blob_finish(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800272}
273
274/* Test that we can read and write some large objects, (exercising the code in
275 * the blob_write functions to realloc blob->data.
276 */
277static void
278test_big_objects(void)
279{
280 void *ctx = ralloc_context(NULL);
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700281 struct blob blob;
Carl Worth977ddec2014-12-15 15:58:34 -0800282 struct blob_reader reader;
283 int size = 1000;
284 int count = 1000;
285 size_t i;
286 char *buf;
287
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700288 blob_init(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800289
290 /* Initialize our buffer. */
291 buf = ralloc_size(ctx, size);
292 for (i = 0; i < size; i++) {
293 buf[i] = i % 256;
294 }
295
296 /* Write it many times. */
297 for (i = 0; i < count; i++) {
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700298 blob_write_bytes(&blob, buf, size);
Carl Worth977ddec2014-12-15 15:58:34 -0800299 }
300
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700301 blob_reader_init(&reader, blob.data, blob.size);
Carl Worth977ddec2014-12-15 15:58:34 -0800302
303 /* Read and verify it many times. */
304 for (i = 0; i < count; i++) {
305 expect_equal_bytes((uint8_t *) buf, blob_read_bytes(&reader, size), size,
306 "read of large objects");
307 }
308
309 expect_equal(reader.end - reader.data, reader.current - reader.data,
310 "number of bytes read reading large objects");
311
312 expect_equal(false, reader.overrun,
313 "overrun flag not set reading large objects");
314
Jason Ekstrand49bb9f72017-10-11 09:44:33 -0700315 blob_finish(&blob);
Carl Worth977ddec2014-12-15 15:58:34 -0800316 ralloc_free(ctx);
317}
318
319int
320main (void)
321{
322 test_write_and_read_functions ();
323 test_alignment ();
324 test_overrun ();
325 test_big_objects ();
326
327 return error ? 1 : 0;
328}