|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """Adversarial policy fixtures; these are not installed-package proof.""" |
| 3 | +import hashlib |
3 | 4 | import importlib.util |
4 | 5 | import json |
5 | 6 | import os |
6 | 7 | import shlex |
7 | 8 | from pathlib import Path |
8 | 9 | import subprocess |
| 10 | +import sys |
9 | 11 | import tempfile |
10 | 12 | import unittest |
11 | 13 | from unittest.mock import patch |
12 | 14 |
|
| 15 | +sys.path.insert(0, str(Path(__file__).parent / 'lib')) |
| 16 | +import package_integration as integration |
| 17 | + |
13 | 18 | CXX = os.environ.get('CXX', 'c++') |
14 | 19 |
|
15 | 20 | spec = importlib.util.spec_from_file_location('audit', Path(__file__).with_name('audit-v3-installed-package.py')) |
16 | 21 | audit = importlib.util.module_from_spec(spec) |
17 | 22 | spec.loader.exec_module(audit) |
18 | 23 |
|
19 | 24 |
|
| 25 | +class CompileProbeTest(unittest.TestCase): |
| 26 | + def setUp(self): |
| 27 | + self.tmp = tempfile.TemporaryDirectory() |
| 28 | + self.addCleanup(self.tmp.cleanup) |
| 29 | + self.root = Path(self.tmp.name).resolve() |
| 30 | + self.source, self.build, self.prefix = (self.root / name for name in ('source', 'build', 'prefix')) |
| 31 | + self.unit = self.root / 'consumer.cpp' |
| 32 | + self.unit.write_text('int main() { return 0; }\n') |
| 33 | + self.header = self.prefix / 'include/httpserver.hpp' |
| 34 | + self.header.parent.mkdir(parents=True) |
| 35 | + self.header.write_text('struct installed_server {};\n') |
| 36 | + self.executable = self.root / 'consumer' |
| 37 | + self.calls = [] |
| 38 | + |
| 39 | + def command(self, argv, cwd, env): |
| 40 | + self.calls.append((argv, cwd, env)) |
| 41 | + if '-MF' in argv: |
| 42 | + Path(argv[argv.index('-MF') + 1]).write_text( |
| 43 | + 'consumer.o: ' + shlex.join([str(self.unit), str(self.header)]) + '\n') |
| 44 | + Path(argv[argv.index('-o') + 1]).write_text('compiled artifact') |
| 45 | + |
| 46 | + def test_compile_probe_uses_host_link_map_syntax_preserving_arguments_and_receipt(self): |
| 47 | + compiler = 'cache-wrapper "compiler path/c++" --driver-mode=g++' |
| 48 | + flags = ['-I' + str(self.prefix / 'include'), '-DFIXTURE=1'] |
| 49 | + libraries = ['-L' + str(self.prefix / 'lib'), '-lhttpserver', '-pthread', '-lssl', '-lcrypto'] |
| 50 | + env = {'PATH': 'selected-toolchain'} |
| 51 | + map_path = str(self.executable) + '.map' |
| 52 | + for host, option in (('Darwin', '-Wl,-map,'), ('Linux', '-Wl,-Map,'), |
| 53 | + ('FreeBSD', '-Wl,-Map,'), ('Windows', '-Wl,-Map,')): |
| 54 | + for options in (None, ['-O2', '-DNDEBUG']): |
| 55 | + with self.subTest(host=host, compile_options=options), patch('platform.system', return_value=host): |
| 56 | + self.calls.clear() |
| 57 | + receipt = integration.compile_probe(compiler, [self.unit], flags, libraries, |
| 58 | + self.executable, self.command, env, self.source, self.build, self.prefix, False, |
| 59 | + compile_options=options) |
| 60 | + obj = str(self.root / 'consumer-0.o') |
| 61 | + expected_options = ['-O0', '-g'] if options is None else options |
| 62 | + self.assertEqual(self.calls, [ |
| 63 | + (shlex.split(compiler) + ['-std=c++20'] + expected_options + flags + |
| 64 | + ['-MMD', '-MF', str(self.root / 'consumer-0.d'), '-c', str(self.unit), '-o', obj], self.root, env), |
| 65 | + (shlex.split(compiler) + [obj] + libraries + [option + map_path, '-o', str(self.executable)], self.root, env)]) |
| 66 | + self.assertEqual(receipt['link_map'], map_path) |
| 67 | + self.assertEqual(receipt['compile_options'], expected_options) |
| 68 | + self.assertEqual(receipt['sources'], {str(self.unit): hashlib.sha256(self.unit.read_bytes()).hexdigest()}) |
| 69 | + self.assertEqual(receipt['dependency_sha256'], { |
| 70 | + str(path): hashlib.sha256(path.read_bytes()).hexdigest() for path in (self.unit, self.header)}) |
| 71 | + self.assertEqual(receipt['dependencies'], sorted([str(self.unit), str(self.header)])) |
| 72 | + self.assertEqual(receipt['artifact_sha256'], hashlib.sha256(self.executable.read_bytes()).hexdigest()) |
| 73 | + self.assertFalse(receipt['private_test_headers']) |
| 74 | + |
| 75 | + def test_compile_probe_rejects_source_header_before_linking(self): |
| 76 | + with patch('platform.system', return_value='Darwin'), self.assertRaisesRegex(ValueError, 'source public header contamination'): |
| 77 | + integration.compile_probe('c++', [self.unit], [], [], self.executable, self.command, |
| 78 | + {}, self.root, self.build, self.prefix, False) |
| 79 | + self.assertEqual(len(self.calls), 1) |
| 80 | + |
| 81 | + |
20 | 82 | class AuditTest(unittest.TestCase): |
21 | 83 | def setUp(self): |
22 | 84 | environment = patch.dict(os.environ) |
|
0 commit comments