Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2018 The ChromiumOS Authors |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unit tests for cros_fuzz.""" |
| 6 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 7 | import logging |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 8 | import os |
| 9 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 10 | from chromite.lib import cros_test_lib |
| 11 | from chromite.scripts import cros_fuzz |
| 12 | |
Mike Frysinger | 03b983f | 2020-02-21 02:31:49 -0500 | [diff] [blame] | 13 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 14 | DEFAULT_MAX_TOTAL_TIME_OPTION = cros_fuzz.GetLibFuzzerOption( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 15 | cros_fuzz.MAX_TOTAL_TIME_OPTION_NAME, cros_fuzz.MAX_TOTAL_TIME_DEFAULT_VALUE |
| 16 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 17 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 18 | FUZZ_TARGET = "fuzzer" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 19 | FUZZER_COVERAGE_PATH = ( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | "/build/amd64-generic/tmp/fuzz/coverage-report/%s" % FUZZ_TARGET |
| 21 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 22 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 23 | BOARD = "amd64-generic" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class SysrootPathTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | """Tests the SysrootPath class.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | def setUp(self): |
| 30 | self.path_to_sysroot = _SetPathToSysroot() |
| 31 | self.sysroot_relative_path = "/dir" |
| 32 | self.basename = os.path.basename(self.sysroot_relative_path) |
| 33 | # Chroot relative path of a path that is in the sysroot. |
| 34 | self.path_in_sysroot = os.path.join(self.path_to_sysroot, self.basename) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 35 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | def testSysroot(self): |
| 37 | """Tests that SysrootPath.sysroot returns expected result.""" |
| 38 | sysroot_path = cros_fuzz.SysrootPath(self.sysroot_relative_path) |
| 39 | self.assertEqual(self.sysroot_relative_path, sysroot_path.sysroot) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 40 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | def testChroot(self): |
| 42 | """Tests that SysrootPath.chroot returns expected result.""" |
| 43 | sysroot_path = cros_fuzz.SysrootPath(self.sysroot_relative_path) |
| 44 | expected = os.path.join(self.path_to_sysroot, self.basename) |
| 45 | self.assertEqual(expected, sysroot_path.chroot) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | def testIsSysrootPath(self): |
| 48 | """Tests that the IsSysrootPath can tell what is in the sysroot.""" |
| 49 | self.assertTrue( |
| 50 | cros_fuzz.SysrootPath.IsPathInSysroot(self.path_to_sysroot) |
| 51 | ) |
| 52 | self.assertTrue( |
| 53 | cros_fuzz.SysrootPath.IsPathInSysroot(self.path_in_sysroot) |
| 54 | ) |
| 55 | path_not_in_sysroot_1 = os.path.join( |
| 56 | os.path.dirname(self.path_to_sysroot), self.basename |
| 57 | ) |
| 58 | self.assertFalse( |
| 59 | cros_fuzz.SysrootPath.IsPathInSysroot(path_not_in_sysroot_1) |
| 60 | ) |
| 61 | path_not_in_sysroot_2 = os.path.join("/dir/build/amd64-generic") |
| 62 | self.assertFalse( |
| 63 | cros_fuzz.SysrootPath.IsPathInSysroot(path_not_in_sysroot_2) |
| 64 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | def testFromChrootPathInSysroot(self): |
| 67 | """Tests that FromChrootPathInSysroot converts paths properly.""" |
| 68 | # Test that it raises an assertion error when the path is not in the |
| 69 | # sysroot. |
| 70 | path_not_in_sysroot_1 = os.path.join( |
| 71 | os.path.dirname(self.path_to_sysroot), "dir" |
| 72 | ) |
| 73 | with self.assertRaises(AssertionError): |
| 74 | cros_fuzz.SysrootPath.FromChrootPathInSysroot(path_not_in_sysroot_1) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | sysroot_path = cros_fuzz.SysrootPath.FromChrootPathInSysroot( |
| 77 | self.path_in_sysroot |
| 78 | ) |
| 79 | self.assertEqual(self.sysroot_relative_path, sysroot_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 80 | |
| 81 | |
| 82 | class GetPathForCopyTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | """Tests GetPathForCopy.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 84 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 85 | def testGetPathForCopy(self): |
| 86 | """Test that GetPathForCopy gives us the correct sysroot directory.""" |
| 87 | _SetPathToSysroot() |
| 88 | directory = "/path/to/directory" |
| 89 | parent = "parent" |
| 90 | child = os.path.basename(directory) |
| 91 | path_to_sysroot = cros_fuzz.SysrootPath.path_to_sysroot |
| 92 | storage_directory = cros_fuzz.SCRIPT_STORAGE_DIRECTORY |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 93 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | sysroot_path = cros_fuzz.GetPathForCopy(parent, child) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | expected_chroot_path = os.path.join( |
| 97 | path_to_sysroot, "tmp", storage_directory, parent, child |
| 98 | ) |
| 99 | self.assertEqual(expected_chroot_path, sysroot_path.chroot) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | expected_sysroot_path = os.path.join( |
| 102 | "/", "tmp", storage_directory, parent, child |
| 103 | ) |
| 104 | self.assertEqual(expected_sysroot_path, sysroot_path.sysroot) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 105 | |
| 106 | |
| 107 | class GetLibFuzzerOptionTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | """Tests GetLibFuzzerOption.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | def testGetLibFuzzerOption(self): |
| 111 | """Tests that GetLibFuzzerOption returns a correct libFuzzer option.""" |
| 112 | expected = "-max_total_time=60" |
| 113 | self.assertEqual( |
| 114 | expected, cros_fuzz.GetLibFuzzerOption("max_total_time", 60) |
| 115 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 116 | |
| 117 | |
| 118 | class LimitFuzzingTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | """Tests LimitFuzzing.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | def setUp(self): |
| 122 | self.fuzz_command = ["./fuzzer", "-rss_limit_mb=4096"] |
| 123 | self.corpus = None |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | def _Helper(self, expected_command=None): |
| 126 | """Calls LimitFuzzing and asserts fuzz_command equals |expected_command|. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 127 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 128 | If |expected| is None, then it is set to self.fuzz_command before calling |
| 129 | LimitFuzzing. |
| 130 | """ |
| 131 | if expected_command is None: |
| 132 | expected_command = self.fuzz_command[:] |
| 133 | cros_fuzz.LimitFuzzing(self.fuzz_command, self.corpus) |
| 134 | self.assertEqual(expected_command, self.fuzz_command) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 135 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | def testCommandHasMaxTotalTime(self): |
| 137 | """Tests that no limit is added when user specifies -max_total_time.""" |
| 138 | self.fuzz_command.append("-max_total_time=60") |
| 139 | self._Helper() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | def testCommandHasRuns(self): |
| 142 | """Tests that no limit is added when user specifies -runs""" |
| 143 | self.fuzz_command.append("-runs=1") |
| 144 | self._Helper() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | def testCommandHasCorpus(self): |
| 147 | """Tests that a limit is added when user specifies a corpus.""" |
| 148 | self.corpus = "corpus" |
| 149 | expected = self.fuzz_command + ["-runs=0"] |
| 150 | self._Helper(expected) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | def testNoLimitOrCorpus(self): |
| 153 | """Tests that a limit is added when user specifies no corpus or limit.""" |
| 154 | expected = self.fuzz_command + [DEFAULT_MAX_TOTAL_TIME_OPTION] |
| 155 | self._Helper(expected) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 156 | |
| 157 | |
| 158 | class RunSysrootCommandMockTestCase(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | """Class for TestCases that call RunSysrootCommand.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 160 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | def setUp(self): |
| 162 | _SetPathToSysroot() |
| 163 | self.expected_command = None |
| 164 | self.expected_extra_env = None |
| 165 | self.PatchObject( |
| 166 | cros_fuzz, |
| 167 | "RunSysrootCommand", |
| 168 | side_effect=self.MockedRunSysrootCommand, |
| 169 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | def MockedRunSysrootCommand( |
| 172 | self, command, extra_env=None, **kwargs |
| 173 | ): # pylint: disable=unused-argument |
| 174 | """The mocked version of RunSysrootCommand. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 175 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | Asserts |command| and |extra_env| are what is expected. |
| 177 | """ |
| 178 | self.assertEqual(self.expected_command, command) |
| 179 | self.assertEqual(self.expected_extra_env, extra_env) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 180 | |
| 181 | |
| 182 | class RunFuzzerTest(RunSysrootCommandMockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 183 | """Tests RunFuzzer.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 184 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | def setUp(self): |
| 186 | self.corpus_path = None |
| 187 | self.fuzz_args = "" |
| 188 | self.testcase_path = None |
| 189 | self.expected_command = [ |
| 190 | cros_fuzz.GetFuzzerSysrootPath(FUZZ_TARGET).sysroot, |
| 191 | ] |
| 192 | self.expected_extra_env = { |
| 193 | "ASAN_OPTIONS": "log_path=stderr:detect_odr_violation=0", |
| 194 | "MSAN_OPTIONS": "log_path=stderr:detect_odr_violation=0", |
| 195 | "UBSAN_OPTIONS": "log_path=stderr:detect_odr_violation=0", |
| 196 | } |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 197 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 198 | def _Helper(self): |
| 199 | """Calls RunFuzzer.""" |
| 200 | cros_fuzz.RunFuzzer( |
| 201 | FUZZ_TARGET, self.corpus_path, self.fuzz_args, self.testcase_path |
| 202 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | def testNoOptional(self): |
| 205 | """Tests correct command and env used when not specifying optional.""" |
| 206 | self.expected_command.append(DEFAULT_MAX_TOTAL_TIME_OPTION) |
| 207 | self._Helper() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 208 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 209 | def testFuzzArgs(self): |
| 210 | """Tests that the correct command is used when fuzz_args is specified.""" |
| 211 | fuzz_args = [DEFAULT_MAX_TOTAL_TIME_OPTION, "-fake_arg=fake_value"] |
| 212 | self.expected_command.extend(fuzz_args) |
| 213 | self.fuzz_args = " ".join(fuzz_args) |
| 214 | self._Helper() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 215 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 216 | def testTestCase(self): |
| 217 | """Tests a testcase is used when specified.""" |
| 218 | self.testcase_path = "/path/to/testcase" |
| 219 | self.expected_command.append(self.testcase_path) |
| 220 | self._Helper() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 221 | |
| 222 | |
| 223 | class MergeProfrawTest(RunSysrootCommandMockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | """Tests MergeProfraw.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 225 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | def testMergeProfraw(self): |
| 227 | """Tests that MergeProfraw works as expected.""" |
| 228 | # Parent class will assert that these commands are used. |
| 229 | profdata_path = cros_fuzz.GetProfdataPath(FUZZ_TARGET) |
| 230 | self.expected_command = [ |
| 231 | "llvm-profdata", |
| 232 | "merge", |
| 233 | "-sparse", |
| 234 | cros_fuzz.DEFAULT_PROFRAW_PATH, |
| 235 | "-o", |
| 236 | profdata_path.sysroot, |
| 237 | ] |
| 238 | cros_fuzz.MergeProfraw(FUZZ_TARGET) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 239 | |
| 240 | |
| 241 | class GenerateCoverageReportTest(cros_test_lib.RunCommandTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | """Tests GenerateCoverageReport.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 243 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 244 | def setUp(self): |
| 245 | _SetPathToSysroot() |
| 246 | self.fuzzer_path = cros_fuzz.GetFuzzerSysrootPath(FUZZ_TARGET).chroot |
| 247 | self.profdata_path = cros_fuzz.GetProfdataPath(FUZZ_TARGET) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 248 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 249 | def testWithSharedLibraries(self): |
| 250 | """Tests that right command is used when specifying shared libraries.""" |
| 251 | shared_libraries = ["shared_lib.so"] |
| 252 | cros_fuzz.GenerateCoverageReport(FUZZ_TARGET, shared_libraries) |
| 253 | instr_profile_option = "-instr-profile=%s" % self.profdata_path.chroot |
| 254 | output_dir_option = "-output-dir=%s" % FUZZER_COVERAGE_PATH |
| 255 | expected_command = [ |
| 256 | "llvm-cov", |
| 257 | "show", |
| 258 | "-object", |
| 259 | self.fuzzer_path, |
| 260 | "-object", |
| 261 | "shared_lib.so", |
| 262 | "-format=html", |
| 263 | instr_profile_option, |
| 264 | output_dir_option, |
| 265 | ] |
| 266 | self.assertCommandCalled( |
| 267 | expected_command, stderr=True, debug_level=logging.DEBUG |
| 268 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 269 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | def testNoSharedLibraries(self): |
| 271 | """Tests the right coverage command is used without shared libraries.""" |
| 272 | shared_libraries = [] |
| 273 | cros_fuzz.GenerateCoverageReport(FUZZ_TARGET, shared_libraries) |
| 274 | instr_profile_option = "-instr-profile=%s" % self.profdata_path.chroot |
| 275 | output_dir_option = "-output-dir=%s" % FUZZER_COVERAGE_PATH |
| 276 | expected_command = [ |
| 277 | "llvm-cov", |
| 278 | "show", |
| 279 | "-object", |
| 280 | self.fuzzer_path, |
| 281 | "-format=html", |
| 282 | instr_profile_option, |
| 283 | output_dir_option, |
| 284 | ] |
| 285 | self.assertCommandCalled( |
| 286 | expected_command, stderr=True, debug_level=logging.DEBUG |
| 287 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 288 | |
| 289 | |
| 290 | class RunSysrootCommandTest(cros_test_lib.RunCommandTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | """Tests RunSysrootCommand.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 293 | def testRunSysrootCommand(self): |
| 294 | """Tests RunSysrootCommand creates a proper command to run in sysroot.""" |
| 295 | command = ["./fuzz", "-rss_limit_mb=4096"] |
| 296 | sysroot_path = _SetPathToSysroot() |
| 297 | cros_fuzz.RunSysrootCommand(command) |
| 298 | expected_command = ["sudo", "--", "chroot", sysroot_path] |
| 299 | expected_command.extend(command) |
| 300 | self.assertCommandCalled(expected_command, debug_level=logging.DEBUG) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 301 | |
| 302 | |
| 303 | class GetBuildExtraEnvTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 304 | """Tests GetBuildExtraEnv.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 305 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | TEST_ENV_VAR = "TEST_VAR" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 307 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 308 | def testUseAndFeaturesNotClobbered(self): |
| 309 | """Tests that values of certain environment variables are appended to.""" |
| 310 | vars_and_values = {"FEATURES": "foo", "USE": "bar"} |
| 311 | for var, value in vars_and_values.items(): |
| 312 | os.environ[var] = value |
| 313 | extra_env = cros_fuzz.GetBuildExtraEnv(cros_fuzz.BuildType.COVERAGE) |
| 314 | for var, value in vars_and_values.items(): |
| 315 | self.assertIn(value, extra_env[var]) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 316 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | def testCoverageBuild(self): |
| 318 | """Tests that a proper environment is returned for a coverage build.""" |
| 319 | extra_env = cros_fuzz.GetBuildExtraEnv(cros_fuzz.BuildType.COVERAGE) |
| 320 | for expected_flag in ["fuzzer", "coverage", "asan"]: |
| 321 | self.assertIn(expected_flag, extra_env["USE"]) |
| 322 | self.assertIn("noclean", extra_env["FEATURES"]) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 323 | |
| 324 | |
| 325 | def _SetPathToSysroot(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | """Calls SysrootPath.SetPathToSysroot and returns result.""" |
| 327 | return cros_fuzz.SysrootPath.SetPathToSysroot(BOARD) |