Merge "[KA03.5] Add stable AIDL parcelable for TcpKeepalivePacketData"
This commit is contained in:
@@ -17,6 +17,7 @@ package android.net;
|
||||
|
||||
import static android.net.SocketKeepalive.ERROR_INVALID_IP_ADDRESS;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.net.SocketKeepalive.InvalidPacketException;
|
||||
import android.net.util.IpUtils;
|
||||
@@ -122,6 +123,7 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
|
||||
// TODO: add buildV6Packet.
|
||||
|
||||
/** Represents tcp/ip information. */
|
||||
// TODO: Replace TcpSocketInfo with TcpKeepalivePacketDataParcelable.
|
||||
public static class TcpSocketInfo {
|
||||
public final InetAddress srcAddress;
|
||||
public final InetAddress dstAddress;
|
||||
@@ -166,7 +168,10 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
|
||||
}
|
||||
|
||||
/* Parcelable Implementation. */
|
||||
/** No special parcel contents. */
|
||||
/* Note that this object implements parcelable (and needs to keep doing this as it inherits
|
||||
* from a class that does), but should usually be parceled as a stable parcelable using
|
||||
* the toStableParcelable() and fromStableParcelable() methods.
|
||||
*/
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
@@ -199,4 +204,31 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
|
||||
return new TcpKeepalivePacketData[size];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert this TcpKeepalivePacketData to a TcpKeepalivePacketDataParcelable.
|
||||
*/
|
||||
@NonNull
|
||||
public TcpKeepalivePacketDataParcelable toStableParcelable() {
|
||||
final TcpKeepalivePacketDataParcelable parcel = new TcpKeepalivePacketDataParcelable();
|
||||
parcel.srcAddress = srcAddress.getAddress();
|
||||
parcel.srcPort = srcPort;
|
||||
parcel.dstAddress = dstAddress.getAddress();
|
||||
parcel.dstPort = dstPort;
|
||||
parcel.seq = tcpSeq;
|
||||
parcel.ack = tcpAck;
|
||||
return parcel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "saddr: " + srcAddress
|
||||
+ " daddr: " + dstAddress
|
||||
+ " sport: " + srcPort
|
||||
+ " dport: " + dstPort
|
||||
+ " seq: " + tcpSeq
|
||||
+ " ack: " + tcpAck
|
||||
+ " wnd: " + tcpWnd
|
||||
+ " wndScale: " + tcpWndScale;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,4 +99,28 @@ public final class TcpKeepalivePacketDataTest {
|
||||
}
|
||||
|
||||
//TODO: add ipv6 test when ipv6 supported
|
||||
|
||||
@Test
|
||||
public void testParcel() throws Exception {
|
||||
final InetAddress srcAddr = InetAddresses.parseNumericAddress("192.168.0.1");
|
||||
final InetAddress dstAddr = InetAddresses.parseNumericAddress("192.168.0.10");
|
||||
final int srcPort = 1234;
|
||||
final int dstPort = 4321;
|
||||
final int sequence = 0x11111111;
|
||||
final int ack = 0x22222222;
|
||||
final int wnd = 48_000;
|
||||
final int wndScale = 2;
|
||||
TcpKeepalivePacketData testData = null;
|
||||
TcpKeepalivePacketDataParcelable resultData = null;
|
||||
TcpSocketInfo testInfo = new TcpSocketInfo(
|
||||
srcAddr, srcPort, dstAddr, dstPort, sequence, ack, wnd, wndScale);
|
||||
testData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
|
||||
resultData = testData.toStableParcelable();
|
||||
assertArrayEquals(resultData.srcAddress, srcAddr.getAddress());
|
||||
assertArrayEquals(resultData.dstAddress, dstAddr.getAddress());
|
||||
assertEquals(resultData.srcPort, srcPort);
|
||||
assertEquals(resultData.dstPort, dstPort);
|
||||
assertEquals(resultData.seq, sequence);
|
||||
assertEquals(resultData.ack, ack);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user