Files
LihonT/tests/test_ghost_patch.py
cheb0chik 1d5a9a1fad LihonT 2 Luna
Ghost mode for stories and chats, several photos in one comment, .ltp plugins (rights, defender, rollback, downloads
only with consent), custom chat bubbles with a text colour of your choice, the lihont-ltp SDK and the plugin docs,
streak flames as widgets with a keeper, avatar shapes, Material 3 surfaces.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 18:19:27 +03:00

169 lines
6.5 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"))
with self.assertRaises(ValueError):
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])
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)
# ghost mode is a choice: off until the person turns it on
self.assertIn('getBoolean(STORIES, false)', source)
self.assertIn('getBoolean(MESSAGES, false)', 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("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)
# both live in their own section, not in the list of functions
self.assertIn('addSection("Приватность", cardPrivacy', screen)
self.assertNotIn("cardFunc.addView(ghostStoriesRow())", screen)
if __name__ == "__main__":
unittest.main()