Files
apk-reverse/tests/unit/test_kotlin_name_recovery.py
T
dabao1955 aa9af54854 Absorb gaps from three public reverse-engineering skills
Batch 1 (full): Kotlin original-name recovery (new reference + zero-dep
kotlin_name_recovery.py mining @DebugMetadata/@Metadata.d2/jadx rename
comments, with -o/-p/--grep query modes); jadx advanced flags and a
Fernflower/Vineflower second-engine tier in toolchain.md; BuildConfig
sweep habit in recon.md; WebSocket/long-connection state-chain section
in protocol-reverse.md.

jsvmp-only (batches 2-3): A/B/C recovery ladder with black-box reuse
boundary and a wire-parameter family index in jsvmp-recovery.md; new
js-ast-deobfuscation.md pipeline layer (family detection, per-family
pass order, safe-rewrite rules, minimal bootstrap eval, residue metrics)
with js_obf_detect.py weighted detector.

Wiring: routing.md/SKILL.md symptom+index rows, coverage-and-limits,
route-inventory, evidence-summary, capability-matrix (+3 entries,
labels kept honest: scripts observed on fixtures, absorbed methodology
unverified). 12 new unit tests.

Also: silence two environment-dependent mypy errors (yaml/frida
sentinel assignments) and bump mypy python_version 3.9 -> 3.10 with the
trade-off recorded.

Gates: check_repo/check_refs/check_budget/check_commands/check_routing
PASS; pytest 787 passed; flake8 clean; mypy clean.
2026-10-20 11:54:59 +08:00

74 lines
3.1 KiB
Python

"""Pinned-shape tests for the Kotlin name recovery parser.
The shipped script (`skills/apk-reverse/scripts/kotlin_name_recovery.py`) is measured by
its embedded fixtures; these tests pin the same shapes independently so a regex regression
fails in CI rather than on a real jadx export. Every fixture here is a *shape*, written by
this repository, not a fragment of any real target.
"""
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'skills',
'apk-reverse', 'scripts'))
from kotlin_name_recovery import ( # noqa: E402
FIXTURE_DEBUG, FIXTURE_METADATA, FIXTURE_PLAIN, FIXTURE_RENAMED, mine_tree,
recover_real_name, write_map,
)
def test_debug_metadata_strips_dollar_scope():
# `$fetch$1` is inner/lambda scope; the declaring class is everything before the first $.
real, source = recover_real_name(FIXTURE_DEBUG)
assert real == 'com.example.feature.account.AccountRepositoryImpl'
assert source == 'debug_metadata'
def test_metadata_d2_takes_first_non_stdlib_descriptor():
real, source = recover_real_name(FIXTURE_METADATA)
assert real == 'com.example.feature.login.LoginViewModel'
assert source == 'metadata_d2'
def test_renamed_comment_is_the_weakest_fallback():
real, source = recover_real_name(FIXTURE_RENAMED)
assert real == 'com.example.core.network.ApiClient'
assert source == 'renamed_comment'
def test_plain_java_recovers_nothing():
# No annotation and no rename comment: the honest result is (None, None), not a guess.
real, source = recover_real_name(FIXTURE_PLAIN)
assert real is None and source is None
def test_debug_metadata_outranks_everything():
# A file carrying both sources must resolve through @DebugMetadata: it is a true
# declaring-class FQN, while d2 descriptors are a hint.
both = FIXTURE_DEBUG + '\n@Metadata(d2 = {"Lcom/example/other/WrongHint;"})\n'
real, source = recover_real_name(both)
assert real == 'com.example.feature.account.AccountRepositoryImpl'
assert source == 'debug_metadata'
def test_mine_tree_skips_third_party_and_maps_the_rest(tmp_path):
src = tmp_path / 'sources'
for rel, body in (('nq/a.java', FIXTURE_DEBUG),
('z/C0123x.java', FIXTURE_RENAMED),
('kotlin/x/Keep.java', FIXTURE_DEBUG)):
path = src / rel
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(body, encoding='utf-8')
mapping, counts = mine_tree(str(src))
# kotlin.* is skipped as already-real even though its fixture carries @DebugMetadata.
assert set(mapping) == {'nq.a', 'z.C0123x'}
assert counts == {'debug_metadata': 1, 'renamed_comment': 1}
out = tmp_path / 'map'
packages = write_map(mapping, counts, str(out))
assert packages == 2
tsv = (out / 'mapping.tsv').read_text(encoding='utf-8')
assert 'nq.a\tcom.example.feature.account.AccountRepositoryImpl\tdebug_metadata' in tsv
# by_package files are keyed by the ORIGINAL package, which is the lookup a reader wants.
assert (out / 'by_package' / 'com_example_core_network.tsv').exists()