test: Add tests for futility show and vbutil_*

Currently, the unit tests verify a 0 exit code, but don't check the
output.  Add tests to check the output.

BUG=chromium:617247
BRANCH=none
TEST=make runtests

Change-Id: I842046bb6f788eee05a1f2f511c6fb08df4a5379
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/349670
Reviewed-by: Daisuke Nojiri <dnojiri@google.com>
diff --git a/tests/futility/expect_output/show.tests_devkeys_kernel.keyblock b/tests/futility/expect_output/show.tests_devkeys_kernel.keyblock
new file mode 100644
index 0000000..16d56a7
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_devkeys_kernel.keyblock
@@ -0,0 +1,7 @@
+Key block:               /mnt/host/source/src/platform/vboot_reference/tests/devkeys/kernel.keyblock
+  Signature:             ignored
+  Size:                  0x4b8
+  Flags:                 7  !DEV DEV !REC
+  Data key algorithm:    4 RSA2048 SHA256
+  Data key version:      1
+  Data key sha1sum:      d6170aa480136f1f29cf339a5ab1b960585fa444
diff --git a/tests/futility/expect_output/show.tests_devkeys_root_key.vbprivk b/tests/futility/expect_output/show.tests_devkeys_root_key.vbprivk
new file mode 100644
index 0000000..40760ec
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_devkeys_root_key.vbprivk
@@ -0,0 +1,4 @@
+Private Key file:      /mnt/host/source/src/platform/vboot_reference/tests/devkeys/root_key.vbprivk
+  Vboot API:           1.0
+  Algorithm:           11 RSA8192 SHA512
+  Key sha1sum:         b11d74edd286c144e1135b49e7f0bc20cf041f10
diff --git a/tests/futility/expect_output/show.tests_devkeys_root_key.vbpubk b/tests/futility/expect_output/show.tests_devkeys_root_key.vbpubk
new file mode 100644
index 0000000..d6f26fb
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_devkeys_root_key.vbpubk
@@ -0,0 +1,5 @@
+Public Key file:       /mnt/host/source/src/platform/vboot_reference/tests/devkeys/root_key.vbpubk
+  Vboot API:           1.0
+  Algorithm:           11 RSA8192 SHA512
+  Key Version:         1
+  Key sha1sum:         b11d74edd286c144e1135b49e7f0bc20cf041f10
diff --git a/tests/futility/expect_output/show.tests_futility_data_bios_mario_mp.bin b/tests/futility/expect_output/show.tests_futility_data_bios_mario_mp.bin
new file mode 100644
index 0000000..6241d7a
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_futility_data_bios_mario_mp.bin
@@ -0,0 +1,64 @@
+BIOS:                    /mnt/host/source/src/platform/vboot_reference/tests/futility/data/bios_mario_mp.bin
+GBB header:              GBB Area
+  Version:               1.0
+  Flags:                 0x00000000
+  Regions:                 offset       size
+    hwid                 0x00000080   0x00000100
+    bmpvf                0x00001180   0x0003de80
+    rootkey              0x00000180   0x00001000
+    recovery_key         0x0003f000   0x00001000
+  Size:                  0x00040000 / 0x00040000
+GBB content:
+  HWID:                  {9D799111-A88A-439E-9E1F-FBBB41B00A9A}
+     digest:             <none>
+  Root Key:
+    Vboot API:           1.0
+    Algorithm:           11 RSA8192 SHA512
+    Key Version:         1
+    Key sha1sum:         541f467a7d8747f55ae9087ee4e34155f5ee3cd7
+  Recovery Key:
+    Vboot API:           1.0
+    Algorithm:           11 RSA8192 SHA512
+    Key Version:         1
+    Key sha1sum:         5d0d163b824cab5ae4f23fb2cc012e2a4124f4fe
+  BmpBlock:              <invalid>
+Firmware body:           Firmware A Data
+  Offset:                0x00040000
+  Size:                  0x000d0000
+Firmware body:           Firmware B Data
+  Offset:                0x00120000
+  Size:                  0x000d0000
+Key block:               Firmware A Key
+  Signature:             valid
+  Size:                  0x8b8
+  Flags:                 7  !DEV DEV !REC
+  Data key algorithm:    8 RSA4096 SHA512
+  Data key version:      1
+  Data key sha1sum:      cd72cfb929765f82baba0a01ed937a73f502f6c5
+Firmware Preamble:
+  Size:                  2164
+  Header version:        2.1
+  Firmware version:      1
+  Kernel key algorithm:  8 RSA4096 SHA512
+  Kernel key version:    1
+  Kernel key sha1sum:    e39e019cb5df3e874f443721540cb261a88c58ef
+  Firmware body size:    851968
+  Preamble flags:        0
+Body verification succeeded.
+Key block:               Firmware B Key
+  Signature:             valid
+  Size:                  0x8b8
+  Flags:                 7  !DEV DEV !REC
+  Data key algorithm:    8 RSA4096 SHA512
+  Data key version:      1
+  Data key sha1sum:      cd72cfb929765f82baba0a01ed937a73f502f6c5
+Firmware Preamble:
+  Size:                  2164
+  Header version:        2.1
+  Firmware version:      1
+  Kernel key algorithm:  8 RSA4096 SHA512
+  Kernel key version:    1
+  Kernel key sha1sum:    e39e019cb5df3e874f443721540cb261a88c58ef
+  Firmware body size:    851968
+  Preamble flags:        0
+Body verification succeeded.
diff --git a/tests/futility/expect_output/show.tests_futility_data_bios_zgb_mp.bin b/tests/futility/expect_output/show.tests_futility_data_bios_zgb_mp.bin
new file mode 100644
index 0000000..2110685
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_futility_data_bios_zgb_mp.bin
@@ -0,0 +1,64 @@
+BIOS:                    /mnt/host/source/src/platform/vboot_reference/tests/futility/data/bios_zgb_mp.bin
+GBB header:              GBB
+  Version:               1.0
+  Flags:                 0x00000000
+  Regions:                 offset       size
+    hwid                 0x00000080   0x00000100
+    bmpvf                0x00001180   0x0003de80
+    rootkey              0x00000180   0x00001000
+    recovery_key         0x0003f000   0x00001000
+  Size:                  0x00040000 / 0x00040000
+GBB content:
+  HWID:                  {FA42644C-CF3A-4692-A9D3-1A667CB232E9}
+     digest:             <none>
+  Root Key:
+    Vboot API:           1.0
+    Algorithm:           11 RSA8192 SHA512
+    Key Version:         1
+    Key sha1sum:         9f59876c7f7dc881f02d934786c6b7c2c17dcaac
+  Recovery Key:
+    Vboot API:           1.0
+    Algorithm:           11 RSA8192 SHA512
+    Key Version:         1
+    Key sha1sum:         9bd99a594c45b6739899a17ec29ac2289ee75463
+  BmpBlock:              <invalid>
+Firmware body:           FW_MAIN_A
+  Offset:                0x00030000
+  Size:                  0x000dffc0
+Firmware body:           FW_MAIN_B
+  Offset:                0x00120000
+  Size:                  0x000dffc0
+Key block:               VBLOCK_A
+  Signature:             valid
+  Size:                  0x8b8
+  Flags:                 6  DEV !REC
+  Data key algorithm:    8 RSA4096 SHA512
+  Data key version:      1
+  Data key sha1sum:      a78aaa1691c2125ef8ccefa1a8a6bea92d38fae6
+Firmware Preamble:
+  Size:                  2164
+  Header version:        2.1
+  Firmware version:      2
+  Kernel key algorithm:  7 RSA4096 SHA256
+  Kernel key version:    2
+  Kernel key sha1sum:    0c9fd5b03ab47d37924ba8a7beb64039d84ed0e1
+  Firmware body size:    917440
+  Preamble flags:        0
+Body verification succeeded.
+Key block:               VBLOCK_B
+  Signature:             valid
+  Size:                  0x8b8
+  Flags:                 7  !DEV DEV !REC
+  Data key algorithm:    8 RSA4096 SHA512
+  Data key version:      1
+  Data key sha1sum:      4fe08ed739069d6834b68612eb707998a0825f34
+Firmware Preamble:
+  Size:                  2164
+  Header version:        2.1
+  Firmware version:      2
+  Kernel key algorithm:  7 RSA4096 SHA256
+  Kernel key version:    2
+  Kernel key sha1sum:    0c9fd5b03ab47d37924ba8a7beb64039d84ed0e1
+  Firmware body size:    917440
+  Preamble flags:        0
+Body verification succeeded.
diff --git a/tests/futility/expect_output/show.tests_futility_data_fw_gbb.bin b/tests/futility/expect_output/show.tests_futility_data_fw_gbb.bin
new file mode 100644
index 0000000..74ebc98
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_futility_data_fw_gbb.bin
@@ -0,0 +1,27 @@
+GBB header:              /mnt/host/source/src/platform/vboot_reference/tests/futility/data/fw_gbb.bin
+  Version:               1.1
+  Flags:                 0x00000039
+  Regions:                 offset       size
+    hwid                 0x00000080   0x00000100
+    bmpvf                0x00001180   0x000ece80
+    rootkey              0x00000180   0x00001000
+    recovery_key         0x000ee000   0x00001000
+  Size:                  0x000ef000 / 0x000ef000
+GBB content:
+  HWID:                  X86 PEPPY TEST 4211
+     digest:             <none>
+  Root Key:
+    Vboot API:           1.0
+    Algorithm:           11 RSA8192 SHA512
+    Key Version:         1
+    Key sha1sum:         fc68bcb88bf9af1907289a9f377d658b3b9fe5b0
+  Recovery Key:
+    Vboot API:           1.0
+    Algorithm:           11 RSA8192 SHA512
+    Key Version:         1
+    Key sha1sum:         bf39d0d3e30cbf6a121416d04df4603ad5310779
+  BmpBlock:
+    Version:             2.0
+    Localizations:       18
+    Screen layouts:      10
+    Image infos:         269
diff --git a/tests/futility/expect_output/show.tests_futility_data_fw_vblock.bin b/tests/futility/expect_output/show.tests_futility_data_fw_vblock.bin
new file mode 100644
index 0000000..67ee12f
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_futility_data_fw_vblock.bin
@@ -0,0 +1,17 @@
+Key block:               /mnt/host/source/src/platform/vboot_reference/tests/futility/data/fw_vblock.bin
+  Signature:             ignored
+  Size:                  0x8b8
+  Flags:                 7  !DEV DEV !REC
+  Data key algorithm:    8 RSA4096 SHA512
+  Data key version:      1
+  Data key sha1sum:      f917ad29e36aa8a286f978c1aa0550ea31c6a561
+Firmware Preamble:
+  Size:                  2164
+  Header version:        2.1
+  Firmware version:      2
+  Kernel key algorithm:  7 RSA4096 SHA256
+  Kernel key version:    2
+  Kernel key sha1sum:    cc05423373b76acbec23ec45dfa3696a2ea6dc0f
+  Firmware body size:    146456
+  Preamble flags:        0
+No firmware body available to verify.
diff --git a/tests/futility/expect_output/show.tests_futility_data_kern_preamble.bin b/tests/futility/expect_output/show.tests_futility_data_kern_preamble.bin
new file mode 100644
index 0000000..c648683
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_futility_data_kern_preamble.bin
@@ -0,0 +1,20 @@
+Kernel partition:        /mnt/host/source/src/platform/vboot_reference/tests/futility/data/kern_preamble.bin
+Key block:
+  Signature:             ignored
+  Size:                  0x5b8
+  Flags:                 15  !DEV DEV !REC REC
+  Data key algorithm:    8 RSA4096 SHA512
+  Data key version:      1
+  Data key sha1sum:      50b28df3cebabeefd134b7bc96512207f57b20a5
+Kernel Preamble:
+  Size:                  0xfa48
+  Header version:        2.0
+  Kernel version:        1
+  Body load address:     0x100000
+  Body size:             0x12000
+  Bootloader address:    0x10a000
+  Bootloader size:       0x8000
+  Flags:                 0x0
+Body verification succeeded.
+Config:
+hi there 
diff --git a/tests/futility/expect_output/show.tests_futility_data_sample.vbprik2 b/tests/futility/expect_output/show.tests_futility_data_sample.vbprik2
new file mode 100644
index 0000000..059972c
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_futility_data_sample.vbprik2
@@ -0,0 +1,6 @@
+Private key file:      /mnt/host/source/src/platform/vboot_reference/tests/futility/data/sample.vbprik2
+  Vboot API:           2.1
+  Desc:                "sample vb21 keypair"
+  Signature Algorithm: 5 RSA8192
+  Hash Algorithm:      3 SHA512
+  ID:                  f1afa44a1aed0d0e9ff630579df920a725e9de5e
diff --git a/tests/futility/expect_output/show.tests_futility_data_sample.vbpubk2 b/tests/futility/expect_output/show.tests_futility_data_sample.vbpubk2
new file mode 100644
index 0000000..cd5654f
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_futility_data_sample.vbpubk2
@@ -0,0 +1,7 @@
+Public Key file:       /mnt/host/source/src/platform/vboot_reference/tests/futility/data/sample.vbpubk2
+  Vboot API:           2.1
+  Desc:                "sample vb21 keypair"
+  Signature Algorithm: 5 RSA8192
+  Hash Algorithm:      3 SHA512
+  Version:             0x00000001
+  ID:                  f1afa44a1aed0d0e9ff630579df920a725e9de5e
diff --git a/tests/futility/expect_output/show.tests_testkeys_key_rsa2048.pem b/tests/futility/expect_output/show.tests_testkeys_key_rsa2048.pem
new file mode 100644
index 0000000..f306996
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_testkeys_key_rsa2048.pem
@@ -0,0 +1,3 @@
+Private Key file:      /mnt/host/source/src/platform/vboot_reference/tests/testkeys/key_rsa2048.pem
+  Key length:          2048
+  Key sha1sum:         bfb2fa9188a87bf766dd7c313ea6802553b646b6
diff --git a/tests/futility/expect_output/show.tests_testkeys_key_rsa8192.pub.pem b/tests/futility/expect_output/show.tests_testkeys_key_rsa8192.pub.pem
new file mode 100644
index 0000000..8b4993a
--- /dev/null
+++ b/tests/futility/expect_output/show.tests_testkeys_key_rsa8192.pub.pem
@@ -0,0 +1,3 @@
+Public Key file:      /mnt/host/source/src/platform/vboot_reference/tests/testkeys/key_rsa8192.pub.pem
+  Key length:          8192
+  Key sha1sum:         f1afa44a1aed0d0e9ff630579df920a725e9de5e
diff --git a/tests/futility/expect_output/vbutil_firmware.tests_futility_data_fw.vblock b/tests/futility/expect_output/vbutil_firmware.tests_futility_data_fw.vblock
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/futility/expect_output/vbutil_firmware.tests_futility_data_fw.vblock
diff --git a/tests/futility/expect_output/vbutil_firmware.tests_futility_data_fw_vblock.bin b/tests/futility/expect_output/vbutil_firmware.tests_futility_data_fw_vblock.bin
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/futility/expect_output/vbutil_firmware.tests_futility_data_fw_vblock.bin
diff --git a/tests/futility/expect_output/vbutil_firmware.verify b/tests/futility/expect_output/vbutil_firmware.verify
new file mode 100644
index 0000000..87b35a6
--- /dev/null
+++ b/tests/futility/expect_output/vbutil_firmware.verify
@@ -0,0 +1,16 @@
+Key block:
+  Size:                2232
+  Flags:               7 (ignored)
+  Data key algorithm:  7 RSA4096 SHA256
+  Data key version:    1
+  Data key sha1sum:    e2c1c92d7d7aa7dfed5e8375edd30b7ae52b7450
+Preamble:
+  Size:                  2164
+  Header version:        2.1
+  Firmware version:      12
+  Kernel key algorithm:  7 RSA4096 SHA256
+  Kernel key version:    1
+  Kernel key sha1sum:    5d2b220899c4403d564092ada3f12d3cc4483223
+  Firmware body size:    16384
+  Preamble flags:        42
+Body verification succeeded.
diff --git a/tests/futility/expect_output/vbutil_key.tests_devkeys_root_key.vbprivk b/tests/futility/expect_output/vbutil_key.tests_devkeys_root_key.vbprivk
new file mode 100644
index 0000000..fa020be
--- /dev/null
+++ b/tests/futility/expect_output/vbutil_key.tests_devkeys_root_key.vbprivk
@@ -0,0 +1,2 @@
+Private Key file:  /mnt/host/source/src/platform/vboot_reference/tests/devkeys/root_key.vbprivk
+Algorithm:         11 RSA8192 SHA512
diff --git a/tests/futility/expect_output/vbutil_key.tests_devkeys_root_key.vbpubk b/tests/futility/expect_output/vbutil_key.tests_devkeys_root_key.vbpubk
new file mode 100644
index 0000000..b0cd54a
--- /dev/null
+++ b/tests/futility/expect_output/vbutil_key.tests_devkeys_root_key.vbpubk
@@ -0,0 +1,4 @@
+Public Key file:   /mnt/host/source/src/platform/vboot_reference/tests/devkeys/root_key.vbpubk
+Algorithm:         11 RSA8192 SHA512
+Key Version:       1
+Key sha1sum:       b11d74edd286c144e1135b49e7f0bc20cf041f10
diff --git a/tests/futility/expect_output/vbutil_keyblock.tests_devkeys_kernel.keyblock b/tests/futility/expect_output/vbutil_keyblock.tests_devkeys_kernel.keyblock
new file mode 100644
index 0000000..0dfa973
--- /dev/null
+++ b/tests/futility/expect_output/vbutil_keyblock.tests_devkeys_kernel.keyblock
@@ -0,0 +1,6 @@
+Key block file:       /mnt/host/source/src/platform/vboot_reference/tests/devkeys/kernel.keyblock
+Signature             valid
+Flags:                7  !DEV DEV !REC
+Data key algorithm:   4 RSA2048 SHA256
+Data key version:     1
+Data key sha1sum:     d6170aa480136f1f29cf339a5ab1b960585fa444
diff --git a/tests/futility/run_test_scripts.sh b/tests/futility/run_test_scripts.sh
index a0d9e47..d55a44d 100755
--- a/tests/futility/run_test_scripts.sh
+++ b/tests/futility/run_test_scripts.sh
@@ -45,6 +45,7 @@
 ${SCRIPTDIR}/test_gbb_utility.sh
 ${SCRIPTDIR}/test_load_fmap.sh
 ${SCRIPTDIR}/test_main.sh
