|
4 | 4 | ## Copyright The Mbed TLS Contributors
|
5 | 5 | ## SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
6 | 6 |
|
| 7 | +import glob |
7 | 8 | import os
|
| 9 | +import re |
8 | 10 | import subprocess
|
9 | 11 | import sys
|
| 12 | +import tempfile |
10 | 13 | import unittest
|
11 | 14 | from typing import List, Optional, Pattern, Union
|
12 | 15 |
|
| 16 | +from . import config_checks_generator |
| 17 | + |
13 | 18 |
|
14 | 19 | class TestConfigChecks(unittest.TestCase):
|
15 | 20 | """Unit tests for checks generated by config_checks_generator."""
|
@@ -63,6 +68,7 @@ def run_with_config_files(self,
|
63 | 68 | if mbedtls_user_config_file is not None:
|
64 | 69 | cmd.append(f'-DMBEDTLS_USER_CONFIG_FILE="{mbedtls_user_config_file}"')
|
65 | 70 | cmd += extra_options
|
| 71 | + assert self.PROJECT_CONFIG_C is not None |
66 | 72 | cmd += ['-Iinclude', '-Idrivers/builtin/include', '-I.',
|
67 | 73 | '-I' + os.path.dirname(self.PROJECT_CONFIG_C),
|
68 | 74 | self.PROJECT_CONFIG_C]
|
@@ -149,3 +155,45 @@ def test_nominal(self) -> None:
|
149 | 155 | def test_error(self) -> None:
|
150 | 156 | self.bad_case('#error "Bad crypto configuration"',
|
151 | 157 | error='"Bad crypto configuration"')
|
| 158 | + |
| 159 | + @staticmethod |
| 160 | + def normalize_generated_file_content(content) -> None: |
| 161 | + """Normalize the content of a generated file. |
| 162 | +
|
| 163 | + The file content is mostly deterministic, but it includes the |
| 164 | + name of the program that generated it. That name is different |
| 165 | + when we generate it from the test code, so we erase the program |
| 166 | + name from the content. |
| 167 | + """ |
| 168 | + return re.sub(r'([Gg]enerated by )\S+(\.py\W*\s)', |
| 169 | + r'\1normalized\2', |
| 170 | + content) |
| 171 | + |
| 172 | + def check_generated_file(self, |
| 173 | + production_directory: str, |
| 174 | + new_file: str) -> None: |
| 175 | + """Check whether a generated file is up to date.""" |
| 176 | + production_file = os.path.join(production_directory, |
| 177 | + os.path.basename(new_file)) |
| 178 | + self.assertTrue(os.path.exists(production_file)) |
| 179 | + with open(new_file) as inp: |
| 180 | + new_content = inp.read() |
| 181 | + with open(production_file) as inp: |
| 182 | + production_content = inp.read() |
| 183 | + # The second line contains "generated by test_script.py" here |
| 184 | + # but "generated by production_script.py" in the production |
| 185 | + # file. Everything else should be identical. |
| 186 | + self.assertEqual( |
| 187 | + self.normalize_generated_file_content(new_content), |
| 188 | + self.normalize_generated_file_content(production_content)) |
| 189 | + |
| 190 | + def up_to_date_case(self, |
| 191 | + production_data: config_checks_generator.BranchData |
| 192 | + ) -> None: |
| 193 | + """Check whether the generated files are up to date.""" |
| 194 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 195 | + temp_data = production_data._replace(header_directory=temp_dir) |
| 196 | + config_checks_generator.generate_header_files(os.curdir, temp_data) |
| 197 | + for new_file in glob.glob(temp_dir + '/*'): |
| 198 | + self.check_generated_file(production_data.header_directory, |
| 199 | + new_file) |
0 commit comments