1#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Wrapper around chrome.
7
8Replaces all the child processes (renderer, GPU, plugins and utility) with the
9IPC fuzzer. The fuzzer will then play back a specified testcase.
10
11Depends on ipc_fuzzer being available on the same directory as chrome.
12"""
13
14import os
15import platform
16import subprocess
17import sys
18
19def main():
20  if len(sys.argv) <= 1:
21    print 'Usage: play_testcase.py [chrome_flag...] testcase'
22    return 1
23
24  script_path = os.path.realpath(__file__)
25  ipc_fuzzer_dir = os.path.dirname(script_path)
26  out_dir = os.path.abspath(os.path.join(ipc_fuzzer_dir, os.pardir,
27                            os.pardir, 'out'));
28  build_dir = ''
29  chrome_path = ''
30  chrome_binary = 'chrome'
31
32  for build in ['Debug', 'Release']:
33    try_build = os.path.join(out_dir, build)
34    try_chrome = os.path.join(try_build, chrome_binary)
35    if os.path.exists(try_chrome):
36      build_dir = try_build
37      chrome_path = try_chrome
38
39  if not chrome_path:
40    print 'chrome executable not found.'
41    return 1
42
43  fuzzer_path = os.path.join(build_dir, 'ipc_fuzzer_replay')
44  if not os.path.exists(fuzzer_path):
45    print fuzzer_path + ' not found.'
46    print ('Please use enable_ipc_fuzzer=1 GYP define and '
47          'build ipc_fuzzer target.')
48    return 1
49
50  prefixes = {
51    '--renderer-cmd-prefix',
52    '--gpu-launcher',
53    '--plugin-launcher',
54    '--ppapi-plugin-launcher',
55    '--utility-cmd-prefix',
56  }
57
58  args = [
59    chrome_path,
60    '--ipc-fuzzer-testcase=' + sys.argv[-1],
61    '--no-sandbox',
62    '--disable-kill-after-bad-ipc',
63  ]
64
65  launchers = {}
66  for prefix in prefixes:
67    launchers[prefix] = fuzzer_path
68
69  for arg in sys.argv[1:-1]:
70    if arg.find('=') != -1:
71      switch, value = arg.split('=', 1)
72      if switch in prefixes:
73        launchers[switch] = value + ' ' + launchers[switch]
74        continue
75    args.append(arg)
76
77  for switch, value in launchers.items():
78    args.append(switch + '=' + value)
79
80  print args
81
82  return subprocess.call(args)
83
84
85if __name__ == "__main__":
86  sys.exit(main())
87