Add test framework for bmpblk_utility.

BUG=chromium-os:11742
TEST=manual

Check out sources, run:

  cd src/platform/vboot_reference
  make
  make runbmptests

It should pass.

Change-Id: I50ebdef26662e7446828315a3f5e2786624508b9

Review URL: http://codereview.chromium.org/6246150
diff --git a/tests/Makefile b/tests/Makefile
index 5c0e2e2..17da312 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -94,7 +94,13 @@
 runfuzztests:
 	./gen_fuzz_test_cases.sh
 
-runtests: genkeys runcgpttests runcryptotests runmisctests runfuzztests
+# Run bmpblk_utility tests
+runbmptests:
+	$(MAKE) BUILD=$(shell readlink -f ${BUILD}) -C bitmaps runtests
+
+ALLTESTS=runcgpttests runcryptotests runmisctests runfuzztests runbmptests
+
+runtests: genkeys ${ALLTESTS}
 
 # TODO: tests to run when ported to new API
 #	./run_image_verification_tests.sh
diff --git a/tests/bitmaps/Makefile b/tests/bitmaps/Makefile
new file mode 100644
index 0000000..0d200ad
--- /dev/null
+++ b/tests/bitmaps/Makefile
@@ -0,0 +1,8 @@
+# Copyright (c) 2011 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.
+
+export BMPBLK=${BUILD}/utility/bmpblk_utility
+
+runtests:
+	./TestBmpBlock.py -v
diff --git a/tests/bitmaps/NotReallyA.bmp b/tests/bitmaps/NotReallyA.bmp
new file mode 100644
index 0000000..1662526
--- /dev/null
+++ b/tests/bitmaps/NotReallyA.bmp
Binary files differ
diff --git a/tests/bitmaps/TestBmpBlock.py b/tests/bitmaps/TestBmpBlock.py
new file mode 100755
index 0000000..9abe72d
--- /dev/null
+++ b/tests/bitmaps/TestBmpBlock.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python -tt
+
+"""Unit tests for bmpblk_utility.
+"""
+
+import os
+import sys
+import subprocess
+import unittest
+
+def runprog(*args):
+  """Runs specified program and args, returns (exitcode, stdout, stderr)."""
+  p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+  out, err = p.communicate()
+  return (p.returncode, out, err)
+
+
+class TestBmpBlock(unittest.TestCase):
+
+  def testNoArgs(self):
+    """Running with no args should print usage and fail."""
+    rc, out, err = runprog(prog)
+    self.assertNotEqual(0, rc)
+    self.assertTrue(out.count("Usage:"))
+
+  def testMissingBmp(self):
+    """Missing a bmp specified in the yaml is an error."""
+    rc, out, err = runprog(prog, '-c', '-C', 'case_nobmp.yaml', 'FOO')
+    self.assertNotEqual(0, rc)
+    self.assertTrue(err.count("No such file or directory"))
+
+  def testInvalidBmp(self):
+    """A .bmp file that isn't really a BMP should fail."""
+    rc, out, err = runprog(prog, '-c', '-C', 'case_badbmp.yaml', 'FOO')
+    self.assertNotEqual(0, rc)
+    self.assertTrue(err.count("Unsupported image format"))
+
+
+# Run these tests
+if __name__ == '__main__':
+  varname = 'BMPBLK'
+  if varname not in os.environ:
+    print('You must specify the path to bmpblk_utility in the $%s '
+          'environment variable.' % varname)
+    sys.exit(1)
+  prog = os.environ[varname]
+  print "Testing prog...", prog
+  unittest.main()
+
diff --git a/tests/bitmaps/case_badbmp.yaml b/tests/bitmaps/case_badbmp.yaml
new file mode 100644
index 0000000..1222aaf
--- /dev/null
+++ b/tests/bitmaps/case_badbmp.yaml
@@ -0,0 +1,19 @@
+# Invalid specification: requests missing image file.
+
+bmpblock: 1.0
+
+# These are the individual images which are layered to produce a screen layout.
+images:
+    devmode:      NotReallyA.bmp
+
+# These are all of the distinct layouts that we know how to display.
+screens:
+  dev_en:
+      - [0, 0, devmode]
+
+
+# To customize the images for a particular locale, just reorder this list so
+# that the default language appears first.
+localizations:
+  - [ dev_en, dev_en, dev_en, dev_en ]
+
diff --git a/tests/bitmaps/case_nobmp.yaml b/tests/bitmaps/case_nobmp.yaml
new file mode 100644
index 0000000..f88b9d5
--- /dev/null
+++ b/tests/bitmaps/case_nobmp.yaml
@@ -0,0 +1,19 @@
+# Invalid specification: requests missing image file.
+
+bmpblock: 1.0
+
+# These are the individual images which are layered to produce a screen layout.
+images:
+    devmode:      NoSuchFile.bmp
+
+# These are all of the distinct layouts that we know how to display.
+screens:
+  dev_en:
+      - [0, 0, devmode]
+
+
+# To customize the images for a particular locale, just reorder this list so
+# that the default language appears first.
+localizations:
+  - [ dev_en, dev_en, dev_en, dev_en ]
+