|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (C) 2026 wolfSSL Inc. |
| 3 | +# |
| 4 | +# This file is part of wolfHSM. |
| 5 | +# |
| 6 | +# wolfHSM is free software; you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation; either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +""" |
| 11 | +Generate wh_test_list.c from WH_TEST_{MISC,CLIENT,SERVER} annotations. |
| 12 | +
|
| 13 | +For every test found it emits: |
| 14 | + - A `WH_TEST_DECL(name);` line, which expands (see |
| 15 | + wh_test_list.h) to a forward prototype plus a weak skip |
| 16 | + implementation returning WH_TEST_SKIPPED. When the real |
| 17 | + test's feature gate is on, its strong definition overrides |
| 18 | + this stub at link time; otherwise the stub wins and the |
| 19 | + test surfaces as SKIPPED at runtime. |
| 20 | + - A row in whTests[]. |
| 21 | +
|
| 22 | +Preprocessor conditionals around the annotation are deliberately |
| 23 | +ignored: the link-time weak override handles the gating. All the |
| 24 | +script needs is the set of annotated function names and the group |
| 25 | +each belongs to. |
| 26 | +
|
| 27 | +Usage: |
| 28 | + gen_test_list.py --output <path> <source-dir>... |
| 29 | +""" |
| 30 | + |
| 31 | +import argparse |
| 32 | +import os |
| 33 | +import re |
| 34 | +import sys |
| 35 | + |
| 36 | +ANNOTATION_RE = re.compile( |
| 37 | + r'^\s*WH_TEST_(MISC|CLIENT|SERVER)\s+int\s+(\w+)\s*\(', |
| 38 | + re.MULTILINE, |
| 39 | +) |
| 40 | + |
| 41 | +# Strip // line comments and /* ... */ block comments so an |
| 42 | +# annotation appearing in commented-out code doesn't register a |
| 43 | +# phantom test. Strings aren't stripped; an annotation inside a |
| 44 | +# string literal would be an exotic footgun we're not solving. |
| 45 | +LINE_COMMENT_RE = re.compile(r'//[^\n]*') |
| 46 | +BLOCK_COMMENT_RE = re.compile(r'/\*.*?\*/', re.DOTALL) |
| 47 | + |
| 48 | +# Each group emits its own whTests<Group>[] registry in the |
| 49 | +# generated file. Order here fixes the section ordering in |
| 50 | +# wh_test_list.c. |
| 51 | +GROUPS = [ |
| 52 | + ('MISC', 'whTestsMisc', 'whTestsMiscCount'), |
| 53 | + ('SERVER', 'whTestsServer', 'whTestsServerCount'), |
| 54 | + ('CLIENT', 'whTestsClient', 'whTestsClientCount'), |
| 55 | +] |
| 56 | + |
| 57 | + |
| 58 | +def strip_comments(src): |
| 59 | + src = BLOCK_COMMENT_RE.sub('', src) |
| 60 | + src = LINE_COMMENT_RE.sub('', src) |
| 61 | + return src |
| 62 | + |
| 63 | + |
| 64 | +def scan_file(path): |
| 65 | + """Return [(group, fn_name)] from one source file.""" |
| 66 | + with open(path, 'r') as f: |
| 67 | + src = strip_comments(f.read()) |
| 68 | + return [(m.group(1), m.group(2)) for m in ANNOTATION_RE.finditer(src)] |
| 69 | + |
| 70 | + |
| 71 | +def discover(dirs): |
| 72 | + """Walk dirs, scan .c files, return sorted unique tests with stable group-major order.""" |
| 73 | + found = [] |
| 74 | + seen_names = {} |
| 75 | + for d in dirs: |
| 76 | + for root, _, files in os.walk(d): |
| 77 | + for name in sorted(files): |
| 78 | + if not name.endswith('.c'): |
| 79 | + continue |
| 80 | + path = os.path.join(root, name) |
| 81 | + for group, fn in scan_file(path): |
| 82 | + if fn in seen_names: |
| 83 | + prev = seen_names[fn] |
| 84 | + sys.stderr.write( |
| 85 | + "gen_test_list.py: duplicate test name '{}' " |
| 86 | + "(in {} and {})\n".format(fn, prev, path)) |
| 87 | + sys.exit(1) |
| 88 | + seen_names[fn] = path |
| 89 | + found.append((group, fn, path)) |
| 90 | + # Stable order: group (misc/server/client), then discovery order. |
| 91 | + group_order = {g[0]: i for i, g in enumerate(GROUPS)} |
| 92 | + found.sort(key=lambda t: (group_order[t[0]], t[2], t[1])) |
| 93 | + return found |
| 94 | + |
| 95 | + |
| 96 | +HEADER = """\ |
| 97 | +/* |
| 98 | + * Copyright (C) 2026 wolfSSL Inc. |
| 99 | + * |
| 100 | + * This file is part of wolfHSM. |
| 101 | + * |
| 102 | + * wolfHSM is free software; you can redistribute it and/or modify |
| 103 | + * it under the terms of the GNU General Public License as published by |
| 104 | + * the Free Software Foundation; either version 3 of the License, or |
| 105 | + * (at your option) any later version. |
| 106 | + * |
| 107 | + * wolfHSM is distributed in the hope that it will be useful, |
| 108 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 109 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 110 | + * GNU General Public License for more details. |
| 111 | + * |
| 112 | + * You should have received a copy of the GNU General Public License |
| 113 | + * along with wolfHSM. If not, see <http://www.gnu.org/licenses/>. |
| 114 | + */ |
| 115 | +
|
| 116 | +/* |
| 117 | + ************************************************************************* |
| 118 | + * wh_test_list.c -- GENERATED BY gen_test_list.py. DO NOT EDIT. |
| 119 | + ************************************************************************* |
| 120 | + */ |
| 121 | +
|
| 122 | +/* |
| 123 | + * Registry of every test function found via WH_TEST_{MISC, |
| 124 | + * CLIENT,SERVER} annotations. Each discovered test gets a weak |
| 125 | + * stub that returns WH_TEST_SKIPPED; the real test, when |
| 126 | + * compiled in, provides a strong symbol that the linker picks |
| 127 | + * instead. |
| 128 | + */ |
| 129 | +
|
| 130 | +#include "wh_test_list.h" |
| 131 | +
|
| 132 | +""" |
| 133 | + |
| 134 | + |
| 135 | +def render(tests): |
| 136 | + out = [HEADER] |
| 137 | + |
| 138 | + # Declarations and weak skip implementations -- one per test. |
| 139 | + # WH_TEST_DECL expands to a forward prototype plus a weak |
| 140 | + # stub returning WH_TEST_SKIPPED; the real test's strong |
| 141 | + # definition overrides it at link time when the feature gate |
| 142 | + # is on. |
| 143 | + out.append( |
| 144 | + '/* Test declarations and weak skip implementations. ' |
| 145 | + '*/\n') |
| 146 | + for group, fn, _ in tests: |
| 147 | + out.append('WH_TEST_DECL({name});\n'.format(name=fn)) |
| 148 | + out.append('\n') |
| 149 | + |
| 150 | + # Per-group registration tables. |
| 151 | + by_group = {g[0]: [] for g in GROUPS} |
| 152 | + for group, fn, _ in tests: |
| 153 | + by_group[group].append(fn) |
| 154 | + |
| 155 | + for i, (group, array, count) in enumerate(GROUPS): |
| 156 | + fns = by_group[group] |
| 157 | + if i > 0: |
| 158 | + out.append('\n') |
| 159 | + if not fns: |
| 160 | + # C forbids zero-length arrays at file scope; emit a |
| 161 | + # single NULL placeholder and a hardcoded count of 0 |
| 162 | + # so the runner treats this group as empty. |
| 163 | + out.append( |
| 164 | + 'const whTestCase {array}[] = ' |
| 165 | + '{{ {{ NULL, NULL }} }};\n'.format(array=array)) |
| 166 | + out.append('const size_t {count} = 0;\n'.format(count=count)) |
| 167 | + continue |
| 168 | + out.append('const whTestCase {array}[] = {{\n'.format(array=array)) |
| 169 | + for fn in fns: |
| 170 | + out.append( |
| 171 | + ' {{ "{name}", {name} }},\n'.format(name=fn)) |
| 172 | + out.append('};\n') |
| 173 | + out.append( |
| 174 | + 'const size_t {count} = ' |
| 175 | + 'sizeof({array}) / sizeof({array}[0]);\n'.format( |
| 176 | + array=array, count=count)) |
| 177 | + |
| 178 | + return ''.join(out) |
| 179 | + |
| 180 | + |
| 181 | +def main(): |
| 182 | + ap = argparse.ArgumentParser() |
| 183 | + ap.add_argument('--output', required=True, |
| 184 | + help='path to wh_test_list.c to (re)generate') |
| 185 | + ap.add_argument('sources', nargs='+', |
| 186 | + help='test source directories to scan') |
| 187 | + args = ap.parse_args() |
| 188 | + |
| 189 | + tests = discover(args.sources) |
| 190 | + if not tests: |
| 191 | + sys.stderr.write('gen_test_list.py: no WH_TEST_* annotations found\n') |
| 192 | + sys.exit(1) |
| 193 | + |
| 194 | + new_content = render(tests) |
| 195 | + |
| 196 | + # Only rewrite when content actually changes, so make doesn't |
| 197 | + # rebuild the world on every invocation. |
| 198 | + try: |
| 199 | + with open(args.output, 'r') as f: |
| 200 | + if f.read() == new_content: |
| 201 | + return |
| 202 | + except IOError: |
| 203 | + pass |
| 204 | + |
| 205 | + with open(args.output, 'w') as f: |
| 206 | + f.write(new_content) |
| 207 | + |
| 208 | + |
| 209 | +if __name__ == '__main__': |
| 210 | + main() |
0 commit comments