Files
LihonT/tests/test_ghost_patch.py
cheb0chik 3540ac84ea LihonT 2.3.0 (Serena): profile music, AI video dubbing, PiP & bg play, fonts, cleaner
- Shared moderated MP3 profile music (with floating mini-player and MediaSession controls, zero bubble when absent)
- AI video dubbing into Russian with burned-in ASS subtitles inside AgentChat (`services/members/dub.py`)
- Picture-in-Picture 9:16 and background audio playback (`PlayControl` + `play_patch.py`)
- System/custom .ttf fonts & emoji packs (`FontsAndEmoji`), account cleaner (`AccountCleaner`)
- Feed blacklist, muted sound restoration, OLED burn-in shift, A/B flag editor, presets, long comments
- Updated site homepage (`src/web/index.html`) for LihonT 2.3.0 Serena
2026-11-26 15:40:32 +03:00

212 lines
8.1 KiB
Python

import unittest
from pathlib import Path
from lihont import ghost_patch
ROOT = Path(__file__).resolve().parents[1]
# LX/0RDS;->LIZJ of TikTok 47.1.3 as baksmali writes it (shortened after the first calls).
SAMPLE = """.class public final LX/0RDS;
.super Ljava/lang/Object;
.method public final LIZIZ()V
.registers 2
return-void
.end method
.method public final LIZJ(Ljava/lang/Boolean;Ljava/lang/String;Ljava/lang/String;)V
.registers 8
.prologue
.line 50331648
if-eqz p1, :cond_3c
.line 50331649
invoke-virtual {p1}, Ljava/lang/Boolean;->booleanValue()Z
move-result v0
invoke-static {v0}, Ljava/lang/Integer;->valueOf(I)Ljava/lang/Integer;
move-result-object v2
:goto_a
sget-object v1, Lcom/ss/android/ugc/aweme/story/api/StoryApi;->LIZIZ:Lcom/ss/android/ugc/aweme/story/api/StoryApi;
invoke-virtual {v1, p2, v0, v2}, Lcom/ss/android/ugc/aweme/story/api/StoryApi;->reportStoryViewed(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Integer;)LX/12YD;
move-result-object v1
return-void
:cond_3c
const/4 v2, 0x0
goto :goto_a
.end method
"""
# LX/13pM;->LIZIZ of TikTok 47.1.3: the suspend entry that marks a conversation as read (shortened).
MARK = """.class public final LX/13pM;
.super Ljava/lang/Object;
.method public final LIZ(LX/13oK;LX/13pS;Z)Lcom/bytedance/im/core/proto/Request;
.registers 4
const/4 v0, 0x0
return-object v0
.end method
.method public final LIZIZ(Ljava/lang/Long;Ljava/lang/String;LX/01h3;)Ljava/lang/Object;
.registers 10
.prologue
.line 50331648
instance-of v0, p3, LX/0c58;
.line 50331649
if-eqz v0, :cond_45
move-object v5, p3
check-cast v5, LX/0c58;
invoke-static {}, LX/0NPd;->LJFF()Ljava/lang/Object;
move-result-object v4
:cond_45
new-instance v5, LX/0c58;
return-object v4
.end method
"""
class MarkReadTest(unittest.TestCase):
def test_the_entry_returns_unit_at_once_while_the_switch_is_on_but_never_cuts_a_resumed_call(self):
text = ghost_patch.rewrite_mark_read(MARK)
self.assertIn(
" .line 50331648\n"
" invoke-static {}, %s\n\n move-result v0\n\n if-eqz v0, :lihont_mark_read\n\n"
" instance-of v0, p3, LX/0c58;\n\n if-nez v0, :lihont_mark_read\n\n"
" sget-object v0, Lkotlin/Unit;->LIZ:Lkotlin/Unit;\n\n return-object v0\n\n :lihont_mark_read\n\n"
" instance-of v0, p3, LX/0c58;\n" % ghost_patch.MARK_READ_HOOK, text)
# nothing else of the class changes: the request builder is untouched
self.assertEqual(text.count("Ghost;"), 1)
self.assertIn("LIZ(LX/13oK;LX/13pS;Z)Lcom/bytedance/im/core/proto/Request;", text)
def test_a_second_pass_is_refused(self):
with self.assertRaises(ValueError):
ghost_patch.rewrite_mark_read(ghost_patch.rewrite_mark_read(MARK))
def test_another_shape_is_refused(self):
with self.assertRaises(ValueError):
ghost_patch.rewrite_mark_read(MARK.replace(".registers 10", ".registers 11"))
with self.assertRaises(ValueError):
ghost_patch.rewrite_mark_read(MARK.replace("LX/0NPd;->LJFF()", "LX/0NPd;->LJFG()"))
with self.assertRaises(ValueError):
ghost_patch.rewrite_mark_read(MARK.replace("instance-of v0, p3", "instance-of v1, p3"))
with self.assertRaises(ValueError):
ghost_patch.rewrite_mark_read(MARK.replace("LIZIZ(", "LIZJ("))
class StoryViewTest(unittest.TestCase):
def test_the_method_returns_before_anything_is_sent_while_the_switch_is_on(self):
text = ghost_patch.rewrite_story_view(SAMPLE)
self.assertIn(
" .line 50331648\n"
" invoke-static {}, %s\n\n move-result v0\n\n if-eqz v0, :lihont_story_view\n\n return-void\n\n"
" :lihont_story_view\n\n if-eqz p1, :cond_3c\n" % ghost_patch.HOOK, text)
# the injected jump skips the return only when the switch is off: if-eqz means "zero, carry on"
self.assertEqual(text.count("return-void"), 3) # the other method's, the injected one, the method's own
def test_other_methods_and_the_report_call_are_untouched(self):
text = ghost_patch.rewrite_story_view(SAMPLE)
self.assertEqual(text.count("Ghost;"), 1)
self.assertIn("reportStoryViewed(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Integer;)LX/12YD;", text)
self.assertTrue(text.startswith(".class public final LX/0RDS;"))
def test_a_second_pass_is_refused(self):
with self.assertRaises(ValueError):
ghost_patch.rewrite_story_view(ghost_patch.rewrite_story_view(SAMPLE))
def test_another_shape_is_refused(self):
with self.assertRaises(ValueError):
ghost_patch.rewrite_story_view(SAMPLE.replace("reportStoryViewed", "reportSomething"))
# the register count differs between TikTok builds (8 in 47.1.3, 7 in 47.7.2): it is not part of the shape
self.assertIn("Ghost;", ghost_patch.rewrite_story_view(SAMPLE.replace(".registers 8", ".registers 9")))
with self.assertRaises(ValueError):
ghost_patch.rewrite_story_view(SAMPLE.replace("if-eqz p1, :cond_3c", "if-nez p1, :cond_3c"))
with self.assertRaises(ValueError):
ghost_patch.rewrite_story_view(SAMPLE.replace("LIZJ(", "LIZK("))
def test_runtime_and_build_are_in_step(self):
self.assertEqual(ghost_patch.runtime_targets(), [ghost_patch.HOOK, ghost_patch.MARK_READ_HOOK, ghost_patch.KEEP_RECALLED_HOOK])
source = (ROOT / "src" / "lihont" / "two" / "ghost" / "Ghost.java").read_text(encoding="utf-8")
self.assertIn("public static boolean stories()", source)
self.assertIn("public static boolean messages()", source)
self.assertIn("public static boolean keepRecalled()", source)
# ghost mode is a choice: off until the person turns it on
self.assertIn("readFlag(context, STORIES)", source)
self.assertIn("readFlag(context, MESSAGES)", source)
self.assertIn("readFlag(context, KEEP_RECALLED)", source)
build = (ROOT / "lihont" / "upgrade.py").read_text(encoding="utf-8")
self.assertIn('step("ghost_stories", patch_ghost_stories', build)
self.assertIn('step("ghost_messages", patch_ghost_messages', build)
self.assertIn('step("keep_recalled", patch_keep_recalled', build)
self.assertIn("ghost_patch.runtime_targets()", build)
screen = (ROOT / "src" / "lihont" / "two" / "settings" / "LihonTSettingsActivity.java").read_text(encoding="utf-8")
self.assertIn("ghostStoriesRow()", screen)
self.assertIn("ghostMessagesRow()", screen)
self.assertIn("keepRecalledRow()", screen)
# both live in their own section, not in the list of functions
self.assertIn('addSection("Приватность", cardPrivacy', screen)
self.assertNotIn("cardFunc.addView(ghostStoriesRow())", screen)
RECALLED_SAMPLE = """.class public LX/13m4;
.super Ljava/lang/Object;
.method public getMessageStatus()LX/13mT;
.registers 2
iget-object v0, p0, LX/13m4;->messageStatus:LX/13mT;
if-nez v0, :cond_18
sget-object v0, LX/13mT;->RECALLED:LX/13mT;
return-object v0
:cond_18
return-object v0
.end method
.method public isRecalled()Z
.registers 3
invoke-virtual {p0}, LX/13m4;->getExt()Ljava/util/Map;
move-result-object v1
const-string v0, "s:is_recalled"
const-string v0, "s:recalled"
const/4 v0, 0x0
return v0
.end method
"""
class KeepRecalledTest(unittest.TestCase):
def test_recalled_returns_false_and_status_returns_available_when_on(self):
text = ghost_patch.rewrite_keep_recalled(RECALLED_SAMPLE)
self.assertIn("invoke-static {}, %s" % ghost_patch.KEEP_RECALLED_HOOK, text)
self.assertIn("sget-object v0, LX/13mT;->AVAILABLE:LX/13mT;", text)
self.assertIn(" .registers 3", text)
self.assertNotIn(" .registers 2", text)
self.assertEqual(text.count("Ghost;"), 2)
def test_second_pass_is_refused(self):
with self.assertRaises(ValueError):
ghost_patch.rewrite_keep_recalled(ghost_patch.rewrite_keep_recalled(RECALLED_SAMPLE))
if __name__ == "__main__":
unittest.main()