Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / tools / ipc_fuzzer / mutate / utils.py
blobc718ac95b2c41738e5bf5616ff33769c5f3e2678
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_REPLAY_APPLICATION = 'ipc_fuzzer_replay'
20 IPCDUMP_EXTENSION = '.ipcdump'
21 LAUNCH_PREFIXES = [
22 '--gpu-launcher',
23 '--plugin-launcher',
24 '--ppapi-plugin-launcher',
25 '--renderer-cmd-prefix',
26 '--utility-cmd-prefix',
29 def application_name_for_platform(application_name):
30 """Return application name for current platform."""
31 if platform() == 'WINDOWS':
32 return application_name + '.exe'
33 return application_name
35 def create_flags_file(ipcdump_testcase_path, ipc_replay_application_path):
36 """Create a flags file to add launch prefix to application command line."""
37 random_launch_prefix = random.choice(LAUNCH_PREFIXES)
38 file_content = '%s=%s' % (random_launch_prefix, ipc_replay_application_path)
40 flags_file_path = ipcdump_testcase_path.replace(FUZZ_PREFIX, FLAGS_PREFIX)
41 file_handle = open(flags_file_path, 'w')
42 file_handle.write(file_content)
43 file_handle.close()
45 def create_temp_file():
46 """Create a temporary file."""
47 temp_file = tempfile.NamedTemporaryFile(delete=False)
48 temp_file.close()
49 return temp_file.name
51 def parse_arguments():
52 """Parse fuzzer arguments."""
53 parser = argparse.ArgumentParser()
54 parser.add_argument('--input_dir')
55 parser.add_argument('--output_dir')
56 parser.add_argument('--no_of_files', type=int)
57 args = parser.parse_args();
58 if (not args.input_dir or
59 not args.output_dir or
60 not args.no_of_files):
61 parser.print_help()
62 sys.exit(1)
64 return args
66 def random_id(size=16, chars=string.ascii_lowercase):
67 """Return a random id string, default 16 characters long."""
68 return ''.join(random.choice(chars) for _ in range(size))
70 def random_ipcdump_testcase_path(ipcdump_directory):
71 """Return a random ipc testcase path."""
72 return os.path.join(
73 ipcdump_directory,
74 '%s%s%s' % (FUZZ_PREFIX, random_id(), IPCDUMP_EXTENSION))
76 def platform():
77 """Return running platform."""
78 if sys.platform.startswith('win'):
79 return 'WINDOWS'
80 if sys.platform.startswith('linux'):
81 return 'LINUX'
82 if sys.platform == 'darwin':
83 return 'MAC'
85 assert False, 'Unknown platform'
87 def get_application_path():
88 """Return chrome application path."""
89 if APP_PATH_KEY not in os.environ:
90 sys.exit(
91 'Environment variable %s should be set to chrome path.' % APP_PATH_KEY)
93 return os.environ[APP_PATH_KEY]