AdVpnThread implements Runnable, DnsPacketProxy.EventLoop { void configurePackages(VpnService.Builder builder, Configuration config) { Set<String> allowOnVpn = new HashSet<>(); Set<String> doNotAllowOnVpn = new HashSet<>(); config.whitelist.resolve(vpnService.getPackageManager(), allowOnVpn, doNotAllowOnVpn); if (config.whitelist.defaultMode == Configuration.Whitelist.DEFAULT_MODE_NOT_ON_VPN) { for (String app : allowOnVpn) { try { Log.d(TAG, "configure: Allowing " + app + " to use the DNS VPN"); builder.addAllowedApplication(app); } catch (Exception e) { Log.w(TAG, "configure: Cannot disallow", e); } } } else { for (String app : doNotAllowOnVpn) { try { Log.d(TAG, "configure: Disallowing " + app + " from using the DNS VPN"); builder.addDisallowedApplication(app); } catch (Exception e) { Log.w(TAG, "configure: Cannot disallow", e); } } } } AdVpnThread(VpnService vpnService, Notify notify); void startThread(); void stopThread(); @Override synchronized void run(); void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket); void queueDeviceWrite(IpPacket ipOutPacket);  }
@Test public void testConfigurePackages() throws Exception { final List<String> disallowed = new ArrayList<>(); final List<String> allowed = new ArrayList<>(); when(builder.addDisallowedApplication(anyString())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { disallowed.add(invocation.getArgumentAt(0, String.class)); return null; } }); when(builder.addAllowedApplication(anyString())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { allowed.add(invocation.getArgumentAt(0, String.class)); return null; } }); allowed.clear(); disallowed.clear(); config.whitelist.defaultMode = Configuration.Whitelist.DEFAULT_MODE_NOT_ON_VPN; thread.configurePackages(builder, config); assertTrue(allowed.contains("onVpn")); assertEquals(new ArrayList<String>(), disallowed); allowed.clear(); disallowed.clear(); config.whitelist.defaultMode = Configuration.Whitelist.DEFAULT_MODE_ON_VPN; thread.configurePackages(builder, config); assertTrue(disallowed.contains("notOnVpn")); assertEquals(new ArrayList<String>(), allowed); allowed.clear(); disallowed.clear(); config.whitelist.defaultMode = Configuration.Whitelist.DEFAULT_MODE_INTELLIGENT; thread.configurePackages(builder, config); assertTrue(disallowed.contains("notOnVpn")); assertEquals(new ArrayList<String>(), allowed); }