Home | History | Annotate | Download | only in tests
      1 /* 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"
     10 #include "test_common.h"
     11 
     12 /**
     13  * Test memory compare functions
     14  */
     15 static 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 /**
     23  * Test alignment functions
     24  */
     25 static 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;
     35 	TEST_SUCC(vb2_align(&ptr, &size, 4, 16), "vb2_align() aligned");
     36 	TEST_EQ(vb2_offset_of(p0, ptr), 0, "ptr");
     37 	TEST_EQ(size, 16, "  size");
     38 	TEST_EQ(vb2_align(&ptr, &size, 4, 17),
     39 		VB2_ERROR_ALIGN_SIZE, "vb2_align() small");
     40 
     41 	/* Offset */
     42 	ptr = p0 + 1;
     43 	size = 15;
     44 	TEST_SUCC(vb2_align(&ptr, &size, 4, 12), "vb2_align() offset");
     45 	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;
     51 	TEST_EQ(vb2_align(&ptr, &size, 4, 15),
     52 		VB2_ERROR_ALIGN_SIZE, "vb2_align() offset small");
     53 
     54 	/* Offset, too small even to align */
     55 	ptr = p0 + 1;
     56 	size = 1;
     57 	TEST_EQ(vb2_align(&ptr, &size, 4, 1),
     58 		VB2_ERROR_ALIGN_BIGGER_THAN_SIZE, "vb2_align() offset tiny");
     59 }
     60 
     61 /**
     62  * Test work buffer functions
     63  */
     64 static void test_workbuf(void)
     65 {
     66 	uint64_t buf[8] __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
     67 	uint8_t *p0 = (uint8_t *)buf, *ptr;
     68 	struct vb2_workbuf wb;
     69 
     70 	/* NOTE: There are several magic numbers below which assume that
     71 	 * VB2_WORKBUF_ALIGN == 16 */
     72 
     73 	/* 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");
     82 
     83 	vb2_workbuf_init(&wb, p0 + 2, 5);
     84 	TEST_EQ(wb.size, 0, "Workbuf init tiny unaligned size");
     85 
     86 	/* Alloc rounds up */
     87 	vb2_workbuf_init(&wb, p0, 64);
     88 	ptr = vb2_workbuf_alloc(&wb, 22);
     89 	TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf alloc");
     90 	TEST_EQ(vb2_offset_of(p0, wb.buf), 32, "  buf");
     91 	TEST_EQ(wb.size, 32, "  size");
     92 
     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 */
    104 	vb2_workbuf_init(&wb, p0, 64);
    105 	vb2_workbuf_alloc(&wb, 6);
    106 	ptr = vb2_workbuf_realloc(&wb, 6, 21);
    107 	TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf realloc");
    108 	TEST_EQ(vb2_offset_of(p0, wb.buf), 32, "  buf");
    109 	TEST_EQ(wb.size, 32, "  size");
    110 }
    111 
    112 int main(int argc, char* argv[])
    113 {
    114 	test_memcmp();
    115 	test_align();
    116 	test_workbuf();
    117 
    118 	return gTestSuccess ? 0 : 255;
    119 }
    120