blob: 638977e21742c26ec4c6746f7f37220bf109f3b0 [file] [log] [blame]
Randall Spangler02e11b32014-11-19 12:48:36 -08001/* 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
8#include <unistd.h>
9
10#include "2sysincludes.h"
11#include "2common.h"
Randall Spangler108d9912014-12-02 15:55:56 -080012#include "vb2_common.h"
Randall Spangler02e11b32014-11-19 12:48:36 -080013#include "host_common.h"
14#include "host_misc.h"
15
16#include "test_common.h"
17
18static void misc_tests(void)
19{
20 TEST_EQ(roundup32(0), 0, "roundup32(0)");
21 TEST_EQ(roundup32(15), 16, "roundup32(15)");
22 TEST_EQ(roundup32(16), 16, "roundup32(16)");
Randall Spanglerc644a8c2014-11-21 11:04:36 -080023
24 TEST_EQ(vb2_desc_size(NULL), 0, "desc size null");
25 TEST_EQ(vb2_desc_size(""), 0, "desc size empty");
26 TEST_EQ(vb2_desc_size("foo"), 4, "desc size 'foo'");
27 TEST_EQ(vb2_desc_size("foob"), 8, "desc size 'foob'");
Randall Spangler02e11b32014-11-19 12:48:36 -080028}
29
30static void file_tests(void)
31{
32 const char *testfile = "file_tests.dat";
33 const uint8_t test_data[] = "Some test data";
34 uint8_t *read_data;
35 uint32_t read_size;
36
37 uint8_t cbuf[sizeof(struct vb2_struct_common) + 12];
38 struct vb2_struct_common *c = (struct vb2_struct_common *)cbuf;
39
40 unlink(testfile);
41
42 TEST_EQ(vb2_read_file(testfile, &read_data, &read_size),
43 VB2_ERROR_READ_FILE_OPEN, "vb2_read_file() missing");
44 TEST_EQ(vb2_write_file("no/such/dir", test_data, sizeof(test_data)),
45 VB2_ERROR_WRITE_FILE_OPEN, "vb2_write_file() open");
46
47 TEST_SUCC(vb2_write_file(testfile, test_data, sizeof(test_data)),
48 "vb2_write_file() good");
49 TEST_SUCC(vb2_read_file(testfile, &read_data, &read_size),
50 "vb2_read_file() good");
51 TEST_EQ(read_size, sizeof(test_data), " data size");
52 TEST_EQ(memcmp(read_data, test_data, read_size), 0, " data");
53 free(read_data);
54 unlink(testfile);
55
56 memset(cbuf, 0, sizeof(cbuf));
57 c->fixed_size = sizeof(*c);
58 c->total_size = sizeof(cbuf);
59 c->magic = 0x1234;
60 cbuf[sizeof(cbuf) - 1] = 0xed; /* Some non-zero data at the end */
61 TEST_SUCC(vb2_write_object(testfile, c), "vb2_write_object() good");
62 TEST_SUCC(vb2_read_file(testfile, &read_data, &read_size),
63 "vb2_read_file() object");
64 TEST_EQ(read_size, c->total_size, " data size");
65 /* Compare the entire buffer, including the non-zero data at the end */
66 TEST_EQ(memcmp(read_data, c, read_size), 0, " data");
67 free(read_data);
68 unlink(testfile);
69}
70
71int main(int argc, char* argv[])
72{
73 misc_tests();
74 file_tests();
75
76 return gTestSuccess ? 0 : 255;
77}