runInSeparateProcess(new TestService.Procedure() { @Override public void run(final Context context) { installCondomProcess(context, new CondomOptions().setOutboundJudge(sBlockAllJudge)); final ContentResolver resolver = context.getContentResolver(); withFakeSelfPackageName(new Runnable() { @Override public void run() { assertNull("Block access to provider", resolver.acquireContentProviderClient("com.oasisfeng.condom.test")); }}); }}); }
runInSeparateProcess(new TestService.Procedure() { @Override public void run(final Context context) { testOrderedBroadcast(context, new Intent(ACTION_TEST).setPackage(context.getPackageName()), true); installCondomProcess(context, new CondomOptions().setOutboundJudge(sBlockAllJudge)); context.sendBroadcast(new Intent(ACTION_TEST).setPackage(context.getPackageName())); // Ensure no exception testOrderedBroadcast(context, new Intent(ACTION_TEST), true); testOrderedBroadcast(context, new Intent(ACTION_TEST).setPackage(context.getPackageName()), true); // Self targeted should always be allowed. withFakeSelfPackageName(new Runnable() { @Override public void run() { testOrderedBroadcast(context, new Intent(ACTION_TEST).setPackage(context.getPackageName()), false); }}); }}); }
runInSeparateProcess(new TestService.Procedure() { @Override public void run(final Context context) { final Intent intent = new Intent(context, TestService.class); assertNotNull("Test service is not properly setup.", context.startService(intent)); assertTrue(context.stopService(intent)); installCondomProcess(context, new CondomOptions().setOutboundJudge(sBlockAllJudge)); withFakeSelfPackageName(new Runnable() { @Override public void run() { assertNull(context.startService(intent)); }}); // Block by outbound judge assertTrue(context.stopService(intent)); }}); // TODO: More cases
runInSeparateProcess(new TestService.Procedure() { @Override public void run(final Context context) { installCondomProcess(context, new CondomOptions()); // Must be installed before the first call to Context.getPackageManager(). final Intent service_intent = new Intent("android.view.InputMethod").addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES/* For consistency */); final ResolveInfo service = context.getPackageManager().resolveService(service_intent, 0); Assume.assumeNotNull(service); final List<ResolveInfo> services = context.getPackageManager().queryIntentServices(service_intent, 0); assertNotNull(services); assertFalse(services.isEmpty()); final List<ResolveInfo> receivers = context.getPackageManager().queryBroadcastReceivers(new Intent(Intent.ACTION_BOOT_COMPLETED), 0); assertNotNull(receivers); assertFalse(receivers.isEmpty()); installCondomProcess(context, new CondomOptions().setOutboundJudge(sBlockAllJudge)); assertNull(context.getPackageManager().resolveService(service_intent, 0)); List<ResolveInfo> result = context.getPackageManager().queryIntentServices(service_intent, 0); assertTrue(result.isEmpty()); service_intent.setPackage(service.serviceInfo.packageName); assertNull(context.getPackageManager().resolveService(service_intent, 0)); result = context.getPackageManager().queryIntentServices(service_intent, 0); assertTrue(result.isEmpty()); service_intent.setPackage(null); }}); }
runInSeparateProcess(new TestService.Procedure() { @Override public void run(final Context context) { final Intent intent = new Intent(context, TestService.class); final ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(final ComponentName name, final IBinder service) {} @Override public void onServiceDisconnected(final ComponentName name) {} }; assertTrue("Test service is not properly setup.", context.bindService(intent, connection, Context.BIND_AUTO_CREATE)); context.unbindService(connection); installCondomProcess(context, new CondomOptions().setOutboundJudge(sBlockAllJudge)); withFakeSelfPackageName(new Runnable() { @Override public void run() { assertFalse(context.bindService(intent, connection, Context.BIND_AUTO_CREATE)); }}); // Block by outbound judge context.unbindService(connection); }}); // TODO: More cases
@Test public void testContentProviderOutboundJudge() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().setOutboundJudge(new OutboundJudge() { @Override public boolean shouldAllow(final OutboundType type, final @Nullable Intent intent, final String target_pkg) { final String settings_pkg = InstrumentationRegistry.getTargetContext().getPackageManager().resolveContentProvider(Settings.System.CONTENT_URI.getAuthority(), 0).packageName; return ! settings_pkg.equals(target_pkg); }}); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); for (final Context context2test : new Context[] {condom, condom.getApplicationContext()}) { assertNull(context2test.getPackageManager().resolveContentProvider(Settings.AUTHORITY, 0)); assertNull(context2test.getContentResolver().acquireContentProviderClient(Settings.System.CONTENT_URI)); } for (final Context context2test : new Context[] {dry_condom, dry_condom.getApplicationContext()}) { assertNotNull(context2test.getPackageManager().resolveContentProvider(Settings.AUTHORITY, 0)); assertNotNull(context2test.getContentResolver().acquireContentProviderClient(Settings.System.CONTENT_URI)); } }
@Test public void testOutboundJudge() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().setOutboundJudge(new OutboundJudge() { @Override public boolean shouldAllow(final OutboundType type, final @Nullable Intent intent, final String target_pkg) { mNumOutboundJudgeCalled.incrementAndGet();
@Test public void testContentProviderOutboundJudge() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().setOutboundJudge(new OutboundJudge() { @Override public boolean shouldAllow(final OutboundType type, final @Nullable Intent intent, final String target_pkg) { final String settings_pkg = InstrumentationRegistry.getTargetContext().getPackageManager().resolveContentProvider(Settings.System.CONTENT_URI.getAuthority(), 0).packageName; return ! settings_pkg.equals(target_pkg); }}); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); assertNull(condom.getPackageManager().resolveContentProvider(Settings.AUTHORITY, 0)); assertNotNull(dry_condom.getPackageManager().resolveContentProvider(Settings.AUTHORITY, 0)); assertNull(condom.getContentResolver().acquireContentProviderClient(Settings.System.CONTENT_URI)); assertNotNull(dry_condom.getContentResolver().acquireContentProviderClient(Settings.System.CONTENT_URI)); }
@Test public void testOutboundJudge() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().setOutboundJudge(new OutboundJudge() { @Override public boolean shouldAllow(final OutboundType type, final @Nullable Intent intent, final String target_pkg) { mNumOutboundJudgeCalled.incrementAndGet();