2020-01-29 19:03:43 +00:00
|
|
|
# Copyright 2020 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.
|
2021-01-26 16:32:41 +00:00
|
|
|
"""Tests the functionality of the cifuzz module."""
|
2020-01-29 19:03:43 +00:00
|
|
|
import os
|
2021-02-04 16:23:41 +00:00
|
|
|
import shutil
|
2020-01-29 19:03:43 +00:00
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
2020-05-13 19:44:11 +00:00
|
|
|
from unittest import mock
|
2020-01-29 19:03:43 +00:00
|
|
|
|
2021-01-28 20:10:57 +00:00
|
|
|
import parameterized
|
|
|
|
|
2020-01-29 19:03:43 +00:00
|
|
|
# pylint: disable=wrong-import-position
|
2020-06-17 17:32:42 +00:00
|
|
|
INFRA_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.append(INFRA_DIR)
|
|
|
|
|
|
|
|
OSS_FUZZ_DIR = os.path.dirname(INFRA_DIR)
|
|
|
|
|
2021-02-04 14:52:22 +00:00
|
|
|
import build_fuzzers
|
2021-01-28 20:10:57 +00:00
|
|
|
import continuous_integration
|
2021-05-24 21:22:01 +00:00
|
|
|
import repo_manager
|
2020-12-07 18:50:11 +00:00
|
|
|
import test_helpers
|
2020-01-29 19:03:43 +00:00
|
|
|
|
|
|
|
# NOTE: This integration test relies on
|
2020-02-21 16:47:13 +00:00
|
|
|
# https://github.com/google/oss-fuzz/tree/master/projects/example project.
|
2020-01-29 19:03:43 +00:00
|
|
|
EXAMPLE_PROJECT = 'example'
|
|
|
|
|
2021-03-19 18:49:38 +00:00
|
|
|
# Location of data used for testing.
|
|
|
|
TEST_DATA_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'test_data')
|
2020-02-21 16:47:13 +00:00
|
|
|
|
2020-03-04 22:58:09 +00:00
|
|
|
# An example fuzzer that triggers an crash.
|
|
|
|
# Binary is a copy of the example project's do_stuff_fuzzer and can be
|
|
|
|
# generated by running "python3 infra/helper.py build_fuzzers example".
|
|
|
|
EXAMPLE_CRASH_FUZZER = 'example_crash_fuzzer'
|
|
|
|
|
|
|
|
# An example fuzzer that does not trigger a crash.
|
|
|
|
# Binary is a modified version of example project's do_stuff_fuzzer. It is
|
|
|
|
# created by removing the bug in my_api.cpp.
|
|
|
|
EXAMPLE_NOCRASH_FUZZER = 'example_nocrash_fuzzer'
|
|
|
|
|
|
|
|
# A fuzzer to be built in build_fuzzers integration tests.
|
|
|
|
EXAMPLE_BUILD_FUZZER = 'do_stuff_fuzzer'
|
2020-02-21 16:47:13 +00:00
|
|
|
|
2021-08-17 06:36:06 +00:00
|
|
|
# pylint: disable=no-self-use,protected-access,too-few-public-methods,unused-argument
|
2021-01-28 20:10:57 +00:00
|
|
|
|
|
|
|
|
2022-12-12 16:00:19 +00:00
|
|
|
def docker_command_has_env_var_arg(command, env_var_arg):
|
|
|
|
"""Returns True if a docker command has a specific env var argument."""
|
|
|
|
for idx, element in enumerate(command):
|
|
|
|
if idx == 0:
|
|
|
|
# We're looking for the variable which can't be the first argument.
|
|
|
|
continue
|
|
|
|
|
|
|
|
if element == env_var_arg and command[idx - 1] == '-e':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-11-24 16:10:05 +00:00
|
|
|
class BuildFuzzersTest(unittest.TestCase):
|
|
|
|
"""Unit tests for build_fuzzers."""
|
|
|
|
|
|
|
|
@mock.patch('build_specified_commit.detect_main_repo',
|
|
|
|
return_value=('example.com', '/path'))
|
2020-12-07 18:50:11 +00:00
|
|
|
@mock.patch('repo_manager._clone', return_value=None)
|
2021-01-28 20:10:57 +00:00
|
|
|
@mock.patch('continuous_integration.checkout_specified_commit')
|
2021-04-21 14:28:26 +00:00
|
|
|
@mock.patch('helper.docker_run', return_value=False) # We want to quit early.
|
2021-08-12 18:25:57 +00:00
|
|
|
def test_cifuzz_env_var(self, mock_docker_run, _, __, ___):
|
2020-11-24 16:10:05 +00:00
|
|
|
"""Tests that the CIFUZZ env var is set."""
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
2021-02-04 14:52:22 +00:00
|
|
|
build_fuzzers.build_fuzzers(
|
2021-07-21 05:32:32 +00:00
|
|
|
test_helpers.create_build_config(
|
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
|
|
|
project_repo_name=EXAMPLE_PROJECT,
|
|
|
|
workspace=tmp_dir,
|
|
|
|
pr_ref='refs/pull/1757/merge'))
|
2021-06-23 14:30:11 +00:00
|
|
|
|
2021-08-12 18:25:57 +00:00
|
|
|
docker_run_command = mock_docker_run.call_args_list[0][0][0]
|
2020-11-24 16:10:05 +00:00
|
|
|
|
2022-12-12 16:00:19 +00:00
|
|
|
self.assertTrue(
|
|
|
|
docker_command_has_env_var_arg(docker_run_command, 'CIFUZZ=True'))
|
2020-11-24 16:10:05 +00:00
|
|
|
|
2022-12-12 16:00:19 +00:00
|
|
|
@mock.patch('build_specified_commit.detect_main_repo',
|
|
|
|
return_value=('example.com', '/path'))
|
|
|
|
@mock.patch('repo_manager._clone', return_value=None)
|
|
|
|
@mock.patch('continuous_integration.checkout_specified_commit')
|
|
|
|
@mock.patch('helper.docker_run', return_value=False) # We want to quit early.
|
|
|
|
def test_extra_env_var(self, mock_docker_run, _, __, ___):
|
|
|
|
"""Tests that the CIFUZZ env var is set."""
|
2020-11-24 16:10:05 +00:00
|
|
|
|
2022-12-12 16:00:19 +00:00
|
|
|
extra_env_var = 'CFL_EXTRA_TOKEN'
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
|
|
with mock.patch.dict(os.environ, {extra_env_var: 'BLAH'}):
|
|
|
|
build_fuzzers.build_fuzzers(
|
|
|
|
test_helpers.create_build_config(
|
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
|
|
|
project_repo_name=EXAMPLE_PROJECT,
|
|
|
|
workspace=tmp_dir,
|
|
|
|
pr_ref='refs/pull/1757/merge'))
|
|
|
|
|
|
|
|
docker_run_command = mock_docker_run.call_args_list[0][0][0]
|
|
|
|
self.assertTrue(
|
|
|
|
docker_command_has_env_var_arg(docker_run_command, extra_env_var))
|
2020-11-24 16:10:05 +00:00
|
|
|
|
|
|
|
|
2021-01-28 20:10:57 +00:00
|
|
|
class InternalGithubBuildTest(unittest.TestCase):
|
2020-12-09 17:55:54 +00:00
|
|
|
"""Tests for building OSS-Fuzz projects on GitHub actions."""
|
|
|
|
PROJECT_REPO_NAME = 'myproject'
|
|
|
|
SANITIZER = 'address'
|
2021-11-01 00:36:07 +00:00
|
|
|
GIT_SHA = 'fake'
|
2020-12-09 17:55:54 +00:00
|
|
|
PR_REF = 'fake'
|
|
|
|
|
2021-08-17 06:36:06 +00:00
|
|
|
def _create_builder(self, tmp_dir, oss_fuzz_project_name='myproject'):
|
2020-12-09 17:55:54 +00:00
|
|
|
"""Creates an InternalGithubBuilder and returns it."""
|
2021-05-26 16:45:22 +00:00
|
|
|
config = test_helpers.create_build_config(
|
2021-08-17 06:36:06 +00:00
|
|
|
oss_fuzz_project_name=oss_fuzz_project_name,
|
2021-05-26 16:45:22 +00:00
|
|
|
project_repo_name=self.PROJECT_REPO_NAME,
|
|
|
|
workspace=tmp_dir,
|
|
|
|
sanitizer=self.SANITIZER,
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha=self.GIT_SHA,
|
2021-05-26 16:45:22 +00:00
|
|
|
pr_ref=self.PR_REF,
|
2021-11-01 12:29:38 +00:00
|
|
|
cfl_platform='github')
|
|
|
|
cfl_platform = continuous_integration.get_ci(config)
|
|
|
|
builder = build_fuzzers.Builder(config, cfl_platform)
|
2021-08-17 06:36:06 +00:00
|
|
|
builder.repo_manager = repo_manager.RepoManager('/fake')
|
|
|
|
return builder
|
2020-12-09 17:55:54 +00:00
|
|
|
|
2021-12-01 14:36:08 +00:00
|
|
|
@mock.patch('helper.docker_run', return_value=True)
|
2021-01-28 20:10:57 +00:00
|
|
|
@mock.patch('continuous_integration.checkout_specified_commit',
|
|
|
|
side_effect=None)
|
2020-12-09 17:55:54 +00:00
|
|
|
def test_correct_host_repo_path(self, _, __):
|
|
|
|
"""Tests that the correct self.host_repo_path is set by
|
|
|
|
build_image_and_checkout_src. Specifically, we want the name of the
|
|
|
|
directory the repo is in to match the name used in the docker
|
|
|
|
image/container, so that it will replace the host's copy properly."""
|
|
|
|
image_repo_path = '/src/repo_dir'
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir, mock.patch(
|
|
|
|
'build_specified_commit.detect_main_repo',
|
|
|
|
return_value=('inferred_url', image_repo_path)):
|
|
|
|
builder = self._create_builder(tmp_dir)
|
|
|
|
builder.build_image_and_checkout_src()
|
|
|
|
|
|
|
|
self.assertEqual(os.path.basename(builder.host_repo_path),
|
|
|
|
os.path.basename(image_repo_path))
|
|
|
|
|
2021-08-17 06:36:06 +00:00
|
|
|
@mock.patch('clusterfuzz_deployment.ClusterFuzzLite.upload_build',
|
|
|
|
return_value=True)
|
|
|
|
def test_upload_build_disabled(self, mock_upload_build):
|
|
|
|
"""Test upload build (disabled)."""
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
|
|
builder = self._create_builder(tmp_dir)
|
|
|
|
builder.upload_build()
|
|
|
|
|
|
|
|
mock_upload_build.assert_not_called()
|
|
|
|
|
|
|
|
@mock.patch('repo_manager.RepoManager.get_current_commit',
|
|
|
|
return_value='commit')
|
|
|
|
@mock.patch('clusterfuzz_deployment.ClusterFuzzLite.upload_build',
|
|
|
|
return_value=True)
|
|
|
|
def test_upload_build(self, mock_upload_build, mock_get_current_commit):
|
|
|
|
"""Test upload build."""
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
|
|
builder = self._create_builder(tmp_dir, oss_fuzz_project_name='')
|
|
|
|
builder.config.upload_build = True
|
|
|
|
builder.upload_build()
|
|
|
|
|
|
|
|
mock_upload_build.assert_called_with('commit')
|
|
|
|
|
2020-12-09 17:55:54 +00:00
|
|
|
|
2020-12-18 18:37:56 +00:00
|
|
|
@unittest.skipIf(not os.getenv('INTEGRATION_TESTS'),
|
|
|
|
'INTEGRATION_TESTS=1 not set')
|
2020-01-29 19:03:43 +00:00
|
|
|
class BuildFuzzersIntegrationTest(unittest.TestCase):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Integration tests for build_fuzzers."""
|
2020-01-29 19:03:43 +00:00
|
|
|
|
2020-12-07 18:50:11 +00:00
|
|
|
def setUp(self):
|
2021-12-01 14:36:08 +00:00
|
|
|
self.temp_dir_ctx_manager = test_helpers.docker_temp_dir()
|
|
|
|
self.workspace = self.temp_dir_ctx_manager.__enter__()
|
2021-06-30 14:34:42 +00:00
|
|
|
self.out_dir = os.path.join(self.workspace, 'build-out')
|
2020-12-07 18:50:11 +00:00
|
|
|
test_helpers.patch_environ(self)
|
|
|
|
|
2021-10-04 15:21:28 +00:00
|
|
|
base_runner_path = os.path.join(INFRA_DIR, 'base-images', 'base-runner')
|
|
|
|
os.environ['PATH'] = os.environ['PATH'] + os.pathsep + base_runner_path
|
|
|
|
|
2021-02-04 17:07:45 +00:00
|
|
|
def tearDown(self):
|
2021-12-01 14:36:08 +00:00
|
|
|
self.temp_dir_ctx_manager.__exit__(None, None, None)
|
2021-02-04 17:07:45 +00:00
|
|
|
|
2021-01-28 20:10:57 +00:00
|
|
|
def test_external_github_project(self):
|
|
|
|
"""Tests building fuzzers from an external project on Github."""
|
2021-07-21 16:18:44 +00:00
|
|
|
project_repo_name = 'external-project'
|
2021-01-28 20:10:57 +00:00
|
|
|
git_url = 'https://github.com/jonathanmetzman/cifuzz-external-example.git'
|
2021-02-04 17:07:45 +00:00
|
|
|
# This test is dependant on the state of
|
|
|
|
# github.com/jonathanmetzman/cifuzz-external-example.
|
2021-05-26 16:45:22 +00:00
|
|
|
config = test_helpers.create_build_config(
|
2021-07-21 16:18:44 +00:00
|
|
|
project_repo_name=project_repo_name,
|
2021-05-26 16:45:22 +00:00
|
|
|
workspace=self.workspace,
|
|
|
|
git_url=git_url,
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha='HEAD',
|
2021-11-01 12:29:38 +00:00
|
|
|
cfl_platform='github',
|
2021-05-26 16:45:22 +00:00
|
|
|
base_commit='HEAD^1')
|
2021-05-24 21:22:01 +00:00
|
|
|
self.assertTrue(build_fuzzers.build_fuzzers(config))
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(os.path.join(self.out_dir, EXAMPLE_BUILD_FUZZER)))
|
|
|
|
|
|
|
|
def test_external_generic_project(self):
|
|
|
|
"""Tests building fuzzers from an external project not on Github."""
|
2021-07-21 16:18:44 +00:00
|
|
|
project_repo_name = 'cifuzz-external-example'
|
2021-05-24 21:22:01 +00:00
|
|
|
git_url = 'https://github.com/jonathanmetzman/cifuzz-external-example.git'
|
|
|
|
# This test is dependant on the state of
|
|
|
|
# github.com/jonathanmetzman/cifuzz-external-example.
|
|
|
|
manager = repo_manager.clone_repo_and_get_manager(
|
|
|
|
'https://github.com/jonathanmetzman/cifuzz-external-example',
|
2021-12-01 14:36:08 +00:00
|
|
|
self.workspace)
|
2021-05-24 21:22:01 +00:00
|
|
|
project_src_path = manager.repo_dir
|
2021-05-26 16:45:22 +00:00
|
|
|
config = test_helpers.create_build_config(
|
2021-07-21 16:18:44 +00:00
|
|
|
project_repo_name=project_repo_name,
|
2021-05-26 16:45:22 +00:00
|
|
|
workspace=self.workspace,
|
|
|
|
git_url=git_url,
|
2021-10-27 14:00:04 +00:00
|
|
|
filestore='no_filestore',
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha='HEAD',
|
2021-05-26 16:45:22 +00:00
|
|
|
project_src_path=project_src_path,
|
|
|
|
base_commit='HEAD^1')
|
2021-02-04 17:07:45 +00:00
|
|
|
self.assertTrue(build_fuzzers.build_fuzzers(config))
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(os.path.join(self.out_dir, EXAMPLE_BUILD_FUZZER)))
|
2020-12-07 18:50:11 +00:00
|
|
|
|
2020-01-31 23:31:18 +00:00
|
|
|
def test_valid_commit(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests building fuzzers with valid inputs."""
|
2021-05-26 16:45:22 +00:00
|
|
|
config = test_helpers.create_build_config(
|
2021-07-21 05:32:32 +00:00
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
2021-02-04 17:07:45 +00:00
|
|
|
project_repo_name='oss-fuzz',
|
|
|
|
workspace=self.workspace,
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha='0b95fe1039ed7c38fea1f97078316bfc1030c523',
|
2021-02-04 17:07:45 +00:00
|
|
|
base_commit='da0746452433dc18bae699e355a9821285d863c8',
|
2021-11-01 12:29:38 +00:00
|
|
|
cfl_platform='github')
|
2021-02-04 17:07:45 +00:00
|
|
|
self.assertTrue(build_fuzzers.build_fuzzers(config))
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(os.path.join(self.out_dir, EXAMPLE_BUILD_FUZZER)))
|
2020-01-29 19:03:43 +00:00
|
|
|
|
2020-01-31 23:31:18 +00:00
|
|
|
def test_valid_pull_request(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests building fuzzers with valid pull request."""
|
2021-07-21 05:32:32 +00:00
|
|
|
config = test_helpers.create_build_config(
|
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
|
|
|
project_repo_name='oss-fuzz',
|
|
|
|
workspace=self.workspace,
|
|
|
|
pr_ref='refs/pull/1757/merge',
|
|
|
|
base_ref='master',
|
2021-11-01 12:29:38 +00:00
|
|
|
cfl_platform='github')
|
2021-02-04 17:07:45 +00:00
|
|
|
self.assertTrue(build_fuzzers.build_fuzzers(config))
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(os.path.join(self.out_dir, EXAMPLE_BUILD_FUZZER)))
|
2020-01-29 19:03:43 +00:00
|
|
|
|
2020-01-31 23:31:18 +00:00
|
|
|
def test_invalid_pull_request(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests building fuzzers with invalid pull request."""
|
2021-07-21 05:32:32 +00:00
|
|
|
config = test_helpers.create_build_config(
|
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
|
|
|
project_repo_name='oss-fuzz',
|
|
|
|
workspace=self.workspace,
|
|
|
|
pr_ref='ref-1/merge',
|
|
|
|
base_ref='master',
|
2021-11-01 12:29:38 +00:00
|
|
|
cfl_platform='github')
|
2021-02-04 17:07:45 +00:00
|
|
|
self.assertTrue(build_fuzzers.build_fuzzers(config))
|
2020-01-31 23:31:18 +00:00
|
|
|
|
2021-07-21 05:32:32 +00:00
|
|
|
def test_invalid_oss_fuzz_project_name(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests building fuzzers with invalid project name."""
|
2021-05-26 16:45:22 +00:00
|
|
|
config = test_helpers.create_build_config(
|
2021-07-21 05:32:32 +00:00
|
|
|
oss_fuzz_project_name='not_a_valid_project',
|
2021-02-04 17:07:45 +00:00
|
|
|
project_repo_name='oss-fuzz',
|
|
|
|
workspace=self.workspace,
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha='0b95fe1039ed7c38fea1f97078316bfc1030c523')
|
2021-02-04 17:07:45 +00:00
|
|
|
self.assertFalse(build_fuzzers.build_fuzzers(config))
|
2020-01-31 23:31:18 +00:00
|
|
|
|
|
|
|
def test_invalid_repo_name(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests building fuzzers with invalid repo name."""
|
2021-05-26 16:45:22 +00:00
|
|
|
config = test_helpers.create_build_config(
|
2021-07-21 05:32:32 +00:00
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
2021-02-04 17:07:45 +00:00
|
|
|
project_repo_name='not-real-repo',
|
|
|
|
workspace=self.workspace,
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha='0b95fe1039ed7c38fea1f97078316bfc1030c523')
|
2021-02-04 17:07:45 +00:00
|
|
|
self.assertFalse(build_fuzzers.build_fuzzers(config))
|
2020-01-31 23:31:18 +00:00
|
|
|
|
2021-11-01 00:36:07 +00:00
|
|
|
def test_invalid_git_sha(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests building fuzzers with invalid commit SHA."""
|
2021-07-21 05:32:32 +00:00
|
|
|
config = test_helpers.create_build_config(
|
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
|
|
|
project_repo_name='oss-fuzz',
|
|
|
|
workspace=self.workspace,
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha='',
|
2021-11-01 12:29:38 +00:00
|
|
|
cfl_platform='github')
|
2021-02-04 17:07:45 +00:00
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
build_fuzzers.build_fuzzers(config)
|
2020-01-31 23:31:18 +00:00
|
|
|
|
|
|
|
def test_invalid_workspace(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests building fuzzers with invalid workspace."""
|
2021-05-26 16:45:22 +00:00
|
|
|
config = test_helpers.create_build_config(
|
2021-07-21 05:32:32 +00:00
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
2021-01-28 20:10:57 +00:00
|
|
|
project_repo_name='oss-fuzz',
|
2021-02-04 17:07:45 +00:00
|
|
|
workspace=os.path.join(self.workspace, 'not', 'a', 'dir'),
|
2021-11-01 00:36:07 +00:00
|
|
|
git_sha='0b95fe1039ed7c38fea1f97078316bfc1030c523')
|
2021-02-04 14:52:22 +00:00
|
|
|
self.assertFalse(build_fuzzers.build_fuzzers(config))
|
2020-01-29 19:03:43 +00:00
|
|
|
|
|
|
|
|
2020-11-17 21:39:57 +00:00
|
|
|
class CheckFuzzerBuildTest(unittest.TestCase):
|
2020-02-27 16:54:52 +00:00
|
|
|
"""Tests the check_fuzzer_build function in the cifuzz module."""
|
|
|
|
|
2021-02-19 19:54:15 +00:00
|
|
|
SANITIZER = 'address'
|
|
|
|
LANGUAGE = 'c++'
|
|
|
|
|
2021-02-04 16:23:41 +00:00
|
|
|
def setUp(self):
|
2021-07-21 17:19:32 +00:00
|
|
|
self.temp_dir_obj = tempfile.TemporaryDirectory()
|
|
|
|
workspace_path = os.path.join(self.temp_dir_obj.name, 'workspace')
|
2021-08-05 20:27:24 +00:00
|
|
|
self.config = test_helpers.create_build_config(
|
|
|
|
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
|
|
|
sanitizer=self.SANITIZER,
|
|
|
|
language=self.LANGUAGE,
|
|
|
|
workspace=workspace_path,
|
|
|
|
pr_ref='refs/pull/1757/merge')
|
2021-06-30 14:34:42 +00:00
|
|
|
self.workspace = test_helpers.create_workspace(workspace_path)
|
|
|
|
shutil.copytree(TEST_DATA_PATH, workspace_path)
|
2021-08-05 20:27:24 +00:00
|
|
|
test_helpers.patch_environ(self, runner=True)
|
2021-02-04 16:23:41 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2021-07-21 17:19:32 +00:00
|
|
|
self.temp_dir_obj.cleanup()
|
2021-02-04 16:23:41 +00:00
|
|
|
|
2020-02-27 16:54:52 +00:00
|
|
|
def test_correct_fuzzer_build(self):
|
|
|
|
"""Checks check_fuzzer_build function returns True for valid fuzzers."""
|
2021-08-05 20:27:24 +00:00
|
|
|
self.assertTrue(build_fuzzers.check_fuzzer_build(self.config))
|
2020-02-27 16:54:52 +00:00
|
|
|
|
2021-06-30 14:34:42 +00:00
|
|
|
def test_not_a_valid_path(self):
|
|
|
|
"""Tests that False is returned when a nonexistent path is given."""
|
2021-08-05 20:27:24 +00:00
|
|
|
self.config.workspace = 'not/a/valid/path'
|
|
|
|
self.assertFalse(build_fuzzers.check_fuzzer_build(self.config))
|
2020-02-27 16:54:52 +00:00
|
|
|
|
2021-06-30 14:34:42 +00:00
|
|
|
def test_no_valid_fuzzers(self):
|
|
|
|
"""Tests that False is returned when an empty directory is given."""
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
2021-08-05 20:27:24 +00:00
|
|
|
self.config.workspace = tmp_dir
|
|
|
|
os.mkdir(os.path.join(self.config.workspace, 'build-out'))
|
|
|
|
self.assertFalse(build_fuzzers.check_fuzzer_build(self.config))
|
2020-02-27 16:54:52 +00:00
|
|
|
|
2021-08-05 20:27:24 +00:00
|
|
|
@mock.patch('utils.execute', return_value=(None, None, 0))
|
2021-08-12 18:25:57 +00:00
|
|
|
def test_allow_broken_fuzz_targets_percentage(self, mock_execute):
|
2020-05-13 19:44:11 +00:00
|
|
|
"""Tests that ALLOWED_BROKEN_TARGETS_PERCENTAGE is set when running
|
2020-12-07 18:50:11 +00:00
|
|
|
docker if passed to check_fuzzer_build."""
|
2021-08-05 20:27:24 +00:00
|
|
|
percentage = '0'
|
|
|
|
self.config.allowed_broken_targets_percentage = percentage
|
|
|
|
build_fuzzers.check_fuzzer_build(self.config)
|
|
|
|
self.assertEqual(
|
2021-08-12 18:25:57 +00:00
|
|
|
mock_execute.call_args[1]['env']['ALLOWED_BROKEN_TARGETS_PERCENTAGE'],
|
2021-08-05 20:27:24 +00:00
|
|
|
percentage)
|
2020-05-13 19:44:11 +00:00
|
|
|
|
2020-02-27 16:54:52 +00:00
|
|
|
|
2020-06-12 01:27:01 +00:00
|
|
|
@unittest.skip('Test is too long to be run with presubmit.')
|
|
|
|
class BuildSantizerIntegrationTest(unittest.TestCase):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Integration tests for the build_fuzzers.
|
|
|
|
Note: This test relies on "curl" being an OSS-Fuzz project."""
|
2021-01-28 20:10:57 +00:00
|
|
|
PROJECT_NAME = 'curl'
|
|
|
|
PR_REF = 'fake_pr'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _create_config(cls, tmp_dir, sanitizer):
|
2021-07-21 05:32:32 +00:00
|
|
|
return test_helpers.create_build_config(
|
|
|
|
oss_fuzz_project_name=cls.PROJECT_NAME,
|
|
|
|
project_repo_name=cls.PROJECT_NAME,
|
|
|
|
workspace=tmp_dir,
|
|
|
|
pr_ref=cls.PR_REF,
|
|
|
|
sanitizer=sanitizer)
|
2021-01-28 20:10:57 +00:00
|
|
|
|
|
|
|
@parameterized.parameterized.expand([('memory',), ('undefined',)])
|
|
|
|
def test_valid_project_curl(self, sanitizer):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests that MSAN can be detected from project.yaml"""
|
2020-06-12 01:27:01 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
|
|
self.assertTrue(
|
2021-02-04 14:52:22 +00:00
|
|
|
build_fuzzers.build_fuzzers(self._create_config(tmp_dir, sanitizer)))
|
2021-01-28 20:10:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetDockerBuildFuzzersArgsNotContainerTest(unittest.TestCase):
|
|
|
|
"""Tests that _get_docker_build_fuzzers_args_not_container works as
|
|
|
|
intended."""
|
|
|
|
|
|
|
|
def test_get_docker_build_fuzzers_args_no_container(self):
|
|
|
|
"""Tests that _get_docker_build_fuzzers_args_not_container works
|
|
|
|
as intended."""
|
|
|
|
host_repo_path = '/host/repo'
|
2021-02-04 14:52:22 +00:00
|
|
|
result = build_fuzzers._get_docker_build_fuzzers_args_not_container(
|
2021-06-23 14:30:11 +00:00
|
|
|
host_repo_path)
|
|
|
|
expected_result = ['-v', '/host/repo:/host/repo']
|
2021-01-28 20:10:57 +00:00
|
|
|
self.assertEqual(result, expected_result)
|
|
|
|
|
|
|
|
|
2020-01-29 19:03:43 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|