aboutsummaryrefslogtreecommitdiff
path: root/infra/utils_test.py
blob: ab3d216fb354f403afd8d5d2eff40fa195186708 (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
94
95
96
97
98
99
100
101
102
# 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.
"""Test the functionality of the utils module's functions:
1. is_fuzz_target_local
2. get_fuzz_targets
3. get_env_var
"""

import os
import unittest

import utils
import helper

EXAMPLE_PROJECT = 'example'


class IsFuzzTargetLocalUnitTest(unittest.TestCase):
  """Test is_fuzz_target_local function in the utils module."""

  def test_invalid_filepath(self):
    """Test the function with an invalid file path."""
    is_local = utils.is_fuzz_target_local('not/a/real/file')
    self.assertFalse(is_local)
    is_local = utils.is_fuzz_target_local('')
    self.assertFalse(is_local)
    is_local = utils.is_fuzz_target_local(' ')
    self.assertFalse(is_local)

  def test_valid_filepath(self):
    """Checks is_fuzz_target_local function with a valid filepath."""
    utils.chdir_to_root()
    helper.build_fuzzers_impl(EXAMPLE_PROJECT,
                              True,
                              'libfuzzer',
                              'address',
                              'x86_64', [],
                              None,
                              no_cache=False,
                              mount_location=None)
    is_local = utils.is_fuzz_target_local(
        os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT,
                     'do_stuff_fuzzer'))
    self.assertTrue(is_local)
    is_local = utils.is_fuzz_target_local(
        os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT,
                     'do_stuff_fuzzer.dict'))
    self.assertFalse(is_local)


class GetFuzzTargetsUnitTest(unittest.TestCase):
  """Test get_fuzz_targets function in the utils module."""

  def test_valid_filepath(self):
    """Tests that fuzz targets can be retrieved once the fuzzers are built."""
    utils.chdir_to_root()
    helper.build_fuzzers_impl(EXAMPLE_PROJECT,
                              True,
                              'libfuzzer',
                              'address',
                              'x86_64', [],
                              None,
                              no_cache=False,
                              mount_location=None)
    fuzz_targets = utils.get_fuzz_targets(
        os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT))
    self.assertCountEqual(fuzz_targets, [
        os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT,
                     'do_stuff_fuzzer')
    ])
    fuzz_targets = utils.get_fuzz_targets(
        os.path.join(helper.OSSFUZZ_DIR, 'infra'))
    self.assertFalse(fuzz_targets)

  def test_invalid_filepath(self):
    """Tests what get_fuzz_targets return when invalid filepath is used."""
    utils.chdir_to_root()
    helper.build_fuzzers_impl(EXAMPLE_PROJECT,
                              True,
                              'libfuzzer',
                              'address',
                              'x86_64', [],
                              None,
                              no_cache=False,
                              mount_location=None)
    fuzz_targets = utils.get_fuzz_targets('not/a/valid/file/path')
    self.assertFalse(fuzz_targets)


if __name__ == '__main__':
  unittest.main()