Add ICU message format support
[chromium-blink-merge.git] / tools / ipc_fuzzer / scripts / utils.py
blob3c7e1498f5138c02f0244161558e5e8feba85a2a
1 #!/usr/bin/env python
2 # Copyright 2014 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.
6 """Utility functions used by Generational and Mutational ClusterFuzz
7 fuzzers."""
9 import argparse
10 import os
11 import random
12 import string
13 import sys
14 import tempfile
16 APP_PATH_KEY = 'APP_PATH'
17 FLAGS_PREFIX = 'flags-'
18 FUZZ_PREFIX = 'fuzz-'
19 IPC_FUZZER_APPLICATION = 'ipc_fuzzer'
20 IPC_REPLAY_APPLICATION = 'ipc_fuzzer_replay'
21 IPCDUMP_EXTENSION = '.ipcdump'
22 LAUNCH_PREFIXES = [
23 '--gpu-launcher',
24 '--plugin-launcher',
25 '--ppapi-plugin-launcher',
26 '--renderer-cmd-prefix',
27 '--utility-cmd-prefix',
30 def application_name_for_platform(application_name):
31 """Return application name for current platform."""
32 if platform() == 'WINDOWS':
33 return application_name + '.exe'
34 return application_name
36 def create_flags_file(ipcdump_testcase_path, ipc_replay_application_path):
37 """Create a flags file to add launch prefix to application command line."""
38 random_launch_prefix = random.choice(LAUNCH_PREFIXES)
39 file_content = '%s=%s' % (random_launch_prefix, ipc_replay_application_path)
41 flags_file_path = ipcdump_testcase_path.replace(FUZZ_PREFIX, FLAGS_PREFIX)
42 file_handle = open(flags_file_path, 'w')
43 file_handle.write(file_content)
44 file_handle.close()
46 def create_temp_file():
47 """Create a temporary file."""
48 temp_file = tempfile.NamedTemporaryFile(delete=False)
49 temp_file.close()
50 return temp_file.name
52 def get_fuzzer_application_name():
53 """Get the application name for the fuzzer binary."""
54 return application_name_for_platform(IPC_FUZZER_APPLICATION)
56 def get_replay_application_name():
57 """Get the application name for the replay binary."""
58 return application_name_for_platform(IPC_REPLAY_APPLICATION)
60 def parse_arguments():
61 """Parse fuzzer arguments."""
62 parser = argparse.ArgumentParser()
63 parser.add_argument('--input_dir')
64 parser.add_argument('--output_dir')
65 parser.add_argument('--no_of_files', type=int)
66 args = parser.parse_args();
67 if (not args.input_dir or
68 not args.output_dir or
69 not args.no_of_files):
70 parser.print_help()
71 sys.exit(1)
73 return args
75 def random_id(size=16, chars=string.ascii_lowercase):
76 """Return a random id string, default 16 characters long."""
77 return ''.join(random.choice(chars) for _ in range(size))
79 def random_ipcdump_testcase_path(ipcdump_directory):
80 """Return a random ipc testcase path."""
81 return os.path.join(
82 ipcdump_directory,
83 '%s%s%s' % (FUZZ_PREFIX, random_id(), IPCDUMP_EXTENSION))
85 def platform():
86 """Return running platform."""
87 if sys.platform.startswith('win'):
88 return 'WINDOWS'
89 if sys.platform.startswith('linux'):
90 return 'LINUX'
91 if sys.platform == 'darwin':
92 return 'MAC'
94 assert False, 'Unknown platform'
96 def get_application_path():
97 """Return chrome application path."""
98 if APP_PATH_KEY not in os.environ:
99 sys.exit(
100 'Environment variable %s should be set to chrome path.' % APP_PATH_KEY)
102 return os.environ[APP_PATH_KEY]