blob: c7e99769abadd6207db8ab03081c1050c009534e [file] [log] [blame]
Randall Spangler786acda2014-05-22 13:27:07 -07001/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Tests for firmware 2common.c
6 */
7
8#include "2sysincludes.h"
9#include "2common.h"
Randall Spangler786acda2014-05-22 13:27:07 -070010#include "test_common.h"
11
12/**
Randall Spangler6d03b522014-10-17 13:00:20 -070013 * Test memory compare functions
14 */
15static void test_memcmp(void)
16{
17 TEST_EQ(vb2_safe_memcmp("foo", "foo", 3), 0, "memcmp equal");
18 TEST_NEQ(vb2_safe_memcmp("foo1", "foo2", 4), 0, "memcmp different");
19 TEST_EQ(vb2_safe_memcmp("foo1", "foo2", 0), 0, "memcmp 0-size");
20}
21
22/**
Randall Spangler786acda2014-05-22 13:27:07 -070023 * Test alignment functions
24 */
25static void test_align(void)
26{
27 uint64_t buf[4];
28 uint8_t *p0, *ptr;
29 uint32_t size;
30
31 /* Already aligned */
32 p0 = (uint8_t *)buf;
33 ptr = p0;
34 size = 16;
Randall Spangler21457212014-06-06 09:30:14 -070035 TEST_SUCC(vb2_align(&ptr, &size, 4, 16), "vb2_align() aligned");
Randall Spangler786acda2014-05-22 13:27:07 -070036 TEST_EQ(vb2_offset_of(p0, ptr), 0, "ptr");
37 TEST_EQ(size, 16, " size");
Randall Spangler21457212014-06-06 09:30:14 -070038 TEST_EQ(vb2_align(&ptr, &size, 4, 17),
39 VB2_ERROR_ALIGN_SIZE, "vb2_align() small");
Randall Spangler786acda2014-05-22 13:27:07 -070040
41 /* Offset */
42 ptr = p0 + 1;
43 size = 15;
Randall Spangler21457212014-06-06 09:30:14 -070044 TEST_SUCC(vb2_align(&ptr, &size, 4, 12), "vb2_align() offset");
Randall Spangler786acda2014-05-22 13:27:07 -070045 TEST_EQ(vb2_offset_of(p0, ptr), 4, "ptr");
46 TEST_EQ(size, 12, " size");
47
48 /* Offset, now too small */
49 ptr = p0 + 1;
50 size = 15;
Randall Spangler21457212014-06-06 09:30:14 -070051 TEST_EQ(vb2_align(&ptr, &size, 4, 15),
52 VB2_ERROR_ALIGN_SIZE, "vb2_align() offset small");
Randall Spangler786acda2014-05-22 13:27:07 -070053
54 /* Offset, too small even to align */
55 ptr = p0 + 1;
56 size = 1;
Randall Spangler21457212014-06-06 09:30:14 -070057 TEST_EQ(vb2_align(&ptr, &size, 4, 1),
58 VB2_ERROR_ALIGN_BIGGER_THAN_SIZE, "vb2_align() offset tiny");
Randall Spangler786acda2014-05-22 13:27:07 -070059}
60
61/**
62 * Test work buffer functions
63 */
64static void test_workbuf(void)
65{
Bill Richardson73e5eb32015-01-26 12:18:25 -080066 uint64_t buf[8] __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
Randall Spangler786acda2014-05-22 13:27:07 -070067 uint8_t *p0 = (uint8_t *)buf, *ptr;
68 struct vb2_workbuf wb;
69
Bill Richardson73e5eb32015-01-26 12:18:25 -080070 /* NOTE: There are several magic numbers below which assume that
71 * VB2_WORKBUF_ALIGN == 16 */
Randall Spangler786acda2014-05-22 13:27:07 -070072
Bill Richardson73e5eb32015-01-26 12:18:25 -080073 /* Init */
74 vb2_workbuf_init(&wb, p0, 64);
75 TEST_EQ(vb2_offset_of(p0, wb.buf), 0, "Workbuf init aligned");
76 TEST_EQ(wb.size, 64, " size");
77
78 vb2_workbuf_init(&wb, p0 + 4, 64);
79 TEST_EQ(vb2_offset_of(p0, wb.buf), VB2_WORKBUF_ALIGN,
80 "Workbuf init unaligned");
81 TEST_EQ(wb.size, 64 - VB2_WORKBUF_ALIGN + 4, " size");
Randall Spangler786acda2014-05-22 13:27:07 -070082
83 vb2_workbuf_init(&wb, p0 + 2, 5);
84 TEST_EQ(wb.size, 0, "Workbuf init tiny unaligned size");
85
86 /* Alloc rounds up */
Bill Richardson73e5eb32015-01-26 12:18:25 -080087 vb2_workbuf_init(&wb, p0, 64);
Randall Spangler786acda2014-05-22 13:27:07 -070088 ptr = vb2_workbuf_alloc(&wb, 22);
89 TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf alloc");
Bill Richardson73e5eb32015-01-26 12:18:25 -080090 TEST_EQ(vb2_offset_of(p0, wb.buf), 32, " buf");
91 TEST_EQ(wb.size, 32, " size");
Randall Spangler786acda2014-05-22 13:27:07 -070092
93 vb2_workbuf_init(&wb, p0, 32);
94 TEST_PTR_EQ(vb2_workbuf_alloc(&wb, 33), NULL, "Workbuf alloc too big");
95
96 /* Free reverses alloc */
97 vb2_workbuf_init(&wb, p0, 32);
98 vb2_workbuf_alloc(&wb, 22);
99 vb2_workbuf_free(&wb, 22);
100 TEST_EQ(vb2_offset_of(p0, wb.buf), 0, "Workbuf free buf");
101 TEST_EQ(wb.size, 32, " size");
102
103 /* Realloc keeps same pointer as alloc */
Bill Richardson73e5eb32015-01-26 12:18:25 -0800104 vb2_workbuf_init(&wb, p0, 64);
Randall Spangler786acda2014-05-22 13:27:07 -0700105 vb2_workbuf_alloc(&wb, 6);
106 ptr = vb2_workbuf_realloc(&wb, 6, 21);
107 TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf realloc");
Bill Richardson73e5eb32015-01-26 12:18:25 -0800108 TEST_EQ(vb2_offset_of(p0, wb.buf), 32, " buf");
109 TEST_EQ(wb.size, 32, " size");
Randall Spangler786acda2014-05-22 13:27:07 -0700110}
111
112int main(int argc, char* argv[])
113{
Randall Spangler6d03b522014-10-17 13:00:20 -0700114 test_memcmp();
Randall Spangler786acda2014-05-22 13:27:07 -0700115 test_align();
116 test_workbuf();
Randall Spangler786acda2014-05-22 13:27:07 -0700117
118 return gTestSuccess ? 0 : 255;
119}