Home | History | Annotate | Download | only in platform_DMVerityCorruption
      1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import logging
      6 import os
      7 from autotest_lib.client.common_lib import error, utils
      8 from autotest_lib.client.cros import verity_utils
      9 
     10 class platform_DMVerityCorruption(verity_utils.VerityImageTest):
     11     version = 1
     12 
     13     def mod_zerofill_block(self, run_count, backing_path, block_size,
     14                            block_count):
     15         logging.info('mod_zerofill_block(%d, %s, %d, %d)' % (
     16                      run_count, backing_path, block_size, block_count))
     17         dd_cmd = 'dd if=/dev/zero of=%s bs=%d seek=%d count=1'
     18         run_count = run_count % block_count
     19         verity_utils.system(dd_cmd % (backing_path, block_size, run_count))
     20 
     21     def mod_Afill_hash_block(self, run_count, backing_path, block_size,
     22                              block_count):
     23         logging.info('mod_Afill_hash_block(%d, %s, %d, %d)' % (
     24                      run_count, backing_path, block_size, block_count))
     25         with open(backing_path, 'wb') as dev:
     26           dev.seek(block_count * block_size, os.SEEK_SET)
     27           dev.seek(run_count * block_size, os.SEEK_CUR)
     28           dev.write('A' * block_size)
     29 
     30     def run_once(self):
     31         # Ensure that basic verification is working.
     32         # This should NOT fail.
     33         self.mod_and_test(self.mod_nothing, 1, True)
     34 
     35         # Corrupt the image once per block (on a per-block basis).
     36         self.mod_and_test(self.mod_zerofill_block, self.image_blocks, False)
     37 
     38         # Repeat except each block in the hash tree data
     39         hash_blocks = (os.path.getsize(self.verity.hash_file) /
     40                        verity_utils.BLOCK_SIZE)
     41         self.mod_and_test(self.mod_Afill_hash_block, hash_blocks, False)
     42