From 271c071344beaacc45201c17406bcf3b4daece88 Mon Sep 17 00:00:00 2001 From: Bill Richardson Date: Thu, 5 Mar 2015 16:51:39 -0800 Subject: futility: add tests for futil_file_type() This adds a test to be sure we can identify the types of input files that futility can handle. BUG=none BRANCH=none TEST=make runtests Change-Id: Iff1dcc05530af2969a82d7c32599850bba59597a Signed-off-by: Bill Richardson Reviewed-on: https://chromium-review.googlesource.com/258501 --- tests/futility/data/fw_gbb.bin | Bin 0 -> 978944 bytes tests/futility/data/fw_vblock.bin | Bin 0 -> 65536 bytes tests/futility/data/kern_preamble.bin | Bin 0 -> 139264 bytes tests/futility/data/random_noise.bin | Bin 0 -> 4096 bytes tests/futility/data/sample.vbprik2 | Bin 0 -> 4724 bytes tests/futility/data/sample.vbpubk2 | Bin 0 -> 2132 bytes tests/futility/test_file_types.c | 95 ++++++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+) create mode 100644 tests/futility/data/fw_gbb.bin create mode 100644 tests/futility/data/fw_vblock.bin create mode 100644 tests/futility/data/kern_preamble.bin create mode 100644 tests/futility/data/random_noise.bin create mode 100644 tests/futility/data/sample.vbprik2 create mode 100644 tests/futility/data/sample.vbpubk2 create mode 100644 tests/futility/test_file_types.c (limited to 'tests/futility') diff --git a/tests/futility/data/fw_gbb.bin b/tests/futility/data/fw_gbb.bin new file mode 100644 index 00000000..e717a00c Binary files /dev/null and b/tests/futility/data/fw_gbb.bin differ diff --git a/tests/futility/data/fw_vblock.bin b/tests/futility/data/fw_vblock.bin new file mode 100644 index 00000000..ec2769dd Binary files /dev/null and b/tests/futility/data/fw_vblock.bin differ diff --git a/tests/futility/data/kern_preamble.bin b/tests/futility/data/kern_preamble.bin new file mode 100644 index 00000000..6d34ee56 Binary files /dev/null and b/tests/futility/data/kern_preamble.bin differ diff --git a/tests/futility/data/random_noise.bin b/tests/futility/data/random_noise.bin new file mode 100644 index 00000000..1e2d76ee Binary files /dev/null and b/tests/futility/data/random_noise.bin differ diff --git a/tests/futility/data/sample.vbprik2 b/tests/futility/data/sample.vbprik2 new file mode 100644 index 00000000..34f12e41 Binary files /dev/null and b/tests/futility/data/sample.vbprik2 differ diff --git a/tests/futility/data/sample.vbpubk2 b/tests/futility/data/sample.vbpubk2 new file mode 100644 index 00000000..aac42f38 Binary files /dev/null and b/tests/futility/data/sample.vbpubk2 differ diff --git a/tests/futility/test_file_types.c b/tests/futility/test_file_types.c new file mode 100644 index 00000000..25beb479 --- /dev/null +++ b/tests/futility/test_file_types.c @@ -0,0 +1,95 @@ +/* + * Copyright 2015 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. + */ +#include +#include +#include +#include + +#include "file_type.h" +#include "futility.h" +#include "test_common.h" + +/* + * Files that exemplify each type. + * Paths are relative to the source directory. + * A missing path means we don't (yet?) know how to identify it reliably. + */ +static struct { + enum futil_file_type type; + const char * const file; +} test_case[] = { + {FILE_TYPE_UNKNOWN, "tests/futility/data/random_noise.bin"}, + {FILE_TYPE_PUBKEY, "tests/devkeys/root_key.vbpubk"}, + {FILE_TYPE_KEYBLOCK, "tests/devkeys/kernel.keyblock"}, + {FILE_TYPE_FW_PREAMBLE, "tests/futility/data/fw_vblock.bin"}, + {FILE_TYPE_GBB, "tests/futility/data/fw_gbb.bin"}, + {FILE_TYPE_BIOS_IMAGE, "tests/futility/data/bios_zgb_mp.bin"}, + {FILE_TYPE_OLD_BIOS_IMAGE, "tests/futility/data/bios_mario_mp.bin"}, + {FILE_TYPE_KERN_PREAMBLE, "tests/futility/data/kern_preamble.bin"}, + {FILE_TYPE_RAW_FIRMWARE, }, + {FILE_TYPE_RAW_KERNEL, }, + {FILE_TYPE_CHROMIUMOS_DISK, }, + {FILE_TYPE_PRIVKEY, "tests/devkeys/root_key.vbprivk"}, + {FILE_TYPE_VB2_PUBKEY, "tests/futility/data/sample.vbpubk2"}, + {FILE_TYPE_VB2_PRIVKEY, "tests/futility/data/sample.vbprik2"}, + {FILE_TYPE_PEM, "tests/testkeys/key_rsa2048.pem"}, +}; +BUILD_ASSERT(ARRAY_SIZE(test_case) == NUM_FILE_TYPES); + +int main(int argc, char *argv[]) +{ + char filename[PATH_MAX]; + char status[80]; + char *srcdir; + enum futil_file_type type; + int i; + + /* Where's the source directory? */ + srcdir = getenv("SRCDIR"); + if (argc > 1) + srcdir = argv[1]; + if (!srcdir) + srcdir = "."; + + /* Complain about some files we can't handle */ + TEST_EQ(futil_file_type("/Sir/Not/Appearing/In/This/Film", &type), + FILE_ERR_OPEN, "Identify missing file"); + TEST_EQ(futil_file_type("/", &type), + FILE_ERR_DIR, "Identify directory"); + TEST_EQ(futil_file_type("/dev/zero", &type), + FILE_ERR_CHR, "Identify char device"); + + /* Now test things we can handle */ + for (i = 0; i < NUM_FILE_TYPES; i++) { + + if (!test_case[i].file) { + printf("%sWarning: No test for file type %d (%s)%s\n", + COL_YELLOW, test_case[i].type, + futil_file_type_str(test_case[i].type), + COL_STOP); + continue; + } + + snprintf(filename, sizeof(filename), "%s/%s", + srcdir, test_case[i].file); + + type = NUM_FILE_TYPES; + snprintf(status, sizeof(status), + "File type %d (%s): examined", + test_case[i].type, + futil_file_type_str(test_case[i].type)); + TEST_EQ(FILE_ERR_NONE, futil_file_type(filename, &type), + status); + + snprintf(status, sizeof(status), + "File type %d (%s) identified", + test_case[i].type, + futil_file_type_str(test_case[i].type)); + TEST_EQ(type, test_case[i].type, status); + } + + return !gTestSuccess; +} -- cgit v1.2.1