import unittest from lihont import region def wrap(body: str, header: str) -> str: return header + "\n" + body + ".end method\n" BPEA_HEADER = ".method public static LIZIZ(Landroid/telephony/TelephonyManager;Lcom/bytedance/bpea/basics/Cert;)Ljava/lang/String;" BPEA_BODY = """ .registers 6 const-string v1, "TelephonyManager_getNetworkCountryIso" const v0, 0x18a39 invoke-static {v0, v1, p1}, LX/1DHU;->LIZ(ILjava/lang/String;Lcom/bytedance/bpea/basics/Cert;)LX/0kmX; return-object v0 """ DELEGATE_HEADER = ".method public static LIZJ(Landroid/telephony/TelephonyManager;)Ljava/lang/String;" DELEGATE_BODY = """ .registers 3 invoke-static {p0, v0}, LX/1DHU;->LIZIZ(Landroid/telephony/TelephonyManager;Lcom/bytedance/bpea/basics/Cert;)Ljava/lang/String; move-result-object v0 return-object v0 """ MONITOR_HEADER = ".method public static LLIIIL(Landroid/telephony/TelephonyManager;LX/0yw4;)Ljava/lang/String;" MONITOR_BODY = """ .registers 19 const/16 v4, 0x2866 const-string v14, "getSimCountryIso" invoke-virtual {v9}, Landroid/telephony/TelephonyManager;->getSimCountryIso()Ljava/lang/String; return-object v7 """ LAMBDA_HEADER = ".class public final LX/0kmK;\n.super LX/0MXs;\n.method public final invoke(Ljava/lang/Object;)Ljava/lang/Object;" LAMBDA_BODY = """ .registers 3 iget-object v0, p0, LX/0kmK;->LL:Landroid/telephony/TelephonyManager; invoke-virtual {v0}, Landroid/telephony/TelephonyManager;->getNetworkOperator()Ljava/lang/String; move-result-object v0 return-object v0 """ class RegionPatchTest(unittest.TestCase): def test_bpea_wrapper_is_replaced_with_constant(self): text = wrap(BPEA_BODY, BPEA_HEADER) changed, hits = region.rewrite(text, "LX/1DHU;") self.assertEqual(hits, {"bpea_LIZIZ": 1}) self.assertIn("LihonTRegion;->iso()", changed) self.assertNotIn("TelephonyManager_getNetworkCountryIso", changed) self.assertEqual(region.rewrite(changed, "LX/1DHU;"), (changed, {})) def test_bpea_delegate_is_replaced(self): text = wrap(DELEGATE_BODY, DELEGATE_HEADER) changed, hits = region.rewrite(text, "LX/1DHU;") self.assertEqual(hits, {"bpea_delegate_LIZJ": 1}) self.assertIn("LihonTRegion;->iso()", changed) def test_bpea_wrapper_drift_raises(self): text = wrap(BPEA_BODY.replace("getNetworkCountryIso", "getSomethingElse").replace("TelephonyManager_", "TelephonyManager_getSomething"), BPEA_HEADER) with self.assertRaises(ValueError): region.rewrite(text, "LX/1DHU;") def test_monitor_body_replaced_by_marker(self): text = wrap(MONITOR_BODY, MONITOR_HEADER) changed, hits = region.rewrite_monitor(text) self.assertEqual(hits, {"monitor_LLIIIL": 1}) self.assertIn("LihonTRegion;->iso()", changed) def test_monitor_other_methods_untouched(self): text = wrap(MONITOR_BODY.replace("LLIIIL", "LLIIJL").replace("0x2866", "0x2999"), MONITOR_HEADER.replace("LLIIIL", "LLIIJL")) changed, hits = region.rewrite_monitor(text) self.assertEqual(hits, {}) self.assertEqual(changed, text) def test_lambda_is_replaced(self): text = LAMBDA_HEADER + "\n" + LAMBDA_BODY + ".end method\n" changed, hits = region.rewrite_lambda(text, "LX/0kmK;") self.assertEqual(hits, {"lambda_LX/0kmK;": 1}) self.assertIn("LihonTRegion;->operator()", changed) self.assertNotIn("getNetworkOperator", changed) def test_expected_hits_cover_every_audited_point(self): expected = region.expected_hits() self.assertEqual(len(expected), 6 + 5 + 5 + 2) self.assertIn("bpea_LJIIJJI", expected) self.assertIn("monitor_LLIIJI", expected) self.assertIn("lambda_LX/0kmL;", expected) if __name__ == "__main__": unittest.main()