@@ -542,12 +542,24 @@ def fail_run(command: list[str], **_kwargs: object) -> subprocess.CompletedProce
542542def test_cleanup_removes_remote_test_directory (monkeypatch : pytest .MonkeyPatch ) -> None :
543543 root_commands : list [str ] = []
544544 adb_commands : list [tuple [str , ...]] = []
545+ snapshot_calls = 0
545546
546547 def fake_root_shell (_serial : str , command : str , ** _kwargs : object ) -> SimpleNamespace :
548+ nonlocal snapshot_calls
547549 root_commands .append (command )
550+ stdout = ""
551+ if "/proc/[0-9]*/exe" in command :
552+ snapshot_calls += 1
553+ if snapshot_calls == 1 :
554+ stdout = (
555+ "lrwxrwxrwx root /proc/101/exe -> "
556+ "/data/local/tmp/phantom-frida-test/oemcodec-server\n "
557+ "lrwxrwxrwx root /proc/102/exe -> "
558+ "/data/local/tmp/phantom-frida-test/gadget-loader\n "
559+ )
548560 return SimpleNamespace (
549561 returncode = 1 if command .startswith (("pkill" , "pgrep" )) else 0 ,
550- stdout = "" ,
562+ stdout = stdout ,
551563 stderr = "" ,
552564 )
553565
@@ -557,12 +569,21 @@ def fake_adb(_serial: str, *arguments: str, **_kwargs: object) -> SimpleNamespac
557569
558570 monkeypatch .setattr (android_smoke , "root_shell" , fake_root_shell )
559571 monkeypatch .setattr (android_smoke , "adb" , fake_adb )
572+ monkeypatch .setattr (android_smoke .time , "sleep" , lambda _seconds : None )
560573 config = SimpleNamespace (name = "oemcodec" , port = 27142 )
561574
562575 android_smoke ._cleanup (config , "SERIAL-1" )
563576
564- assert "pkill -TERM -x oemcodec-server" in root_commands
565- assert "pkill -TERM -x gadget-loader" in root_commands
577+ term_commands = [command for command in root_commands if "kill -TERM" in command ]
578+ assert len (term_commands ) == 2
579+ assert all ("readlink /proc/" in command for command in term_commands )
580+ assert any (
581+ "/data/local/tmp/phantom-frida-test/oemcodec-server" in command for command in term_commands
582+ )
583+ assert any (
584+ "/data/local/tmp/phantom-frida-test/gadget-loader" in command for command in term_commands
585+ )
586+ assert not any (command .startswith ("pkill" ) for command in root_commands )
566587 assert "rm -rf -- /data/local/tmp/phantom-frida-test" in root_commands
567588 assert ("forward" , "--list" ) in adb_commands
568589
@@ -594,8 +615,8 @@ def fake_adb(_serial: str, *arguments: str, **_kwargs: object) -> SimpleNamespac
594615 with pytest .raises (android_smoke .SmokeFailure , match = "remove remote test directory" ):
595616 android_smoke ._cleanup (config , "SERIAL-1" )
596617
597- assert len ([command for command in root_commands if command . startswith ( "pkill" ) ]) == 2
598- assert len ([ command for command in root_commands if command .startswith (" pgrep" )]) == 2
618+ assert len ([command for command in root_commands if "/proc/[0-9]*/exe" in command ]) == 2
619+ assert not any ( command .startswith (( "pkill" , " pgrep" )) for command in root_commands )
599620 assert ("forward" , "--remove" , "tcp:27142" ) in adb_commands
600621 assert ("forward" , "--remove" , "tcp:27143" ) in adb_commands
601622 assert ("forward" , "--list" ) in adb_commands
@@ -650,27 +671,25 @@ def fake_root_shell(_serial: str, command: str, **_kwargs: object) -> SimpleName
650671 android_smoke ._cleanup (config , "SERIAL-1" )
651672
652673
653- def test_cleanup_uses_kernel_comm_limit_for_long_server_name (
654- monkeypatch : pytest .MonkeyPatch ,
655- ) -> None :
656- root_commands : list [str ] = []
674+ def test_remote_signal_is_scoped_to_exact_long_name_executable () -> None :
675+ remote_executable = "/data/local/tmp/phantom-frida-test/abcdefghijklmnopqrst-server"
657676
658- def fake_root_shell (_serial : str , command : str , ** _kwargs : object ) -> SimpleNamespace :
659- root_commands .append (command )
660- return SimpleNamespace (
661- returncode = 1 if command .startswith (("pkill" , "pgrep" )) else 0 ,
662- stdout = "" ,
663- stderr = "" ,
664- )
677+ command = android_smoke ._remote_signal_command (remote_executable , 123 , "TERM" )
665678
666- monkeypatch .setattr (android_smoke , "root_shell" , fake_root_shell )
667- monkeypatch .setattr (
668- android_smoke ,
669- "adb" ,
670- lambda * _args , ** _kwargs : SimpleNamespace (returncode = 0 , stdout = "" , stderr = "" ),
671- )
672- config = SimpleNamespace (name = "abcdefghijklmnopqrst" , port = 27142 )
679+ assert "readlink /proc/123/exe" in command
680+ assert remote_executable in command
681+ assert "kill -TERM 123" in command
682+ assert "pkill" not in command
673683
674- android_smoke ._cleanup (config , "SERIAL-1" )
675684
676- assert "pkill -TERM -x abcdefghijklmno" in root_commands
685+ def test_parse_remote_processes_excludes_same_basename_outside_test_directory () -> None :
686+ remote_executable = "/data/local/tmp/phantom-frida-test/oemcodec-server"
687+ output = (
688+ f"lrwxrwxrwx root /proc/101/exe -> { remote_executable } \n "
689+ f"lrwxrwxrwx root /proc/102/exe -> { remote_executable } (deleted)\n "
690+ "lrwxrwxrwx root /proc/103/exe -> /other/path/oemcodec-server\n "
691+ )
692+
693+ assert android_smoke ._parse_remote_processes (output , (remote_executable ,)) == {
694+ remote_executable : [101 , 102 ]
695+ }
0 commit comments