diff --git a/test/fake_radio/manifest_contract_test.dart b/test/fake_radio/manifest_contract_test.dart index 08b0a5c6..17209732 100644 --- a/test/fake_radio/manifest_contract_test.dart +++ b/test/fake_radio/manifest_contract_test.dart @@ -102,6 +102,26 @@ void main() { expect(_compare(client, offband).unknown, isEmpty); }); + test('no two names reduce to the same comparison key', () { + // `_key` drops case and underscores. If two names collapsed together, + // one of them would never be compared. + List collisions(Iterable names) { + final seen = {}; + return [ + for (final n in names) + if (seen.putIfAbsent(_key(n), () => n) != n) + '$n and ${seen[_key(n)]}', + ]; + } + + expect(collisions(client.keys), isEmpty); + expect( + collisions([for (final s in _sections) ...offband.section(s).keys]), + isEmpty, + ); + expect(collisions(['CMD_FOO_BAR', 'cmdFooBar']), hasLength(1)); + }); + test('a deliberately wrong constant is caught', () { final doctored = Map.of(client) ..['offbandCapCaplog'] = 0x08 diff --git a/tool/fake_radio/gen_protocol_manifest.py b/tool/fake_radio/gen_protocol_manifest.py index 5449b343..2a9a1a52 100644 --- a/tool/fake_radio/gen_protocol_manifest.py +++ b/tool/fake_radio/gen_protocol_manifest.py @@ -39,8 +39,36 @@ def show(repo, ref, path): return out.stdout +PROTOCOL = re.compile(r"(CMD_|RESP_CODE_|PUSH_CODE_|ERR_CODE_|OFFBAND_)") + + +def conditional_definitions(text): + """Protocol definitions inside #if/#ifdef/#ifndef. Reading source with + regexes can't tell which branch a build takes, so the manifest would be + wrong for some build; refuse instead of guessing.""" + depth, found = 0, [] + for n, line in enumerate(text.splitlines(), 1): + s = line.strip() + if re.match(r"#\s*if", s): + depth += 1 + elif re.match(r"#\s*endif", s): + depth -= 1 + elif depth > 0: + m = DEFINE.match(s) or CONSTEXPR.match(s) + if m and PROTOCOL.match(m.group(1)): + found.append(f"line {n}: {m.group(1)}") + return found + + def pick(pairs, prefix): - return {name: int(value, 0) for name, value in pairs if name.startswith(prefix)} + out = {} + for name, value in pairs: + if not name.startswith(prefix): + continue + if name in out and out[name] != int(value, 0): + sys.exit(f"{name} is defined twice with different values") + out[name] = int(value, 0) + return out def main(): @@ -67,6 +95,12 @@ def main(): if offband is None: sys.exit("offband flavor but OffbandConfigProtocol.h is missing") pairs += CONSTEXPR.findall(offband) + DEFINE.findall(offband) + for name, text in [("MyMesh.cpp", source)] + ( + [("OffbandConfigProtocol.h", offband)] if args.flavor == "offband" else [] + ): + found = conditional_definitions(text) + if found: + sys.exit(f"{name} defines protocol codes inside #if: {', '.join(found)}") commands = pick(pairs, "CMD_") responses = pick(pairs, "RESP_CODE_")