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.
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests the functionality of the utils module's functions"""
|
2020-01-29 19:03:43 +00:00
|
|
|
|
|
|
|
import os
|
2020-02-04 19:51:18 +00:00
|
|
|
import tempfile
|
2020-01-29 19:03:43 +00:00
|
|
|
import unittest
|
2020-12-08 17:05:48 +00:00
|
|
|
from unittest import mock
|
2020-01-29 19:03:43 +00:00
|
|
|
|
|
|
|
import utils
|
|
|
|
import helper
|
|
|
|
|
|
|
|
EXAMPLE_PROJECT = 'example'
|
|
|
|
|
2020-02-21 16:47:13 +00:00
|
|
|
TEST_OUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'cifuzz', 'test_files', 'out')
|
|
|
|
|
2020-01-29 19:03:43 +00:00
|
|
|
|
2020-11-17 21:39:57 +00:00
|
|
|
class IsFuzzTargetLocalTest(unittest.TestCase):
|
|
|
|
"""Tests the is_fuzz_target_local function."""
|
2020-01-29 19:03:43 +00:00
|
|
|
|
|
|
|
def test_invalid_filepath(self):
|
2020-11-17 21:39:57 +00:00
|
|
|
"""Tests the function with an invalid file path."""
|
2020-01-29 19:03:43 +00:00
|
|
|
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."""
|
2020-02-21 16:47:13 +00:00
|
|
|
|
2020-01-29 19:03:43 +00:00
|
|
|
is_local = utils.is_fuzz_target_local(
|
2020-03-05 16:44:18 +00:00
|
|
|
os.path.join(TEST_OUT_DIR, 'example_crash_fuzzer'))
|
2020-01-29 19:03:43 +00:00
|
|
|
self.assertTrue(is_local)
|
2020-02-21 16:47:13 +00:00
|
|
|
is_local = utils.is_fuzz_target_local(TEST_OUT_DIR)
|
2020-01-29 19:03:43 +00:00
|
|
|
self.assertFalse(is_local)
|
|
|
|
|
|
|
|
|
2020-11-17 21:39:57 +00:00
|
|
|
class GetFuzzTargetsTest(unittest.TestCase):
|
|
|
|
"""Tests the get_fuzz_targets function."""
|
2020-01-29 19:03:43 +00:00
|
|
|
|
|
|
|
def test_valid_filepath(self):
|
|
|
|
"""Tests that fuzz targets can be retrieved once the fuzzers are built."""
|
2020-02-21 16:47:13 +00:00
|
|
|
fuzz_targets = utils.get_fuzz_targets(TEST_OUT_DIR)
|
2020-03-05 16:44:18 +00:00
|
|
|
crash_fuzzer_path = os.path.join(TEST_OUT_DIR, 'example_crash_fuzzer')
|
|
|
|
nocrash_fuzzer_path = os.path.join(TEST_OUT_DIR, 'example_nocrash_fuzzer')
|
2020-02-21 16:47:13 +00:00
|
|
|
self.assertCountEqual(fuzz_targets,
|
2020-03-05 16:44:18 +00:00
|
|
|
[crash_fuzzer_path, nocrash_fuzzer_path])
|
2020-04-01 17:10:51 +00:00
|
|
|
|
|
|
|
# Testing on a arbitrary directory with no fuzz targets in it.
|
2020-01-29 19:03:43 +00:00
|
|
|
fuzz_targets = utils.get_fuzz_targets(
|
2020-04-01 17:10:51 +00:00
|
|
|
os.path.join(helper.OSS_FUZZ_DIR, 'infra', 'travis'))
|
2020-01-29 19:03:43 +00:00
|
|
|
self.assertFalse(fuzz_targets)
|
|
|
|
|
|
|
|
def test_invalid_filepath(self):
|
|
|
|
"""Tests what get_fuzz_targets return when invalid filepath is used."""
|
|
|
|
fuzz_targets = utils.get_fuzz_targets('not/a/valid/file/path')
|
|
|
|
self.assertFalse(fuzz_targets)
|
|
|
|
|
|
|
|
|
2020-11-17 21:39:57 +00:00
|
|
|
class ExecuteTest(unittest.TestCase):
|
|
|
|
"""Tests the execute function."""
|
2020-02-04 19:51:18 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-12-08 17:05:48 +00:00
|
|
|
class BinaryPrintTest(unittest.TestCase):
|
|
|
|
"""Tests for utils.binary_print."""
|
|
|
|
|
|
|
|
@unittest.skip('Causes spurious failures because of side-effects.')
|
|
|
|
def test_string(self): # pylint: disable=no-self-use
|
|
|
|
"""Tests that utils.binary_print can print a regular string."""
|
|
|
|
# Should execute without raising any exceptions.
|
|
|
|
with mock.patch('sys.stdout.buffer.write') as mocked_write:
|
|
|
|
utils.binary_print('hello')
|
|
|
|
mocked_write.assert_called_with('hello\n')
|
|
|
|
|
|
|
|
@unittest.skip('Causes spurious failures because of side-effects.')
|
|
|
|
def test_binary_string(self): # pylint: disable=no-self-use
|
|
|
|
"""Tests that utils.binary_print can print a bianry string."""
|
|
|
|
# Should execute without raising any exceptions.
|
|
|
|
with mock.patch('sys.stdout.buffer.write') as mocked_write:
|
|
|
|
utils.binary_print(b'hello')
|
|
|
|
mocked_write.assert_called_with(b'hello\n')
|
|
|
|
|
|
|
|
|
2020-01-29 19:03:43 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|