diff options
Diffstat (limited to 'tests')
5 files changed, 390 insertions, 40 deletions
diff --git a/tests/net/OWNERS b/tests/net/OWNERS index ce50558bf4f6..7311eee32a4c 100644 --- a/tests/net/OWNERS +++ b/tests/net/OWNERS @@ -1,6 +1,8 @@ set noparent +codewiz@google.com ek@google.com jchalard@google.com lorenzo@google.com +reminv@google.com satk@google.com diff --git a/tests/net/java/android/net/NetworkUtilsTest.java b/tests/net/java/android/net/NetworkUtilsTest.java index a5ee8e37553d..2b172dac4865 100644 --- a/tests/net/java/android/net/NetworkUtilsTest.java +++ b/tests/net/java/android/net/NetworkUtilsTest.java @@ -16,17 +16,32 @@ package android.net; -import android.net.NetworkUtils; -import android.test.suitebuilder.annotation.SmallTest; +import static android.net.NetworkUtils.getImplicitNetmask; +import static android.net.NetworkUtils.inet4AddressToIntHTH; +import static android.net.NetworkUtils.inet4AddressToIntHTL; +import static android.net.NetworkUtils.intToInet4AddressHTH; +import static android.net.NetworkUtils.intToInet4AddressHTL; +import static android.net.NetworkUtils.netmaskToPrefixLength; +import static android.net.NetworkUtils.prefixLengthToV4NetmaskIntHTH; +import static android.net.NetworkUtils.prefixLengthToV4NetmaskIntHTL; + +import static junit.framework.Assert.assertEquals; + +import static org.junit.Assert.fail; + +import android.support.test.runner.AndroidJUnit4; import java.math.BigInteger; import java.net.Inet4Address; import java.net.InetAddress; import java.util.TreeSet; -import junit.framework.TestCase; +import org.junit.Test; +import org.junit.runner.RunWith; -public class NetworkUtilsTest extends TestCase { +@RunWith(AndroidJUnit4.class) +@android.support.test.filters.SmallTest +public class NetworkUtilsTest { private InetAddress Address(String addr) { return InetAddress.parseNumericAddress(addr); @@ -36,41 +51,126 @@ public class NetworkUtilsTest extends TestCase { return (Inet4Address) Address(addr); } - @SmallTest + @Test public void testGetImplicitNetmask() { - assertEquals(8, NetworkUtils.getImplicitNetmask(IPv4Address("4.2.2.2"))); - assertEquals(8, NetworkUtils.getImplicitNetmask(IPv4Address("10.5.6.7"))); - assertEquals(16, NetworkUtils.getImplicitNetmask(IPv4Address("173.194.72.105"))); - assertEquals(16, NetworkUtils.getImplicitNetmask(IPv4Address("172.23.68.145"))); - assertEquals(24, NetworkUtils.getImplicitNetmask(IPv4Address("192.0.2.1"))); - assertEquals(24, NetworkUtils.getImplicitNetmask(IPv4Address("192.168.5.1"))); - assertEquals(32, NetworkUtils.getImplicitNetmask(IPv4Address("224.0.0.1"))); - assertEquals(32, NetworkUtils.getImplicitNetmask(IPv4Address("255.6.7.8"))); + assertEquals(8, getImplicitNetmask(IPv4Address("4.2.2.2"))); + assertEquals(8, getImplicitNetmask(IPv4Address("10.5.6.7"))); + assertEquals(16, getImplicitNetmask(IPv4Address("173.194.72.105"))); + assertEquals(16, getImplicitNetmask(IPv4Address("172.23.68.145"))); + assertEquals(24, getImplicitNetmask(IPv4Address("192.0.2.1"))); + assertEquals(24, getImplicitNetmask(IPv4Address("192.168.5.1"))); + assertEquals(32, getImplicitNetmask(IPv4Address("224.0.0.1"))); + assertEquals(32, getImplicitNetmask(IPv4Address("255.6.7.8"))); } private void assertInvalidNetworkMask(Inet4Address addr) { try { - NetworkUtils.netmaskToPrefixLength(addr); + netmaskToPrefixLength(addr); fail("Invalid netmask " + addr.getHostAddress() + " did not cause exception"); } catch (IllegalArgumentException expected) { } } - @SmallTest + @Test + public void testInet4AddressToIntHTL() { + assertEquals(0, inet4AddressToIntHTL(IPv4Address("0.0.0.0"))); + assertEquals(0x000080ff, inet4AddressToIntHTL(IPv4Address("255.128.0.0"))); + assertEquals(0x0080ff0a, inet4AddressToIntHTL(IPv4Address("10.255.128.0"))); + assertEquals(0x00feff0a, inet4AddressToIntHTL(IPv4Address("10.255.254.0"))); + assertEquals(0xfeffa8c0, inet4AddressToIntHTL(IPv4Address("192.168.255.254"))); + assertEquals(0xffffa8c0, inet4AddressToIntHTL(IPv4Address("192.168.255.255"))); + } + + @Test + public void testIntToInet4AddressHTL() { + assertEquals(IPv4Address("0.0.0.0"), intToInet4AddressHTL(0)); + assertEquals(IPv4Address("255.128.0.0"), intToInet4AddressHTL(0x000080ff)); + assertEquals(IPv4Address("10.255.128.0"), intToInet4AddressHTL(0x0080ff0a)); + assertEquals(IPv4Address("10.255.254.0"), intToInet4AddressHTL(0x00feff0a)); + assertEquals(IPv4Address("192.168.255.254"), intToInet4AddressHTL(0xfeffa8c0)); + assertEquals(IPv4Address("192.168.255.255"), intToInet4AddressHTL(0xffffa8c0)); + } + + @Test + public void testInet4AddressToIntHTH() { + assertEquals(0, inet4AddressToIntHTH(IPv4Address("0.0.0.0"))); + assertEquals(0xff800000, inet4AddressToIntHTH(IPv4Address("255.128.0.0"))); + assertEquals(0x0aff8000, inet4AddressToIntHTH(IPv4Address("10.255.128.0"))); + assertEquals(0x0afffe00, inet4AddressToIntHTH(IPv4Address("10.255.254.0"))); + assertEquals(0xc0a8fffe, inet4AddressToIntHTH(IPv4Address("192.168.255.254"))); + assertEquals(0xc0a8ffff, inet4AddressToIntHTH(IPv4Address("192.168.255.255"))); + } + + @Test + public void testIntToInet4AddressHTH() { + assertEquals(IPv4Address("0.0.0.0"), intToInet4AddressHTH(0)); + assertEquals(IPv4Address("255.128.0.0"), intToInet4AddressHTH(0xff800000)); + assertEquals(IPv4Address("10.255.128.0"), intToInet4AddressHTH(0x0aff8000)); + assertEquals(IPv4Address("10.255.254.0"), intToInet4AddressHTH(0x0afffe00)); + assertEquals(IPv4Address("192.168.255.254"), intToInet4AddressHTH(0xc0a8fffe)); + assertEquals(IPv4Address("192.168.255.255"), intToInet4AddressHTH(0xc0a8ffff)); + } + + @Test public void testNetmaskToPrefixLength() { - assertEquals(0, NetworkUtils.netmaskToPrefixLength(IPv4Address("0.0.0.0"))); - assertEquals(9, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.128.0.0"))); - assertEquals(17, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.128.0"))); - assertEquals(23, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.254.0"))); - assertEquals(31, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.255.254"))); - assertEquals(32, NetworkUtils.netmaskToPrefixLength(IPv4Address("255.255.255.255"))); + assertEquals(0, netmaskToPrefixLength(IPv4Address("0.0.0.0"))); + assertEquals(9, netmaskToPrefixLength(IPv4Address("255.128.0.0"))); + assertEquals(17, netmaskToPrefixLength(IPv4Address("255.255.128.0"))); + assertEquals(23, netmaskToPrefixLength(IPv4Address("255.255.254.0"))); + assertEquals(31, netmaskToPrefixLength(IPv4Address("255.255.255.254"))); + assertEquals(32, netmaskToPrefixLength(IPv4Address("255.255.255.255"))); assertInvalidNetworkMask(IPv4Address("0.0.0.1")); assertInvalidNetworkMask(IPv4Address("255.255.255.253")); assertInvalidNetworkMask(IPv4Address("255.255.0.255")); } - @SmallTest + + @Test + public void testPrefixLengthToV4NetmaskIntHTL() { + assertEquals(0, prefixLengthToV4NetmaskIntHTL(0)); + assertEquals(0x000080ff /* 255.128.0.0 */, prefixLengthToV4NetmaskIntHTL(9)); + assertEquals(0x0080ffff /* 255.255.128.0 */, prefixLengthToV4NetmaskIntHTL(17)); + assertEquals(0x00feffff /* 255.255.254.0 */, prefixLengthToV4NetmaskIntHTL(23)); + assertEquals(0xfeffffff /* 255.255.255.254 */, prefixLengthToV4NetmaskIntHTL(31)); + assertEquals(0xffffffff /* 255.255.255.255 */, prefixLengthToV4NetmaskIntHTL(32)); + } + + @Test + public void testPrefixLengthToV4NetmaskIntHTH() { + assertEquals(0, prefixLengthToV4NetmaskIntHTH(0)); + assertEquals(0xff800000 /* 255.128.0.0 */, prefixLengthToV4NetmaskIntHTH(9)); + assertEquals(0xffff8000 /* 255.255.128.0 */, prefixLengthToV4NetmaskIntHTH(17)); + assertEquals(0xfffffe00 /* 255.255.254.0 */, prefixLengthToV4NetmaskIntHTH(23)); + assertEquals(0xfffffffe /* 255.255.255.254 */, prefixLengthToV4NetmaskIntHTH(31)); + assertEquals(0xffffffff /* 255.255.255.255 */, prefixLengthToV4NetmaskIntHTH(32)); + } + + @Test(expected = IllegalArgumentException.class) + public void testPrefixLengthToV4NetmaskIntHTH_NegativeLength() { + prefixLengthToV4NetmaskIntHTH(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testPrefixLengthToV4NetmaskIntHTH_LengthTooLarge() { + prefixLengthToV4NetmaskIntHTH(33); + } + + private void checkAddressMasking(String expectedAddr, String addr, int prefixLength) { + final int prefix = prefixLengthToV4NetmaskIntHTH(prefixLength); + final int addrInt = inet4AddressToIntHTH(IPv4Address(addr)); + assertEquals(IPv4Address(expectedAddr), intToInet4AddressHTH(prefix & addrInt)); + } + + @Test + public void testPrefixLengthToV4NetmaskIntHTH_MaskAddr() { + checkAddressMasking("192.168.0.0", "192.168.128.1", 16); + checkAddressMasking("255.240.0.0", "255.255.255.255", 12); + checkAddressMasking("255.255.255.255", "255.255.255.255", 32); + checkAddressMasking("0.0.0.0", "255.255.255.255", 0); + } + + @Test public void testRoutedIPv4AddressCount() { final TreeSet<IpPrefix> set = new TreeSet<>(IpPrefix.lengthComparator()); // No routes routes to no addresses. @@ -118,7 +218,7 @@ public class NetworkUtilsTest extends TestCase { assertEquals(7l - 4 + 4 + 16 + 65536, NetworkUtils.routedIPv4AddressCount(set)); } - @SmallTest + @Test public void testRoutedIPv6AddressCount() { final TreeSet<IpPrefix> set = new TreeSet<>(IpPrefix.lengthComparator()); // No routes routes to no addresses. diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java index 142c88b2f67d..e3db7e8a1354 100644 --- a/tests/net/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java @@ -880,7 +880,7 @@ public class ConnectivityServiceTest { NetworkAgentInfo networkAgentInfo, NetworkRequest defaultRequest, IpConnectivityLog log) { super(context, handler, networkAgentInfo, defaultRequest, log, - NetworkMonitor.NetworkMonitorSettings.DEFAULT); + NetworkMonitor.Dependencies.DEFAULT); connectivityHandler = handler; } diff --git a/tests/net/java/com/android/server/connectivity/NetworkMonitorTest.java b/tests/net/java/com/android/server/connectivity/NetworkMonitorTest.java index 27a897d175a2..b01713006254 100644 --- a/tests/net/java/com/android/server/connectivity/NetworkMonitorTest.java +++ b/tests/net/java/com/android/server/connectivity/NetworkMonitorTest.java @@ -16,22 +16,35 @@ package com.android.server.connectivity; -import static org.junit.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; + +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; +import android.net.ConnectivityManager; +import android.net.LinkProperties; import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkInfo; import android.net.NetworkRequest; +import android.net.captiveportal.CaptivePortalProbeResult; import android.net.metrics.IpConnectivityLog; import android.net.wifi.WifiManager; import android.os.Handler; +import android.provider.Settings; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; import android.telephony.TelephonyManager; +import android.util.ArrayMap; import org.junit.Before; import org.junit.Test; @@ -39,38 +52,273 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.InetAddress; +import java.net.URL; +import java.util.Random; + +import javax.net.ssl.SSLHandshakeException; + @RunWith(AndroidJUnit4.class) @SmallTest public class NetworkMonitorTest { + private static final String LOCATION_HEADER = "location"; - static final int TEST_ID = 60; // should be less than min netid 100 + private @Mock Context mContext; + private @Mock Handler mHandler; + private @Mock IpConnectivityLog mLogger; + private @Mock NetworkAgentInfo mAgent; + private @Mock NetworkInfo mNetworkInfo; + private @Mock NetworkRequest mRequest; + private @Mock TelephonyManager mTelephony; + private @Mock WifiManager mWifi; + private @Mock Network mNetwork; + private @Mock HttpURLConnection mHttpConnection; + private @Mock HttpURLConnection mHttpsConnection; + private @Mock HttpURLConnection mFallbackConnection; + private @Mock HttpURLConnection mOtherFallbackConnection; + private @Mock Random mRandom; + private @Mock NetworkMonitor.Dependencies mDependencies; - @Mock Context mContext; - @Mock Handler mHandler; - @Mock IpConnectivityLog mLogger; - @Mock NetworkAgentInfo mAgent; - @Mock NetworkMonitor.NetworkMonitorSettings mSettings; - @Mock NetworkRequest mRequest; - @Mock TelephonyManager mTelephony; - @Mock WifiManager mWifi; + private static final String TEST_HTTP_URL = "http://www.google.com/gen_204"; + private static final String TEST_HTTPS_URL = "https://www.google.com/gen_204"; + private static final String TEST_FALLBACK_URL = "http://fallback.google.com/gen_204"; + private static final String TEST_OTHER_FALLBACK_URL = "http://otherfallback.google.com/gen_204"; @Before - public void setUp() { + public void setUp() throws IOException { MockitoAnnotations.initMocks(this); + mAgent.linkProperties = new LinkProperties(); + mAgent.networkCapabilities = new NetworkCapabilities() + .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); + mAgent.networkInfo = mNetworkInfo; + + when(mAgent.network()).thenReturn(mNetwork); + when(mDependencies.getNetwork(any())).thenReturn(mNetwork); + when(mDependencies.getRandom()).thenReturn(mRandom); + when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_MODE), anyInt())) + .thenReturn(Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); + when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_USE_HTTPS), + anyInt())).thenReturn(1); + when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTP_URL), + anyString())).thenReturn(TEST_HTTP_URL); + when(mDependencies.getSetting(any(), eq(Settings.Global.CAPTIVE_PORTAL_HTTPS_URL), + anyString())).thenReturn(TEST_HTTPS_URL); - when(mAgent.network()).thenReturn(new Network(TEST_ID)); when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephony); when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifi); + + when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); + setFallbackUrl(TEST_FALLBACK_URL); + setOtherFallbackUrls(TEST_OTHER_FALLBACK_URL); + setFallbackSpecs(null); // Test with no fallback spec by default + when(mRandom.nextInt()).thenReturn(0); + + when(mNetwork.openConnection(any())).then((invocation) -> { + URL url = invocation.getArgument(0); + switch(url.toString()) { + case TEST_HTTP_URL: + return mHttpConnection; + case TEST_HTTPS_URL: + return mHttpsConnection; + case TEST_FALLBACK_URL: + return mFallbackConnection; + case TEST_OTHER_FALLBACK_URL: + return mOtherFallbackConnection; + default: + fail("URL not mocked: " + url.toString()); + return null; + } + }); + when(mHttpConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); + when(mHttpsConnection.getRequestProperties()).thenReturn(new ArrayMap<>()); + when(mNetwork.getAllByName(any())).thenReturn(new InetAddress[] { + InetAddress.parseNumericAddress("192.168.0.0") + }); } NetworkMonitor makeMonitor() { - return new NetworkMonitor(mContext, mHandler, mAgent, mRequest, mLogger, mSettings); + return new NetworkMonitor( + mContext, mHandler, mAgent, mRequest, mLogger, mDependencies); + } + + @Test + public void testIsCaptivePortal_HttpProbeIsPortal() throws IOException { + setSslException(mHttpsConnection); + setPortal302(mHttpConnection); + + assertPortal(makeMonitor().isCaptivePortal()); + } + + @Test + public void testIsCaptivePortal_HttpsProbeIsNotPortal() throws IOException { + setStatus(mHttpsConnection, 204); + setStatus(mHttpConnection, 500); + + assertNotPortal(makeMonitor().isCaptivePortal()); + } + + @Test + public void testIsCaptivePortal_HttpsProbeFailedHttpSuccessNotUsed() throws IOException { + setSslException(mHttpsConnection); + // Even if HTTP returns a 204, do not use the result unless HTTPS succeeded + setStatus(mHttpConnection, 204); + setStatus(mFallbackConnection, 500); + + assertFailed(makeMonitor().isCaptivePortal()); + } + + @Test + public void testIsCaptivePortal_FallbackProbeIsPortal() throws IOException { + setSslException(mHttpsConnection); + setStatus(mHttpConnection, 500); + setPortal302(mFallbackConnection); + + assertPortal(makeMonitor().isCaptivePortal()); + } + + @Test + public void testIsCaptivePortal_FallbackProbeIsNotPortal() throws IOException { + setSslException(mHttpsConnection); + setStatus(mHttpConnection, 500); + setStatus(mFallbackConnection, 204); + + // Fallback probe did not see portal, HTTPS failed -> inconclusive + assertFailed(makeMonitor().isCaptivePortal()); + } + + @Test + public void testIsCaptivePortal_OtherFallbackProbeIsPortal() throws IOException { + // Set all fallback probes but one to invalid URLs to verify they are being skipped + setFallbackUrl(TEST_FALLBACK_URL); + setOtherFallbackUrls(TEST_FALLBACK_URL + "," + TEST_OTHER_FALLBACK_URL); + + setSslException(mHttpsConnection); + setStatus(mHttpConnection, 500); + setStatus(mFallbackConnection, 500); + setPortal302(mOtherFallbackConnection); + + // TEST_OTHER_FALLBACK_URL is third + when(mRandom.nextInt()).thenReturn(2); + + final NetworkMonitor monitor = makeMonitor(); + + // First check always uses the first fallback URL: inconclusive + assertFailed(monitor.isCaptivePortal()); + verify(mFallbackConnection, times(1)).getResponseCode(); + verify(mOtherFallbackConnection, never()).getResponseCode(); + + // Second check uses the URL chosen by Random + assertPortal(monitor.isCaptivePortal()); + verify(mOtherFallbackConnection, times(1)).getResponseCode(); + } + + @Test + public void testIsCaptivePortal_AllProbesFailed() throws IOException { + setSslException(mHttpsConnection); + setStatus(mHttpConnection, 500); + setStatus(mFallbackConnection, 404); + + assertFailed(makeMonitor().isCaptivePortal()); + verify(mFallbackConnection, times(1)).getResponseCode(); + verify(mOtherFallbackConnection, never()).getResponseCode(); + } + + @Test + public void testIsCaptivePortal_InvalidUrlSkipped() throws IOException { + setFallbackUrl("invalid"); + setOtherFallbackUrls("otherinvalid," + TEST_OTHER_FALLBACK_URL + ",yetanotherinvalid"); + + setSslException(mHttpsConnection); + setStatus(mHttpConnection, 500); + setPortal302(mOtherFallbackConnection); + + assertPortal(makeMonitor().isCaptivePortal()); + verify(mOtherFallbackConnection, times(1)).getResponseCode(); + verify(mFallbackConnection, never()).getResponseCode(); + } + + private void setupFallbackSpec() throws IOException { + setFallbackSpecs("http://example.com@@/@@204@@/@@" + + "@@,@@" + + TEST_OTHER_FALLBACK_URL + "@@/@@30[12]@@/@@https://(www\\.)?google.com/?.*"); + + setSslException(mHttpsConnection); + setStatus(mHttpConnection, 500); + + // Use the 2nd fallback spec + when(mRandom.nextInt()).thenReturn(1); } @Test - public void testCreatingNetworkMonitor() { - NetworkMonitor monitor = makeMonitor(); + public void testIsCaptivePortal_FallbackSpecIsNotPortal() throws IOException { + setupFallbackSpec(); + set302(mOtherFallbackConnection, "https://www.google.com/test?q=3"); + + // HTTPS failed, fallback spec did not see a portal -> inconclusive + assertFailed(makeMonitor().isCaptivePortal()); + verify(mOtherFallbackConnection, times(1)).getResponseCode(); + verify(mFallbackConnection, never()).getResponseCode(); + } + + @Test + public void testIsCaptivePortal_FallbackSpecIsPortal() throws IOException { + setupFallbackSpec(); + set302(mOtherFallbackConnection, "http://login.portal.example.com"); + + assertPortal(makeMonitor().isCaptivePortal()); + } + + private void setFallbackUrl(String url) { + when(mDependencies.getSetting(any(), + eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL), any())).thenReturn(url); + } + + private void setOtherFallbackUrls(String urls) { + when(mDependencies.getSetting(any(), + eq(Settings.Global.CAPTIVE_PORTAL_OTHER_FALLBACK_URLS), any())).thenReturn(urls); + } + + private void setFallbackSpecs(String specs) { + when(mDependencies.getSetting(any(), + eq(Settings.Global.CAPTIVE_PORTAL_FALLBACK_PROBE_SPECS), any())).thenReturn(specs); + } + + private void assertPortal(CaptivePortalProbeResult result) { + assertTrue(result.isPortal()); + assertFalse(result.isFailed()); + assertFalse(result.isSuccessful()); + } + + private void assertNotPortal(CaptivePortalProbeResult result) { + assertFalse(result.isPortal()); + assertFalse(result.isFailed()); + assertTrue(result.isSuccessful()); + } + + private void assertFailed(CaptivePortalProbeResult result) { + assertFalse(result.isPortal()); + assertTrue(result.isFailed()); + assertFalse(result.isSuccessful()); + } + + private void setSslException(HttpURLConnection connection) throws IOException { + when(connection.getResponseCode()).thenThrow(new SSLHandshakeException("Invalid cert")); + } + + private void set302(HttpURLConnection connection, String location) throws IOException { + setStatus(connection, 302); + when(connection.getHeaderField(LOCATION_HEADER)).thenReturn(location); + } + + private void setPortal302(HttpURLConnection connection) throws IOException { + set302(connection, "http://login.example.com"); + } + + private void setStatus(HttpURLConnection connection, int status) throws IOException { + when(connection.getResponseCode()).thenReturn(status); } } diff --git a/tests/permission/src/com/android/framework/permission/tests/VibratorServicePermissionTest.java b/tests/permission/src/com/android/framework/permission/tests/VibratorServicePermissionTest.java index 2757296f588f..388c7d03dff2 100644 --- a/tests/permission/src/com/android/framework/permission/tests/VibratorServicePermissionTest.java +++ b/tests/permission/src/com/android/framework/permission/tests/VibratorServicePermissionTest.java @@ -52,7 +52,7 @@ public class VibratorServicePermissionTest extends TestCase { final VibrationEffect effect = VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE); mVibratorService.vibrate(Process.myUid(), null, effect, AudioManager.STREAM_ALARM, - new Binder()); + "testVibrate", new Binder()); fail("vibrate did not throw SecurityException as expected"); } catch (SecurityException e) { // expected |