| 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. | 10 # Contains the different types of base tokens used when generating a test case. |
| 11 BASE_TOKENS = [ | 11 BASE_TOKENS = [ |
| 12 'TRANSFORM_BASIC_BASE', | 12 'TRANSFORM_BASIC_BASE', |
| 13 'TRANSFORM_DEVICE_DISCOVERY_BASE', | 13 'TRANSFORM_DEVICE_DISCOVERY_BASE', |
| 14 'TRANSFORM_CONNECTABLE_BASE', | 14 'TRANSFORM_CONNECTABLE_BASE', |
| 15 'TRANSFORM_SERVICE_RETRIEVED_BASE', | 15 'TRANSFORM_SERVICES_RETRIEVED_BASE', |
| 16 'TRANSFORM_CHARACTERISTICS_RETRIEVED_BASE', |
| 16 ] | 17 ] |
| 17 | 18 |
| 18 # Contains strings that represent calls to the Web Bluetooth API. These | 19 # Contains strings that represent calls to the Web Bluetooth API. These |
| 19 # strings can be sequentially placed together to generate sequences of | 20 # strings can be sequentially placed together to generate sequences of |
| 20 # calls to the API. These strings are separated by line and placed so | 21 # calls to the API. These strings are separated by line and placed so |
| 21 # that indentation can be performed more easily. | 22 # that indentation can be performed more easily. |
| 22 TOKENS = [ | 23 TOKENS = [ |
| 23 [ | |
| 24 '})', | |
| 25 '.catch(e => console.log(e.name + \': \' + e.message))', | |
| 26 '.then(() => {', | |
| 27 ], | |
| 28 # Request Device Tokens | 24 # Request Device Tokens |
| 29 [ | 25 [ |
| 30 ' requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);' | 26 ' requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);' |
| 31 ], | 27 ], |
| 32 [ | 28 [ |
| 33 ' return requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);', | 29 ' return requestDeviceWithKeyDown(TRANSFORM_REQUEST_DEVICE_OPTIONS);', |
| 34 '})', | 30 '})', |
| 31 'TRANSFORM_CATCH', |
| 35 '.then(device => {', | 32 '.then(device => {', |
| 33 ' var gatt = device ? device.gatt : undefined;', |
| 36 ], | 34 ], |
| 37 # Connect Tokens | 35 # Connect Tokens |
| 38 [ | 36 [ |
| 39 ' device.gatt.connect();', | 37 ' if (typeof gatt !== \'undefined\') gatt.connect();', |
| 40 ], | |
| 41 [ | |
| 42 ' return device.gatt.connect();', | |
| 43 '})' | |
| 44 '.then(gatt => {', | |
| 45 ], | |
| 46 [ | |
| 47 ' gatt.connect();', | |
| 48 ], | 38 ], |
| 49 [ | 39 [ |
| 50 ' return gatt.connect();', | 40 ' return gatt.connect();', |
| 51 '})' | 41 '})', |
| 42 'TRANSFORM_CATCH', |
| 52 '.then(gatt => {', | 43 '.then(gatt => {', |
| 44 ' var device = gatt ? gatt.device : undefined;', |
| 53 ], | 45 ], |
| 54 # GetPrimaryService(s) Tokens | 46 # Disconnect |
| 55 [ | 47 [ |
| 56 ' device.gatt.getPrimaryServices();', | 48 ' if (typeof gatt !==\'undefined\') gatt.disconnect();', |
| 49 ], |
| 50 # GetPrimaryServices Tokens |
| 51 [ |
| 52 ' if (typeof gatt !== \'undefined\') gatt.TRANSFORM_GET_PRIMARY_SERVICE
S;', |
| 57 ], | 53 ], |
| 58 [ | 54 [ |
| 59 ' gatt.getPrimaryServices();', | 55 ' return gatt.TRANSFORM_GET_PRIMARY_SERVICES;', |
| 56 '})', |
| 57 'TRANSFORM_CATCH', |
| 58 '.then(services => {', |
| 59 ' var device = services ? Array.isArray(services) ? services[0].device
: services.device : undefined;', |
| 60 ' var gatt = device ? device.gatt : undefined;', |
| 61 ], |
| 62 # GetCharacteristics Tokens |
| 63 [ |
| 64 ' TRANSFORM_PICK_A_SERVICE;', |
| 65 ' if (typeof service !== \'undefined\') service.TRANSFORM_GET_CHARACTER
ISTICS;', |
| 60 ], | 66 ], |
| 61 [ | 67 [ |
| 62 ' return device.gatt.getPrimaryServices();', | 68 ' TRANSFORM_PICK_A_SERVICE;', |
| 63 '})' | 69 ' return service.TRANSFORM_GET_CHARACTERISTICS;', |
| 64 '.then(services => {', | 70 '})', |
| 71 'TRANSFORM_CATCH', |
| 72 '.then(characteristics => {', |
| 73 ' var service = characteristics ? Array.isArray(services) ? characteris
tics[0].service : characteristics.service : undefined;' |
| 74 ' var device = service ? service.device : undefined;', |
| 75 ' var gatt = device ? device.gatt : undefined;', |
| 76 ], |
| 77 # ReadValue Tokens |
| 78 [ |
| 79 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 80 ' if (typeof characteristic != \'undefined\') characteristic.readValue(
);', |
| 65 ], | 81 ], |
| 66 [ | 82 [ |
| 67 ' return gatt.getPrimaryServices();', | 83 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 68 '})' | 84 ' return characteristic.readValue().then(_ => characteristics);', |
| 69 '.then(services => {', | 85 '})', |
| 86 'TRANSFORM_CATCH', |
| 87 '.then((characteristics) => {', |
| 88 ], |
| 89 # WriteValue Tokens |
| 90 [ |
| 91 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 92 ' if (typeof characteristic != \'undefined\') characteristic.writeValue
(TRANSFORM_WRITE_VALUE);' |
| 93 ], |
| 94 [ |
| 95 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 96 ' return characteristic.writeValue(TRANSFORM_WRITE_VALUE).then(() => ch
aracteristics);', |
| 97 '})', |
| 98 'TRANSFORM_CATCH', |
| 99 '.then(characteristics => {', |
| 100 ], |
| 101 # Notifications Tokens |
| 102 [ |
| 103 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 104 ' if (typeof characteristic != \'undefined\') characteristic.startNotif
ications();', |
| 105 ], |
| 106 [ |
| 107 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 108 ' return characteristic.startNotitications().then(() => characteristics
);', |
| 109 '})', |
| 110 'TRANSFORM_CATCH', |
| 111 '.then(characteristics => {', |
| 112 ], |
| 113 [ |
| 114 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 115 ' if (typeof characteristic != \'undefined\') characteristic.stopNotifi
cations();', |
| 116 ], |
| 117 [ |
| 118 ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 119 ' return characteristic.stopNotitications().then(() => characteristics)
;', |
| 120 '})', |
| 121 'TRANSFORM_CATCH', |
| 122 '.then(characteristics => {', |
| 123 ], |
| 124 # Event listener tokens |
| 125 # [ |
| 126 # ' if (typeof device != \'undefined\')', |
| 127 # ' device.TRANSFORM_LISTENER_CALL;', |
| 128 # ], |
| 129 # [ |
| 130 # ' if (typeof gatt != \'undefined\')', |
| 131 # ' gatt.TRANSFORM_LISTENER_CALL;', |
| 132 # ], |
| 133 # [ |
| 134 # ' TRANSFORM_PICK_A_SERVICE;', |
| 135 # ' if (typeof service != \'undefined\')', |
| 136 # ' service.TRANSFORM_LISTENER_CALL;', |
| 137 # ], |
| 138 # [ |
| 139 # ' TRANSFORM_PICK_A_CHARACTERISTIC;', |
| 140 # ' if (typeof characteristic != \'undefined\')', |
| 141 # ' characteristic.TRANSFORM_LISTENER_CALL;', |
| 142 # ], |
| 143 # Garbage Collection |
| 144 [ |
| 145 ' runGarbageCollection();', |
| 146 ], |
| 147 [ |
| 148 ' return runGarbageCollection();', |
| 149 '})', |
| 150 '.then(() => {', |
| 151 ], |
| 152 # Reload |
| 153 [ |
| 154 ' (() => {', |
| 155 ' let reload_id = TRANSFORM_RELOAD_ID;', |
| 156 ' if (!sessionStorage.getItem(reload_id)) {', |
| 157 ' sessionStorage.setItem(reload_id, true);', |
| 158 ' location.reload();', |
| 159 ' }', |
| 160 ' })();', |
| 70 ], | 161 ], |
| 71 ] | 162 ] |
| 72 | 163 |
| 73 INDENT = ' ' | 164 INDENT = ' ' |
| 74 BREAK = '\n' | 165 BREAK = '\n' |
| 75 END_TOKEN = '});' | 166 END_TOKEN = '});' |
| 76 | 167 |
| 77 # Maximum number of tokens that will be inserted in the generated | 168 # Maximum number of tokens that will be inserted in the generated |
| 78 # test case. | 169 # test case. |
| 79 MAX_NUM_OF_TOKENS = 100 | 170 MAX_NUM_OF_TOKENS = 100 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 Returns: | 204 Returns: |
| 114 A string consisting of template_file_data with the string | 205 A string consisting of template_file_data with the string |
| 115 'TRANSFORM_RANDOM_TOKENS' replaced with a sequence of calls to the Web | 206 'TRANSFORM_RANDOM_TOKENS' replaced with a sequence of calls to the Web |
| 116 Bluetooth API and calls to reload the page and perform garbage | 207 Bluetooth API and calls to reload the page and perform garbage |
| 117 collection. | 208 collection. |
| 118 """ | 209 """ |
| 119 | 210 |
| 120 return FillInParameter('TRANSFORM_RANDOM_TOKENS', | 211 return FillInParameter('TRANSFORM_RANDOM_TOKENS', |
| 121 _GenerateSequenceOfRandomTokens, | 212 _GenerateSequenceOfRandomTokens, |
| 122 template_file_data) | 213 template_file_data) |
| OLD | NEW |