vb2_common_tests.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "test_common.h"
  10. /**
  11. * Test memory compare functions
  12. */
  13. static void test_memcmp(void)
  14. {
  15. TEST_EQ(vb2_safe_memcmp("foo", "foo", 3), 0, "memcmp equal");
  16. TEST_NEQ(vb2_safe_memcmp("foo1", "foo2", 4), 0, "memcmp different");
  17. TEST_EQ(vb2_safe_memcmp("foo1", "foo2", 0), 0, "memcmp 0-size");
  18. }
  19. /**
  20. * Test alignment functions
  21. */
  22. static void test_align(void)
  23. {
  24. uint64_t buf[4];
  25. uint8_t *p0, *ptr;
  26. uint32_t size;
  27. /* Already aligned */
  28. p0 = (uint8_t *)buf;
  29. ptr = p0;
  30. size = 16;
  31. TEST_SUCC(vb2_align(&ptr, &size, 4, 16), "vb2_align() aligned");
  32. TEST_EQ(vb2_offset_of(p0, ptr), 0, "ptr");
  33. TEST_EQ(size, 16, " size");
  34. TEST_EQ(vb2_align(&ptr, &size, 4, 17),
  35. VB2_ERROR_ALIGN_SIZE, "vb2_align() small");
  36. /* Offset */
  37. ptr = p0 + 1;
  38. size = 15;
  39. TEST_SUCC(vb2_align(&ptr, &size, 4, 12), "vb2_align() offset");
  40. TEST_EQ(vb2_offset_of(p0, ptr), 4, "ptr");
  41. TEST_EQ(size, 12, " size");
  42. /* Offset, now too small */
  43. ptr = p0 + 1;
  44. size = 15;
  45. TEST_EQ(vb2_align(&ptr, &size, 4, 15),
  46. VB2_ERROR_ALIGN_SIZE, "vb2_align() offset small");
  47. /* Offset, too small even to align */
  48. ptr = p0 + 1;
  49. size = 1;
  50. TEST_EQ(vb2_align(&ptr, &size, 4, 1),
  51. VB2_ERROR_ALIGN_BIGGER_THAN_SIZE, "vb2_align() offset tiny");
  52. }
  53. /**
  54. * Test work buffer functions
  55. */
  56. static void test_workbuf(void)
  57. {
  58. uint64_t buf[8] __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  59. uint8_t *p0 = (uint8_t *)buf, *ptr;
  60. struct vb2_workbuf wb;
  61. /* NOTE: There are several magic numbers below which assume that
  62. * VB2_WORKBUF_ALIGN == 16 */
  63. /* Init */
  64. vb2_workbuf_init(&wb, p0, 64);
  65. TEST_EQ(vb2_offset_of(p0, wb.buf), 0, "Workbuf init aligned");
  66. TEST_EQ(wb.size, 64, " size");
  67. vb2_workbuf_init(&wb, p0 + 4, 64);
  68. TEST_EQ(vb2_offset_of(p0, wb.buf), VB2_WORKBUF_ALIGN,
  69. "Workbuf init unaligned");
  70. TEST_EQ(wb.size, 64 - VB2_WORKBUF_ALIGN + 4, " size");
  71. vb2_workbuf_init(&wb, p0 + 2, 5);
  72. TEST_EQ(wb.size, 0, "Workbuf init tiny unaligned size");
  73. /* Alloc rounds up */
  74. vb2_workbuf_init(&wb, p0, 64);
  75. ptr = vb2_workbuf_alloc(&wb, 22);
  76. TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf alloc");
  77. TEST_EQ(vb2_offset_of(p0, wb.buf), 32, " buf");
  78. TEST_EQ(wb.size, 32, " size");
  79. vb2_workbuf_init(&wb, p0, 32);
  80. TEST_PTR_EQ(vb2_workbuf_alloc(&wb, 33), NULL, "Workbuf alloc too big");
  81. /* Free reverses alloc */
  82. vb2_workbuf_init(&wb, p0, 32);
  83. vb2_workbuf_alloc(&wb, 22);
  84. vb2_workbuf_free(&wb, 22);
  85. TEST_EQ(vb2_offset_of(p0, wb.buf), 0, "Workbuf free buf");
  86. TEST_EQ(wb.size, 32, " size");
  87. /* Realloc keeps same pointer as alloc */
  88. vb2_workbuf_init(&wb, p0, 64);
  89. vb2_workbuf_alloc(&wb, 6);
  90. ptr = vb2_workbuf_realloc(&wb, 6, 21);
  91. TEST_EQ(vb2_offset_of(p0, ptr), 0, "Workbuf realloc");
  92. TEST_EQ(vb2_offset_of(p0, wb.buf), 32, " buf");
  93. TEST_EQ(wb.size, 32, " size");
  94. }
  95. int main(int argc, char* argv[])
  96. {
  97. test_memcmp();
  98. test_align();
  99. test_workbuf();
  100. return gTestSuccess ? 0 : 255;
  101. }