• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
24
25# pylint: disable=protected-access
26
27# NOTE: This integration test relies on
28# https://github.com/google/oss-fuzz/tree/master/projects/example project.
29EXAMPLE_PROJECT = 'example'
30
31EXAMPLE_FILE_CHANGED = 'test.txt'
32
33TEST_DATA_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
34                              'test_data')
35
36
37class RemoveUnaffectedFuzzTargets(unittest.TestCase):
38  """Tests remove_unaffected_fuzzers."""
39
40  TEST_FUZZER_1 = os.path.join(TEST_DATA_PATH, 'out', 'example_crash_fuzzer')
41  TEST_FUZZER_2 = os.path.join(TEST_DATA_PATH, 'out', 'example_nocrash_fuzzer')
42
43  # yapf: disable
44  @parameterized.parameterized.expand([
45      # Tests a specific affected fuzzers is kept.
46      ([[EXAMPLE_FILE_CHANGED], None], 2,),
47
48      # Tests specific affected fuzzer is kept.
49      ([[EXAMPLE_FILE_CHANGED], ['not/a/real/file']], 1),
50
51      # Tests all fuzzers are kept if none are deemed affected.
52      ([None, None], 2),
53
54      # Tests that multiple fuzzers are kept if multiple fuzzers are affected.
55      ([[EXAMPLE_FILE_CHANGED], [EXAMPLE_FILE_CHANGED]], 2),
56      ])
57  # yapf: enable
58  def test_remove_unaffected_fuzz_targets(self, side_effect, expected_dir_len):
59    """Tests that remove_unaffected_fuzzers has the intended effect."""
60    # We can't use fakefs in this test because this test executes
61    # utils.is_fuzz_target_local. This function relies on the executable bit
62    # being set, which doesn't work properly in fakefs.
63    with tempfile.TemporaryDirectory() as tmp_dir, mock.patch(
64        'coverage.OssFuzzCoverageGetter.get_files_covered_by_target'
65    ) as mocked_get_files:
66      with mock.patch('coverage._get_fuzzer_stats_dir_url', return_value=1):
67        mocked_get_files.side_effect = side_effect
68        shutil.copy(self.TEST_FUZZER_1, tmp_dir)
69        shutil.copy(self.TEST_FUZZER_2, tmp_dir)
70        affected_fuzz_targets.remove_unaffected_fuzz_targets(
71            EXAMPLE_PROJECT, tmp_dir, [EXAMPLE_FILE_CHANGED], '')
72        self.assertEqual(expected_dir_len, len(os.listdir(tmp_dir)))
73
74
75if __name__ == '__main__':
76  unittest.main()
77