import unittest from core.h5_kws import ( DEFAULT_KWS_SIGN_SCRIPT, build_h5_kws_script_ticket, ) from core.h5_kws_vm import ( H5_KWS_KNOWN_SCRIPT_CODE, analyze_h5_kws_function_ranges, disassemble_h5_kws_range, extract_h5_kws_opcode_handlers, kwscode_from_known_h5_kws_script, parse_h5_kws_vm_script, ) class H5KwsVmTests(unittest.TestCase): def test_parse_known_kws_script_container(self): summary = parse_h5_kws_vm_script(DEFAULT_KWS_SIGN_SCRIPT) self.assertEqual( summary.script_sha256, "d944b2bc3754bec85c0a238fb052859295a3ecb0756789c47d99417d3f957615", ) self.assertEqual( summary.bytecode_sha256, "7668fe4c01dc4721a862b3102cd3c183dbd4721cac6af4129fc2adcdd1d701d8", ) self.assertEqual( summary.constants_sha256, "226815ab9e5d21ce285afa698b618be3ff5122cf13880c846600b6e7d1396afe", ) self.assertEqual(summary.instruction_count, 4676) self.assertEqual(summary.constant_count, 286) self.assertEqual(summary.max_opcode, 65) self.assertEqual(summary.opcode_histogram[8], 1978) self.assertEqual(len(summary.function_ranges), 19) self.assertEqual(summary.function_ranges[0].start, 1308) self.assertEqual(summary.function_ranges[0].end, 1447) self.assertEqual(summary.function_ranges[-1].start, 4664) self.assertEqual(summary.function_ranges[-1].end, 4675) def test_known_kws_script_code_is_available_without_node(self): code = kwscode_from_known_h5_kws_script(DEFAULT_KWS_SIGN_SCRIPT) self.assertEqual(code, H5_KWS_KNOWN_SCRIPT_CODE) self.assertEqual( code, "04dd303d63222chdfec0087d058fb1c4c5f2eb16eefgce98b2a503578e5f8433", ) def test_extract_opcode_handlers_labels_jimbei_interpreter(self): handlers = extract_h5_kws_opcode_handlers(DEFAULT_KWS_SIGN_SCRIPT) by_index = {handler.index: handler for handler in handlers} self.assertEqual(len(handlers), 67) self.assertFalse(by_index[15].present) self.assertTrue(by_index[66].present) self.assertEqual(by_index[66].used_count, 0) self.assertEqual(by_index[8].label, "add") self.assertEqual(by_index[8].used_count, 1978) self.assertEqual(by_index[8].body_sha16, "7be97b9f85080954") self.assertEqual(by_index[12].label, "make_function") self.assertEqual(by_index[24].label, "call_apply") self.assertEqual(by_index[44].label, "jump_if_false") self.assertEqual(by_index[61].label, "jump_if_true") self.assertEqual(by_index[65].label, "assign_reference") self.assertEqual(by_index[65].used_count, 302) def test_disassemble_tail_helper_range_resolves_operands(self): instructions = disassemble_h5_kws_range(DEFAULT_KWS_SIGN_SCRIPT, 4664, 4675) self.assertEqual(len(instructions), 12) self.assertEqual(instructions[0].index, 4664) self.assertEqual(instructions[0].label, "enter_closure_scope") self.assertEqual(instructions[0].operand_a, "unused(8)") self.assertEqual(instructions[2].label, "assign_reference") self.assertEqual(instructions[2].operand_a, "scope[22]") self.assertEqual(instructions[2].operand_b, "arg[0]") self.assertEqual(instructions[8].label, "call_apply") self.assertEqual(instructions[8].operand_a, "const[100]=1") self.assertEqual(instructions[-1].label, "return_undefined") def test_analyze_function_ranges_names_main_and_tail_helpers(self): functions = analyze_h5_kws_function_ranges(DEFAULT_KWS_SIGN_SCRIPT) by_start = {function.start: function for function in functions} self.assertEqual(len(functions), 19) self.assertEqual(by_start[1308].name, "scope36_fn_1308_1447") self.assertEqual(by_start[1308].assigned_scope, 36) self.assertEqual(by_start[1308].call_apply_count, 4) self.assertEqual(by_start[3063].name, "scope80_main_orchestrator") self.assertEqual(by_start[3063].assigned_scope, 80) self.assertEqual(by_start[3063].length, 1345) self.assertEqual(by_start[3063].call_apply_count, 16) self.assertIn(1341, by_start[3063].branch_targets) self.assertIn(1342, by_start[3063].branch_targets) self.assertEqual(by_start[4662].name, "inline_return_undefined_stub") self.assertIsNone(by_start[4662].assigned_scope) self.assertEqual(by_start[4664].name, "inline_call_scope107_with_arg0") self.assertEqual(by_start[4664].call_apply_count, 1) self.assertEqual(by_start[4664].branch_targets, []) def test_script_ticket_uses_known_static_code_before_node_runner(self): ticket = build_h5_kws_script_ticket( sec_token="S" * 88, runner="missing-node-runner.mjs", ) self.assertEqual(ticket["kwssectoken"], "S" * 88) self.assertEqual(ticket["kwscode"], H5_KWS_KNOWN_SCRIPT_CODE) if __name__ == "__main__": unittest.main()