2019-12-12 17:26:37 +00:00
|
|
|
# Copyright 2019 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.
|
2020-01-06 20:17:26 +00:00
|
|
|
"""Test the functionality of the build image from commit module.
|
2020-01-15 21:30:57 +00:00
|
|
|
The will consist of the following functional tests:
|
2020-01-30 18:27:56 +00:00
|
|
|
1. The inference of the main repo for a specific project.
|
|
|
|
2. The building of a projects fuzzers from a specific commit.
|
|
|
|
|
|
|
|
IMPORTANT: This test needs to be run with root privileges.
|
2019-12-12 17:26:37 +00:00
|
|
|
"""
|
2020-01-06 20:17:26 +00:00
|
|
|
import os
|
|
|
|
import tempfile
|
2019-12-12 17:26:37 +00:00
|
|
|
import unittest
|
|
|
|
|
2019-12-17 00:48:49 +00:00
|
|
|
import build_specified_commit
|
|
|
|
import helper
|
2020-01-15 21:30:57 +00:00
|
|
|
import repo_manager
|
2020-01-30 18:27:56 +00:00
|
|
|
import test_repos
|
2019-12-12 17:26:37 +00:00
|
|
|
|
2020-01-06 20:17:26 +00:00
|
|
|
# Necessary because __file__ changes with os.chdir
|
|
|
|
TEST_DIR_PATH = os.path.dirname(os.path.realpath(__file__))
|
2019-12-12 17:26:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BuildImageIntegrationTests(unittest.TestCase):
|
2020-01-15 21:30:57 +00:00
|
|
|
"""Testing if an image can be built from different states e.g. a commit."""
|
2019-12-12 17:26:37 +00:00
|
|
|
|
|
|
|
def test_build_fuzzers_from_commit(self):
|
|
|
|
"""Tests if the fuzzers can build at a proper commit.
|
|
|
|
|
|
|
|
This is done by using a known regression range for a specific test case.
|
|
|
|
The old commit should show the error when its fuzzers run and the new one
|
|
|
|
should not.
|
|
|
|
"""
|
2020-01-06 20:17:26 +00:00
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
2020-01-30 18:27:56 +00:00
|
|
|
test_case = test_repos.TEST_REPOS[0]
|
2020-04-14 01:38:23 +00:00
|
|
|
self.assertTrue(helper.build_image_impl(test_case.project_name))
|
|
|
|
host_src_dir = build_specified_commit.copy_src_from_docker(
|
|
|
|
test_case.project_name, tmp_dir)
|
|
|
|
|
2020-01-30 18:27:56 +00:00
|
|
|
test_repo_manager = repo_manager.RepoManager(
|
2020-04-14 01:38:23 +00:00
|
|
|
test_case.git_url, host_src_dir, repo_name=test_case.oss_repo_name)
|
2020-01-30 18:27:56 +00:00
|
|
|
build_data = build_specified_commit.BuildData(
|
|
|
|
sanitizer='address',
|
|
|
|
architecture='x86_64',
|
|
|
|
engine='libfuzzer',
|
|
|
|
project_name=test_case.project_name)
|
|
|
|
|
|
|
|
build_specified_commit.build_fuzzers_from_commit(test_case.old_commit,
|
|
|
|
test_repo_manager,
|
2020-04-14 01:38:23 +00:00
|
|
|
host_src_dir, build_data)
|
2020-01-30 18:27:56 +00:00
|
|
|
old_error_code = helper.reproduce_impl(test_case.project_name,
|
|
|
|
test_case.fuzz_target, False, [],
|
|
|
|
[], test_case.test_case_path)
|
|
|
|
build_specified_commit.build_fuzzers_from_commit(test_case.new_commit,
|
|
|
|
test_repo_manager,
|
2020-04-14 01:38:23 +00:00
|
|
|
host_src_dir, build_data)
|
2020-01-30 18:27:56 +00:00
|
|
|
new_error_code = helper.reproduce_impl(test_case.project_name,
|
|
|
|
test_case.fuzz_target, False, [],
|
|
|
|
[], test_case.test_case_path)
|
2020-01-06 20:17:26 +00:00
|
|
|
self.assertNotEqual(new_error_code, old_error_code)
|
|
|
|
|
2020-01-15 21:30:57 +00:00
|
|
|
def test_detect_main_repo_from_commit(self):
|
|
|
|
"""Test the detect main repo function from build specific commit module."""
|
2020-01-30 18:27:56 +00:00
|
|
|
for example_repo in test_repos.TEST_REPOS:
|
2020-02-06 17:33:54 +00:00
|
|
|
if example_repo.new_commit:
|
|
|
|
repo_origin, repo_name = build_specified_commit.detect_main_repo(
|
|
|
|
example_repo.project_name, commit=example_repo.new_commit)
|
|
|
|
self.assertEqual(repo_origin, example_repo.git_url)
|
|
|
|
self.assertEqual(repo_name,
|
|
|
|
os.path.join('/src', example_repo.oss_repo_name))
|
2020-01-30 18:27:56 +00:00
|
|
|
|
|
|
|
repo_origin, repo_name = build_specified_commit.detect_main_repo(
|
|
|
|
test_repos.INVALID_REPO.project_name,
|
|
|
|
test_repos.INVALID_REPO.new_commit)
|
2020-01-15 21:30:57 +00:00
|
|
|
self.assertIsNone(repo_origin)
|
2020-01-30 18:27:56 +00:00
|
|
|
self.assertIsNone(repo_name)
|
2020-01-15 21:30:57 +00:00
|
|
|
|
|
|
|
def test_detect_main_repo_from_name(self):
|
|
|
|
"""Test the detect main repo function from build specific commit module."""
|
2020-01-30 18:27:56 +00:00
|
|
|
for example_repo in test_repos.TEST_REPOS:
|
|
|
|
repo_origin, repo_name = build_specified_commit.detect_main_repo(
|
|
|
|
example_repo.project_name, repo_name=example_repo.git_repo_name)
|
|
|
|
self.assertEqual(repo_origin, example_repo.git_url)
|
2020-02-06 23:35:42 +00:00
|
|
|
self.assertEqual(
|
|
|
|
repo_name,
|
|
|
|
os.path.join(example_repo.image_location, example_repo.oss_repo_name))
|
2020-01-30 18:27:56 +00:00
|
|
|
|
|
|
|
repo_origin, repo_name = build_specified_commit.detect_main_repo(
|
|
|
|
test_repos.INVALID_REPO.project_name,
|
|
|
|
test_repos.INVALID_REPO.oss_repo_name)
|
2020-01-06 20:17:26 +00:00
|
|
|
self.assertIsNone(repo_origin)
|
2020-01-30 18:27:56 +00:00
|
|
|
self.assertIsNone(repo_name)
|
2019-12-12 17:26:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-01-06 20:17:26 +00:00
|
|
|
|
2020-01-15 21:30:57 +00:00
|
|
|
# Change to oss-fuzz main directory so helper.py runs correctly.
|
2020-01-06 20:17:26 +00:00
|
|
|
if os.getcwd() != os.path.dirname(TEST_DIR_PATH):
|
|
|
|
os.chdir(os.path.dirname(TEST_DIR_PATH))
|
2019-12-12 17:26:37 +00:00
|
|
|
unittest.main()
|