def test_recommended_dictionary_merge(self):
        """Test merging with GCS copy of recommended dictionary."""
        fake_gcs_dict_path = os.path.join(
            DATA_DIRECTORY, 'fake_gcs_recommended_dictionary.txt')

        dict_manager = dictionary_manager.DictionaryManager('fuzzer_name')
        log_data = utils.read_data_from_file(os.path.join(
            DATA_DIRECTORY, 'log_with_recommended_dict.txt'),
                                             eval_data=False).decode('utf-8')

        dict_from_log = dict_manager.parse_recommended_dictionary_from_data(
            log_data)
        utils.write_data_to_file('\n'.join(dict_from_log),
                                 self.local_dict_path)

        dictionary_manager.merge_dictionary_files(self.local_dict_path,
                                                  fake_gcs_dict_path,
                                                  self.local_dict_path)

        # Compare resulting dictionary with its expected result.
        merged_dictionary = self._parse_dictionary_file(self.local_dict_path)
        expected_dictionary_path = os.path.join(
            DATA_DIRECTORY, 'expected_merged_recommended_dictionary.txt')
        expected_dictionary = self._parse_dictionary_file(
            expected_dictionary_path)

        self.assertEqual(sorted(merged_dictionary),
                         sorted(expected_dictionary))
Example #2
0
def add_recommended_dictionary(arguments, fuzzer_name, fuzzer_path):
    """Add recommended dictionary from GCS to existing .dict file or create
  a new one and update the arguments as needed.
  This function modifies |arguments| list in some cases."""
    recommended_dictionary_path = os.path.join(
        fuzzer_utils.get_temp_dir(),
        dictionary_manager.RECOMMENDED_DICTIONARY_FILENAME)

    dict_manager = dictionary_manager.DictionaryManager(fuzzer_name)

    try:
        # Bail out if cannot download recommended dictionary from GCS.
        if not dict_manager.download_recommended_dictionary_from_gcs(
                recommended_dictionary_path):
            return False
    except Exception as ex:
        logs.log_error('Exception downloading recommended dictionary:\n%s.' %
                       str(ex))
        return False

    # Bail out if the downloaded dictionary is empty.
    if not os.path.getsize(recommended_dictionary_path):
        return False

    # Check if there is an existing dictionary file in arguments.
    original_dictionary_path = fuzzer_utils.extract_argument(
        arguments, constants.DICT_FLAG)
    merged_dictionary_path = (
        original_dictionary_path
        or dictionary_manager.get_default_dictionary_path(fuzzer_path))
    merged_dictionary_path += MERGED_DICT_SUFFIX

    dictionary_manager.merge_dictionary_files(original_dictionary_path,
                                              recommended_dictionary_path,
                                              merged_dictionary_path)
    arguments.append(constants.DICT_FLAG + merged_dictionary_path)
    return True
Example #3
0
        return False

    # Bail out if the downloaded dictionary is empty.
    if not os.path.getsize(recommended_dictionary_path):
        return False

    # Check if there is an existing dictionary file in arguments.
    original_dictionary_path = fuzzer_utils.extract_argument(
        arguments, constants.DICT_FLAG)
    merged_dictionary_path = (
        original_dictionary_path
        or dictionary_manager.get_default_dictionary_path(fuzzer_path))
    merged_dictionary_path += MERGED_DICT_SUFFIX

    dictionary_manager.merge_dictionary_files(original_dictionary_path,
                                              recommended_dictionary_path,
                                              merged_dictionary_path)
    arguments.append(constants.DICT_FLAG + merged_dictionary_path)
    return True


def get_dictionary_analysis_timeout():
    """Get timeout for dictionary analysis."""
    return engine_common.get_overridable_timeout(
        5 * 60, 'DICTIONARY_TIMEOUT_OVERRIDE')


def get_new_testcase_mutations_timeout():
    """Get the timeout for new testcase mutations."""
    return engine_common.get_overridable_timeout(10 * 60,
                                                 'MUTATIONS_TIMEOUT_OVERRIDE')