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 generate_coverage_report.""" 15 16import unittest 17from unittest import mock 18 19import generate_coverage_report 20import test_helpers 21 22OUT_DIR = '/outdir' 23PROJECT = 'example-project' 24SANITIZER = 'coverage' 25 26 27class TestRunCoverageCommand(unittest.TestCase): 28 """Tests run_coverage_command""" 29 30 def setUp(self): 31 test_helpers.patch_environ(self, empty=True) 32 33 @mock.patch('utils.execute') 34 def test_run_coverage_command(self, mock_execute): # pylint: disable=no-self-use 35 """Tests that run_coverage_command works as intended.""" 36 config = test_helpers.create_run_config(oss_fuzz_project_name=PROJECT, 37 sanitizer=SANITIZER) 38 workspace = test_helpers.create_workspace() 39 generate_coverage_report.run_coverage_command(config, workspace) 40 expected_command = 'coverage' 41 expected_env = { 42 'SANITIZER': config.sanitizer, 43 'FUZZING_LANGUAGE': config.language, 44 'OUT': workspace.out, 45 'CIFUZZ': 'True', 46 'FUZZING_ENGINE': 'libfuzzer', 47 'ARCHITECTURE': 'x86_64', 48 'FUZZER_ARGS': '-rss_limit_mb=2560 -timeout=25', 49 'HTTP_PORT': '', 50 'COVERAGE_EXTRA_ARGS': '', 51 'CORPUS_DIR': workspace.corpora, 52 'COVERAGE_OUTPUT_DIR': workspace.coverage_report 53 } 54 mock_execute.assert_called_with(expected_command, env=expected_env) 55 56 57class DownloadCorporaTest(unittest.TestCase): 58 """Tests for download_corpora.""" 59 60 def test_download_corpora(self): # pylint: disable=no-self-use 61 """Tests that download_corpora works as intended.""" 62 clusterfuzz_deployment = mock.Mock() 63 clusterfuzz_deployment.workspace = test_helpers.create_workspace() 64 fuzz_target_paths = ['/path/to/fuzzer1', '/path/to/fuzzer2'] 65 expected_calls = [ 66 mock.call('fuzzer1', '/workspace/cifuzz-corpus/fuzzer1'), 67 mock.call('fuzzer2', '/workspace/cifuzz-corpus/fuzzer2') 68 ] 69 generate_coverage_report.download_corpora(fuzz_target_paths, 70 clusterfuzz_deployment) 71 clusterfuzz_deployment.download_corpus.assert_has_calls(expected_calls) 72