oss-fuzz/infra/utils_test.py

140 lines
5.3 KiB
Python
Raw Normal View History

[Infra] CIFuzz pipeline complete. (#3281) * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working fuzzers with out error surface * Working fuzzers with out error surface * Working fuzzers with out error surface * Printing std err * Adding fuzzer timeout * Adding fuzzer timeout * Changing fuzzer timeout to fuzz time * Formatting and refactoring * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Refactoring error codes. * reverting helper.py * reverting helper.py * reverting helper.py * chaning method to static * moving cifuzz file * Jonathan changes * Oliver and Jonathan comments * Oliver and Jonathan comments * Oliver and Jonathan comments * Utils unit tests * Test formatting and documentation * Build fuzzer test added * Changed repo manager errors * Unit and integration tests complete * Jonathan comments pt.1 * Jonathan comments pt.1 * Jonathan comments pt.1 * adding cifuzz_test * Build fuzzer test completed * Run fuzzers test finished. * Removed SRC dependency * Jonathan comments pt.2 * Max comments pt.1 * Max comments pt.2 * removing log specified out stream * Max comments pt.3 * Adding OSS_FUZZ_HOME env var * Jonathan comments pt.3 * Formatting * Olivers comments * Jonathan comments
2020-01-29 19:03:43 +00:00
# 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"""
[Infra] CIFuzz pipeline complete. (#3281) * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working fuzzers with out error surface * Working fuzzers with out error surface * Working fuzzers with out error surface * Printing std err * Adding fuzzer timeout * Adding fuzzer timeout * Changing fuzzer timeout to fuzz time * Formatting and refactoring * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Refactoring error codes. * reverting helper.py * reverting helper.py * reverting helper.py * chaning method to static * moving cifuzz file * Jonathan changes * Oliver and Jonathan comments * Oliver and Jonathan comments * Oliver and Jonathan comments * Utils unit tests * Test formatting and documentation * Build fuzzer test added * Changed repo manager errors * Unit and integration tests complete * Jonathan comments pt.1 * Jonathan comments pt.1 * Jonathan comments pt.1 * adding cifuzz_test * Build fuzzer test completed * Run fuzzers test finished. * Removed SRC dependency * Jonathan comments pt.2 * Max comments pt.1 * Max comments pt.2 * removing log specified out stream * Max comments pt.3 * Adding OSS_FUZZ_HOME env var * Jonathan comments pt.3 * Formatting * Olivers comments * Jonathan comments
2020-01-29 19:03:43 +00:00
import os
import tempfile
[Infra] CIFuzz pipeline complete. (#3281) * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working fuzzers with out error surface * Working fuzzers with out error surface * Working fuzzers with out error surface * Printing std err * Adding fuzzer timeout * Adding fuzzer timeout * Changing fuzzer timeout to fuzz time * Formatting and refactoring * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Refactoring error codes. * reverting helper.py * reverting helper.py * reverting helper.py * chaning method to static * moving cifuzz file * Jonathan changes * Oliver and Jonathan comments * Oliver and Jonathan comments * Oliver and Jonathan comments * Utils unit tests * Test formatting and documentation * Build fuzzer test added * Changed repo manager errors * Unit and integration tests complete * Jonathan comments pt.1 * Jonathan comments pt.1 * Jonathan comments pt.1 * adding cifuzz_test * Build fuzzer test completed * Run fuzzers test finished. * Removed SRC dependency * Jonathan comments pt.2 * Max comments pt.1 * Max comments pt.2 * removing log specified out stream * Max comments pt.3 * Adding OSS_FUZZ_HOME env var * Jonathan comments pt.3 * Formatting * Olivers comments * Jonathan comments
2020-01-29 19:03:43 +00:00
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)
class ExecuteUnitTest(unittest.TestCase):
"""Test execute function in the utils module."""
def test_valid_command(self):
"""Tests that execute can produce valid output."""
with tempfile.TemporaryDirectory() as tmp_dir:
out, err, err_code = utils.execute(['ls', '.'],
location=tmp_dir,
check_result=False)
self.assertEqual(err_code, 0)
self.assertEqual(err, '')
self.assertEqual(out, '')
out, err, err_code = utils.execute(['mkdir', 'tmp'],
location=tmp_dir,
check_result=False)
self.assertEqual(err_code, 0)
self.assertEqual(err, '')
self.assertEqual(out, '')
out, err, err_code = utils.execute(['ls', '.'],
location=tmp_dir,
check_result=False)
self.assertEqual(err_code, 0)
self.assertEqual(err, '')
self.assertEqual(out, 'tmp\n')
def test_error_command(self):
"""Tests that execute can correctly surface errors."""
with tempfile.TemporaryDirectory() as tmp_dir:
out, err, err_code = utils.execute(['ls', 'notarealdir'],
location=tmp_dir,
check_result=False)
self.assertEqual(err_code, 2)
self.assertIsNotNone(err)
self.assertEqual(out, '')
with self.assertRaises(RuntimeError):
out, err, err_code = utils.execute(['ls', 'notarealdir'],
location=tmp_dir,
check_result=True)
[Infra] CIFuzz pipeline complete. (#3281) * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working fuzzers with out error surface * Working fuzzers with out error surface * Working fuzzers with out error surface * Printing std err * Adding fuzzer timeout * Adding fuzzer timeout * Changing fuzzer timeout to fuzz time * Formatting and refactoring * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Refactoring error codes. * reverting helper.py * reverting helper.py * reverting helper.py * chaning method to static * moving cifuzz file * Jonathan changes * Oliver and Jonathan comments * Oliver and Jonathan comments * Oliver and Jonathan comments * Utils unit tests * Test formatting and documentation * Build fuzzer test added * Changed repo manager errors * Unit and integration tests complete * Jonathan comments pt.1 * Jonathan comments pt.1 * Jonathan comments pt.1 * adding cifuzz_test * Build fuzzer test completed * Run fuzzers test finished. * Removed SRC dependency * Jonathan comments pt.2 * Max comments pt.1 * Max comments pt.2 * removing log specified out stream * Max comments pt.3 * Adding OSS_FUZZ_HOME env var * Jonathan comments pt.3 * Formatting * Olivers comments * Jonathan comments
2020-01-29 19:03:43 +00:00
if __name__ == '__main__':
unittest.main()