• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7"""Unit tests for bmpblk_utility.
8"""
9
10import os
11import sys
12import subprocess
13import unittest
14
15def runprog(*args):
16  """Runs specified program and args, returns (exitcode, stdout, stderr)."""
17  p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18  out, err = p.communicate()
19  return (p.returncode, out, err)
20
21
22class TestFailures(unittest.TestCase):
23
24  def testNoArgs(self):
25    """Running with no args should print usage and fail."""
26    rc, out, err = runprog(prog)
27    self.assertNotEqual(0, rc)
28    self.assertTrue(err.count("missing BMPBLOCK name"))
29    self.assertTrue(out.count("bmpblk_utility"))
30
31  def testMissingBmp(self):
32    """Missing a bmp specified in the yaml is an error."""
33    rc, out, err = runprog(prog, '-c', 'case_nobmp.yaml', 'FOO')
34    self.assertNotEqual(0, rc)
35    self.assertTrue(err.count("No such file or directory"))
36
37  def testInvalidBmp(self):
38    """A .bmp file that isn't really a BMP should fail."""
39    rc, out, err = runprog(prog, '-c', 'case_badbmp.yaml', 'FOO')
40    self.assertNotEqual(0, rc)
41    self.assertTrue(err.count("Unsupported image format"))
42
43  def testBadCompression(self):
44    """Wrong compression types should fail."""
45    rc, out, err = runprog(prog, '-z', '99', '-c', 'case_simple.yaml', 'FOO')
46    self.assertNotEqual(0, rc)
47    self.assertTrue(err.count("compression type"))
48
49
50class TestOverWrite(unittest.TestCase):
51
52  def setUp(self):
53    rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
54    self.assertEqual(0, rc)
55
56  def testOverwrite(self):
57    """Create, unpack, unpack again, with and without -f"""
58    rc, out, err = runprog(prog, '-c', 'case_simple.yaml', 'FOO')
59    self.assertEqual(0, rc)
60    rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
61    self.assertEqual(0, rc)
62    rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
63    self.assertNotEqual(0, rc)
64    self.assertTrue(err.count("File exists"))
65    rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', '-f', 'FOO')
66    self.assertEqual(0, rc)
67
68  def tearDown(self):
69    rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
70    self.assertEqual(0, rc)
71
72
73class TestPackUnpack(unittest.TestCase):
74
75  def setUp(self):
76    self._cwd = os.getcwd()
77    rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
78    self.assertEqual(0, rc)
79
80  def testPackUnpack(self):
81    """Create, unpack, recreate without compression"""
82    rc, out, err = runprog(prog, '-c', 'case_simple.yaml', 'FOO')
83    self.assertEqual(0, rc)
84    rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
85    self.assertEqual(0, rc)
86    os.chdir('./FOO_DIR')
87    rc, out, err = runprog(prog, '-c', 'config.yaml', 'BAR')
88    self.assertEqual(0, rc)
89    rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
90    self.assertEqual(0, rc)
91    os.chdir('..')
92
93  def doPackUnpackZ(self, comp):
94    """Create, unpack, recreate with a given compression"""
95    rc, out, err = runprog(prog, '-z', comp, '-c', 'case_simple.yaml', 'FOO')
96    self.assertEqual(0, rc)
97    rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
98    self.assertEqual(0, rc)
99    os.chdir('./FOO_DIR')
100    rc, out, err = runprog(prog, '-z', comp, '-c', 'config.yaml', 'BAR')
101    self.assertEqual(0, rc)
102    rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
103    self.assertEqual(0, rc)
104    os.chdir('..')
105
106  def testPackUnpackZ1(self):
107    """Create, unpack, recreate with EFIv1 compression"""
108    self.doPackUnpackZ('1');
109
110  def testPackUnpackZ2(self):
111    """Create, unpack, recreate with LZMA compression"""
112    self.doPackUnpackZ('2');
113
114  def doPackUnpackImplicitZ(self, comp, noncomp):
115    """Create with given compression, unpack, repack without specifying"""
116    # create with the specified compression scheme
117    rc, out, err = runprog(prog, '-z', comp, '-c', 'case_simple.yaml', 'FOO')
118    self.assertEqual(0, rc)
119    # unpack. yaml file should have compression scheme in it
120    rc, out, err = runprog(prog, '-f', '-x', '-d', './FOO_DIR', 'FOO')
121    self.assertEqual(0, rc)
122    os.chdir('./FOO_DIR')
123    # create with no compression specified, should use default from yaml
124    rc, out, err = runprog(prog, '-c', 'config.yaml', 'BAR')
125    self.assertEqual(0, rc)
126    # so new output should match original
127    rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
128    self.assertEqual(0, rc)
129    # Now make sure that specifying a compression arg will override the default
130    for mycomp in noncomp:
131      # create with compression scheme different from default
132      rc, out, err = runprog(prog, '-z', str(mycomp), '-c', 'config.yaml', 'BAR')
133      self.assertEqual(0, rc)
134      # should be different binary
135      rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
136      self.assertNotEqual(0, rc)
137    os.chdir('..')
138
139  def testPackUnpackImplicitZ(self):
140    """Create, unpack, recreate with implicit compression"""
141    self._allowed = range(3)
142    for c in self._allowed:
143      self.doPackUnpackImplicitZ(str(c), [x for x in self._allowed if x != c])
144
145  def tearDown(self):
146    os.chdir(self._cwd)
147    rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
148    self.assertEqual(0, rc)
149
150
151class TestReproducable(unittest.TestCase):
152
153  def setUp(self):
154    rc, out, err = runprog('/bin/rm', '-f', 'ORDER1', 'ORDER2')
155    self.assertEqual(0, rc)
156
157  def disabledTestReproduce(self):
158    """Equivalent yaml files should produce identical bmpblocks"""
159    # TODO: This test is currently broken because bmpblock_utility
160    # uses a map to hold the images, and the map doesn't preserve image
161    # order.  So a simple compare is insufficient to determine that
162    # the bmpblocks are equivalent.  See crosbug.com/19541.
163    rc, out, err = runprog(prog, '-c', 'case_order1.yaml', 'ORDER1')
164    self.assertEqual(0, rc)
165    rc, out, err = runprog(prog, '-c', 'case_order2.yaml', 'ORDER2')
166    self.assertEqual(0, rc)
167    rc, out, err = runprog('/usr/bin/cmp', 'ORDER1', 'ORDER2')
168    self.assertEqual(0, rc)
169
170  def tearDown(self):
171    rc, out, err = runprog('/bin/rm', '-f', 'ORDER1', 'ORDER2')
172    self.assertEqual(0, rc)
173
174class TestReuse(unittest.TestCase):
175
176  def setUp(self):
177    rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
178    self.assertEqual(0, rc)
179
180  def testReuse(self):
181    """Reusing screens in the yaml file should be okay"""
182    rc, out, err = runprog(prog, '-c', 'case_reuse.yaml', 'FOO')
183    self.assertEqual(0, rc)
184    rc, out, err = runprog(prog, '-x', '-d', './FOO_DIR', 'FOO')
185    self.assertEqual(0, rc)
186    os.chdir('./FOO_DIR')
187    rc, out, err = runprog(prog, '-c', 'config.yaml', 'BAR')
188    self.assertEqual(0, rc)
189    rc, out, err = runprog('/usr/bin/cmp', '../FOO', 'BAR')
190    self.assertEqual(0, rc)
191    os.chdir('..')
192
193  def tearDown(self):
194    rc, out, err = runprog('/bin/rm', '-rf', './FOO_DIR', 'FOO')
195    self.assertEqual(0, rc)
196
197
198# Run these tests
199if __name__ == '__main__':
200  varname = 'BMPBLK'
201  if varname not in os.environ:
202    print('You must specify the path to bmpblk_utility in the $%s '
203          'environment variable.' % varname)
204    sys.exit(1)
205  prog = os.environ[varname]
206  print "Testing prog...", prog
207  unittest.main()
208