• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 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#
15################################################################################
16"""Tests for build.py"""
17
18import os
19import sys
20import unittest
21from unittest import mock
22
23# pylint: disable=wrong-import-position
24INFRA_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
25sys.path.append(INFRA_DIR)
26
27from ci import build
28
29
30def patch_environ(testcase_obj):
31  """Patch environment."""
32  env = {}
33  patcher = mock.patch.dict(os.environ, env)
34  testcase_obj.addCleanup(patcher.stop)
35  patcher.start()
36
37
38def _set_coverage_build():
39  """Set the right environment variables for a coverage build."""
40  os.environ['SANITIZER'] = 'coverage'
41  os.environ['ENGINE'] = 'libfuzzer'
42  os.environ['ARCHITECTURE'] = 'x86_64'
43
44
45class TestShouldBuild(unittest.TestCase):
46  """Tests that should_build() works as intended."""
47
48  def setUp(self):
49    patch_environ(self)
50
51  def test_none_engine_coverage_build(self):
52    """Tests that should_build returns False for a coverage build of a
53    project that specifies 'none' for fuzzing_engines."""
54    _set_coverage_build()
55    project_yaml = {
56        'language': 'c++',
57        'fuzzing_engines': ['none'],
58        'sanitizers': ['address']
59    }
60    self.assertFalse(build.should_build(project_yaml))
61
62  def test_unspecified_engines_coverage_build(self):
63    """Tests that should_build returns True for a coverage build of a
64    project that doesn't specify fuzzing_engines."""
65    _set_coverage_build()
66    project_yaml = {'language': 'c++'}
67    self.assertTrue(build.should_build(project_yaml))
68
69  def test_libfuzzer_coverage_build(self):
70    """Tests that should_build returns True for coverage build of a project
71    specifying 'libfuzzer' and for fuzzing_engines."""
72    _set_coverage_build()
73    project_yaml = {
74        'language': 'c++',
75        'fuzzing_engines': ['libfuzzer'],
76        'sanitizers': ['address']
77    }
78    self.assertTrue(build.should_build(project_yaml))
79
80  def test_go_coverage_build(self):
81    """Tests that should_build returns True for coverage build of a project
82    specifying 'libfuzzer' and for fuzzing_engines."""
83    _set_coverage_build()
84    project_yaml = {'language': 'go'}
85    self.assertTrue(build.should_build(project_yaml))
86
87  def test_engine_project_none_build(self):
88    """Tests that should_build returns False for an engine: 'none' build when
89    the project doesn't specify engines."""
90    os.environ['SANITIZER'] = 'address'
91    os.environ['ENGINE'] = 'none'
92    os.environ['ARCHITECTURE'] = 'x86_64'
93    project_yaml = {
94        'language': 'c++',
95        'fuzzing_engines': ['libfuzzer'],
96        'sanitizers': ['address']
97    }
98    self.assertFalse(build.should_build(project_yaml))
99