| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Module to generate a test file with random calls to the Web Bluetooth API.""" | 5 """Module to generate a test file with random calls to the Web Bluetooth API.""" |
| 6 | 6 |
| 7 import random | 7 import random |
| 8 from fuzzer_helpers import FillInParameter | 8 from fuzzer_helpers import FillInParameter |
| 9 | 9 |
| 10 # Contains the different types of base tokens used when generating a test case. |
| 11 BASE_TOKENS = [ |
| 12 'TRANSFORM_BASIC_BASE', |
| 13 'TRANSFORM_DEVICE_DISCOVERY_BASE', |
| 14 'TRANSFORM_CONNECTABLE_BASE', |
| 15 'TRANSFORM_SERVICE_RETRIEVED_BASE', |
| 16 ] |
| 17 |
| 10 # Contains strings that represent calls to the Web Bluetooth API. These | 18 # Contains strings that represent calls to the Web Bluetooth API. These |
| 11 # strings can be sequentially placed together to generate sequences of | 19 # strings can be sequentially placed together to generate sequences of |
| 12 # calls to the API. These strings are separated by line and placed so | 20 # calls to the API. These strings are separated by line and placed so |
| 13 # that indentation can be performed more easily. | 21 # that indentation can be performed more easily. |
| 14 TOKENS = [ | 22 TOKENS = [ |
| 15 [ | 23 [ |
| 16 ' requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);', | 24 '})', |
| 25 '.catch(e => console.log(e.name + \': \' + e.message))', |
| 26 '.then(() => {', |
| 27 ], |
| 28 # Request Device Tokens |
| 29 [ |
| 30 ' requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);' |
| 17 ], | 31 ], |
| 18 [ | 32 [ |
| 19 ' return requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);', | 33 ' return requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);', |
| 20 '})', | 34 '})', |
| 21 '.then(device => {', | 35 '.then(device => {', |
| 22 ], | 36 ], |
| 37 # Connect Tokens |
| 38 [ |
| 39 ' device.gatt.connect();', |
| 40 ], |
| 41 [ |
| 42 ' return device.gatt.connect();', |
| 43 '})' |
| 44 '.then(gatt => {', |
| 45 ], |
| 46 [ |
| 47 ' gatt.connect();', |
| 48 ], |
| 49 [ |
| 50 ' return gatt.connect();', |
| 51 '})' |
| 52 '.then(gatt => {', |
| 53 ], |
| 54 # GetPrimaryService(s) Tokens |
| 55 [ |
| 56 ' device.gatt.getPrimaryServices();', |
| 57 ], |
| 58 [ |
| 59 ' gatt.getPrimaryServices();', |
| 60 ], |
| 61 [ |
| 62 ' return device.gatt.getPrimaryServices();', |
| 63 '})' |
| 64 '.then(services => {', |
| 65 ], |
| 66 [ |
| 67 ' return gatt.getPrimaryServices();', |
| 68 '})' |
| 69 '.then(services => {', |
| 70 ], |
| 23 ] | 71 ] |
| 24 | 72 |
| 25 INDENT = ' ' | 73 INDENT = ' ' |
| 26 BREAK = '\n' | 74 BREAK = '\n' |
| 27 END_TOKEN = '});' | 75 END_TOKEN = '});' |
| 28 | 76 |
| 29 # Maximum number of tokens that will be inserted in the generated | 77 # Maximum number of tokens that will be inserted in the generated |
| 30 # test case. | 78 # test case. |
| 31 MAX_NUM_OF_TOKENS = 100 | 79 MAX_NUM_OF_TOKENS = 100 |
| 32 | 80 |
| 33 | 81 |
| 34 def _GenerateSequenceOfRandomTokens(): | 82 def _GenerateSequenceOfRandomTokens(): |
| 35 """Generates a sequence of calls to the Web Bluetooth API. | 83 """Generates a sequence of calls to the Web Bluetooth API. |
| 36 | 84 |
| 37 Uses the arrays of strings in TOKENS and randomly picks a number between | 85 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, | 86 [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. | 87 calls to reload the page, and calls to perform garbage collection. |
| 40 | 88 |
| 41 Returns: | 89 Returns: |
| 42 A string containing a sequence of calls to the Web Bluetooth API. | 90 A string containing a sequence of calls to the Web Bluetooth API. |
| 43 """ | 91 """ |
| 44 result = '' | 92 result = random.choice(BASE_TOKENS) |
| 93 |
| 45 for _ in xrange(random.randint(1, MAX_NUM_OF_TOKENS)): | 94 for _ in xrange(random.randint(1, MAX_NUM_OF_TOKENS)): |
| 46 # Get random token. | 95 # Get random token. |
| 47 token = random.choice(TOKENS) | 96 token = random.choice(TOKENS) |
| 48 | 97 |
| 49 # Indent and break line. | 98 # Indent and break line. |
| 50 for line in token: | 99 for line in token: |
| 51 result += INDENT + line + BREAK | 100 result += INDENT + line + BREAK |
| 52 | 101 |
| 53 result += INDENT + END_TOKEN | 102 result += INDENT + END_TOKEN |
| 103 |
| 54 return result | 104 return result |
| 55 | 105 |
| 56 | 106 |
| 57 def GenerateTestFile(template_file_data): | 107 def GenerateTestFile(template_file_data): |
| 58 """Inserts a sequence of calls to the Web Bluetooth API into a template. | 108 """Inserts a sequence of calls to the Web Bluetooth API into a template. |
| 59 | 109 |
| 60 Args: | 110 Args: |
| 61 template_file_data: A template containing the 'TRANSFORM_RANDOM_TOKENS' | 111 template_file_data: A template containing the 'TRANSFORM_RANDOM_TOKENS' |
| 62 string. | 112 string. |
| 63 Returns: | 113 Returns: |
| 64 A string consisting of template_file_data with the string | 114 A string consisting of template_file_data with the string |
| 65 'TRANSFORM_RANDOM_TOKENS' replaced with a sequence of calls to the Web | 115 'TRANSFORM_RANDOM_TOKENS' replaced with a sequence of calls to the Web |
| 66 Bluetooth API and calls to reload the page and perform garbage | 116 Bluetooth API and calls to reload the page and perform garbage |
| 67 collection. | 117 collection. |
| 68 """ | 118 """ |
| 69 | 119 |
| 70 return FillInParameter('TRANSFORM_RANDOM_TOKENS', | 120 return FillInParameter('TRANSFORM_RANDOM_TOKENS', |
| 71 _GenerateSequenceOfRandomTokens, | 121 _GenerateSequenceOfRandomTokens, |
| 72 template_file_data) | 122 template_file_data) |
| OLD | NEW |