Search in sources :

Example 1 with PacketKeepalive

use of android.net.ConnectivityManager.PacketKeepalive in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testPacketKeepalives.

@SmallTest
public void testPacketKeepalives() throws Exception {
    InetAddress myIPv4 = InetAddress.getByName("192.0.2.129");
    InetAddress notMyIPv4 = InetAddress.getByName("192.0.2.35");
    InetAddress myIPv6 = InetAddress.getByName("2001:db8::1");
    InetAddress dstIPv4 = InetAddress.getByName("8.8.8.8");
    InetAddress dstIPv6 = InetAddress.getByName("2001:4860:4860::8888");
    LinkProperties lp = new LinkProperties();
    lp.setInterfaceName("wlan12");
    lp.addLinkAddress(new LinkAddress(myIPv6, 64));
    lp.addLinkAddress(new LinkAddress(myIPv4, 25));
    lp.addRoute(new RouteInfo(InetAddress.getByName("fe80::1234")));
    lp.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
    Network notMyNet = new Network(61234);
    Network myNet = connectKeepaliveNetwork(lp);
    TestKeepaliveCallback callback = new TestKeepaliveCallback();
    PacketKeepalive ka;
    // Attempt to start keepalives with invalid parameters and check for errors.
    ka = mCm.startNattKeepalive(notMyNet, 25, callback, myIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    ka = mCm.startNattKeepalive(myNet, 19, callback, notMyIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_INTERVAL);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 1234, dstIPv6);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv6);
    // NAT-T is IPv4-only.
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    // Check that a started keepalive can be stopped.
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.setStopKeepaliveError(PacketKeepalive.SUCCESS);
    ka.stop();
    callback.expectStopped();
    // Check that deleting the IP address stops the keepalive.
    LinkProperties bogusLp = new LinkProperties(lp);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    bogusLp.removeLinkAddress(new LinkAddress(myIPv4, 25));
    bogusLp.addLinkAddress(new LinkAddress(notMyIPv4, 25));
    mWiFiNetworkAgent.sendLinkProperties(bogusLp);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    mWiFiNetworkAgent.sendLinkProperties(lp);
    // Check that a started keepalive is stopped correctly when the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.disconnect();
    waitFor(mWiFiNetworkAgent.getDisconnectedCV());
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    // ... and that stopping it after that has no adverse effects.
    mService.waitForIdle();
    final Network myNetAlias = myNet;
    assertNull(mCm.getNetworkCapabilities(myNetAlias));
    ka.stop();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check things work as expected when the keepalive is stopped and the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    ka.stop();
    mWiFiNetworkAgent.disconnect();
    waitFor(mWiFiNetworkAgent.getDisconnectedCV());
    mService.waitForIdle();
    callback.expectStopped();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check that keepalive slots start from 1 and increment. The first one gets slot 1.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    // The second one gets slot 2.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(2);
    TestKeepaliveCallback callback2 = new TestKeepaliveCallback();
    PacketKeepalive ka2 = mCm.startNattKeepalive(myNet, 25, callback2, myIPv4, 6789, dstIPv4);
    callback2.expectStarted();
    // Now stop the first one and create a third. This also gets slot 1.
    ka.stop();
    callback.expectStopped();
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    TestKeepaliveCallback callback3 = new TestKeepaliveCallback();
    PacketKeepalive ka3 = mCm.startNattKeepalive(myNet, 25, callback3, myIPv4, 9876, dstIPv4);
    callback3.expectStarted();
    ka2.stop();
    callback2.expectStopped();
    ka3.stop();
    callback3.expectStopped();
}
Also used : LinkAddress(android.net.LinkAddress) Network(android.net.Network) PacketKeepalive(android.net.ConnectivityManager.PacketKeepalive) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress) LinkProperties(android.net.LinkProperties) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 2 with PacketKeepalive

use of android.net.ConnectivityManager.PacketKeepalive in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testPacketKeepalives.

