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 8 #include <unistd.h> 9 10 #include "2sysincludes.h" 11 #include "2common.h" 12 #include "vb2_common.h" 13 #include "host_common.h" 14 #include "host_misc.h" 15 16 #include "test_common.h" 17 18 static 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)"); 23 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'"); 28 } 29 30 static 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 71 int main(int argc, char* argv[]) 72 { 73 misc_tests(); 74 file_tests(); 75 76 return gTestSuccess ? 0 : 255; 77 } 78