API review: access field by method

- InvalidPacketException, public field should be a method so
  add getter to get error code.
- KeepalivePacketData, public fields should be methods so
   add getter for fields.

Bug: 151322799
Test: atest FrameworksNetTests
      atest FrameworksWifiTests
      atest FrameworksTelephonyTests: some failure in CarrierAppUtilsTest
Copy from ag/10731108
Change-Id: Id01e6135193716cc21bba11da529bf1507a954f7
Merged-In: Id01e6135193716cc21bba11da529bf1507a954f7
This commit is contained in:
Aaron Huang 2020-03-18 19:24:31 +08:00
parent 6982a7dc69
commit 8e1ce70353
8 changed files with 101 additions and 60 deletions

View File

@ -4405,10 +4405,10 @@ package android.net {
public class InvalidPacketException extends java.lang.Exception {
ctor public InvalidPacketException(int);
method public int getError();
field public static final int ERROR_INVALID_IP_ADDRESS = -21; // 0xffffffeb
field public static final int ERROR_INVALID_LENGTH = -23; // 0xffffffe9
field public static final int ERROR_INVALID_PORT = -22; // 0xffffffea
field public final int error;
}
public final class IpConfiguration implements android.os.Parcelable {
@ -4462,12 +4462,12 @@ package android.net {
}
public class KeepalivePacketData {
ctor protected KeepalivePacketData(@NonNull java.net.InetAddress, int, @NonNull java.net.InetAddress, int, @NonNull byte[]) throws android.net.InvalidPacketException;
ctor protected KeepalivePacketData(@NonNull java.net.InetAddress, @IntRange(from=0, to=65535) int, @NonNull java.net.InetAddress, @IntRange(from=0, to=65535) int, @NonNull byte[]) throws android.net.InvalidPacketException;
method @NonNull public java.net.InetAddress getDstAddress();
method public int getDstPort();
method @NonNull public byte[] getPacket();
field @NonNull public final java.net.InetAddress dstAddress;
field public final int dstPort;
field @NonNull public final java.net.InetAddress srcAddress;
field public final int srcPort;
method @NonNull public java.net.InetAddress getSrcAddress();
method public int getSrcPort();
}
public class LinkAddress implements android.os.Parcelable {

View File

@ -28,7 +28,7 @@ import java.lang.annotation.RetentionPolicy;
*/
@SystemApi
public class InvalidPacketException extends Exception {
public final int error;
private final int mError;
// Must match SocketKeepalive#ERROR_INVALID_IP_ADDRESS.
/** Invalid IP address. */
@ -56,6 +56,11 @@ public class InvalidPacketException extends Exception {
* See the error code for details.
*/
public InvalidPacketException(@ErrorCode final int error) {
this.error = error;
this.mError = error;
}
/** Get error code. */
public int getError() {
return mError;
}
}

View File

@ -19,6 +19,7 @@ package android.net;
import static android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS;
import static android.net.InvalidPacketException.ERROR_INVALID_PORT;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.net.util.IpUtils;
@ -37,17 +38,17 @@ public class KeepalivePacketData {
/** Source IP address */
@NonNull
public final InetAddress srcAddress;
private final InetAddress mSrcAddress;
/** Destination IP address */
@NonNull
public final InetAddress dstAddress;
private final InetAddress mDstAddress;
/** Source port */
public final int srcPort;
private final int mSrcPort;
/** Destination port */
public final int dstPort;
private final int mDstPort;
/** Packet data. A raw byte string of packet data, not including the link-layer header. */
private final byte[] mPacket;
@ -60,13 +61,14 @@ public class KeepalivePacketData {
/**
* A holding class for data necessary to build a keepalive packet.
*/
protected KeepalivePacketData(@NonNull InetAddress srcAddress, int srcPort,
@NonNull InetAddress dstAddress, int dstPort,
@NonNull byte[] data) throws InvalidPacketException {
this.srcAddress = srcAddress;
this.dstAddress = dstAddress;
this.srcPort = srcPort;
this.dstPort = dstPort;
protected KeepalivePacketData(@NonNull InetAddress srcAddress,
@IntRange(from = 0, to = 65535) int srcPort, @NonNull InetAddress dstAddress,
@IntRange(from = 0, to = 65535) int dstPort,
@NonNull byte[] data) throws InvalidPacketException {
this.mSrcAddress = srcAddress;
this.mDstAddress = dstAddress;
this.mSrcPort = srcPort;
this.mDstPort = dstPort;
this.mPacket = data;
// Check we have two IP addresses of the same family.
@ -83,6 +85,31 @@ public class KeepalivePacketData {
}
}
/** Get source IP address. */
@NonNull
public InetAddress getSrcAddress() {
return mSrcAddress;
}
/** Get destination IP address. */
@NonNull
public InetAddress getDstAddress() {
return mDstAddress;
}
/** Get source port number. */
public int getSrcPort() {
return mSrcPort;
}
/** Get destination port number. */
public int getDstPort() {
return mDstPort;
}
/**
* Returns a byte array of the given packet data.
*/
@NonNull
public byte[] getPacket() {
return mPacket.clone();

View File

@ -94,10 +94,10 @@ public final class NattKeepalivePacketData extends KeepalivePacketData implement
/** Write to parcel */
public void writeToParcel(@NonNull Parcel out, int flags) {
out.writeString(srcAddress.getHostAddress());
out.writeString(dstAddress.getHostAddress());
out.writeInt(srcPort);
out.writeInt(dstPort);
out.writeString(getSrcAddress().getHostAddress());
out.writeString(getDstAddress().getHostAddress());
out.writeInt(getSrcPort());
out.writeInt(getDstPort());
}
/** Parcelable Creator */
@ -115,7 +115,7 @@ public final class NattKeepalivePacketData extends KeepalivePacketData implement
dstAddress, dstPort);
} catch (InvalidPacketException e) {
throw new IllegalArgumentException(
"Invalid NAT-T keepalive data: " + e.error);
"Invalid NAT-T keepalive data: " + e.getError());
}
}
@ -128,14 +128,16 @@ public final class NattKeepalivePacketData extends KeepalivePacketData implement
public boolean equals(@Nullable final Object o) {
if (!(o instanceof NattKeepalivePacketData)) return false;
final NattKeepalivePacketData other = (NattKeepalivePacketData) o;
return this.srcAddress.equals(other.srcAddress)
&& this.dstAddress.equals(other.dstAddress)
&& this.srcPort == other.srcPort
&& this.dstPort == other.dstPort;
final InetAddress srcAddress = getSrcAddress();
final InetAddress dstAddress = getDstAddress();
return srcAddress.equals(other.getSrcAddress())
&& dstAddress.equals(other.getDstAddress())
&& getSrcPort() == other.getSrcPort()
&& getDstPort() == other.getDstPort();
}
@Override
public int hashCode() {
return Objects.hash(srcAddress, dstAddress, srcPort, dstPort);
return Objects.hash(getSrcAddress(), getDstAddress(), getSrcPort(), getDstPort());
}
}

View File

@ -220,9 +220,9 @@ public class KeepaliveTracker {
+ " network=" + mNai.network
+ " startedState=" + startedStateString(mStartedState)
+ " "
+ IpUtils.addressAndPortToString(mPacket.srcAddress, mPacket.srcPort)
+ IpUtils.addressAndPortToString(mPacket.getSrcAddress(), mPacket.getSrcPort())
+ "->"
+ IpUtils.addressAndPortToString(mPacket.dstAddress, mPacket.dstPort)
+ IpUtils.addressAndPortToString(mPacket.getDstAddress(), mPacket.getDstPort())
+ " interval=" + mInterval
+ " uid=" + mUid + " pid=" + mPid + " privileged=" + mPrivileged
+ " packetData=" + HexDump.toHexString(mPacket.getPacket())
@ -250,7 +250,7 @@ public class KeepaliveTracker {
private int checkSourceAddress() {
// Check that we have the source address.
for (InetAddress address : mNai.linkProperties.getAddresses()) {
if (address.equals(mPacket.srcAddress)) {
if (address.equals(mPacket.getSrcAddress())) {
return SUCCESS;
}
}
@ -619,7 +619,7 @@ public class KeepaliveTracker {
packet = NattKeepalivePacketData.nattKeepalivePacket(
srcAddress, srcPort, dstAddress, NATT_PORT);
} catch (InvalidPacketException e) {
notifyErrorCallback(cb, e.error);
notifyErrorCallback(cb, e.getError());
return;
}
KeepaliveInfo ki = null;
@ -662,7 +662,7 @@ public class KeepaliveTracker {
notifyErrorCallback(cb, e.error);
return;
} catch (InvalidPacketException e) {
notifyErrorCallback(cb, e.error);
notifyErrorCallback(cb, e.getError());
return;
}
KeepaliveInfo ki = null;

View File

@ -152,10 +152,12 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
public boolean equals(@Nullable final Object o) {
if (!(o instanceof TcpKeepalivePacketData)) return false;
final TcpKeepalivePacketData other = (TcpKeepalivePacketData) o;
return this.srcAddress.equals(other.srcAddress)
&& this.dstAddress.equals(other.dstAddress)
&& this.srcPort == other.srcPort
&& this.dstPort == other.dstPort
final InetAddress srcAddress = getSrcAddress();
final InetAddress dstAddress = getDstAddress();
return srcAddress.equals(other.getSrcAddress())
&& dstAddress.equals(other.getDstAddress())
&& getSrcPort() == other.getSrcPort()
&& getDstPort() == other.getDstPort()
&& this.tcpAck == other.tcpAck
&& this.tcpSeq == other.tcpSeq
&& this.tcpWnd == other.tcpWnd
@ -166,8 +168,8 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
@Override
public int hashCode() {
return Objects.hash(srcAddress, dstAddress, srcPort, dstPort, tcpAck, tcpSeq, tcpWnd,
tcpWndScale, ipTos, ipTtl);
return Objects.hash(getSrcAddress(), getDstAddress(), getSrcPort(), getDstPort(),
tcpAck, tcpSeq, tcpWnd, tcpWndScale, ipTos, ipTtl);
}
/**
@ -182,10 +184,10 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
/** Write to parcel. */
public void writeToParcel(Parcel out, int flags) {
out.writeString(srcAddress.getHostAddress());
out.writeString(dstAddress.getHostAddress());
out.writeInt(srcPort);
out.writeInt(dstPort);
out.writeString(getSrcAddress().getHostAddress());
out.writeString(getDstAddress().getHostAddress());
out.writeInt(getSrcPort());
out.writeInt(getDstPort());
out.writeByteArray(getPacket());
out.writeInt(tcpSeq);
out.writeInt(tcpAck);
@ -219,7 +221,7 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
return readFromParcel(in);
} catch (InvalidPacketException e) {
throw new IllegalArgumentException(
"Invalid NAT-T keepalive data: " + e.error);
"Invalid NAT-T keepalive data: " + e.getError());
}
}
@ -234,10 +236,12 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
@NonNull
public TcpKeepalivePacketDataParcelable toStableParcelable() {
final TcpKeepalivePacketDataParcelable parcel = new TcpKeepalivePacketDataParcelable();
final InetAddress srcAddress = getSrcAddress();
final InetAddress dstAddress = getDstAddress();
parcel.srcAddress = srcAddress.getAddress();
parcel.srcPort = srcPort;
parcel.srcPort = getSrcPort();
parcel.dstAddress = dstAddress.getAddress();
parcel.dstPort = dstPort;
parcel.dstPort = getDstPort();
parcel.seq = tcpSeq;
parcel.ack = tcpAck;
parcel.rcvWnd = tcpWnd;
@ -249,10 +253,10 @@ public class TcpKeepalivePacketData extends KeepalivePacketData implements Parce
@Override
public String toString() {
return "saddr: " + srcAddress
+ " daddr: " + dstAddress
+ " sport: " + srcPort
+ " dport: " + dstPort
return "saddr: " + getSrcAddress()
+ " daddr: " + getDstAddress()
+ " sport: " + getSrcPort()
+ " dport: " + getDstPort()
+ " seq: " + tcpSeq
+ " ack: " + tcpAck
+ " wnd: " + tcpWnd

View File

@ -20,6 +20,8 @@ import android.annotation.NonNull;
import android.net.NattKeepalivePacketData;
import android.net.NattKeepalivePacketDataParcelable;
import java.net.InetAddress;
/** @hide */
public final class KeepalivePacketDataUtil {
/**
@ -29,11 +31,12 @@ public final class KeepalivePacketDataUtil {
public static NattKeepalivePacketDataParcelable toStableParcelable(
NattKeepalivePacketData pkt) {
final NattKeepalivePacketDataParcelable parcel = new NattKeepalivePacketDataParcelable();
parcel.srcAddress = pkt.srcAddress.getAddress();
parcel.srcPort = pkt.srcPort;
parcel.dstAddress = pkt.dstAddress.getAddress();
parcel.dstPort = pkt.dstPort;
final InetAddress srcAddress = pkt.getSrcAddress();
final InetAddress dstAddress = pkt.getDstAddress();
parcel.srcAddress = srcAddress.getAddress();
parcel.srcPort = pkt.getSrcPort();
parcel.dstAddress = dstAddress.getAddress();
parcel.dstPort = pkt.getDstPort();
return parcel;
}
}

View File

@ -66,10 +66,10 @@ public final class TcpKeepalivePacketDataTest {
fail("InvalidPacketException: " + e);
}
assertEquals(InetAddress.getByAddress(testInfo.srcAddress), resultData.srcAddress);
assertEquals(InetAddress.getByAddress(testInfo.dstAddress), resultData.dstAddress);
assertEquals(testInfo.srcPort, resultData.srcPort);
assertEquals(testInfo.dstPort, resultData.dstPort);
assertEquals(InetAddress.getByAddress(testInfo.srcAddress), resultData.getSrcAddress());
assertEquals(InetAddress.getByAddress(testInfo.dstAddress), resultData.getDstAddress());
assertEquals(testInfo.srcPort, resultData.getSrcPort());
assertEquals(testInfo.dstPort, resultData.getDstPort());
assertEquals(testInfo.seq, resultData.tcpSeq);
assertEquals(testInfo.ack, resultData.tcpAck);
assertEquals(testInfo.rcvWnd, resultData.tcpWnd);