| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2016 The Chromium 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 """Module to generate a test file with random calls to the Web Bluetooth API.""" |
| 6 |
| 7 import random |
| 8 from fuzzer_helpers import FillInParameter |
| 9 |
| 10 # Contains strings that represent calls to the Web Bluetooth API. These |
| 11 # strings can be sequentially placed together to generate sequences of |
| 12 # calls to the API. These strings are separated by line and placed so |
| 13 # that indentation can be performed more easily. |
| 14 TOKENS = [ |
| 15 [ |
| 16 ' requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);', |
| 17 ], |
| 18 [ |
| 19 ' return requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);', |
| 20 '})', |
| 21 '.then(device => {', |
| 22 ], |
| 23 ] |
| 24 |
| 25 INDENT = ' ' |
| 26 BREAK = '\n' |
| 27 END_TOKEN = '});' |
| 28 |
| 29 # Maximum number of tokens that will be inserted in the generated |
| 30 # test case. |
| 31 MAX_NUM_OF_TOKENS = 100 |
| 32 |
| 33 |
| 34 def _GenerateSequenceOfRandomTokens(): |
| 35 """Generates a sequence of calls to the Web Bluetooth API. |
| 36 |
| 37 Uses the arrays of strings in TOKENS and randomly picks a number between |
| 38 [1, 100] to generate a random sequence of calls to the Web Bluetooth API, |
| 39 calls to reload the page, and calls to perform garbage collection. |
| 40 |
| 41 Returns: |
| 42 A string containing a sequence of calls to the Web Bluetooth API. |
| 43 """ |
| 44 result = '' |
| 45 for _ in xrange(random.randint(1, MAX_NUM_OF_TOKENS)): |
| 46 # Get random token. |
| 47 token = random.choice(TOKENS) |
| 48 |
| 49 # Indent and break line. |
| 50 for line in token: |
| 51 result += INDENT + line + BREAK |
| 52 |
| 53 result += INDENT + END_TOKEN |
| 54 return result |
| 55 |
| 56 |
| 57 def GenerateTestFile(template_file_data): |
| 58 """Inserts a sequence of calls to the Web Bluetooth API into a template. |
| 59 |
| 60 Args: |
| 61 template_file_data: A template containing the 'TRANSFORM_RANDOM_TOKENS' |
| 62 string. |
| 63 Returns: |
| 64 A string consisting of template_file_data with the string |
| 65 'TRANSFORM_RANDOM_TOKENS' replaced with a sequence of calls to the Web |
| 66 Bluetooth API and calls to reload the page and perform garbage |
| 67 collection. |
| 68 """ |
| 69 |
| 70 return FillInParameter('TRANSFORM_RANDOM_TOKENS', |
| 71 _GenerateSequenceOfRandomTokens, |
| 72 template_file_data) |
| OLD | NEW |