TestBmpBlock.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/python2 -tt
  2. #
  3. # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Unit tests for bmpblk_utility.
  7. """
  8. import os
  9. import sys
  10. import subprocess
  11. import tempfile
  12. import unittest
  13. def runprog(*args):
  14. """Runs specified program and args, returns (exitcode, stdout, stderr)."""
  15. p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  16. out, err = p.communicate()
  17. return (p.returncode, out, err)
  18. class TempDirTestCase(unittest.TestCase):
  19. """A TestCase that sets up self.tempdir with a temporary directory."""
  20. def setUp(self):
  21. self.tempdir = tempfile.mkdtemp(prefix='tmp_test_bmp_block')
  22. self.tempfile = os.path.join(self.tempdir, 'FOO')
  23. self._cwd = os.getcwd()
  24. def tearDown(self):
  25. os.chdir(self._cwd)
  26. runprog('rm', '-rf', self.tempdir)
  27. class TestFailures(TempDirTestCase):
  28. def testNoArgs(self):
  29. """Running with no args should print usage and fail."""
  30. rc, out, err = runprog(prog)
  31. self.assertNotEqual(0, rc)
  32. self.assertTrue(err.count("missing BMPBLOCK name"))
  33. self.assertTrue(out.count("bmpblk_utility"))
  34. def testMissingBmp(self):
  35. """Missing a bmp specified in the yaml is an error."""
  36. rc, out, err = runprog(prog, '-c', 'case_nobmp.yaml', self.tempfile)
  37. self.assertNotEqual(0, rc)
  38. self.assertTrue(err.count("No such file or directory"))
  39. def testInvalidBmp(self):
  40. """A .bmp file that isn't really a BMP should fail."""
  41. rc, out, err = runprog(prog, '-c', 'case_badbmp.yaml', self.tempfile)
  42. self.assertNotEqual(0, rc)
  43. self.assertTrue(err.count("Unsupported image format"))
  44. def testBadCompression(self):
  45. """Wrong compression types should fail."""
  46. rc, out, err = runprog(prog, '-z', '99', '-c', 'case_simple.yaml', self.tempfile)
  47. self.assertNotEqual(0, rc)
  48. self.assertTrue(err.count("compression type"))
  49. class TestOverWrite(TempDirTestCase):
  50. def testOverwrite(self):
  51. """Create, unpack, unpack again, with and without -f"""
  52. rc, out, err = runprog(prog, '-c', 'case_simple.yaml', self.tempfile)
  53. self.assertEqual(0, rc)
  54. rc, out, err = runprog(prog, '-x', '-d', self.tempdir, self.tempfile)
  55. self.assertEqual(0, rc)
  56. rc, out, err = runprog(prog, '-x', '-d', self.tempdir, self.tempfile)
  57. self.assertNotEqual(0, rc)
  58. self.assertTrue(err.count("File exists"))
  59. rc, out, err = runprog(prog, '-x', '-d', self.tempdir, '-f', self.tempfile)
  60. self.assertEqual(0, rc)
  61. class TestPackUnpack(TempDirTestCase):
  62. def testPackUnpack(self):
  63. """Create, unpack, recreate without compression"""
  64. foo = os.path.join(self.tempdir, 'FOO')
  65. bar = os.path.join(self.tempdir, 'BAR')
  66. rc, out, err = runprog(prog, '-c', 'case_simple.yaml', foo)
  67. self.assertEqual(0, rc)
  68. rc, out, err = runprog(prog, '-x', '-d', self.tempdir, foo)
  69. self.assertEqual(0, rc)
  70. os.chdir(self.tempdir)
  71. rc, out, err = runprog(prog, '-c', 'config.yaml', bar)
  72. self.assertEqual(0, rc)
  73. rc, out, err = runprog('/usr/bin/cmp', foo, bar)
  74. self.assertEqual(0, rc)
  75. def doPackUnpackZ(self, comp):
  76. """Create, unpack, recreate with a given compression"""
  77. foo = os.path.join(self.tempdir, 'FOO')
  78. bar = os.path.join(self.tempdir, 'BAR')
  79. rc, out, err = runprog(prog, '-z', comp, '-c', 'case_simple.yaml', foo)
  80. self.assertEqual(0, rc)
  81. rc, out, err = runprog(prog, '-x', '-d', self.tempdir, foo)
  82. self.assertEqual(0, rc)
  83. os.chdir(self.tempdir)
  84. rc, out, err = runprog(prog, '-z', comp, '-c', 'config.yaml', bar)
  85. self.assertEqual(0, rc)
  86. rc, out, err = runprog('/usr/bin/cmp', foo, bar)
  87. self.assertEqual(0, rc)
  88. def testPackUnpackZ1(self):
  89. """Create, unpack, recreate with EFIv1 compression"""
  90. self.doPackUnpackZ('1')
  91. def testPackUnpackZ2(self):
  92. """Create, unpack, recreate with LZMA compression"""
  93. self.doPackUnpackZ('2')
  94. def doPackUnpackImplicitZ(self, comp, noncomp):
  95. """Create with given compression, unpack, repack without specifying"""
  96. foo = os.path.join(self.tempdir, 'FOO')
  97. bar = os.path.join(self.tempdir, 'BAR')
  98. # create with the specified compression scheme
  99. rc, out, err = runprog(prog, '-z', comp, '-c', 'case_simple.yaml', foo)
  100. self.assertEqual(0, rc)
  101. # unpack. yaml file should have compression scheme in it
  102. rc, out, err = runprog(prog, '-f', '-x', '-d', self.tempdir, foo)
  103. self.assertEqual(0, rc)
  104. os.chdir(self.tempdir)
  105. # create with no compression specified, should use default from yaml
  106. rc, out, err = runprog(prog, '-c', 'config.yaml', bar)
  107. self.assertEqual(0, rc)
  108. # so new output should match original
  109. rc, out, err = runprog('/usr/bin/cmp', foo, bar)
  110. self.assertEqual(0, rc)
  111. # Now make sure that specifying a compression arg will override the default
  112. for mycomp in noncomp:
  113. # create with compression scheme different from default
  114. rc, out, err = runprog(prog, '-z', str(mycomp), '-c', 'config.yaml', bar)
  115. self.assertEqual(0, rc)
  116. # should be different binary
  117. rc, out, err = runprog('/usr/bin/cmp', foo, bar)
  118. self.assertNotEqual(0, rc)
  119. def testPackUnpackImplicitZ(self):
  120. """Create, unpack, recreate with implicit compression"""
  121. self._allowed = range(3)
  122. for c in self._allowed:
  123. os.chdir(self._cwd)
  124. self.doPackUnpackImplicitZ(str(c), [x for x in self._allowed if x != c])
  125. class TestReproducable(TempDirTestCase):
  126. def disabledTestReproduce(self):
  127. """Equivalent yaml files should produce identical bmpblocks"""
  128. # TODO: This test is currently broken because bmpblock_utility
  129. # uses a map to hold the images, and the map doesn't preserve image
  130. # order. So a simple compare is insufficient to determine that
  131. # the bmpblocks are equivalent. See crosbug.com/19541.
  132. order1 = os.path.join(self.tempdir, 'ORDER1')
  133. order2 = os.path.join(self.tempdir, 'ORDER2')
  134. rc, out, err = runprog(prog, '-c', 'case_order1.yaml', order1)
  135. self.assertEqual(0, rc)
  136. rc, out, err = runprog(prog, '-c', 'case_order2.yaml', order2)
  137. self.assertEqual(0, rc)
  138. rc, out, err = runprog('/usr/bin/cmp', order1, order2)
  139. self.assertEqual(0, rc)
  140. class TestReuse(TempDirTestCase):
  141. def testReuse(self):
  142. """Reusing screens in the yaml file should be okay"""
  143. foo = os.path.join(self.tempdir, 'FOO')
  144. bar = os.path.join(self.tempdir, 'BAR')
  145. rc, out, err = runprog(prog, '-c', 'case_reuse.yaml', foo)
  146. self.assertEqual(0, rc)
  147. rc, out, err = runprog(prog, '-x', '-d', self.tempdir, foo)
  148. self.assertEqual(0, rc)
  149. os.chdir(self.tempdir)
  150. rc, out, err = runprog(prog, '-c', 'config.yaml', bar)
  151. self.assertEqual(0, rc)
  152. rc, out, err = runprog('/usr/bin/cmp', foo, bar)
  153. self.assertEqual(0, rc)
  154. # Run these tests
  155. if __name__ == '__main__':
  156. varname = 'BMPBLK'
  157. if varname not in os.environ:
  158. print('You must specify the path to bmpblk_utility in the $%s '
  159. 'environment variable.' % varname)
  160. sys.exit(1)
  161. prog = os.environ[varname]
  162. print "Testing prog...", prog
  163. unittest.main()