public void testPacketKeepalives() throws Exception {
    InetAddress myIPv4 = InetAddress.getByName("192.0.2.129");
    InetAddress notMyIPv4 = InetAddress.getByName("192.0.2.35");
    InetAddress myIPv6 = InetAddress.getByName("2001:db8::1");
    InetAddress dstIPv4 = InetAddress.getByName("8.8.8.8");
    InetAddress dstIPv6 = InetAddress.getByName("2001:4860:4860::8888");
    LinkProperties lp = new LinkProperties();
    lp.setInterfaceName("wlan12");
    lp.addLinkAddress(new LinkAddress(myIPv6, 64));
    lp.addLinkAddress(new LinkAddress(myIPv4, 25));
    lp.addRoute(new RouteInfo(InetAddress.getByName("fe80::1234")));
    lp.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
    Network notMyNet = new Network(61234);
    Network myNet = connectKeepaliveNetwork(lp);
    TestKeepaliveCallback callback = new TestKeepaliveCallback();
    PacketKeepalive ka;
    // Attempt to start keepalives with invalid parameters and check for errors.
    ka = mCm.startNattKeepalive(notMyNet, 25, callback, myIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    ka = mCm.startNattKeepalive(myNet, 19, callback, notMyIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_INTERVAL);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 1234, dstIPv6);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv6);
    // NAT-T is IPv4-only.
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    // Check that a started keepalive can be stopped.
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.setStopKeepaliveError(PacketKeepalive.SUCCESS);
    ka.stop();
    callback.expectStopped();
    // Check that deleting the IP address stops the keepalive.
    LinkProperties bogusLp = new LinkProperties(lp);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    bogusLp.removeLinkAddress(new LinkAddress(myIPv4, 25));
    bogusLp.addLinkAddress(new LinkAddress(notMyIPv4, 25));
    mWiFiNetworkAgent.sendLinkProperties(bogusLp);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    mWiFiNetworkAgent.sendLinkProperties(lp);
    // Check that a started keepalive is stopped correctly when the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.disconnect();
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    // ... and that stopping it after that has no adverse effects.
    assertNull(mCm.getNetworkCapabilities(myNet));
    ka.stop();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check things work as expected when the keepalive is stopped and the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    ka.stop();
    mWiFiNetworkAgent.disconnect();
    mService.waitForIdle();
    callback.expectStopped();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check that keepalive slots start from 1 and increment. The first one gets slot 1.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    // The second one gets slot 2.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(2);
    TestKeepaliveCallback callback2 = new TestKeepaliveCallback();
    PacketKeepalive ka2 = mCm.startNattKeepalive(myNet, 25, callback2, myIPv4, 6789, dstIPv4);
    callback2.expectStarted();
    // Now stop the first one and create a third. This also gets slot 1.
    ka.stop();
    callback.expectStopped();
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    TestKeepaliveCallback callback3 = new TestKeepaliveCallback();
    PacketKeepalive ka3 = mCm.startNattKeepalive(myNet, 25, callback3, myIPv4, 9876, dstIPv4);
    callback3.expectStarted();
    ka2.stop();
    callback2.expectStopped();
    ka3.stop();
    callback3.expectStopped();
}
Also used : LinkAddress(android.net.LinkAddress) Network(android.net.Network) PacketKeepalive(android.net.ConnectivityManager.PacketKeepalive) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress) LinkProperties(android.net.LinkProperties)

Example 3 with PacketKeepalive

use of android.net.ConnectivityManager.PacketKeepalive in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testPacketKeepalives.

