Skip to content

Commit 3f382e2

Browse files
Error out if a test case uses an unknown macro name
Insist that test cases must only use macro names that are declared in a header. This may catch errors such as not parsing the intended files. Make this check easily overridden in a derived class.
1 parent d58e3a5 commit 3f382e2

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

tests/scripts/test_psa_constant_names.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Inputs:
6868
"""
6969

7070
def __init__(self):
71+
self.all_declared = set()
7172
# Sets of names per type
7273
self.statuses = set(['PSA_SUCCESS'])
7374
self.algorithms = set(['0xffffffff'])
@@ -202,6 +203,7 @@ def parse_header_line(self, line):
202203
if not m:
203204
return
204205
name = m.group(1)
206+
self.all_declared.add(name)
205207
if re.search(self._excluded_name_re, name) or \
206208
name in self._excluded_names:
207209
return
@@ -218,6 +220,19 @@ def parse_header(self, filename):
218220
for line in lines:
219221
self.parse_header_line(line)
220222

223+
_macro_identifier_re = r'[A-Z]\w+'
224+
def generate_undeclared_names(self, expr):
225+
for name in re.findall(self._macro_identifier_re, expr):
226+
if name not in self.all_declared:
227+
yield name
228+
229+
def accept_test_case_line(self, function, argument):
230+
#pylint: disable=unused-argument
231+
undeclared = list(self.generate_undeclared_names(argument))
232+
if undeclared:
233+
raise Exception('Undeclared names in test case', undeclared)
234+
return True
235+
221236
def add_test_case_line(self, function, argument):
222237
"""Parse a test case data line, looking for algorithm metadata tests."""
223238
sets = []
@@ -239,8 +254,11 @@ def add_test_case_line(self, function, argument):
239254
sets.append(self.ecc_curves)
240255
elif function == 'dh_key_types':
241256
sets.append(self.dh_groups)
242-
for s in sets:
243-
s.add(argument)
257+
else:
258+
return
259+
if self.accept_test_case_line(function, argument):
260+
for s in sets:
261+
s.add(argument)
244262

245263
# Regex matching a *.data line containing a test function call and
246264
# its arguments. The actual definition is partly positional, but this

0 commit comments

Comments
 (0)