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
Change-Id: Id01e6135193716cc21bba11da529bf1507a954f7
This commit is contained in:
Aaron Huang
2020-03-18 19:24:31 +08:00
parent f3777c09f5
commit 410236b92e
5 changed files with 66 additions and 32 deletions

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

@@ -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);