Skip to content

Commit c00ddf3

Browse files
Test that the generated files are up to date
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent ac64fe7 commit c00ddf3

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

scripts/mbedtls_framework/test_config_checks_generator.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
## Copyright The Mbed TLS Contributors
55
## SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
66

7+
import glob
78
import os
9+
import re
810
import subprocess
911
import sys
12+
import tempfile
1013
import unittest
1114
from typing import List, Optional, Pattern, Union
1215

16+
from . import config_checks_generator
17+
1318

1419
class TestConfigChecks(unittest.TestCase):
1520
"""Unit tests for checks generated by config_checks_generator."""
@@ -63,6 +68,7 @@ def run_with_config_files(self,
6368
if mbedtls_user_config_file is not None:
6469
cmd.append(f'-DMBEDTLS_USER_CONFIG_FILE="{mbedtls_user_config_file}"')
6570
cmd += extra_options
71+
assert self.PROJECT_CONFIG_C is not None
6672
cmd += ['-Iinclude', '-Idrivers/builtin/include', '-I.',
6773
'-I' + os.path.dirname(self.PROJECT_CONFIG_C),
6874
self.PROJECT_CONFIG_C]
@@ -149,3 +155,45 @@ def test_nominal(self) -> None:
149155
def test_error(self) -> None:
150156
self.bad_case('#error "Bad crypto configuration"',
151157
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

Comments
 (0)