2525import java .util .List ;
2626import java .util .Optional ;
2727
28- import dalvik .system .VMRuntime ;
29-
3028@ SuppressWarnings ("JavaReflectionMemberAccess" )
3129@ FixMethodOrder (MethodSorters .NAME_ASCENDING )
3230@ SdkSuppress (minSdkVersion = Build .VERSION_CODES .P )
3331@ RunWith (AndroidJUnit4 .class )
3432public class HiddenApiBypassTest {
3533
34+ private final Class <?> runtime = Class .forName ("dalvik.system.VMRuntime" );
35+
3636 @ Rule
3737 public ExpectedException exception = ExpectedException .none ();
3838
39+ public HiddenApiBypassTest () throws ClassNotFoundException {
40+ }
41+
3942 @ Test
4043 public void AgetDeclaredMethods () {
41- List <Executable > methods = HiddenApiBypass .getDeclaredMethods (VMRuntime . class );
44+ List <Executable > methods = HiddenApiBypass .getDeclaredMethods (runtime );
4245 Optional <Executable > getRuntime = methods .stream ().filter (it -> it .getName ().equals ("getRuntime" )).findFirst ();
4346 assertTrue (getRuntime .isPresent ());
4447 Optional <Executable > setHiddenApiExemptions = methods .stream ().filter (it -> it .getName ().equals ("setHiddenApiExemptions" )).findFirst ();
@@ -52,7 +55,7 @@ public void BusesNonSdkApiIsHiddenApi() throws NoSuchMethodException {
5255
5356 @ Test (expected = NoSuchMethodException .class )
5457 public void CsetHiddenApiExemptionsIsHiddenApi () throws NoSuchMethodException {
55- VMRuntime . class .getMethod ("setHiddenApiExemptions" , String [].class );
58+ runtime .getMethod ("setHiddenApiExemptions" , String [].class );
5659 }
5760
5861 @ Test (expected = NoSuchMethodException .class )
@@ -69,6 +72,7 @@ public void ElongVersionCodeIsHiddenApi() throws NoSuchFieldException {
6972 public void FHiddenApiEnforcementDefaultIsHiddenApi () throws NoSuchFieldException {
7073 ApplicationInfo .class .getDeclaredField ("HIDDEN_API_ENFORCEMENT_DEFAULT" );
7174 }
75+
7276 @ Test
7377 public void GtestGetInstanceFields () {
7478 assertTrue (HiddenApiBypass .getInstanceFields (ApplicationInfo .class ).stream ().anyMatch (i -> i .getName ().equals ("longVersionCode" )));
@@ -112,30 +116,30 @@ public void MclearHiddenApiExemptions() throws NoSuchMethodException {
112116 assertTrue (HiddenApiBypass .setHiddenApiExemptions ("L" ));
113117 ApplicationInfo .class .getMethod ("getHiddenApiEnforcementPolicy" );
114118 assertTrue (HiddenApiBypass .clearHiddenApiExemptions ());
115- VMRuntime . class .getMethod ("setHiddenApiExemptions" , String [].class );
119+ runtime .getMethod ("setHiddenApiExemptions" , String [].class );
116120 }
117121
118122 @ Test
119123 public void NaddHiddenApiExemptionsTest () throws NoSuchMethodException {
120124 assertTrue (HiddenApiBypass .addHiddenApiExemptions ("Landroid/content/pm/ApplicationInfo;" ));
121125 ApplicationInfo .class .getMethod ("getHiddenApiEnforcementPolicy" );
122126 assertTrue (HiddenApiBypass .addHiddenApiExemptions ("Ldalvik/system/VMRuntime;" ));
123- VMRuntime . class .getMethod ("setHiddenApiExemptions" , String [].class );
127+ runtime .getMethod ("setHiddenApiExemptions" , String [].class );
124128 }
125129
126130 @ Test
127131 public void OtestCheckArgsForInvokeMethod () {
128132 class X {
129133 }
130- assertFalse (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{}, new Object []{new Object ()}));
131- assertTrue (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{int .class }, new Object []{1 }));
132- assertFalse (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{int .class }, new Object []{1.0 }));
133- assertFalse (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{int .class }, new Object []{null }));
134- assertTrue (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{Integer .class }, new Object []{1 }));
135- assertTrue (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{Integer .class }, new Object []{null }));
136- assertTrue (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{Object .class }, new Object []{new X ()}));
137- assertFalse (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{X .class }, new Object []{new Object ()}));
138- assertTrue (HiddenApiBypass .checkArgsForInvokeMethod (new Class []{Object .class , int .class , byte .class , short .class , char .class , double .class , float .class , boolean .class , long .class }, new Object []{new X (), 1 , (byte ) 0 , (short ) 2 , 'c' , 1.1 , 1.2f , false , 114514L }));
134+ assertFalse (Helper .checkArgsForInvokeMethod (new Class []{}, new Object []{new Object ()}));
135+ assertTrue (Helper .checkArgsForInvokeMethod (new Class []{int .class }, new Object []{1 }));
136+ assertFalse (Helper .checkArgsForInvokeMethod (new Class []{int .class }, new Object []{1.0 }));
137+ assertFalse (Helper .checkArgsForInvokeMethod (new Class []{int .class }, new Object []{null }));
138+ assertTrue (Helper .checkArgsForInvokeMethod (new Class []{Integer .class }, new Object []{1 }));
139+ assertTrue (Helper .checkArgsForInvokeMethod (new Class []{Integer .class }, new Object []{null }));
140+ assertTrue (Helper .checkArgsForInvokeMethod (new Class []{Object .class }, new Object []{new X ()}));
141+ assertFalse (Helper .checkArgsForInvokeMethod (new Class []{X .class }, new Object []{new Object ()}));
142+ assertTrue (Helper .checkArgsForInvokeMethod (new Class []{Object .class , int .class , byte .class , short .class , char .class , double .class , float .class , boolean .class , long .class }, new Object []{new X (), 1 , (byte ) 0 , (short ) 2 , 'c' , 1.1 , 1.2f , false , 114514L }));
139143 }
140144
141145}
0 commit comments