aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/ci/build_test.py
blob: 59fa3cf5cb846c0bb21b411efd09bf5761ff9f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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.
#
################################################################################
"""Tests for build.py"""

import os
import unittest
from unittest import mock

from ci import build


def patch_environ(testcase_obj):
  """Patch environment."""
  env = {}
  patcher = mock.patch.dict(os.environ, env)
  testcase_obj.addCleanup(patcher.stop)
  patcher.start()


def _set_coverage_build():
  """Set the right environment variables for a coverage build."""
  os.environ['SANITIZER'] = 'coverage'
  os.environ['ENGINE'] = 'libfuzzer'
  os.environ['ARCHITECTURE'] = 'x86_64'


class TestShouldBuild(unittest.TestCase):
  """Tests that should_build() works as intended."""

  def setUp(self):
    patch_environ(self)

  def test_none_engine_coverage_build(self):
    """Tests that should_build returns False for a coverage build of a
    project that specifies 'none' for fuzzing_engines."""
    _set_coverage_build()
    project_yaml = {
        'language': 'c++',
        'fuzzing_engines': ['none'],
        'sanitizers': ['address']
    }
    self.assertFalse(build.should_build(project_yaml))

  def test_unspecified_engines_coverage_build(self):
    """Tests that should_build returns True for a coverage build of a
    project that doesn't specify fuzzing_engines."""
    _set_coverage_build()
    project_yaml = {'language': 'c++'}
    self.assertTrue(build.should_build(project_yaml))

  def test_libfuzzer_coverage_build(self):
    """Tests that should_build returns True for coverage build of a project
    specifying 'libfuzzer' and for fuzzing_engines."""
    _set_coverage_build()
    project_yaml = {
        'language': 'c++',
        'fuzzing_engines': ['libfuzzer'],
        'sanitizers': ['address']
    }
    self.assertTrue(build.should_build(project_yaml))

  def test_go_coverage_build(self):
    """Tests that should_build returns True for coverage build of a project
    specifying 'libfuzzer' and for fuzzing_engines."""
    _set_coverage_build()
    project_yaml = {'language': 'go'}
    self.assertTrue(build.should_build(project_yaml))

  def test_engine_project_none_build(self):
    """Tests that should_build returns False for an engine: 'none' build when
    the project doesn't specify engines."""
    os.environ['SANITIZER'] = 'address'
    os.environ['ENGINE'] = 'none'
    os.environ['ARCHITECTURE'] = 'x86_64'
    project_yaml = {
        'language': 'c++',
        'fuzzing_engines': ['libfuzzer'],
        'sanitizers': ['address']
    }
    self.assertFalse(build.should_build(project_yaml))