public void testPacketKeepalives() throws Exception {
    InetAddress myIPv4 = InetAddress.getByName("192.0.2.129");
    InetAddress notMyIPv4 = InetAddress.getByName("192.0.2.35");
    InetAddress myIPv6 = InetAddress.getByName("2001:db8::1");
    InetAddress dstIPv4 = InetAddress.getByName("8.8.8.8");
    InetAddress dstIPv6 = InetAddress.getByName("2001:4860:4860::8888");
    LinkProperties lp = new LinkProperties();
    lp.setInterfaceName("wlan12");
    lp.addLinkAddress(new LinkAddress(myIPv6, 64));
    lp.addLinkAddress(new LinkAddress(myIPv4, 25));
    lp.addRoute(new RouteInfo(InetAddress.getByName("fe80::1234")));
    lp.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
    Network notMyNet = new Network(61234);
    Network myNet = connectKeepaliveNetwork(lp);
    TestKeepaliveCallback callback = new TestKeepaliveCallback();
    PacketKeepalive ka;
    // Attempt to start keepalives with invalid parameters and check for errors.
    ka = mCm.startNattKeepalive(notMyNet, 25, callback, myIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    ka = mCm.startNattKeepalive(myNet, 19, callback, notMyIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_INTERVAL);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 1234, dstIPv6);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv6);
    // NAT-T is IPv4-only.
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    // Check that a started keepalive can be stopped.
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.setStopKeepaliveError(PacketKeepalive.SUCCESS);
    ka.stop();
    callback.expectStopped();
    // Check that deleting the IP address stops the keepalive.
    LinkProperties bogusLp = new LinkProperties(lp);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    bogusLp.removeLinkAddress(new LinkAddress(myIPv4, 25));
    bogusLp.addLinkAddress(new LinkAddress(notMyIPv4, 25));
    mWiFiNetworkAgent.sendLinkProperties(bogusLp);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    mWiFiNetworkAgent.sendLinkProperties(lp);
    // Check that a started keepalive is stopped correctly when the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.disconnect();
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    // ... and that stopping it after that has no adverse effects.
    assertNull(mCm.getNetworkCapabilities(myNet));
    ka.stop();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check things work as expected when the keepalive is stopped and the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    ka.stop();
    mWiFiNetworkAgent.disconnect();
    mService.waitForIdle();
    callback.expectStopped();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check that keepalive slots start from 1 and increment. The first one gets slot 1.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    // The second one gets slot 2.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(2);
    TestKeepaliveCallback callback2 = new TestKeepaliveCallback();
    PacketKeepalive ka2 = mCm.startNattKeepalive(myNet, 25, callback2, myIPv4, 6789, dstIPv4);
    callback2.expectStarted();
    // Now stop the first one and create a third. This also gets slot 1.
    ka.stop();
    callback.expectStopped();
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    TestKeepaliveCallback callback3 = new TestKeepaliveCallback();
    PacketKeepalive ka3 = mCm.startNattKeepalive(myNet, 25, callback3, myIPv4, 9876, dstIPv4);
    callback3.expectStarted();
    ka2.stop();
    callback2.expectStopped();
    ka3.stop();
    callback3.expectStopped();
}
Also used : LinkAddress(android.net.LinkAddress) Network(android.net.Network) PacketKeepalive(android.net.ConnectivityManager.PacketKeepalive) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress) LinkProperties(android.net.LinkProperties)

Example 4 with PacketKeepalive

use of android.net.ConnectivityManager.PacketKeepalive in project android_frameworks_base by crdroidandroid.

the class ConnectivityServiceTest method testPacketKeepalives.

