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"""Test the functionality of the utils module's functions: 151. is_fuzz_target_local 162. get_fuzz_targets 173. get_env_var 18""" 19 20import os 21import unittest 22 23import utils 24import helper 25 26EXAMPLE_PROJECT = 'example' 27 28 29class IsFuzzTargetLocalUnitTest(unittest.TestCase): 30 """Test is_fuzz_target_local function in the utils module.""" 31 32 def test_invalid_filepath(self): 33 """Test the function with an invalid file path.""" 34 is_local = utils.is_fuzz_target_local('not/a/real/file') 35 self.assertFalse(is_local) 36 is_local = utils.is_fuzz_target_local('') 37 self.assertFalse(is_local) 38 is_local = utils.is_fuzz_target_local(' ') 39 self.assertFalse(is_local) 40 41 def test_valid_filepath(self): 42 """Checks is_fuzz_target_local function with a valid filepath.""" 43 utils.chdir_to_root() 44 helper.build_fuzzers_impl(EXAMPLE_PROJECT, 45 True, 46 'libfuzzer', 47 'address', 48 'x86_64', [], 49 None, 50 no_cache=False, 51 mount_location=None) 52 is_local = utils.is_fuzz_target_local( 53 os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT, 54 'do_stuff_fuzzer')) 55 self.assertTrue(is_local) 56 is_local = utils.is_fuzz_target_local( 57 os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT, 58 'do_stuff_fuzzer.dict')) 59 self.assertFalse(is_local) 60 61 62class GetFuzzTargetsUnitTest(unittest.TestCase): 63 """Test get_fuzz_targets function in the utils module.""" 64 65 def test_valid_filepath(self): 66 """Tests that fuzz targets can be retrieved once the fuzzers are built.""" 67 utils.chdir_to_root() 68 helper.build_fuzzers_impl(EXAMPLE_PROJECT, 69 True, 70 'libfuzzer', 71 'address', 72 'x86_64', [], 73 None, 74 no_cache=False, 75 mount_location=None) 76 fuzz_targets = utils.get_fuzz_targets( 77 os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT)) 78 self.assertCountEqual(fuzz_targets, [ 79 os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT, 80 'do_stuff_fuzzer') 81 ]) 82 fuzz_targets = utils.get_fuzz_targets( 83 os.path.join(helper.OSSFUZZ_DIR, 'infra')) 84 self.assertFalse(fuzz_targets) 85 86 def test_invalid_filepath(self): 87 """Tests what get_fuzz_targets return when invalid filepath is used.""" 88 utils.chdir_to_root() 89 helper.build_fuzzers_impl(EXAMPLE_PROJECT, 90 True, 91 'libfuzzer', 92 'address', 93 'x86_64', [], 94 None, 95 no_cache=False, 96 mount_location=None) 97 fuzz_targets = utils.get_fuzz_targets('not/a/valid/file/path') 98 self.assertFalse(fuzz_targets) 99 100 101if __name__ == '__main__': 102 unittest.main() 103