提交 407ba084 编写于 作者: M Maria Kustova 提交者: Stefan Hajnoczi

image-fuzzer: Trivial readability and formatting improvements

Signed-off-by: NMaria Kustova <maria.k@catit.be>
Signed-off-by: NStefan Hajnoczi <stefanha@redhat.com>
上级 5b0ed2be
...@@ -332,9 +332,8 @@ def l1_entry(current): ...@@ -332,9 +332,8 @@ def l1_entry(current):
constraints = UINT64_V constraints = UINT64_V
# Reserved bits are ignored # Reserved bits are ignored
# Added a possibility when only flags are fuzzed # Added a possibility when only flags are fuzzed
offset = 0x7fffffffffffffff & random.choice([selector(current, offset = 0x7fffffffffffffff & \
constraints), random.choice([selector(current, constraints), current])
current])
is_cow = random.randint(0, 1) is_cow = random.randint(0, 1)
return offset + (is_cow << UINT64_M) return offset + (is_cow << UINT64_M)
...@@ -344,9 +343,8 @@ def l2_entry(current): ...@@ -344,9 +343,8 @@ def l2_entry(current):
constraints = UINT64_V constraints = UINT64_V
# Reserved bits are ignored # Reserved bits are ignored
# Add a possibility when only flags are fuzzed # Add a possibility when only flags are fuzzed
offset = 0x3ffffffffffffffe & random.choice([selector(current, offset = 0x3ffffffffffffffe & \
constraints), random.choice([selector(current, constraints), current])
current])
is_compressed = random.randint(0, 1) is_compressed = random.randint(0, 1)
is_cow = random.randint(0, 1) is_cow = random.randint(0, 1)
is_zero = random.randint(0, 1) is_zero = random.randint(0, 1)
......
...@@ -70,7 +70,7 @@ def run_app(fd, q_args): ...@@ -70,7 +70,7 @@ def run_app(fd, q_args):
"""Exception for signal.alarm events.""" """Exception for signal.alarm events."""
pass pass
def handler(*arg): def handler(*args):
"""Notify that an alarm event occurred.""" """Notify that an alarm event occurred."""
raise Alarm raise Alarm
...@@ -134,8 +134,8 @@ class TestEnv(object): ...@@ -134,8 +134,8 @@ class TestEnv(object):
self.init_path = os.getcwd() self.init_path = os.getcwd()
self.work_dir = work_dir self.work_dir = work_dir
self.current_dir = os.path.join(work_dir, 'test-' + test_id) self.current_dir = os.path.join(work_dir, 'test-' + test_id)
self.qemu_img = os.environ.get('QEMU_IMG', 'qemu-img')\ self.qemu_img = \
.strip().split(' ') os.environ.get('QEMU_IMG', 'qemu-img').strip().split(' ')
self.qemu_io = os.environ.get('QEMU_IO', 'qemu-io').strip().split(' ') self.qemu_io = os.environ.get('QEMU_IO', 'qemu-io').strip().split(' ')
self.commands = [['qemu-img', 'check', '-f', 'qcow2', '$test_img'], self.commands = [['qemu-img', 'check', '-f', 'qcow2', '$test_img'],
['qemu-img', 'info', '-f', 'qcow2', '$test_img'], ['qemu-img', 'info', '-f', 'qcow2', '$test_img'],
...@@ -212,10 +212,8 @@ class TestEnv(object): ...@@ -212,10 +212,8 @@ class TestEnv(object):
os.chdir(self.current_dir) os.chdir(self.current_dir)
backing_file_name, backing_file_fmt = self._create_backing_file() backing_file_name, backing_file_fmt = self._create_backing_file()
img_size = image_generator.create_image('test.img', img_size = image_generator.create_image(
backing_file_name, 'test.img', backing_file_name, backing_file_fmt, fuzz_config)
backing_file_fmt,
fuzz_config)
for item in commands: for item in commands:
shutil.copy('test.img', 'copy.img') shutil.copy('test.img', 'copy.img')
# 'off' and 'len' are multiple of the sector size # 'off' and 'len' are multiple of the sector size
...@@ -228,7 +226,7 @@ class TestEnv(object): ...@@ -228,7 +226,7 @@ class TestEnv(object):
elif item[0] == 'qemu-io': elif item[0] == 'qemu-io':
current_cmd = list(self.qemu_io) current_cmd = list(self.qemu_io)
else: else:
multilog("Warning: test command '%s' is not defined.\n" \ multilog("Warning: test command '%s' is not defined.\n"
% item[0], sys.stderr, self.log, self.parent_log) % item[0], sys.stderr, self.log, self.parent_log)
continue continue
# Replace all placeholders with their real values # Replace all placeholders with their real values
...@@ -244,29 +242,28 @@ class TestEnv(object): ...@@ -244,29 +242,28 @@ class TestEnv(object):
"Backing file: %s\n" \ "Backing file: %s\n" \
% (self.seed, " ".join(current_cmd), % (self.seed, " ".join(current_cmd),
self.current_dir, backing_file_name) self.current_dir, backing_file_name)
temp_log = StringIO.StringIO() temp_log = StringIO.StringIO()
try: try:
retcode = run_app(temp_log, current_cmd) retcode = run_app(temp_log, current_cmd)
except OSError, e: except OSError, e:
multilog(test_summary + "Error: Start of '%s' failed. " \ multilog("%sError: Start of '%s' failed. Reason: %s\n\n"
"Reason: %s\n\n" % (os.path.basename( % (test_summary, os.path.basename(current_cmd[0]),
current_cmd[0]), e[1]), e[1]),
sys.stderr, self.log, self.parent_log) sys.stderr, self.log, self.parent_log)
raise TestException raise TestException
if retcode < 0: if retcode < 0:
self.log.write(temp_log.getvalue()) self.log.write(temp_log.getvalue())
multilog(test_summary + "FAIL: Test terminated by signal " + multilog("%sFAIL: Test terminated by signal %s\n\n"
"%s\n\n" % str_signal(-retcode), sys.stderr, self.log, % (test_summary, str_signal(-retcode)),
self.parent_log) sys.stderr, self.log, self.parent_log)
self.failed = True self.failed = True
else: else:
if self.log_all: if self.log_all:
self.log.write(temp_log.getvalue()) self.log.write(temp_log.getvalue())
multilog(test_summary + "PASS: Application exited with" + multilog("%sPASS: Application exited with the code " \
" the code '%d'\n\n" % retcode, sys.stdout, "'%d'\n\n" % (test_summary, retcode),
self.log, self.parent_log) sys.stdout, self.log, self.parent_log)
temp_log.close() temp_log.close()
os.remove('copy.img') os.remove('copy.img')
...@@ -286,8 +283,9 @@ if __name__ == '__main__': ...@@ -286,8 +283,9 @@ if __name__ == '__main__':
Set up test environment in TEST_DIR and run a test in it. A module for Set up test environment in TEST_DIR and run a test in it. A module for
test image generation should be specified via IMG_GENERATOR. test image generation should be specified via IMG_GENERATOR.
Example: Example:
runner.py -c '[["qemu-img", "info", "$test_img"]]' /tmp/test qcow2 runner.py -c '[["qemu-img", "info", "$test_img"]]' /tmp/test qcow2
Optional arguments: Optional arguments:
-h, --help display this help and exit -h, --help display this help and exit
...@@ -305,20 +303,22 @@ if __name__ == '__main__': ...@@ -305,20 +303,22 @@ if __name__ == '__main__':
'--command' accepts a JSON array of commands. Each command presents '--command' accepts a JSON array of commands. Each command presents
an application under test with all its paramaters as a list of strings, an application under test with all its paramaters as a list of strings,
e.g. e.g. ["qemu-io", "$test_img", "-c", "write $off $len"].
["qemu-io", "$test_img", "-c", "write $off $len"]
Supported application aliases: 'qemu-img' and 'qemu-io'. Supported application aliases: 'qemu-img' and 'qemu-io'.
Supported argument aliases: $test_img for the fuzzed image, $off Supported argument aliases: $test_img for the fuzzed image, $off
for an offset, $len for length. for an offset, $len for length.
Values for $off and $len will be generated based on the virtual disk Values for $off and $len will be generated based on the virtual disk
size of the fuzzed image size of the fuzzed image.
Paths to 'qemu-img' and 'qemu-io' are retrevied from 'QEMU_IMG' and Paths to 'qemu-img' and 'qemu-io' are retrevied from 'QEMU_IMG' and
'QEMU_IO' environment variables 'QEMU_IO' environment variables.
'--config' accepts a JSON array of fields to be fuzzed, e.g. '--config' accepts a JSON array of fields to be fuzzed, e.g.
'[["header"], ["header", "version"]]' '[["header"], ["header", "version"]]'.
Each of the list elements can consist of a complex image element only Each of the list elements can consist of a complex image element only
as ["header"] or ["feature_name_table"] or an exact field as as ["header"] or ["feature_name_table"] or an exact field as
["header", "version"]. In the first case random portion of the element ["header", "version"]. In the first case random portion of the element
...@@ -368,7 +368,6 @@ if __name__ == '__main__': ...@@ -368,7 +368,6 @@ if __name__ == '__main__':
seed = None seed = None
config = None config = None
duration = None duration = None
for opt, arg in opts: for opt, arg in opts:
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
usage() usage()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册