Skip to content

Commit 2bcfc71

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 98a710c commit 2bcfc71

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

tests/scripts/test_psa_constant_names.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Inputs:
6969
"""
7070

7171
def __init__(self):
72+
self.all_declared = set()
7273
# Sets of names per type
7374
self.statuses = set(['PSA_SUCCESS'])
7475
self.algorithms = set(['0xffffffff'])
@@ -213,6 +214,7 @@ def parse_header_line(self, line):
213214
if not m:
214215
return
215216
name = m.group(1)
217+
self.all_declared.add(name)
216218
if re.search(self._excluded_name_re, name) or \
217219
name in self._excluded_names:
218220
return
@@ -229,6 +231,19 @@ def parse_header(self, filename):
229231
for line in lines:
230232
self.parse_header_line(line)
231233

234+
_macro_identifier_re = r'[A-Z]\w+'
235+
def generate_undeclared_names(self, expr):
236+
for name in re.findall(self._macro_identifier_re, expr):
237+
if name not in self.all_declared:
238+
yield name
239+
240+
def accept_test_case_line(self, function, argument):
241+
#pylint: disable=unused-argument
242+
undeclared = list(self.generate_undeclared_names(argument))
243+
if undeclared:
244+
raise Exception('Undeclared names in test case', undeclared)
245+
return True
246+
232247
def add_test_case_line(self, function, argument):
233248
"""Parse a test case data line, looking for algorithm metadata tests."""
234249
sets = []
@@ -240,8 +255,9 @@ def add_test_case_line(self, function, argument):
240255
sets.append(self.algorithms)
241256
if function in self.table_by_test_function:
242257
sets.append(self.table_by_test_function[function])
243-
for s in sets:
244-
s.add(argument)
258+
if self.accept_test_case_line(function, argument):
259+
for s in sets:
260+
s.add(argument)
245261

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

0 commit comments

Comments
 (0)