Chromium Code Reviews| Index: third_party/WebKit/Source/modules/bluetooth/testing/clusterfuzz/constraints.py |
| diff --git a/third_party/WebKit/Source/modules/bluetooth/testing/clusterfuzz/constraints.py b/third_party/WebKit/Source/modules/bluetooth/testing/clusterfuzz/constraints.py |
| index 3a78f0a6dad066563322dd5d9e0135a6de85b6ec..cc71339b5091fa7976aeff9eca144e97bb9495d3 100644 |
| --- a/third_party/WebKit/Source/modules/bluetooth/testing/clusterfuzz/constraints.py |
| +++ b/third_party/WebKit/Source/modules/bluetooth/testing/clusterfuzz/constraints.py |
| @@ -20,48 +20,49 @@ from resources import utils |
| from resources import fuzzy_types |
| import gatt_aliases |
| +import wbt_fakes |
| -# List of services that are included in our fake adapters. |
| -FAKE_SERVICES = [ |
| - 'generic_access', |
| - 'glucose', |
| - 'heart_rate', |
| - 'battery_service', |
| - 'human_interface_device', |
| - '000000a0-97e5-4cd7-b9f1-f5a427670c59', # Error UUID |
| - 'device_information', |
| - '611c954a-263b-4f4a-aab6-01ddb953f985', # Blacklisted |
| - '01d7d889-7451-419f-aeb8-d65e7b9277af', # Request Disconnection |
| -] |
| - |
| -# List of available fake adapters. |
| -FAKE_ADAPTERS = [ |
| - 'BaseAdapter', |
| - 'NotPresentAdapter', |
| - 'NotPoweredAdapter', |
| - 'EmptyAdapter', |
| - 'FailStartDiscoveryAdapter', |
| - 'GlucoseHeartRateAdapter', |
| - 'UnicodeDeviceAdapter', |
| - 'MissingServiceHeartRateAdapter', |
| - 'MissingCharacteristicHeartRateAdapter', |
| - 'HeartRateAdapter', |
| - 'TwoHeartRateServicesAdapter', |
| - 'DisconnectingHeartRateAdapter', |
| - 'BlacklistTestAdapter', |
| - 'FailingConnectionsAdapter', |
| - 'FailingGATTOperationsAdapter', |
| - 'DelayedServicesDiscoveryAdapter', |
| -] |
| +# Strings that are used at the beginning of a test. |
|
scheib
2016/07/26 19:04:44
Comment that these are going to have parameters fi
ortuno
2016/07/28 22:26:22
I added names to the parameters but I kept format
|
| +BASIC_BASE = \ |
| + ' return setBluetoothFakeAdapter({})\n'\ |
| + ' .then(() => {{\n' |
| + |
| +DEVICE_DISCOVERY_BASE = BASIC_BASE + \ |
| + ' return requestDeviceWithKeyDown({{filters: [{{services: [{}]}}]}});\n'\ |
| + ' }})\n'\ |
| + ' .then(device => {{\n' |
| + |
| +CONNECTABLE_BASE = DEVICE_DISCOVERY_BASE + \ |
| + ' return device.gatt.connect();\n'\ |
| + ' }})\n'\ |
| + ' .then(gatt => {{\n' |
| + |
| +SERVICE_RETRIEVED_BASE = CONNECTABLE_BASE + \ |
| + ' return gatt.getPrimaryService({});\n'\ |
| + ' }})\n'\ |
| + ' .then(service => {{\n' |
| def _ToJsStr(s): |
| return u'\'{}\''.format(s) |
| -def GetFakeAdapter(): |
| - return _ToJsStr(random.choice(FAKE_ADAPTERS)) |
| +def GetAnyFakeAdapter(): |
| + """Returns the name of a fake adapter surrounded by quotation marks.""" |
| + return _ToJsStr(random.choice(wbt_fakes.ALL_ADAPTERS)) |
| + |
| + |
| +def GetAdapterWithDevice(): |
| + """Returns an adapter name and an advertised service in that adapter.""" |
| + adapter, services = random.choice(wbt_fakes.ADAPTERS_WITH_DEVICES) |
| + return _ToJsStr(adapter), _ToJsStr(random.choice(services)) |
| + |
| + |
| +def GetAdapterWithServices(): |
| + """Returns two strings, the adapter name and a service in that adapter.""" |
| + adapter, services = random.choice(wbt_fakes.ADAPTERS_WITH_SERVICES) |
| + return _ToJsStr(adapter), random.choice(services) |
| def _GetFuzzedJsString(s): |
| @@ -84,9 +85,9 @@ def _GetFuzzedJsString(s): |
| return _ToJsStr(fuzzed_string) |
| -def GetServiceUUIDFromFakes(): |
| +def GetAdvertisedServiceUUIDFromFakes(): |
| """Returns a random service string from the list of fake services.""" |
| - return _ToJsStr(random.choice(FAKE_SERVICES)) |
| + return _ToJsStr(random.choice(wbt_fakes.ADVERTISED_SERVICES)) |
| def GetValidServiceAlias(): |
| @@ -111,7 +112,7 @@ def GetRandomUUID(): |
| return _GetFuzzedJsString(alias) |
| -def GetServiceUUID(): |
| +def GetAdvertisedServiceUUID(): |
| """Generates a random Service UUID from a set of functions. |
| See GetServiceUUIDFromFakes(), GetValidServiceAlias() and GetRandomUUID() |
| @@ -127,7 +128,7 @@ def GetServiceUUID(): |
| """ |
| roll = random.random() |
| if roll < 0.8: |
| - return GetServiceUUIDFromFakes() |
| + return GetAdvertisedServiceUUIDFromFakes() |
| elif roll < 0.9: |
| return GetValidServiceAlias() |
| else: |
| @@ -138,4 +139,39 @@ def GetRequestDeviceOptions(): |
| """Returns an object used by navigator.bluetooth.requestDevice.""" |
| # TODO(ortuno): Randomize the members, number of filters, services, etc. |
| - return '{filters: [{services: [ %s ]}]}' % GetServiceUUID() |
| + return '{filters: [{services: [ %s ]}]}' % GetAdvertisedServiceUUID() |
| + |
| + |
| +def GetBasicBase(): |
| + """Returns a string that sets a random fake adapter.""" |
| + return BASIC_BASE.format(GetAnyFakeAdapter()) |
| + |
| + |
| +def GetDeviceDiscoveryBase(): |
| + """Generates a string that contains all steps to find a device.""" |
| + return DEVICE_DISCOVERY_BASE.format(*GetAdapterWithDevice()) |
| + |
| + |
| +def GetConnectableBase(): |
| + """Generates a string that contains all steps to connect to a device. |
| + |
| + Returns: A string that: |
| + 1. Sets an adapter to a fake adapter with a connectable device. |
| + 2. Looks for the connectable device. |
| + 3. Connects to it. |
| + """ |
| + return CONNECTABLE_BASE.format(*GetAdapterWithDevice()) |
| + |
| + |
| +def GetServiceRetrievedBase(): |
| + """Returns a string that contains all steps to retrieve a service. |
| + |
| + Returns: A string that: |
| + 1. Sets an adapter to a fake adapter with a connectable device with |
| + services. |
| + 2. Use one of the device's services to look for that device. |
| + 3. Connects to it. |
| + 4. Retrieve the device's service used in 2. |
| + """ |
| + adapter, service = GetAdapterWithServices() |
| + return SERVICE_RETRIEVED_BASE.format(adapter, service, service) |