generate_test_code.py 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. #!/usr/bin/env python3
  2. # Test suites code generator.
  3. #
  4. # Copyright The Mbed TLS Contributors
  5. # SPDX-License-Identifier: Apache-2.0
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. """
  19. This script is a key part of Mbed TLS test suites framework. For
  20. understanding the script it is important to understand the
  21. framework. This doc string contains a summary of the framework
  22. and explains the function of this script.
  23. Mbed TLS test suites:
  24. =====================
  25. Scope:
  26. ------
  27. The test suites focus on unit testing the crypto primitives and also
  28. include x509 parser tests. Tests can be added to test any Mbed TLS
  29. module. However, the framework is not capable of testing SSL
  30. protocol, since that requires full stack execution and that is best
  31. tested as part of the system test.
  32. Test case definition:
  33. ---------------------
  34. Tests are defined in a test_suite_<module>[.<optional sub module>].data
  35. file. A test definition contains:
  36. test name
  37. optional build macro dependencies
  38. test function
  39. test parameters
  40. Test dependencies are build macros that can be specified to indicate
  41. the build config in which the test is valid. For example if a test
  42. depends on a feature that is only enabled by defining a macro. Then
  43. that macro should be specified as a dependency of the test.
  44. Test function is the function that implements the test steps. This
  45. function is specified for different tests that perform same steps
  46. with different parameters.
  47. Test parameters are specified in string form separated by ':'.
  48. Parameters can be of type string, binary data specified as hex
  49. string and integer constants specified as integer, macro or
  50. as an expression. Following is an example test definition:
  51. AES 128 GCM Encrypt and decrypt 8 bytes
  52. depends_on:MBEDTLS_AES_C:MBEDTLS_GCM_C
  53. enc_dec_buf:MBEDTLS_CIPHER_AES_128_GCM:"AES-128-GCM":128:8:-1
  54. Test functions:
  55. ---------------
  56. Test functions are coded in C in test_suite_<module>.function files.
  57. Functions file is itself not compilable and contains special
  58. format patterns to specify test suite dependencies, start and end
  59. of functions and function dependencies. Check any existing functions
  60. file for example.
  61. Execution:
  62. ----------
  63. Tests are executed in 3 steps:
  64. - Generating test_suite_<module>[.<optional sub module>].c file
  65. for each corresponding .data file.
  66. - Building each source file into executables.
  67. - Running each executable and printing report.
  68. Generating C test source requires more than just the test functions.
  69. Following extras are required:
  70. - Process main()
  71. - Reading .data file and dispatching test cases.
  72. - Platform specific test case execution
  73. - Dependency checking
  74. - Integer expression evaluation
  75. - Test function dispatch
  76. Build dependencies and integer expressions (in the test parameters)
  77. are specified as strings in the .data file. Their run time value is
  78. not known at the generation stage. Hence, they need to be translated
  79. into run time evaluations. This script generates the run time checks
  80. for dependencies and integer expressions.
  81. Similarly, function names have to be translated into function calls.
  82. This script also generates code for function dispatch.
  83. The extra code mentioned here is either generated by this script
  84. or it comes from the input files: helpers file, platform file and
  85. the template file.
  86. Helper file:
  87. ------------
  88. Helpers file contains common helper/utility functions and data.
  89. Platform file:
  90. --------------
  91. Platform file contains platform specific setup code and test case
  92. dispatch code. For example, host_test.function reads test data
  93. file from host's file system and dispatches tests.
  94. Template file:
  95. ---------
  96. Template file for example main_test.function is a template C file in
  97. which generated code and code from input files is substituted to
  98. generate a compilable C file. It also contains skeleton functions for
  99. dependency checks, expression evaluation and function dispatch. These
  100. functions are populated with checks and return codes by this script.
  101. Template file contains "replacement" fields that are formatted
  102. strings processed by Python string.Template.substitute() method.
  103. This script:
  104. ============
  105. Core function of this script is to fill the template file with
  106. code that is generated or read from helpers and platform files.
  107. This script replaces following fields in the template and generates
  108. the test source file:
  109. __MBEDTLS_TEST_TEMPLATE__TEST_COMMON_HELPERS
  110. All common code from helpers.function
  111. is substituted here.
  112. __MBEDTLS_TEST_TEMPLATE__FUNCTIONS_CODE
  113. Test functions are substituted here
  114. from the input test_suit_xyz.function
  115. file. C preprocessor checks are generated
  116. for the build dependencies specified
  117. in the input file. This script also
  118. generates wrappers for the test
  119. functions with code to expand the
  120. string parameters read from the data
  121. file.
  122. __MBEDTLS_TEST_TEMPLATE__EXPRESSION_CODE
  123. This script enumerates the
  124. expressions in the .data file and
  125. generates code to handle enumerated
  126. expression Ids and return the values.
  127. __MBEDTLS_TEST_TEMPLATE__DEP_CHECK_CODE
  128. This script enumerates all
  129. build dependencies and generate
  130. code to handle enumerated build
  131. dependency Id and return status: if
  132. the dependency is defined or not.
  133. __MBEDTLS_TEST_TEMPLATE__DISPATCH_CODE
  134. This script enumerates the functions
  135. specified in the input test data file
  136. and generates the initializer for the
  137. function table in the template
  138. file.
  139. __MBEDTLS_TEST_TEMPLATE__PLATFORM_CODE
  140. Platform specific setup and test
  141. dispatch code.
  142. """
  143. import io
  144. import os
  145. import re
  146. import sys
  147. import string
  148. import argparse
  149. BEGIN_HEADER_REGEX = r'/\*\s*BEGIN_HEADER\s*\*/'
  150. END_HEADER_REGEX = r'/\*\s*END_HEADER\s*\*/'
  151. BEGIN_SUITE_HELPERS_REGEX = r'/\*\s*BEGIN_SUITE_HELPERS\s*\*/'
  152. END_SUITE_HELPERS_REGEX = r'/\*\s*END_SUITE_HELPERS\s*\*/'
  153. BEGIN_DEP_REGEX = r'BEGIN_DEPENDENCIES'
  154. END_DEP_REGEX = r'END_DEPENDENCIES'
  155. BEGIN_CASE_REGEX = r'/\*\s*BEGIN_CASE\s*(?P<depends_on>.*?)\s*\*/'
  156. END_CASE_REGEX = r'/\*\s*END_CASE\s*\*/'
  157. DEPENDENCY_REGEX = r'depends_on:(?P<dependencies>.*)'
  158. C_IDENTIFIER_REGEX = r'!?[a-z_][a-z0-9_]*'
  159. CONDITION_OPERATOR_REGEX = r'[!=]=|[<>]=?'
  160. # forbid 0ddd which might be accidentally octal or accidentally decimal
  161. CONDITION_VALUE_REGEX = r'[-+]?(0x[0-9a-f]+|0|[1-9][0-9]*)'
  162. CONDITION_REGEX = r'({})(?:\s*({})\s*({}))?$'.format(C_IDENTIFIER_REGEX,
  163. CONDITION_OPERATOR_REGEX,
  164. CONDITION_VALUE_REGEX)
  165. TEST_FUNCTION_VALIDATION_REGEX = r'\s*void\s+(?P<func_name>\w+)\s*\('
  166. INT_CHECK_REGEX = r'int\s+.*'
  167. CHAR_CHECK_REGEX = r'char\s*\*\s*.*'
  168. DATA_T_CHECK_REGEX = r'data_t\s*\*\s*.*'
  169. FUNCTION_ARG_LIST_END_REGEX = r'.*\)'
  170. EXIT_LABEL_REGEX = r'^exit:'
  171. class GeneratorInputError(Exception):
  172. """
  173. Exception to indicate error in the input files to this script.
  174. This includes missing patterns, test function names and other
  175. parsing errors.
  176. """
  177. pass
  178. class FileWrapper(io.FileIO):
  179. """
  180. This class extends built-in io.FileIO class with attribute line_no,
  181. that indicates line number for the line that is read.
  182. """
  183. def __init__(self, file_name):
  184. """
  185. Instantiate the base class and initialize the line number to 0.
  186. :param file_name: File path to open.
  187. """
  188. super().__init__(file_name, 'r')
  189. self._line_no = 0
  190. def __next__(self):
  191. """
  192. This method overrides base class's __next__ method and extends it
  193. method to count the line numbers as each line is read.
  194. :return: Line read from file.
  195. """
  196. line = super().__next__()
  197. if line is not None:
  198. self._line_no += 1
  199. # Convert byte array to string with correct encoding and
  200. # strip any whitespaces added in the decoding process.
  201. return line.decode(sys.getdefaultencoding()).rstrip() + '\n'
  202. return None
  203. def get_line_no(self):
  204. """
  205. Gives current line number.
  206. """
  207. return self._line_no
  208. line_no = property(get_line_no)
  209. def split_dep(dep):
  210. """
  211. Split NOT character '!' from dependency. Used by gen_dependencies()
  212. :param dep: Dependency list
  213. :return: string tuple. Ex: ('!', MACRO) for !MACRO and ('', MACRO) for
  214. MACRO.
  215. """
  216. return ('!', dep[1:]) if dep[0] == '!' else ('', dep)
  217. def gen_dependencies(dependencies):
  218. """
  219. Test suite data and functions specifies compile time dependencies.
  220. This function generates C preprocessor code from the input
  221. dependency list. Caller uses the generated preprocessor code to
  222. wrap dependent code.
  223. A dependency in the input list can have a leading '!' character
  224. to negate a condition. '!' is separated from the dependency using
  225. function split_dep() and proper preprocessor check is generated
  226. accordingly.
  227. :param dependencies: List of dependencies.
  228. :return: if defined and endif code with macro annotations for
  229. readability.
  230. """
  231. dep_start = ''.join(['#if %sdefined(%s)\n' % (x, y) for x, y in
  232. map(split_dep, dependencies)])
  233. dep_end = ''.join(['#endif /* %s */\n' %
  234. x for x in reversed(dependencies)])
  235. return dep_start, dep_end
  236. def gen_dependencies_one_line(dependencies):
  237. """
  238. Similar to gen_dependencies() but generates dependency checks in one line.
  239. Useful for generating code with #else block.
  240. :param dependencies: List of dependencies.
  241. :return: Preprocessor check code
  242. """
  243. defines = '#if ' if dependencies else ''
  244. defines += ' && '.join(['%sdefined(%s)' % (x, y) for x, y in map(
  245. split_dep, dependencies)])
  246. return defines
  247. def gen_function_wrapper(name, local_vars, args_dispatch):
  248. """
  249. Creates test function wrapper code. A wrapper has the code to
  250. unpack parameters from parameters[] array.
  251. :param name: Test function name
  252. :param local_vars: Local variables declaration code
  253. :param args_dispatch: List of dispatch arguments.
  254. Ex: ['(char *)params[0]', '*((int *)params[1])']
  255. :return: Test function wrapper.
  256. """
  257. # Then create the wrapper
  258. wrapper = '''
  259. void {name}_wrapper( void ** params )
  260. {{
  261. {unused_params}{locals}
  262. {name}( {args} );
  263. }}
  264. '''.format(name=name,
  265. unused_params='' if args_dispatch else ' (void)params;\n',
  266. args=', '.join(args_dispatch),
  267. locals=local_vars)
  268. return wrapper
  269. def gen_dispatch(name, dependencies):
  270. """
  271. Test suite code template main_test.function defines a C function
  272. array to contain test case functions. This function generates an
  273. initializer entry for a function in that array. The entry is
  274. composed of a compile time check for the test function
  275. dependencies. At compile time the test function is assigned when
  276. dependencies are met, else NULL is assigned.
  277. :param name: Test function name
  278. :param dependencies: List of dependencies
  279. :return: Dispatch code.
  280. """
  281. if dependencies:
  282. preprocessor_check = gen_dependencies_one_line(dependencies)
  283. dispatch_code = '''
  284. {preprocessor_check}
  285. {name}_wrapper,
  286. #else
  287. NULL,
  288. #endif
  289. '''.format(preprocessor_check=preprocessor_check, name=name)
  290. else:
  291. dispatch_code = '''
  292. {name}_wrapper,
  293. '''.format(name=name)
  294. return dispatch_code
  295. def parse_until_pattern(funcs_f, end_regex):
  296. """
  297. Matches pattern end_regex to the lines read from the file object.
  298. Returns the lines read until end pattern is matched.
  299. :param funcs_f: file object for .function file
  300. :param end_regex: Pattern to stop parsing
  301. :return: Lines read before the end pattern
  302. """
  303. headers = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name)
  304. for line in funcs_f:
  305. if re.search(end_regex, line):
  306. break
  307. headers += line
  308. else:
  309. raise GeneratorInputError("file: %s - end pattern [%s] not found!" %
  310. (funcs_f.name, end_regex))
  311. return headers
  312. def validate_dependency(dependency):
  313. """
  314. Validates a C macro and raises GeneratorInputError on invalid input.
  315. :param dependency: Input macro dependency
  316. :return: input dependency stripped of leading & trailing white spaces.
  317. """
  318. dependency = dependency.strip()
  319. if not re.match(CONDITION_REGEX, dependency, re.I):
  320. raise GeneratorInputError('Invalid dependency %s' % dependency)
  321. return dependency
  322. def parse_dependencies(inp_str):
  323. """
  324. Parses dependencies out of inp_str, validates them and returns a
  325. list of macros.
  326. :param inp_str: Input string with macros delimited by ':'.
  327. :return: list of dependencies
  328. """
  329. dependencies = list(map(validate_dependency, inp_str.split(':')))
  330. return dependencies
  331. def parse_suite_dependencies(funcs_f):
  332. """
  333. Parses test suite dependencies specified at the top of a
  334. .function file, that starts with pattern BEGIN_DEPENDENCIES
  335. and end with END_DEPENDENCIES. Dependencies are specified
  336. after pattern 'depends_on:' and are delimited by ':'.
  337. :param funcs_f: file object for .function file
  338. :return: List of test suite dependencies.
  339. """
  340. dependencies = []
  341. for line in funcs_f:
  342. match = re.search(DEPENDENCY_REGEX, line.strip())
  343. if match:
  344. try:
  345. dependencies = parse_dependencies(match.group('dependencies'))
  346. except GeneratorInputError as error:
  347. raise GeneratorInputError(
  348. str(error) + " - %s:%d" % (funcs_f.name, funcs_f.line_no))
  349. if re.search(END_DEP_REGEX, line):
  350. break
  351. else:
  352. raise GeneratorInputError("file: %s - end dependency pattern [%s]"
  353. " not found!" % (funcs_f.name,
  354. END_DEP_REGEX))
  355. return dependencies
  356. def parse_function_dependencies(line):
  357. """
  358. Parses function dependencies, that are in the same line as
  359. comment BEGIN_CASE. Dependencies are specified after pattern
  360. 'depends_on:' and are delimited by ':'.
  361. :param line: Line from .function file that has dependencies.
  362. :return: List of dependencies.
  363. """
  364. dependencies = []
  365. match = re.search(BEGIN_CASE_REGEX, line)
  366. dep_str = match.group('depends_on')
  367. if dep_str:
  368. match = re.search(DEPENDENCY_REGEX, dep_str)
  369. if match:
  370. dependencies += parse_dependencies(match.group('dependencies'))
  371. return dependencies
  372. def parse_function_arguments(line):
  373. """
  374. Parses test function signature for validation and generates
  375. a dispatch wrapper function that translates input test vectors
  376. read from the data file into test function arguments.
  377. :param line: Line from .function file that has a function
  378. signature.
  379. :return: argument list, local variables for
  380. wrapper function and argument dispatch code.
  381. """
  382. args = []
  383. local_vars = ''
  384. args_dispatch = []
  385. arg_idx = 0
  386. # Remove characters before arguments
  387. line = line[line.find('(') + 1:]
  388. # Process arguments, ex: <type> arg1, <type> arg2 )
  389. # This script assumes that the argument list is terminated by ')'
  390. # i.e. the test functions will not have a function pointer
  391. # argument.
  392. for arg in line[:line.find(')')].split(','):
  393. arg = arg.strip()
  394. if arg == '':
  395. continue
  396. if re.search(INT_CHECK_REGEX, arg.strip()):
  397. args.append('int')
  398. args_dispatch.append('*( (int *) params[%d] )' % arg_idx)
  399. elif re.search(CHAR_CHECK_REGEX, arg.strip()):
  400. args.append('char*')
  401. args_dispatch.append('(char *) params[%d]' % arg_idx)
  402. elif re.search(DATA_T_CHECK_REGEX, arg.strip()):
  403. args.append('hex')
  404. # create a structure
  405. pointer_initializer = '(uint8_t *) params[%d]' % arg_idx
  406. len_initializer = '*( (uint32_t *) params[%d] )' % (arg_idx+1)
  407. local_vars += """ data_t data%d = {%s, %s};
  408. """ % (arg_idx, pointer_initializer, len_initializer)
  409. args_dispatch.append('&data%d' % arg_idx)
  410. arg_idx += 1
  411. else:
  412. raise ValueError("Test function arguments can only be 'int', "
  413. "'char *' or 'data_t'\n%s" % line)
  414. arg_idx += 1
  415. return args, local_vars, args_dispatch
  416. def generate_function_code(name, code, local_vars, args_dispatch,
  417. dependencies):
  418. """
  419. Generate function code with preprocessor checks and parameter dispatch
  420. wrapper.
  421. :param name: Function name
  422. :param code: Function code
  423. :param local_vars: Local variables for function wrapper
  424. :param args_dispatch: Argument dispatch code
  425. :param dependencies: Preprocessor dependencies list
  426. :return: Final function code
  427. """
  428. # Add exit label if not present
  429. if code.find('exit:') == -1:
  430. split_code = code.rsplit('}', 1)
  431. if len(split_code) == 2:
  432. code = """exit:
  433. ;
  434. }""".join(split_code)
  435. code += gen_function_wrapper(name, local_vars, args_dispatch)
  436. preprocessor_check_start, preprocessor_check_end = \
  437. gen_dependencies(dependencies)
  438. return preprocessor_check_start + code + preprocessor_check_end
  439. COMMENT_START_REGEX = re.compile(r'/[*/]')
  440. def skip_comments(line, stream):
  441. """Remove comments in line.
  442. If the line contains an unfinished comment, read more lines from stream
  443. until the line that contains the comment.
  444. :return: The original line with inner comments replaced by spaces.
  445. Trailing comments and whitespace may be removed completely.
  446. """
  447. pos = 0
  448. while True:
  449. opening = COMMENT_START_REGEX.search(line, pos)
  450. if not opening:
  451. break
  452. if line[opening.start(0) + 1] == '/': # //...
  453. continuation = line
  454. # Count the number of line breaks, to keep line numbers aligned
  455. # in the output.
  456. line_count = 1
  457. while continuation.endswith('\\\n'):
  458. # This errors out if the file ends with an unfinished line
  459. # comment. That's acceptable to not complicate the code further.
  460. continuation = next(stream)
  461. line_count += 1
  462. return line[:opening.start(0)].rstrip() + '\n' * line_count
  463. # Parsing /*...*/, looking for the end
  464. closing = line.find('*/', opening.end(0))
  465. while closing == -1:
  466. # This errors out if the file ends with an unfinished block
  467. # comment. That's acceptable to not complicate the code further.
  468. line += next(stream)
  469. closing = line.find('*/', opening.end(0))
  470. pos = closing + 2
  471. # Replace inner comment by spaces. There needs to be at least one space
  472. # for things like 'int/*ihatespaces*/foo'. Go further and preserve the
  473. # width of the comment and line breaks, this way positions in error
  474. # messages remain correct.
  475. line = (line[:opening.start(0)] +
  476. re.sub(r'.', r' ', line[opening.start(0):pos]) +
  477. line[pos:])
  478. # Strip whitespace at the end of lines (it's irrelevant to error messages).
  479. return re.sub(r' +(\n|\Z)', r'\1', line)
  480. def parse_function_code(funcs_f, dependencies, suite_dependencies):
  481. """
  482. Parses out a function from function file object and generates
  483. function and dispatch code.
  484. :param funcs_f: file object of the functions file.
  485. :param dependencies: List of dependencies
  486. :param suite_dependencies: List of test suite dependencies
  487. :return: Function name, arguments, function code and dispatch code.
  488. """
  489. line_directive = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name)
  490. code = ''
  491. has_exit_label = False
  492. for line in funcs_f:
  493. # Check function signature. Function signature may be split
  494. # across multiple lines. Here we try to find the start of
  495. # arguments list, then remove '\n's and apply the regex to
  496. # detect function start.
  497. line = skip_comments(line, funcs_f)
  498. up_to_arg_list_start = code + line[:line.find('(') + 1]
  499. match = re.match(TEST_FUNCTION_VALIDATION_REGEX,
  500. up_to_arg_list_start.replace('\n', ' '), re.I)
  501. if match:
  502. # check if we have full signature i.e. split in more lines
  503. name = match.group('func_name')
  504. if not re.match(FUNCTION_ARG_LIST_END_REGEX, line):
  505. for lin in funcs_f:
  506. line += skip_comments(lin, funcs_f)
  507. if re.search(FUNCTION_ARG_LIST_END_REGEX, line):
  508. break
  509. args, local_vars, args_dispatch = parse_function_arguments(
  510. line)
  511. code += line
  512. break
  513. code += line
  514. else:
  515. raise GeneratorInputError("file: %s - Test functions not found!" %
  516. funcs_f.name)
  517. # Prefix test function name with 'test_'
  518. code = code.replace(name, 'test_' + name, 1)
  519. name = 'test_' + name
  520. for line in funcs_f:
  521. if re.search(END_CASE_REGEX, line):
  522. break
  523. if not has_exit_label:
  524. has_exit_label = \
  525. re.search(EXIT_LABEL_REGEX, line.strip()) is not None
  526. code += line
  527. else:
  528. raise GeneratorInputError("file: %s - end case pattern [%s] not "
  529. "found!" % (funcs_f.name, END_CASE_REGEX))
  530. code = line_directive + code
  531. code = generate_function_code(name, code, local_vars, args_dispatch,
  532. dependencies)
  533. dispatch_code = gen_dispatch(name, suite_dependencies + dependencies)
  534. return (name, args, code, dispatch_code)
  535. def parse_functions(funcs_f):
  536. """
  537. Parses a test_suite_xxx.function file and returns information
  538. for generating a C source file for the test suite.
  539. :param funcs_f: file object of the functions file.
  540. :return: List of test suite dependencies, test function dispatch
  541. code, function code and a dict with function identifiers
  542. and arguments info.
  543. """
  544. suite_helpers = ''
  545. suite_dependencies = []
  546. suite_functions = ''
  547. func_info = {}
  548. function_idx = 0
  549. dispatch_code = ''
  550. for line in funcs_f:
  551. if re.search(BEGIN_HEADER_REGEX, line):
  552. suite_helpers += parse_until_pattern(funcs_f, END_HEADER_REGEX)
  553. elif re.search(BEGIN_SUITE_HELPERS_REGEX, line):
  554. suite_helpers += parse_until_pattern(funcs_f,
  555. END_SUITE_HELPERS_REGEX)
  556. elif re.search(BEGIN_DEP_REGEX, line):
  557. suite_dependencies += parse_suite_dependencies(funcs_f)
  558. elif re.search(BEGIN_CASE_REGEX, line):
  559. try:
  560. dependencies = parse_function_dependencies(line)
  561. except GeneratorInputError as error:
  562. raise GeneratorInputError(
  563. "%s:%d: %s" % (funcs_f.name, funcs_f.line_no,
  564. str(error)))
  565. func_name, args, func_code, func_dispatch =\
  566. parse_function_code(funcs_f, dependencies, suite_dependencies)
  567. suite_functions += func_code
  568. # Generate dispatch code and enumeration info
  569. if func_name in func_info:
  570. raise GeneratorInputError(
  571. "file: %s - function %s re-declared at line %d" %
  572. (funcs_f.name, func_name, funcs_f.line_no))
  573. func_info[func_name] = (function_idx, args)
  574. dispatch_code += '/* Function Id: %d */\n' % function_idx
  575. dispatch_code += func_dispatch
  576. function_idx += 1
  577. func_code = (suite_helpers +
  578. suite_functions).join(gen_dependencies(suite_dependencies))
  579. return suite_dependencies, dispatch_code, func_code, func_info
  580. def escaped_split(inp_str, split_char):
  581. """
  582. Split inp_str on character split_char but ignore if escaped.
  583. Since, return value is used to write back to the intermediate
  584. data file, any escape characters in the input are retained in the
  585. output.
  586. :param inp_str: String to split
  587. :param split_char: Split character
  588. :return: List of splits
  589. """
  590. if len(split_char) > 1:
  591. raise ValueError('Expected split character. Found string!')
  592. out = re.sub(r'(\\.)|' + split_char,
  593. lambda m: m.group(1) or '\n', inp_str,
  594. len(inp_str)).split('\n')
  595. out = [x for x in out if x]
  596. return out
  597. def parse_test_data(data_f):
  598. """
  599. Parses .data file for each test case name, test function name,
  600. test dependencies and test arguments. This information is
  601. correlated with the test functions file for generating an
  602. intermediate data file replacing the strings for test function
  603. names, dependencies and integer constant expressions with
  604. identifiers. Mainly for optimising space for on-target
  605. execution.
  606. :param data_f: file object of the data file.
  607. :return: Generator that yields test name, function name,
  608. dependency list and function argument list.
  609. """
  610. __state_read_name = 0
  611. __state_read_args = 1
  612. state = __state_read_name
  613. dependencies = []
  614. name = ''
  615. for line in data_f:
  616. line = line.strip()
  617. # Skip comments
  618. if line.startswith('#'):
  619. continue
  620. # Blank line indicates end of test
  621. if not line:
  622. if state == __state_read_args:
  623. raise GeneratorInputError("[%s:%d] Newline before arguments. "
  624. "Test function and arguments "
  625. "missing for %s" %
  626. (data_f.name, data_f.line_no, name))
  627. continue
  628. if state == __state_read_name:
  629. # Read test name
  630. name = line
  631. state = __state_read_args
  632. elif state == __state_read_args:
  633. # Check dependencies
  634. match = re.search(DEPENDENCY_REGEX, line)
  635. if match:
  636. try:
  637. dependencies = parse_dependencies(
  638. match.group('dependencies'))
  639. except GeneratorInputError as error:
  640. raise GeneratorInputError(
  641. str(error) + " - %s:%d" %
  642. (data_f.name, data_f.line_no))
  643. else:
  644. # Read test vectors
  645. parts = escaped_split(line, ':')
  646. test_function = parts[0]
  647. args = parts[1:]
  648. yield name, test_function, dependencies, args
  649. dependencies = []
  650. state = __state_read_name
  651. if state == __state_read_args:
  652. raise GeneratorInputError("[%s:%d] Newline before arguments. "
  653. "Test function and arguments missing for "
  654. "%s" % (data_f.name, data_f.line_no, name))
  655. def gen_dep_check(dep_id, dep):
  656. """
  657. Generate code for checking dependency with the associated
  658. identifier.
  659. :param dep_id: Dependency identifier
  660. :param dep: Dependency macro
  661. :return: Dependency check code
  662. """
  663. if dep_id < 0:
  664. raise GeneratorInputError("Dependency Id should be a positive "
  665. "integer.")
  666. _not, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep)
  667. if not dep:
  668. raise GeneratorInputError("Dependency should not be an empty string.")
  669. dependency = re.match(CONDITION_REGEX, dep, re.I)
  670. if not dependency:
  671. raise GeneratorInputError('Invalid dependency %s' % dep)
  672. _defined = '' if dependency.group(2) else 'defined'
  673. _cond = dependency.group(2) if dependency.group(2) else ''
  674. _value = dependency.group(3) if dependency.group(3) else ''
  675. dep_check = '''
  676. case {id}:
  677. {{
  678. #if {_not}{_defined}({macro}{_cond}{_value})
  679. ret = DEPENDENCY_SUPPORTED;
  680. #else
  681. ret = DEPENDENCY_NOT_SUPPORTED;
  682. #endif
  683. }}
  684. break;'''.format(_not=_not, _defined=_defined,
  685. macro=dependency.group(1), id=dep_id,
  686. _cond=_cond, _value=_value)
  687. return dep_check
  688. def gen_expression_check(exp_id, exp):
  689. """
  690. Generates code for evaluating an integer expression using
  691. associated expression Id.
  692. :param exp_id: Expression Identifier
  693. :param exp: Expression/Macro
  694. :return: Expression check code
  695. """
  696. if exp_id < 0:
  697. raise GeneratorInputError("Expression Id should be a positive "
  698. "integer.")
  699. if not exp:
  700. raise GeneratorInputError("Expression should not be an empty string.")
  701. exp_code = '''
  702. case {exp_id}:
  703. {{
  704. *out_value = {expression};
  705. }}
  706. break;'''.format(exp_id=exp_id, expression=exp)
  707. return exp_code
  708. def write_dependencies(out_data_f, test_dependencies, unique_dependencies):
  709. """
  710. Write dependencies to intermediate test data file, replacing
  711. the string form with identifiers. Also, generates dependency
  712. check code.
  713. :param out_data_f: Output intermediate data file
  714. :param test_dependencies: Dependencies
  715. :param unique_dependencies: Mutable list to track unique dependencies
  716. that are global to this re-entrant function.
  717. :return: returns dependency check code.
  718. """
  719. dep_check_code = ''
  720. if test_dependencies:
  721. out_data_f.write('depends_on')
  722. for dep in test_dependencies:
  723. if dep not in unique_dependencies:
  724. unique_dependencies.append(dep)
  725. dep_id = unique_dependencies.index(dep)
  726. dep_check_code += gen_dep_check(dep_id, dep)
  727. else:
  728. dep_id = unique_dependencies.index(dep)
  729. out_data_f.write(':' + str(dep_id))
  730. out_data_f.write('\n')
  731. return dep_check_code
  732. def write_parameters(out_data_f, test_args, func_args, unique_expressions):
  733. """
  734. Writes test parameters to the intermediate data file, replacing
  735. the string form with identifiers. Also, generates expression
  736. check code.
  737. :param out_data_f: Output intermediate data file
  738. :param test_args: Test parameters
  739. :param func_args: Function arguments
  740. :param unique_expressions: Mutable list to track unique
  741. expressions that are global to this re-entrant function.
  742. :return: Returns expression check code.
  743. """
  744. expression_code = ''
  745. for i, _ in enumerate(test_args):
  746. typ = func_args[i]
  747. val = test_args[i]
  748. # check if val is a non literal int val (i.e. an expression)
  749. if typ == 'int' and not re.match(r'(\d+|0x[0-9a-f]+)$',
  750. val, re.I):
  751. typ = 'exp'
  752. if val not in unique_expressions:
  753. unique_expressions.append(val)
  754. # exp_id can be derived from len(). But for
  755. # readability and consistency with case of existing
  756. # let's use index().
  757. exp_id = unique_expressions.index(val)
  758. expression_code += gen_expression_check(exp_id, val)
  759. val = exp_id
  760. else:
  761. val = unique_expressions.index(val)
  762. out_data_f.write(':' + typ + ':' + str(val))
  763. out_data_f.write('\n')
  764. return expression_code
  765. def gen_suite_dep_checks(suite_dependencies, dep_check_code, expression_code):
  766. """
  767. Generates preprocessor checks for test suite dependencies.
  768. :param suite_dependencies: Test suite dependencies read from the
  769. .function file.
  770. :param dep_check_code: Dependency check code
  771. :param expression_code: Expression check code
  772. :return: Dependency and expression code guarded by test suite
  773. dependencies.
  774. """
  775. if suite_dependencies:
  776. preprocessor_check = gen_dependencies_one_line(suite_dependencies)
  777. dep_check_code = '''
  778. {preprocessor_check}
  779. {code}
  780. #endif
  781. '''.format(preprocessor_check=preprocessor_check, code=dep_check_code)
  782. expression_code = '''
  783. {preprocessor_check}
  784. {code}
  785. #endif
  786. '''.format(preprocessor_check=preprocessor_check, code=expression_code)
  787. return dep_check_code, expression_code
  788. def gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies):
  789. """
  790. This function reads test case name, dependencies and test vectors
  791. from the .data file. This information is correlated with the test
  792. functions file for generating an intermediate data file replacing
  793. the strings for test function names, dependencies and integer
  794. constant expressions with identifiers. Mainly for optimising
  795. space for on-target execution.
  796. It also generates test case dependency check code and expression
  797. evaluation code.
  798. :param data_f: Data file object
  799. :param out_data_f: Output intermediate data file
  800. :param func_info: Dict keyed by function and with function id
  801. and arguments info
  802. :param suite_dependencies: Test suite dependencies
  803. :return: Returns dependency and expression check code
  804. """
  805. unique_dependencies = []
  806. unique_expressions = []
  807. dep_check_code = ''
  808. expression_code = ''
  809. for test_name, function_name, test_dependencies, test_args in \
  810. parse_test_data(data_f):
  811. out_data_f.write(test_name + '\n')
  812. # Write dependencies
  813. dep_check_code += write_dependencies(out_data_f, test_dependencies,
  814. unique_dependencies)
  815. # Write test function name
  816. test_function_name = 'test_' + function_name
  817. if test_function_name not in func_info:
  818. raise GeneratorInputError("Function %s not found!" %
  819. test_function_name)
  820. func_id, func_args = func_info[test_function_name]
  821. out_data_f.write(str(func_id))
  822. # Write parameters
  823. if len(test_args) != len(func_args):
  824. raise GeneratorInputError("Invalid number of arguments in test "
  825. "%s. See function %s signature." %
  826. (test_name, function_name))
  827. expression_code += write_parameters(out_data_f, test_args, func_args,
  828. unique_expressions)
  829. # Write a newline as test case separator
  830. out_data_f.write('\n')
  831. dep_check_code, expression_code = gen_suite_dep_checks(
  832. suite_dependencies, dep_check_code, expression_code)
  833. return dep_check_code, expression_code
  834. def add_input_info(funcs_file, data_file, template_file,
  835. c_file, snippets):
  836. """
  837. Add generator input info in snippets.
  838. :param funcs_file: Functions file object
  839. :param data_file: Data file object
  840. :param template_file: Template file object
  841. :param c_file: Output C file object
  842. :param snippets: Dictionary to contain code pieces to be
  843. substituted in the template.
  844. :return:
  845. """
  846. snippets['test_file'] = c_file
  847. snippets['test_main_file'] = template_file
  848. snippets['test_case_file'] = funcs_file
  849. snippets['test_case_data_file'] = data_file
  850. def read_code_from_input_files(platform_file, helpers_file,
  851. out_data_file, snippets):
  852. """
  853. Read code from input files and create substitutions for replacement
  854. strings in the template file.
  855. :param platform_file: Platform file object
  856. :param helpers_file: Helper functions file object
  857. :param out_data_file: Output intermediate data file object
  858. :param snippets: Dictionary to contain code pieces to be
  859. substituted in the template.
  860. :return:
  861. """
  862. # Read helpers
  863. with open(helpers_file, 'r') as help_f, open(platform_file, 'r') as \
  864. platform_f:
  865. snippets['test_common_helper_file'] = helpers_file
  866. snippets['test_common_helpers'] = help_f.read()
  867. snippets['test_platform_file'] = platform_file
  868. snippets['platform_code'] = platform_f.read().replace(
  869. 'DATA_FILE', out_data_file.replace('\\', '\\\\')) # escape '\'
  870. def write_test_source_file(template_file, c_file, snippets):
  871. """
  872. Write output source file with generated source code.
  873. :param template_file: Template file name
  874. :param c_file: Output source file
  875. :param snippets: Generated and code snippets
  876. :return:
  877. """
  878. # Create a placeholder pattern with the correct named capture groups
  879. # to override the default provided with Template.
  880. # Match nothing (no way of escaping placeholders).
  881. escaped = "(?P<escaped>(?!))"
  882. # Match the "__MBEDTLS_TEST_TEMPLATE__PLACEHOLDER_NAME" pattern.
  883. named = "__MBEDTLS_TEST_TEMPLATE__(?P<named>[A-Z][_A-Z0-9]*)"
  884. # Match nothing (no braced placeholder syntax).
  885. braced = "(?P<braced>(?!))"
  886. # If not already matched, a "__MBEDTLS_TEST_TEMPLATE__" prefix is invalid.
  887. invalid = "(?P<invalid>__MBEDTLS_TEST_TEMPLATE__)"
  888. placeholder_pattern = re.compile("|".join([escaped, named, braced, invalid]))
  889. with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f:
  890. for line_no, line in enumerate(template_f.readlines(), 1):
  891. # Update line number. +1 as #line directive sets next line number
  892. snippets['line_no'] = line_no + 1
  893. template = string.Template(line)
  894. template.pattern = placeholder_pattern
  895. snippets = {k.upper():v for (k, v) in snippets.items()}
  896. code = template.substitute(**snippets)
  897. c_f.write(code)
  898. def parse_function_file(funcs_file, snippets):
  899. """
  900. Parse function file and generate function dispatch code.
  901. :param funcs_file: Functions file name
  902. :param snippets: Dictionary to contain code pieces to be
  903. substituted in the template.
  904. :return:
  905. """
  906. with FileWrapper(funcs_file) as funcs_f:
  907. suite_dependencies, dispatch_code, func_code, func_info = \
  908. parse_functions(funcs_f)
  909. snippets['functions_code'] = func_code
  910. snippets['dispatch_code'] = dispatch_code
  911. return suite_dependencies, func_info
  912. def generate_intermediate_data_file(data_file, out_data_file,
  913. suite_dependencies, func_info, snippets):
  914. """
  915. Generates intermediate data file from input data file and
  916. information read from functions file.
  917. :param data_file: Data file name
  918. :param out_data_file: Output/Intermediate data file
  919. :param suite_dependencies: List of suite dependencies.
  920. :param func_info: Function info parsed from functions file.
  921. :param snippets: Dictionary to contain code pieces to be
  922. substituted in the template.
  923. :return:
  924. """
  925. with FileWrapper(data_file) as data_f, \
  926. open(out_data_file, 'w') as out_data_f:
  927. dep_check_code, expression_code = gen_from_test_data(
  928. data_f, out_data_f, func_info, suite_dependencies)
  929. snippets['dep_check_code'] = dep_check_code
  930. snippets['expression_code'] = expression_code
  931. def generate_code(**input_info):
  932. """
  933. Generates C source code from test suite file, data file, common
  934. helpers file and platform file.
  935. input_info expands to following parameters:
  936. funcs_file: Functions file object
  937. data_file: Data file object
  938. template_file: Template file object
  939. platform_file: Platform file object
  940. helpers_file: Helper functions file object
  941. suites_dir: Test suites dir
  942. c_file: Output C file object
  943. out_data_file: Output intermediate data file object
  944. :return:
  945. """
  946. funcs_file = input_info['funcs_file']
  947. data_file = input_info['data_file']
  948. template_file = input_info['template_file']
  949. platform_file = input_info['platform_file']
  950. helpers_file = input_info['helpers_file']
  951. suites_dir = input_info['suites_dir']
  952. c_file = input_info['c_file']
  953. out_data_file = input_info['out_data_file']
  954. for name, path in [('Functions file', funcs_file),
  955. ('Data file', data_file),
  956. ('Template file', template_file),
  957. ('Platform file', platform_file),
  958. ('Helpers code file', helpers_file),
  959. ('Suites dir', suites_dir)]:
  960. if not os.path.exists(path):
  961. raise IOError("ERROR: %s [%s] not found!" % (name, path))
  962. snippets = {'generator_script': os.path.basename(__file__)}
  963. read_code_from_input_files(platform_file, helpers_file,
  964. out_data_file, snippets)
  965. add_input_info(funcs_file, data_file, template_file,
  966. c_file, snippets)
  967. suite_dependencies, func_info = parse_function_file(funcs_file, snippets)
  968. generate_intermediate_data_file(data_file, out_data_file,
  969. suite_dependencies, func_info, snippets)
  970. write_test_source_file(template_file, c_file, snippets)
  971. def main():
  972. """
  973. Command line parser.
  974. :return:
  975. """
  976. parser = argparse.ArgumentParser(
  977. description='Dynamically generate test suite code.')
  978. parser.add_argument("-f", "--functions-file",
  979. dest="funcs_file",
  980. help="Functions file",
  981. metavar="FUNCTIONS_FILE",
  982. required=True)
  983. parser.add_argument("-d", "--data-file",
  984. dest="data_file",
  985. help="Data file",
  986. metavar="DATA_FILE",
  987. required=True)
  988. parser.add_argument("-t", "--template-file",
  989. dest="template_file",
  990. help="Template file",
  991. metavar="TEMPLATE_FILE",
  992. required=True)
  993. parser.add_argument("-s", "--suites-dir",
  994. dest="suites_dir",
  995. help="Suites dir",
  996. metavar="SUITES_DIR",
  997. required=True)
  998. parser.add_argument("--helpers-file",
  999. dest="helpers_file",
  1000. help="Helpers file",
  1001. metavar="HELPERS_FILE",
  1002. required=True)
  1003. parser.add_argument("-p", "--platform-file",
  1004. dest="platform_file",
  1005. help="Platform code file",
  1006. metavar="PLATFORM_FILE",
  1007. required=True)
  1008. parser.add_argument("-o", "--out-dir",
  1009. dest="out_dir",
  1010. help="Dir where generated code and scripts are copied",
  1011. metavar="OUT_DIR",
  1012. required=True)
  1013. args = parser.parse_args()
  1014. data_file_name = os.path.basename(args.data_file)
  1015. data_name = os.path.splitext(data_file_name)[0]
  1016. out_c_file = os.path.join(args.out_dir, data_name + '.c')
  1017. out_data_file = os.path.join(args.out_dir, data_name + '.datax')
  1018. out_c_file_dir = os.path.dirname(out_c_file)
  1019. out_data_file_dir = os.path.dirname(out_data_file)
  1020. for directory in [out_c_file_dir, out_data_file_dir]:
  1021. if not os.path.exists(directory):
  1022. os.makedirs(directory)
  1023. generate_code(funcs_file=args.funcs_file, data_file=args.data_file,
  1024. template_file=args.template_file,
  1025. platform_file=args.platform_file,
  1026. helpers_file=args.helpers_file, suites_dir=args.suites_dir,
  1027. c_file=out_c_file, out_data_file=out_data_file)
  1028. if __name__ == "__main__":
  1029. try:
  1030. main()
  1031. except GeneratorInputError as err:
  1032. sys.exit("%s: input error: %s" %
  1033. (os.path.basename(sys.argv[0]), str(err)))