@Test public void testContentProvider() { final TestContext context = new TestContext(); final CondomContext condom = CondomContext.wrap(context, TAG), dry_condom = CondomContext.wrap(context, TAG, new CondomOptions().setDryRun(true)); // Regular provider access final String android_id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); assertNotNull(android_id); for (final Context context2test : new Context[] {condom, condom.getApplicationContext(), dry_condom, dry_condom.getApplicationContext()}) { final String condom_android_id = Settings.Secure.getString(context2test.getContentResolver(), Settings.Secure.ANDROID_ID); assertEquals(android_id, condom_android_id); } context.mTestingStoppedProvider = true; for (final Context context2test : new Context[] {condom, condom.getApplicationContext()}) { // Prevent stopped packages, assertNull(context2test.getPackageManager().resolveContentProvider(TEST_AUTHORITY, 0)); assertNull(context2test.getContentResolver().acquireContentProviderClient(TEST_CONTENT_URI)); // Providers in system package should not be blocked. assertNotNull(context2test.getPackageManager().resolveContentProvider(Settings.AUTHORITY, 0)); assertNotNull(context2test.getContentResolver().acquireContentProviderClient(Settings.System.CONTENT_URI)); } for (final Context context2test : new Context[] {dry_condom, dry_condom.getApplicationContext()}) { assertNotNull(context2test.getPackageManager().resolveContentProvider(TEST_AUTHORITY, 0)); assertNotNull(context2test.getContentResolver().acquireContentProviderClient(TEST_CONTENT_URI)); } context.mTestingStoppedProvider = false; } private static final String TEST_AUTHORITY = "com.oasisfeng.condom.test";
@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 testPreventServiceInBackgroundPackages() { Assume.assumeTrue(SDK_INT < O); final TestContext context = new TestContext(); context.mTestingBackgroundUid = true; final CondomOptions options = new CondomOptions().preventServiceInBackgroundPackages(true).preventBroadcastToBackgroundPackages(false); 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()}) { final List<ResolveInfo> services = context2test.getPackageManager().queryIntentServices(intent(), 0); assertEquals(3, services.size()); context.assertBaseCalled(); final ResolveInfo resolve = context2test.getPackageManager().resolveService(intent(), 0); assertEquals("non.bg.service", resolve.serviceInfo.packageName); context.assertBaseCalled(); } for (final Context context2test : new Context[] {dry_condom, dry_condom.getApplicationContext()}) { final List<ResolveInfo> services = context2test.getPackageManager().queryIntentServices(intent(), 0); assertEquals(4, services.size()); context.assertBaseCalled(); assertEquals(7777777, context2test.getPackageManager().resolveService(intent(), 0).serviceInfo.applicationInfo.uid); context.assertBaseCalled(); } }
@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) {
@Test public void testContentProvider() { final TestContext context = new TestContext(); final CondomContext condom = CondomContext.wrap(context, TAG), dry_condom = CondomContext.wrap(context, TAG, new CondomOptions().setDryRun(true)); // Regular provider access final String android_id = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID); assertNotNull(android_id); final String condom_android_id = Settings.System.getString(condom.getContentResolver(), Settings.System.ANDROID_ID); assertEquals(android_id, condom_android_id); final String dry_android_id = Settings.System.getString(dry_condom.getContentResolver(), Settings.System.ANDROID_ID); assertEquals(android_id, dry_android_id); context.mTestingStoppedProvider = true; // Prevent stopped packages, assertNull(condom.getPackageManager().resolveContentProvider(TEST_AUTHORITY, 0)); assertNotNull(dry_condom.getPackageManager().resolveContentProvider(TEST_AUTHORITY, 0)); assertNull(condom.getContentResolver().acquireContentProviderClient(TEST_CONTENT_URI)); assertNotNull(dry_condom.getContentResolver().acquireContentProviderClient(TEST_CONTENT_URI)); // Providers in system package should not be blocked. assertNotNull(condom.getPackageManager().resolveContentProvider(Settings.AUTHORITY, 0)); assertNotNull(dry_condom.getPackageManager().resolveContentProvider(Settings.AUTHORITY, 0)); assertNotNull(condom.getContentResolver().acquireContentProviderClient(Settings.System.CONTENT_URI)); assertNotNull(dry_condom.getContentResolver().acquireContentProviderClient(Settings.System.CONTENT_URI)); context.mTestingStoppedProvider = false; } private static final String TEST_AUTHORITY = "com.oasisfeng.condom.test";
@Test public void testSelfTargeted() { final TestContext context = new TestContext(); final CondomContext condom = CondomContext.wrap(context, TAG), dry_condom = CondomContext.wrap(context, TAG, new CondomOptions().setDryRun(true)); // Self-targeting test final String self_pkg = condom.getPackageName(); final Intent[] self_targeted_intents = new Intent[] { intent().setPackage(self_pkg), intent().setComponent(new ComponentName(self_pkg, "X")) }; with(self_targeted_intents, allBroadcastApis(condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); with(self_targeted_intents, allServiceApis(condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); with(self_targeted_intents, allBroadcastApis(dry_condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); with(self_targeted_intents, allServiceApis(dry_condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); }
@Test public void testPreventNone() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().preventServiceInBackgroundPackages(false).preventBroadcastToBackgroundPackages(false); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); //noinspection deprecation, intentional test for deprecated method condom.preventWakingUpStoppedPackages(false); //noinspection deprecation dry_condom.preventWakingUpStoppedPackages(false); with(ALL_SORT_OF_INTENTS, allBroadcastApis(condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); with(ALL_SORT_OF_INTENTS, allServiceApis(condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); with(ALL_SORT_OF_INTENTS, allBroadcastApis(dry_condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); with(ALL_SORT_OF_INTENTS, allServiceApis(dry_condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); }
@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 testPreventBroadcastToBackgroundPackages() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().preventBroadcastToBackgroundPackages(true); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); final int extra_flag = SDK_INT >= N ? CondomCore.FLAG_RECEIVER_EXCLUDE_BACKGROUND : FLAG_RECEIVER_REGISTERED_ONLY; with(ALL_SORT_OF_INTENTS, allBroadcastApis(condom), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_EXCLUDE_STOPPED_PACKAGES | extra_flag)); with(ALL_SORT_OF_INTENTS, allBroadcastApis(dry_condom), context.EXPECT_BASE_CALLED, context.expectFlags(0)); }
@Test public void testPreventWakingUpStoppedPackages_IncludingDryRun() { final Intent[] intents_with_inc_stop = ALL_SORT_OF_INTENTS.clone(); for (int i = 0; i < intents_with_inc_stop.length; i++) intents_with_inc_stop[i] = new Intent(intents_with_inc_stop[i]).addFlags(FLAG_INCLUDE_STOPPED_PACKAGES); final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().preventBroadcastToBackgroundPackages(false).preventServiceInBackgroundPackages(false); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); with(intents_with_inc_stop, allBroadcastApis(condom), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_EXCLUDE_STOPPED_PACKAGES)); with(intents_with_inc_stop, allServiceApis(condom), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_EXCLUDE_STOPPED_PACKAGES)); with(intents_with_inc_stop, allBroadcastApis(dry_condom), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_INCLUDE_STOPPED_PACKAGES)); with(intents_with_inc_stop, allServiceApis(dry_condom), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_INCLUDE_STOPPED_PACKAGES)); }
@Test public void testPreventServiceInBackgroundPackages() { final TestContext context = new TestContext(); context.mTestingBackgroundUid = true; final CondomOptions options = new CondomOptions().preventServiceInBackgroundPackages(true).preventBroadcastToBackgroundPackages(false); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); assertEquals(3, condom.getPackageManager().queryIntentServices(intent(), 0).size()); context.assertBaseCalled(); assertEquals(4, dry_condom.getPackageManager().queryIntentServices(intent(), 0).size()); context.assertBaseCalled(); assertEquals("non.bg.service", condom.getPackageManager().resolveService(intent(), 0).serviceInfo.packageName); context.assertBaseCalled(); assertEquals(7777777, dry_condom.getPackageManager().resolveService(intent(), 0).serviceInfo.applicationInfo.uid); context.assertBaseCalled(); }
@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) {
@Test public void testSelfTargeted() { final TestContext context = new TestContext(); final CondomContext condom = CondomContext.wrap(context, TAG), dry_condom = CondomContext.wrap(context, TAG, new CondomOptions().setDryRun(true)); // Self-targeting test final String self_pkg = condom.getPackageName(); final Intent[] self_targeted_intents = new Intent[] { intent().setPackage(self_pkg), intent().setComponent(new ComponentName(self_pkg, "X")) }; for (final Context context2test : new Context[] {condom, condom.getApplicationContext(), dry_condom, dry_condom.getApplicationContext()}) with(self_targeted_intents, allBroadcastAndServiceApis(context2test), context.EXPECT_BASE_CALLED, context.expectFlags(0)); }
@Test public void testPreventWakingUpStoppedPackages_IncludingDryRun() { final Intent[] intents_with_inc_stop = ALL_SORT_OF_INTENTS.clone(); for (int i = 0; i < intents_with_inc_stop.length; i++) intents_with_inc_stop[i] = new Intent(intents_with_inc_stop[i]).addFlags(FLAG_INCLUDE_STOPPED_PACKAGES); final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().preventBroadcastToBackgroundPackages(false).preventServiceInBackgroundPackages(false); 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()}) with(intents_with_inc_stop, allBroadcastAndServiceApis(context2test), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_EXCLUDE_STOPPED_PACKAGES)); for (final Context context2test : new Context[] {dry_condom, dry_condom.getApplicationContext()}) with(intents_with_inc_stop, allBroadcastAndServiceApis(context2test), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_INCLUDE_STOPPED_PACKAGES)); }
@Test public void testPreventBroadcastToBackgroundPackages() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().preventBroadcastToBackgroundPackages(true); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); final int extra_flag = SDK_INT >= N ? CondomCore.FLAG_RECEIVER_EXCLUDE_BACKGROUND : FLAG_RECEIVER_REGISTERED_ONLY; for (final Context context2test : new Context[] {condom, condom.getApplicationContext()}) with(ALL_SORT_OF_INTENTS, allBroadcastApis(context2test), context.EXPECT_BASE_CALLED, context.expectFlags(FLAG_EXCLUDE_STOPPED_PACKAGES | extra_flag)); for (final Context context2test : new Context[] {dry_condom, dry_condom.getApplicationContext()}) with(ALL_SORT_OF_INTENTS, allBroadcastApis(context2test), context.EXPECT_BASE_CALLED, context.expectFlags(0)); }
@Test public void testPreventNone() { final TestContext context = new TestContext(); final CondomOptions options = new CondomOptions().preventServiceInBackgroundPackages(false).preventBroadcastToBackgroundPackages(false); final CondomContext condom = CondomContext.wrap(context, TAG, options), dry_condom = CondomContext.wrap(context, TAG, options.setDryRun(true)); //noinspection deprecation, intentional test for deprecated method condom.preventWakingUpStoppedPackages(false); //noinspection deprecation dry_condom.preventWakingUpStoppedPackages(false); for (final Context context2test : new Context[] {condom, condom.getApplicationContext(), dry_condom, dry_condom.getApplicationContext()}) with(ALL_SORT_OF_INTENTS, allBroadcastAndServiceApis(context2test), context.EXPECT_BASE_CALLED, context.expectFlags(0)); }