summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2014-11-19 12:48:36 -0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-11-27 05:22:32 +0000
commit02e11b323b819140590d99b6af440d36c12d161b (patch)
tree005edf6e64093741553e8520028ef0ad26a978b8 /tests
parent8577b5360ca4c9514d9091ed9aded2bb3193f1f0 (diff)
downloadvboot-02e11b323b819140590d99b6af440d36c12d161b.tar.gz
vboot2: Add host library functions to read/write files and objects
And unit tests for them. Move roundup32() into hostlib. Fix WriteFile() returning success even if it failed to write to the file. BUG=chromium:423882 BRANCH=none TEST=VBOOT2=1 make runtests Change-Id: I8a115335c088dc5c66c88423d1ccbda7eaca1996 Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/230844
Diffstat (limited to 'tests')
-rw-r--r--tests/vb2_convert_structs.c2
-rw-r--r--tests/vb2_convert_structs.h8
-rw-r--r--tests/vb2_host_misc_tests.c71
3 files changed, 73 insertions, 8 deletions
diff --git a/tests/vb2_convert_structs.c b/tests/vb2_convert_structs.c
index 0c4a7ec3..d30b337f 100644
--- a/tests/vb2_convert_structs.c
+++ b/tests/vb2_convert_structs.c
@@ -8,6 +8,8 @@
#include "2sysincludes.h"
#include "2common.h"
#include "2rsa.h"
+#include "host_common.h"
+#include "host_misc.h"
#include "vb2_convert_structs.h"
#include "vboot_struct.h" /* For old struct sizes */
diff --git a/tests/vb2_convert_structs.h b/tests/vb2_convert_structs.h
index 032b5bfb..0ba1ca3a 100644
--- a/tests/vb2_convert_structs.h
+++ b/tests/vb2_convert_structs.h
@@ -10,14 +10,6 @@
#include "2struct.h"
/**
- * Round up a size to a multiple of 32 bits (4 bytes).
- */
-static __inline const uint32_t roundup32(uint32_t v)
-{
- return (v + 3) & ~3;
-}
-
-/**
* Convert a packed key from vboot data format to vboot2 data format.
*
* Intended for use by unit tests. Does NOT validate the original struct
diff --git a/tests/vb2_host_misc_tests.c b/tests/vb2_host_misc_tests.c
new file mode 100644
index 00000000..e8c2a588
--- /dev/null
+++ b/tests/vb2_host_misc_tests.c
@@ -0,0 +1,71 @@
+/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Tests for host misc library vboot2 functions
+ */
+
+#include <unistd.h>
+
+#include "2sysincludes.h"
+#include "2common.h"
+#include "host_common.h"
+#include "host_misc.h"
+
+#include "test_common.h"
+
+static void misc_tests(void)
+{
+ TEST_EQ(roundup32(0), 0, "roundup32(0)");
+ TEST_EQ(roundup32(15), 16, "roundup32(15)");
+ TEST_EQ(roundup32(16), 16, "roundup32(16)");
+}
+
+static void file_tests(void)
+{
+ const char *testfile = "file_tests.dat";
+ const uint8_t test_data[] = "Some test data";
+ uint8_t *read_data;
+ uint32_t read_size;
+
+ uint8_t cbuf[sizeof(struct vb2_struct_common) + 12];
+ struct vb2_struct_common *c = (struct vb2_struct_common *)cbuf;
+
+ unlink(testfile);
+
+ TEST_EQ(vb2_read_file(testfile, &read_data, &read_size),
+ VB2_ERROR_READ_FILE_OPEN, "vb2_read_file() missing");
+ TEST_EQ(vb2_write_file("no/such/dir", test_data, sizeof(test_data)),
+ VB2_ERROR_WRITE_FILE_OPEN, "vb2_write_file() open");
+
+ TEST_SUCC(vb2_write_file(testfile, test_data, sizeof(test_data)),
+ "vb2_write_file() good");
+ TEST_SUCC(vb2_read_file(testfile, &read_data, &read_size),
+ "vb2_read_file() good");
+ TEST_EQ(read_size, sizeof(test_data), " data size");
+ TEST_EQ(memcmp(read_data, test_data, read_size), 0, " data");
+ free(read_data);
+ unlink(testfile);
+
+ memset(cbuf, 0, sizeof(cbuf));
+ c->fixed_size = sizeof(*c);
+ c->total_size = sizeof(cbuf);
+ c->magic = 0x1234;
+ cbuf[sizeof(cbuf) - 1] = 0xed; /* Some non-zero data at the end */
+ TEST_SUCC(vb2_write_object(testfile, c), "vb2_write_object() good");
+ TEST_SUCC(vb2_read_file(testfile, &read_data, &read_size),
+ "vb2_read_file() object");
+ TEST_EQ(read_size, c->total_size, " data size");
+ /* Compare the entire buffer, including the non-zero data at the end */
+ TEST_EQ(memcmp(read_data, c, read_size), 0, " data");
+ free(read_data);
+ unlink(testfile);
+}
+
+int main(int argc, char* argv[])
+{
+ misc_tests();
+ file_tests();
+
+ return gTestSuccess ? 0 : 255;
+}