From 2800e4eefa919f2f59baec7b8c7cf0784702d226 Mon Sep 17 00:00:00 2001 From: Jonathan Metzman Date: Wed, 20 Jan 2021 10:31:03 -0800 Subject: [PATCH] Add test --- infra/cifuzz/affected_fuzz_targets.py | 2 +- infra/cifuzz/affected_fuzz_targets_test.py | 74 ++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 infra/cifuzz/affected_fuzz_targets_test.py diff --git a/infra/cifuzz/affected_fuzz_targets.py b/infra/cifuzz/affected_fuzz_targets.py index b9468f4a6..e8c42a79e 100644 --- a/infra/cifuzz/affected_fuzz_targets.py +++ b/infra/cifuzz/affected_fuzz_targets.py @@ -79,7 +79,7 @@ def remove_unaffected_fuzz_targets(project_name, out_dir, files_changed, def is_fuzz_target_affected(coverage_getter, fuzz_target_path, files_changed): - """Returns True if a fuzz target (|fuzz_target_path| is affected by + """Returns True if a fuzz target (|fuzz_target_path|) is affected by |files_changed|.""" fuzz_target = os.path.basename(fuzz_target_path) covered_files = coverage_getter.get_files_covered_by_target(fuzz_target) diff --git a/infra/cifuzz/affected_fuzz_targets_test.py b/infra/cifuzz/affected_fuzz_targets_test.py new file mode 100644 index 000000000..3d1863d15 --- /dev/null +++ b/infra/cifuzz/affected_fuzz_targets_test.py @@ -0,0 +1,74 @@ +# 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 + +# 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' + +TEST_FILES_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), + 'test_files') + +class RemoveUnaffectedFuzzTargets(unittest.TestCase): + """Tests remove_unaffected_fuzzers.""" + + TEST_FUZZER_1 = os.path.join(TEST_FILES_PATH, 'out', 'example_crash_fuzzer') + TEST_FUZZER_2 = os.path.join(TEST_FILES_PATH, 'out', 'example_nocrash_fuzzer') + + # 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.""" + with tempfile.TemporaryDirectory() as tmp_dir, mock.patch( + 'coverage._get_fuzzer_stats_dir_url', return_value=1): + with mock.patch( + 'coverage.OssFuzzCoverageGetter.get_files_covered_by_target' + ) as mocked_get_files: + mocked_get_files.side_effect = side_effect + shutil.copy(self.TEST_FUZZER_1, tmp_dir) + shutil.copy(self.TEST_FUZZER_2, tmp_dir) + affected_fuzz_targets.remove_unaffected_fuzz_targets( + EXAMPLE_PROJECT, tmp_dir, + [EXAMPLE_FILE_CHANGED], '') + self.assertEqual(expected_dir_len, len(os.listdir(tmp_dir))) + + +if __name__ == '__main__': + unittest.main()