Loading src/com/android/networkstack/netlink/TcpInfo.java +4 −0 Original line number Original line Diff line number Diff line Loading @@ -112,6 +112,10 @@ public class TcpInfo { } } } } mFieldsValues = Collections.unmodifiableMap(fields); mFieldsValues = Collections.unmodifiableMap(fields); // tcp_info structure grows over time as new fields are added. Jump to the end of the // structure, as unknown fields might remain at the end of the structure if the tcp_info // struct was expanded. bytes.position(Math.min(infolen + start, bytes.limit())); } } @VisibleForTesting @VisibleForTesting Loading tests/unit/src/com/android/networkstack/netlink/TcpInfoTest.java +24 −0 Original line number Original line Diff line number Diff line Loading @@ -90,6 +90,13 @@ public class TcpInfoTest { private static final byte[] TCP_INFO_BYTES = private static final byte[] TCP_INFO_BYTES = HexEncoding.decode(TCP_INFO_HEX.toCharArray(), false); HexEncoding.decode(TCP_INFO_HEX.toCharArray(), false); private static final String EXPANDED_TCP_INFO_HEX = TCP_INFO_HEX + "00000000" // tcpi_delivered + "00000000"; // tcpi_delivered_ce private static final byte[] EXPANDED_TCP_INFO_BYTES = HexEncoding.decode(EXPANDED_TCP_INFO_HEX.toCharArray(), false); private static final int EXPANDED_TCP_INFO_LENGTH = EXPANDED_TCP_INFO_BYTES.length - TCP_INFO_BYTES.length; @Test @Test public void testParseTcpInfo() { public void testParseTcpInfo() { final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); Loading @@ -99,6 +106,23 @@ public class TcpInfoTest { assertEquals(parsedInfo, new TcpInfo(expected)); assertEquals(parsedInfo, new TcpInfo(expected)); } } @Test public void testParseTcpInfoExpanded() { final ByteBuffer buffer = ByteBuffer.wrap(EXPANDED_TCP_INFO_BYTES); final Map<TcpInfo.Field, Number> expected = makeTestTcpInfoHash(); final TcpInfo parsedInfo = TcpInfo.parse(buffer, TCP_INFO_LENGTH_V1 + EXPANDED_TCP_INFO_LENGTH); assertEquals(parsedInfo, new TcpInfo(expected)); assertEquals(buffer.limit(), buffer.position()); // reset the index. buffer.position(0); final TcpInfo parsedInfoShorterLen = TcpInfo.parse(buffer, TCP_INFO_LENGTH_V1); assertEquals(parsedInfoShorterLen, new TcpInfo(expected)); assertEquals(TCP_INFO_LENGTH_V1, buffer.position()); } @Test @Test public void testValidOffset() { public void testValidOffset() { final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); Loading Loading
src/com/android/networkstack/netlink/TcpInfo.java +4 −0 Original line number Original line Diff line number Diff line Loading @@ -112,6 +112,10 @@ public class TcpInfo { } } } } mFieldsValues = Collections.unmodifiableMap(fields); mFieldsValues = Collections.unmodifiableMap(fields); // tcp_info structure grows over time as new fields are added. Jump to the end of the // structure, as unknown fields might remain at the end of the structure if the tcp_info // struct was expanded. bytes.position(Math.min(infolen + start, bytes.limit())); } } @VisibleForTesting @VisibleForTesting Loading
tests/unit/src/com/android/networkstack/netlink/TcpInfoTest.java +24 −0 Original line number Original line Diff line number Diff line Loading @@ -90,6 +90,13 @@ public class TcpInfoTest { private static final byte[] TCP_INFO_BYTES = private static final byte[] TCP_INFO_BYTES = HexEncoding.decode(TCP_INFO_HEX.toCharArray(), false); HexEncoding.decode(TCP_INFO_HEX.toCharArray(), false); private static final String EXPANDED_TCP_INFO_HEX = TCP_INFO_HEX + "00000000" // tcpi_delivered + "00000000"; // tcpi_delivered_ce private static final byte[] EXPANDED_TCP_INFO_BYTES = HexEncoding.decode(EXPANDED_TCP_INFO_HEX.toCharArray(), false); private static final int EXPANDED_TCP_INFO_LENGTH = EXPANDED_TCP_INFO_BYTES.length - TCP_INFO_BYTES.length; @Test @Test public void testParseTcpInfo() { public void testParseTcpInfo() { final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); Loading @@ -99,6 +106,23 @@ public class TcpInfoTest { assertEquals(parsedInfo, new TcpInfo(expected)); assertEquals(parsedInfo, new TcpInfo(expected)); } } @Test public void testParseTcpInfoExpanded() { final ByteBuffer buffer = ByteBuffer.wrap(EXPANDED_TCP_INFO_BYTES); final Map<TcpInfo.Field, Number> expected = makeTestTcpInfoHash(); final TcpInfo parsedInfo = TcpInfo.parse(buffer, TCP_INFO_LENGTH_V1 + EXPANDED_TCP_INFO_LENGTH); assertEquals(parsedInfo, new TcpInfo(expected)); assertEquals(buffer.limit(), buffer.position()); // reset the index. buffer.position(0); final TcpInfo parsedInfoShorterLen = TcpInfo.parse(buffer, TCP_INFO_LENGTH_V1); assertEquals(parsedInfoShorterLen, new TcpInfo(expected)); assertEquals(TCP_INFO_LENGTH_V1, buffer.position()); } @Test @Test public void testValidOffset() { public void testValidOffset() { final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); final ByteBuffer buffer = ByteBuffer.wrap(TCP_INFO_BYTES); Loading