def FuzzTemplate(template_path, resources_path): """Uses a template to return a test case that can be run as a layout test. This functions reads the template in |template_path|, injects the necessary js files to run as a layout test and fuzzes the template's parameters to generate a test case. Args: template_path: The path to the template that will be used to generate a new test case. resources_path: Path to the js files that need to be included. Returns: A string containing the test case. """ print 'Generating test file based on {}'.format(template_path) # Read the template. template_file_handle = open(template_path) template_file_data = template_file_handle.read().decode('utf-8') template_file_handle.close() # Generate a test file based on the template. generated_test = test_case_fuzzer.GenerateTestFile(template_file_data) # Fuzz parameters. fuzzed_file_data = parameter_fuzzer.FuzzParameters(generated_test) # Add includes for (js_file_name, include_parameter) in JS_FILES_AND_PARAMETERS: # Read js file. js_file_handle = open(os.path.join(resources_path, js_file_name)) js_file_data = js_file_handle.read() js_file_handle.close() js_file_data = (SCRIPT_PREFIX + js_file_data + SCRIPT_SUFFIX) fuzzed_file_data = FillInParameter(include_parameter, lambda data=js_file_data: data, fuzzed_file_data) return fuzzed_file_data.encode('utf-8')
def FuzzTemplate(template_path, resources_path): """Uses a template to return a test case that can be run as a web test. This functions reads the template in |template_path|, injects the necessary js files to run as a web test and fuzzes the template's parameters to generate a test case. Args: template_path: The path to the template that will be used to generate a new test case. resources_path: Path to the js files that need to be included. Returns: A string containing the test case. """ print 'Generating test file based on {}'.format(template_path) # Read the template. template_file_handle = open(template_path) template_file_data = template_file_handle.read().decode('utf-8') template_file_handle.close() # Generate a test file based on the template. generated_test = test_case_fuzzer.GenerateTestFile(template_file_data) # Fuzz parameters. fuzzed_file_data = parameter_fuzzer.FuzzParameters(generated_test) # Add includes for (js_file_name, include_parameter) in JS_FILES_AND_PARAMETERS: # Read js file. js_file_handle = open(os.path.join(resources_path, js_file_name)) js_file_data = js_file_handle.read() js_file_handle.close() js_file_data = (SCRIPT_PREFIX + js_file_data + SCRIPT_SUFFIX) fuzzed_file_data = FillInParameter(include_parameter, lambda data=js_file_data: data, fuzzed_file_data) return fuzzed_file_data.encode('utf-8')
def GenerateTestFile(template_file_data): """Inserts a sequence of calls to the Web Bluetooth API into a template. Args: template_file_data: A template containing the 'TRANSFORM_RANDOM_TOKENS' string. Returns: A string consisting of template_file_data with the string 'TRANSFORM_RANDOM_TOKENS' replaced with a sequence of calls to the Web Bluetooth API and calls to reload the page and perform garbage collection. """ return FillInParameter('TRANSFORM_RANDOM_TOKENS', _GenerateSequenceOfRandomTokens, template_file_data)
def FuzzParameters(test_file_data): """Fuzzes the data in the string provided. For now this function only replaces the TRANSFORM_SERVICE_UUID parameter with a valid UUID string. Args: test_file_data: String that contains parameters to be replaced. Returns: A string containing the value of test_file_data but with all its parameters replaced. """ test_file_data = FillInParameter('TRANSFORM_BASIC_BASE', constraints.GetBasicBase, test_file_data) test_file_data = FillInParameter('TRANSFORM_DEVICE_DISCOVERY_BASE', constraints.GetDeviceDiscoveryBase, test_file_data) test_file_data = FillInParameter('TRANSFORM_CONNECTABLE_BASE', constraints.GetConnectableBase, test_file_data) test_file_data = FillInParameter('TRANSFORM_SERVICES_RETRIEVED_BASE', constraints.get_services_retrieved_base, test_file_data) test_file_data = FillInParameter( 'TRANSFORM_CHARACTERISTICS_RETRIEVED_BASE', constraints.get_characteristics_retrieved_base, test_file_data) test_file_data = FillInParameter('TRANSFORM_REQUEST_DEVICE_OPTIONS', constraints.GetRequestDeviceOptions, test_file_data) test_file_data = FillInParameter('TRANSFORM_GET_PRIMARY_SERVICES', constraints.get_get_primary_services_call, test_file_data) test_file_data = FillInParameter('TRANSFORM_GET_CHARACTERISTICS', constraints.get_characteristics_call, test_file_data) test_file_data = FillInParameter('TRANSFORM_PICK_A_SERVICE', constraints.get_pick_a_service, test_file_data) test_file_data = FillInParameter('TRANSFORM_PICK_A_CHARACTERISTIC', constraints.get_pick_a_characteristic, test_file_data) test_file_data = FillInParameter('TRANSFORM_VALUE', constraints.get_buffer_source, test_file_data) test_file_data = FillInParameter('TRANSFORM_RELOAD_ID', constraints.get_reload_id, test_file_data) return test_file_data