public void testPacketKeepalives() throws Exception {
    InetAddress myIPv4 = InetAddress.getByName("192.0.2.129");
    InetAddress notMyIPv4 = InetAddress.getByName("192.0.2.35");
    InetAddress myIPv6 = InetAddress.getByName("2001:db8::1");
    InetAddress dstIPv4 = InetAddress.getByName("8.8.8.8");
    InetAddress dstIPv6 = InetAddress.getByName("2001:4860:4860::8888");
    LinkProperties lp = new LinkProperties();
    lp.setInterfaceName("wlan12");
    lp.addLinkAddress(new LinkAddress(myIPv6, 64));
    lp.addLinkAddress(new LinkAddress(myIPv4, 25));
    lp.addRoute(new RouteInfo(InetAddress.getByName("fe80::1234")));
    lp.addRoute(new RouteInfo(InetAddress.getByName("192.0.2.254")));
    Network notMyNet = new Network(61234);
    Network myNet = connectKeepaliveNetwork(lp);
    TestKeepaliveCallback callback = new TestKeepaliveCallback();
    PacketKeepalive ka;
    // Attempt to start keepalives with invalid parameters and check for errors.
    ka = mCm.startNattKeepalive(notMyNet, 25, callback, myIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    ka = mCm.startNattKeepalive(myNet, 19, callback, notMyIPv4, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_INTERVAL);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 1234, dstIPv6);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv6, 1234, dstIPv6);
    // NAT-T is IPv4-only.
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 123456, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_INVALID_PORT);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectError(PacketKeepalive.ERROR_HARDWARE_UNSUPPORTED);
    // Check that a started keepalive can be stopped.
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.setStopKeepaliveError(PacketKeepalive.SUCCESS);
    ka.stop();
    callback.expectStopped();
    // Check that deleting the IP address stops the keepalive.
    LinkProperties bogusLp = new LinkProperties(lp);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    bogusLp.removeLinkAddress(new LinkAddress(myIPv4, 25));
    bogusLp.addLinkAddress(new LinkAddress(notMyIPv4, 25));
    mWiFiNetworkAgent.sendLinkProperties(bogusLp);
    callback.expectError(PacketKeepalive.ERROR_INVALID_IP_ADDRESS);
    mWiFiNetworkAgent.sendLinkProperties(lp);
    // Check that a started keepalive is stopped correctly when the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    mWiFiNetworkAgent.disconnect();
    callback.expectError(PacketKeepalive.ERROR_INVALID_NETWORK);
    // ... and that stopping it after that has no adverse effects.
    assertNull(mCm.getNetworkCapabilities(myNet));
    ka.stop();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check things work as expected when the keepalive is stopped and the network disconnects.
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    ka.stop();
    mWiFiNetworkAgent.disconnect();
    mService.waitForIdle();
    callback.expectStopped();
    // Reconnect.
    myNet = connectKeepaliveNetwork(lp);
    mWiFiNetworkAgent.setStartKeepaliveError(PacketKeepalive.SUCCESS);
    // Check that keepalive slots start from 1 and increment. The first one gets slot 1.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    ka = mCm.startNattKeepalive(myNet, 25, callback, myIPv4, 12345, dstIPv4);
    callback.expectStarted();
    // The second one gets slot 2.
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(2);
    TestKeepaliveCallback callback2 = new TestKeepaliveCallback();
    PacketKeepalive ka2 = mCm.startNattKeepalive(myNet, 25, callback2, myIPv4, 6789, dstIPv4);
    callback2.expectStarted();
    // Now stop the first one and create a third. This also gets slot 1.
    ka.stop();
    callback.expectStopped();
    mWiFiNetworkAgent.setExpectedKeepaliveSlot(1);
    TestKeepaliveCallback callback3 = new TestKeepaliveCallback();
    PacketKeepalive ka3 = mCm.startNattKeepalive(myNet, 25, callback3, myIPv4, 9876, dstIPv4);
    callback3.expectStarted();
    ka2.stop();
    callback2.expectStopped();
    ka3.stop();
    callback3.expectStopped();
}
Also used : LinkAddress(android.net.LinkAddress) Network(android.net.Network) PacketKeepalive(android.net.ConnectivityManager.PacketKeepalive) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress) LinkProperties(android.net.LinkProperties)

Aggregations

PacketKeepalive (android.net.ConnectivityManager.PacketKeepalive)4 LinkAddress (android.net.LinkAddress)4 LinkProperties (android.net.LinkProperties)4 Network (android.net.Network)4 RouteInfo (android.net.RouteInfo)4 InetAddress (java.net.InetAddress)4 SmallTest (android.test.suitebuilder.annotation.SmallTest)1