1# Copyright 2021 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests for affected_fuzz_targets.py""" 15import os 16import shutil 17import tempfile 18import unittest 19from unittest import mock 20 21import parameterized 22 23import affected_fuzz_targets 24import clusterfuzz_deployment 25import test_helpers 26import workspace_utils 27 28# pylint: disable=protected-access 29 30# NOTE: This integration test relies on 31# https://github.com/google/oss-fuzz/tree/master/projects/example project. 32EXAMPLE_PROJECT = 'example' 33 34EXAMPLE_FILE_CHANGED = 'test.txt' 35 36TEST_DATA_OUT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 37 'test_data', 'build-out') 38 39 40class RemoveUnaffectedFuzzTargets(unittest.TestCase): 41 """Tests remove_unaffected_fuzzers.""" 42 43 TEST_FUZZER_1 = os.path.join(TEST_DATA_OUT_PATH, 'example_crash_fuzzer') 44 TEST_FUZZER_2 = os.path.join(TEST_DATA_OUT_PATH, 'example_nocrash_fuzzer') 45 46 # yapf: disable 47 @parameterized.parameterized.expand([ 48 # Tests a specific affected fuzzers is kept. 49 ([[EXAMPLE_FILE_CHANGED], None], 2,), 50 51 # Tests specific affected fuzzer is kept. 52 ([[EXAMPLE_FILE_CHANGED], ['not/a/real/file']], 1), 53 54 # Tests all fuzzers are kept if none are deemed affected. 55 ([None, None], 2), 56 57 # Tests that multiple fuzzers are kept if multiple fuzzers are affected. 58 ([[EXAMPLE_FILE_CHANGED], [EXAMPLE_FILE_CHANGED]], 2), 59 ]) 60 # yapf: enable 61 def test_remove_unaffected_fuzz_targets(self, side_effect, expected_dir_len): 62 """Tests that remove_unaffected_fuzzers has the intended effect.""" 63 config = test_helpers.create_run_config( 64 is_github=True, 65 oss_fuzz_project_name=EXAMPLE_PROJECT, 66 workspace='/workspace') 67 workspace = workspace_utils.Workspace(config) 68 deployment = clusterfuzz_deployment.get_clusterfuzz_deployment( 69 config, workspace) 70 # We can't use fakefs in this test because this test executes 71 # utils.is_fuzz_target_local. This function relies on the executable bit 72 # being set, which doesn't work properly in fakefs. 73 with tempfile.TemporaryDirectory() as tmp_dir, mock.patch( 74 'get_coverage.OSSFuzzCoverage.get_files_covered_by_target' 75 ) as mock_get_files: 76 with mock.patch('get_coverage._get_oss_fuzz_fuzzer_stats_dir_url', 77 return_value=1): 78 mock_get_files.side_effect = side_effect 79 shutil.copy(self.TEST_FUZZER_1, tmp_dir) 80 shutil.copy(self.TEST_FUZZER_2, tmp_dir) 81 82 affected_fuzz_targets.remove_unaffected_fuzz_targets( 83 deployment, tmp_dir, [EXAMPLE_FILE_CHANGED], '') 84 self.assertEqual(expected_dir_len, len(os.listdir(tmp_dir))) 85 86 87if __name__ == '__main__': 88 unittest.main() 89