2021-01-20 18:31:03 +00:00
|
|
|
# Copyright 2021 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""Tests for affected_fuzz_targets.py"""
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import parameterized
|
|
|
|
|
|
|
|
import affected_fuzz_targets
|
2021-07-20 17:04:57 +00:00
|
|
|
import clusterfuzz_deployment
|
2021-07-27 19:11:53 +00:00
|
|
|
import config_utils
|
2021-07-20 17:04:57 +00:00
|
|
|
import test_helpers
|
2021-01-20 18:31:03 +00:00
|
|
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
|
|
|
# NOTE: This integration test relies on
|
|
|
|
# https://github.com/google/oss-fuzz/tree/master/projects/example project.
|
|
|
|
EXAMPLE_PROJECT = 'example'
|
|
|
|
|
|
|
|
EXAMPLE_FILE_CHANGED = 'test.txt'
|
|
|
|
|
2021-06-30 14:34:42 +00:00
|
|
|
TEST_DATA_OUT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'test_data', 'build-out')
|
2021-01-20 18:31:03 +00:00
|
|
|
|
2021-01-20 18:31:18 +00:00
|
|
|
|
2021-01-20 18:31:03 +00:00
|
|
|
class RemoveUnaffectedFuzzTargets(unittest.TestCase):
|
|
|
|
"""Tests remove_unaffected_fuzzers."""
|
|
|
|
|
2021-06-30 14:34:42 +00:00
|
|
|
TEST_FUZZER_1 = os.path.join(TEST_DATA_OUT_PATH, 'example_crash_fuzzer')
|
|
|
|
TEST_FUZZER_2 = os.path.join(TEST_DATA_OUT_PATH, 'example_nocrash_fuzzer')
|
2021-01-20 18:31:03 +00:00
|
|
|
|
|
|
|
# yapf: disable
|
|
|
|
@parameterized.parameterized.expand([
|
|
|
|
# Tests a specific affected fuzzers is kept.
|
|
|
|
([[EXAMPLE_FILE_CHANGED], None], 2,),
|
|
|
|
|
|
|
|
# Tests specific affected fuzzer is kept.
|
|
|
|
([[EXAMPLE_FILE_CHANGED], ['not/a/real/file']], 1),
|
|
|
|
|
|
|
|
# Tests all fuzzers are kept if none are deemed affected.
|
|
|
|
([None, None], 2),
|
|
|
|
|
|
|
|
# Tests that multiple fuzzers are kept if multiple fuzzers are affected.
|
|
|
|
([[EXAMPLE_FILE_CHANGED], [EXAMPLE_FILE_CHANGED]], 2),
|
|
|
|
])
|
|
|
|
# yapf: enable
|
|
|
|
def test_remove_unaffected_fuzz_targets(self, side_effect, expected_dir_len):
|
|
|
|
"""Tests that remove_unaffected_fuzzers has the intended effect."""
|
2021-07-21 05:32:32 +00:00
|
|
|
config = test_helpers.create_run_config(
|
|
|
|
is_github=True,
|
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
|
|
|
workspace='/workspace')
|
2021-07-27 19:11:53 +00:00
|
|
|
workspace = config_utils.Workspace(config)
|
2021-07-20 17:04:57 +00:00
|
|
|
deployment = clusterfuzz_deployment.get_clusterfuzz_deployment(
|
|
|
|
config, workspace)
|
2021-02-03 23:18:56 +00:00
|
|
|
# We can't use fakefs in this test because this test executes
|
|
|
|
# utils.is_fuzz_target_local. This function relies on the executable bit
|
|
|
|
# being set, which doesn't work properly in fakefs.
|
2021-01-20 18:31:03 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir, mock.patch(
|
2021-07-20 17:04:57 +00:00
|
|
|
'get_coverage.OSSFuzzCoverage.get_files_covered_by_target'
|
2021-02-03 23:18:56 +00:00
|
|
|
) as mocked_get_files:
|
2021-07-20 17:04:57 +00:00
|
|
|
with mock.patch('get_coverage._get_oss_fuzz_fuzzer_stats_dir_url',
|
|
|
|
return_value=1):
|
2021-01-20 18:31:03 +00:00
|
|
|
mocked_get_files.side_effect = side_effect
|
|
|
|
shutil.copy(self.TEST_FUZZER_1, tmp_dir)
|
|
|
|
shutil.copy(self.TEST_FUZZER_2, tmp_dir)
|
2021-07-20 17:04:57 +00:00
|
|
|
|
2021-01-20 18:31:03 +00:00
|
|
|
affected_fuzz_targets.remove_unaffected_fuzz_targets(
|
2021-07-20 17:04:57 +00:00
|
|
|
deployment, tmp_dir, [EXAMPLE_FILE_CHANGED], '')
|
2021-01-20 18:31:03 +00:00
|
|
|
self.assertEqual(expected_dir_len, len(os.listdir(tmp_dir)))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|