+${SCRIPTDIR}/test_show_contents.sh
 ${SCRIPTDIR}/test_show_kernel.sh
 ${SCRIPTDIR}/test_show_vs_verify.sh
 ${SCRIPTDIR}/test_show_usbpd1.sh
diff --git a/tests/futility/test_show_contents.sh b/tests/futility/test_show_contents.sh
new file mode 100755
index 0000000..f24c434
--- /dev/null
+++ b/tests/futility/test_show_contents.sh
@@ -0,0 +1,106 @@
+#!/bin/bash -eux
+# 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.
+
+me=${0##*/}
+TMP="$me.tmp"
+
+# Work in scratch directory
+cd "$OUTDIR"
+
+# Test 'futility show' against expected output
+SHOW_FILES="
+  tests/devkeys/root_key.vbpubk
+  tests/devkeys/root_key.vbprivk
+  tests/devkeys/kernel.keyblock
+  tests/futility/data/fw_vblock.bin
+  tests/futility/data/fw_gbb.bin
+  tests/futility/data/bios_zgb_mp.bin
+  tests/futility/data/bios_mario_mp.bin
+  tests/futility/data/kern_preamble.bin
+  tests/futility/data/sample.vbpubk2
+  tests/futility/data/sample.vbprik2
+  tests/testkeys/key_rsa2048.pem
+  tests/testkeys/key_rsa8192.pub.pem
+"
+
+for file in $SHOW_FILES; do
+    outfile="show.${file//\//_}"
+    gotfile="${OUTDIR}/${outfile}"
+    wantfile="${SRCDIR}/tests/futility/expect_output/${outfile}"
+    ${FUTILITY} show "${SRCDIR}/${file}" | tee "${gotfile}"
+
+    # Uncomment this to update the expected output
+    #cp ${gotfile} ${wantfile}
+
+    diff ${wantfile} ${gotfile}
+done
+
+
+# Test 'futility vbutil_key' against expected output
+VBUTIL_KEY_FILES="
+  tests/devkeys/root_key.vbpubk
+  tests/devkeys/root_key.vbprivk
+"
+
+for file in $VBUTIL_KEY_FILES; do
+    outfile="vbutil_key.${file//\//_}"
+    gotfile="${OUTDIR}/${outfile}"
+    wantfile="${SRCDIR}/tests/futility/expect_output/${outfile}"
+    ${FUTILITY} vbutil_key --unpack "${SRCDIR}/${file}" | tee "${gotfile}"
+
+    # Uncomment this to update the expected output
+    #cp ${gotfile} ${wantfile}
+
+    diff ${wantfile} ${gotfile}
+done
+
+
+# Test 'futility vbutil_keyblock' against expected output
+file="tests/devkeys/kernel.keyblock"
+outfile="vbutil_keyblock.${file//\//_}"
+gotfile="${OUTDIR}/${outfile}"
+wantfile="${SRCDIR}/tests/futility/expect_output/${outfile}"
+${FUTILITY} vbutil_keyblock --unpack "${SRCDIR}/${file}" \
+    --signpubkey "${SRCDIR}/tests/devkeys/kernel_subkey.vbpubk" \
+    | tee "${gotfile}"
+
+# Uncomment this to update the expected output
+#cp ${gotfile} ${wantfile}
+
+diff ${wantfile} ${gotfile}
+
+
+# Test 'futility vbutil_firmware' against expected output
+KEYDIR=${SRCDIR}/tests/devkeys
+outfile="vbutil_firmware.verify"
+gotfile="${OUTDIR}/${outfile}"
+wantfile="${SRCDIR}/tests/futility/expect_output/${outfile}"
+
+# Create a firmware blob and vblock.  Version and flags are just
+# arbitrary non-zero numbers so we can verify they're printed
+# properly.
+dd bs=1024 count=16 if=/dev/urandom of=${TMP}.fw_main
+${FUTILITY} vbutil_firmware --vblock ${TMP}.vblock.old \
+  --keyblock ${KEYDIR}/firmware.keyblock \
+  --signprivate ${KEYDIR}/firmware_data_key.vbprivk \
+  --version 12 \
+  --fv ${TMP}.fw_main \
+  --kernelkey ${KEYDIR}/kernel_subkey.vbpubk \
+  --flags 42
+
+# Verify
+${FUTILITY} vbutil_firmware --verify ${TMP}.vblock.old \
+  --signpubkey ${KEYDIR}/root_key.vbpubk \
+  --fv ${TMP}.fw_main | tee "${gotfile}"
+
+# Uncomment this to update the expected output
+#cp ${gotfile} ${wantfile}
+
+diff ${wantfile} ${gotfile}
+
+
+# cleanup
+rm -rf ${TMP}*
+exit 0