|
| 1 | +# Copyright 2024 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 | +"""Tests for googlefuzztest engine.""" |
| 15 | +# pylint: disable=unused-argument |
| 16 | +import os |
| 17 | +import tempfile |
| 18 | +import unittest |
| 19 | + |
| 20 | +from clusterfuzz._internal.bot.fuzzers import engine_common |
| 21 | +from clusterfuzz._internal.bot.fuzzers.googlefuzztest import engine |
| 22 | +from clusterfuzz._internal.system import new_process |
| 23 | +from clusterfuzz._internal.tests.test_libs import helpers |
| 24 | + |
| 25 | +TEST_PATH = os.path.abspath(os.path.dirname(__file__)) |
| 26 | +BINARY_NAME = "mock_binary" |
| 27 | + |
| 28 | + |
| 29 | +class GoogleFuzzTestUnitTests(unittest.TestCase): |
| 30 | + """Tests to make sure the fuzzing engine correctly handles logging by passing correct abseil flags""" |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + self.mock_temp = tempfile.TemporaryDirectory() |
| 34 | + self.mock_build = tempfile.TemporaryDirectory() |
| 35 | + self.mock_reproducers = tempfile.TemporaryDirectory() |
| 36 | + self.mock_binary = tempfile.TemporaryFile() |
| 37 | + |
| 38 | + def tearDown(self): |
| 39 | + self.mock_temp.cleanup() |
| 40 | + self.mock_build.cleanup() |
| 41 | + self.mock_reproducers.cleanup() |
| 42 | + self.mock_binary.close() |
| 43 | + |
| 44 | + def test_googlefuzztest_invoked_with_low_log_volume(self): |
| 45 | + """Test if we call fuzztest with the correct abseil flags to reduce logging volume.""" |
| 46 | + helpers.patch(self, [ |
| 47 | + 'clusterfuzz._internal.bot.fuzzers.engine_common.find_fuzzer_path', |
| 48 | + 'clusterfuzz._internal.system.new_process.wait_process', |
| 49 | + 'subprocess.Popen', |
| 50 | + ]) |
| 51 | + engine_impl = engine.Engine() |
| 52 | + |
| 53 | + self.mock.find_fuzzer_path.return_value = self.mock_binary.name |
| 54 | + self.mock.wait_process.return_value = new_process.ProcessResult(output='') |
| 55 | + self.mock.Popen.return_value = None |
| 56 | + |
| 57 | + target_path = engine_common.find_fuzzer_path(self.mock_build.name, |
| 58 | + BINARY_NAME) |
| 59 | + |
| 60 | + options = engine_impl.prepare(None, self.mock_binary.name, |
| 61 | + self.mock_build.name) |
| 62 | + results = engine_impl.fuzz(target_path, options, self.mock_reproducers.name, |
| 63 | + 10) |
| 64 | + |
| 65 | + self.assertIn('--logtostderr', results.command) |
| 66 | + self.assertIn('--minloglevel=3', results.command) |
0 commit comments