• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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"""Test the functionality of the build image from commit module.
15The will consist of the following functional tests:
16  1. The inference of the main repo for a specific project.
17  2. The building of a projects fuzzers from a specific commit.
18
19"""
20import os
21import tempfile
22import unittest
23
24import build_specified_commit
25import helper
26import repo_manager
27import test_repos
28
29# necessary because __file__ changes with os.chdir
30TEST_DIR_PATH = os.path.dirname(os.path.realpath(__file__))
31
32
33@unittest.skipIf(not os.getenv('INTEGRATION_TESTS'),
34                 'INTEGRATION_TESTS=1 not set')
35class BuildImageIntegrationTest(unittest.TestCase):
36  """Tests if an image can be built from different states e.g. a commit."""
37
38  @unittest.skip('Test is failing (spuriously?).')
39  def test_build_fuzzers_from_commit(self):
40    """Tests if the fuzzers can build at a specified commit.
41
42    This is done by using a known regression range for a specific test case.
43    The old commit should show the error when its fuzzers run and the new one
44    should not.
45    """
46    with tempfile.TemporaryDirectory() as tmp_dir:
47      test_repo = test_repos.TEST_REPOS[1]
48      self.assertTrue(helper.build_image_impl(test_repo.project_name))
49      host_src_dir = build_specified_commit.copy_src_from_docker(
50          test_repo.project_name, tmp_dir)
51
52      test_repo_manager = repo_manager.clone_repo_and_get_manager(
53          test_repo.git_url, host_src_dir, test_repo.oss_repo_name)
54      build_data = build_specified_commit.BuildData(
55          sanitizer='address',
56          architecture='x86_64',
57          engine='libfuzzer',
58          project_name=test_repo.project_name)
59
60      build_specified_commit.build_fuzzers_from_commit(test_repo.old_commit,
61                                                       test_repo_manager,
62                                                       host_src_dir, build_data)
63      project = helper.Project(test_repo.project_name)
64      old_result = helper.reproduce_impl(project=project,
65                                         fuzzer_name=test_repo.fuzz_target,
66                                         valgrind=False,
67                                         env_to_add=[],
68                                         fuzzer_args=[],
69                                         testcase_path=test_repo.testcase_path)
70      build_specified_commit.build_fuzzers_from_commit(test_repo.project_name,
71                                                       test_repo_manager,
72                                                       host_src_dir, build_data)
73      new_result = helper.reproduce_impl(project=project,
74                                         fuzzer_name=test_repo.fuzz_target,
75                                         valgrind=False,
76                                         env_to_add=[],
77                                         fuzzer_args=[],
78                                         testcase_path=test_repo.testcase_path)
79      self.assertNotEqual(new_result, old_result)
80
81  def test_detect_main_repo_from_commit(self):
82    """Test the detect main repo function from build specific commit module."""
83    # TODO(metzman): Fix these tests so they don't randomly break because of
84    # changes in the outside world.
85    for example_repo in test_repos.TEST_REPOS:
86      if example_repo.new_commit:
87        # TODO(metzman): This function calls _build_image_with_retries which
88        # has a long delay (30 seconds). Figure out how to make this quicker.
89        repo_origin, repo_name = build_specified_commit.detect_main_repo(
90            example_repo.project_name, commit=example_repo.new_commit)
91        self.assertEqual(repo_origin, example_repo.git_url)
92        self.assertEqual(repo_name,
93                         os.path.join('/src', example_repo.oss_repo_name))
94
95    repo_origin, repo_name = build_specified_commit.detect_main_repo(
96        test_repos.INVALID_REPO.project_name,
97        test_repos.INVALID_REPO.new_commit)
98    self.assertIsNone(repo_origin)
99    self.assertIsNone(repo_name)
100
101  def test_detect_main_repo_from_name(self):
102    """Test the detect main repo function from build specific commit module."""
103    for example_repo in test_repos.TEST_REPOS:
104      if example_repo.project_name == 'gonids':
105        # It's unclear how this test ever passed, but we can't infer the repo
106        # because gonids doesn't really check it out, it uses "go get".
107        continue
108      repo_origin, repo_name = build_specified_commit.detect_main_repo(
109          example_repo.project_name, repo_name=example_repo.git_repo_name)
110      self.assertEqual(repo_origin, example_repo.git_url)
111      self.assertEqual(
112          repo_name,
113          os.path.join(example_repo.image_location, example_repo.oss_repo_name))
114
115    repo_origin, repo_name = build_specified_commit.detect_main_repo(
116        test_repos.INVALID_REPO.project_name,
117        test_repos.INVALID_REPO.oss_repo_name)
118    self.assertIsNone(repo_origin)
119    self.assertIsNone(repo_name)
120
121
122if __name__ == '__main__':
123  # Change to oss-fuzz main directory so helper.py runs correctly.
124  if os.getcwd() != os.path.dirname(TEST_DIR_PATH):
125    os.chdir(os.path.dirname(TEST_DIR_PATH))
126  unittest.main()
127