vb21_host_misc_tests.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 host misc library vboot2 functions
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include "2sysincludes.h"
  10. #include "2common.h"
  11. #include "vb21_common.h"
  12. #include "host_common.h"
  13. #include "host_misc.h"
  14. #include "test_common.h"
  15. static void misc_tests(void)
  16. {
  17. TEST_EQ(roundup32(0), 0, "roundup32(0)");
  18. TEST_EQ(roundup32(15), 16, "roundup32(15)");
  19. TEST_EQ(roundup32(16), 16, "roundup32(16)");
  20. TEST_EQ(vb2_desc_size(NULL), 0, "desc size null");
  21. TEST_EQ(vb2_desc_size(""), 0, "desc size empty");
  22. TEST_EQ(vb2_desc_size("foo"), 4, "desc size 'foo'");
  23. TEST_EQ(vb2_desc_size("foob"), 8, "desc size 'foob'");
  24. }
  25. static void file_tests(const char *temp_dir)
  26. {
  27. char *testfile;
  28. const uint8_t test_data[] = "Some test data";
  29. uint8_t *read_data;
  30. uint32_t read_size;
  31. uint8_t cbuf[sizeof(struct vb21_struct_common) + 12];
  32. struct vb21_struct_common *c = (struct vb21_struct_common *)cbuf;
  33. xasprintf(&testfile, "%s/file_tests.dat", temp_dir);
  34. unlink(testfile);
  35. TEST_EQ(vb2_read_file(testfile, &read_data, &read_size),
  36. VB2_ERROR_READ_FILE_OPEN, "vb2_read_file() missing");
  37. TEST_EQ(vb2_write_file("no/such/dir", test_data, sizeof(test_data)),
  38. VB2_ERROR_WRITE_FILE_OPEN, "vb2_write_file() open");
  39. TEST_SUCC(vb2_write_file(testfile, test_data, sizeof(test_data)),
  40. "vb2_write_file() good");
  41. TEST_SUCC(vb2_read_file(testfile, &read_data, &read_size),
  42. "vb2_read_file() good");
  43. TEST_EQ(read_size, sizeof(test_data), " data size");
  44. TEST_EQ(memcmp(read_data, test_data, read_size), 0, " data");
  45. free(read_data);
  46. unlink(testfile);
  47. memset(cbuf, 0, sizeof(cbuf));
  48. c->fixed_size = sizeof(*c);
  49. c->total_size = sizeof(cbuf);
  50. c->magic = 0x1234;
  51. cbuf[sizeof(cbuf) - 1] = 0xed; /* Some non-zero data at the end */
  52. TEST_SUCC(vb21_write_object(testfile, c), "vb2_write_object() good");
  53. TEST_SUCC(vb2_read_file(testfile, &read_data, &read_size),
  54. "vb2_read_file() object");
  55. TEST_EQ(read_size, c->total_size, " data size");
  56. /* Compare the entire buffer, including the non-zero data at the end */
  57. TEST_EQ(memcmp(read_data, c, read_size), 0, " data");
  58. free(read_data);
  59. unlink(testfile);
  60. }
  61. int main(int argc, char* argv[])
  62. {
  63. if (argc != 2) {
  64. fprintf(stderr, "Usage: %s <temp_dir>\n", argv[0]);
  65. return -1;
  66. }
  67. const char *temp_dir = argv[1];
  68. misc_tests();
  69. file_tests(temp_dir);
  70. return gTestSuccess ? 0 : 255;
  71. }