summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorXiao Ma <xiaom@google.com>2022-05-25 22:03:54 +0900
committerCherrypicker Worker <android-build-cherrypicker-worker@google.com>2022-05-27 04:41:54 +0000
commit7cdfea03de8c910cfa4af27231f0c32f8b86c59e (patch)
treef8db4bbc19e00cc797454a1c87f849bed70d6af2 /common
parent5c6972977d507f8ab96aaafc0cdd9167d2790000 (diff)
downloadnet-7cdfea03de8c910cfa4af27231f0c32f8b86c59e.tar.gz
Add test to verify parsing multiple netlink messages in one buffer.
Currently the netlink parsing code doesn't advance to the end of message if it doesn't consume the entire message, that results in the remaining attributes will be considered as a new netlink message, parsing the attributes will fail and return null. Have a testcase to verify this case. Bug: 163492391 Test: atest NetworkStaticLibsTests Change-Id: I2719cd917f9932d5e77002e615845076784139a2 (cherry picked from commit a8b53fd42b5f198e77733b292473c4bbe5ea80d8) Merged-In: I2719cd917f9932d5e77002e615845076784139a2
Diffstat (limited to 'common')
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java55
1 files changed, 44 insertions, 11 deletions
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java
index 392314fb..93b9894b 100644
--- a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java
+++ b/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java
@@ -63,15 +63,7 @@ public class RtNetlinkRouteMessageTest {
return ByteBuffer.wrap(HexDump.hexStringToByteArray(hexString));
}
- @Test
- public void testParseRtmRouteAddress() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkRouteMessage);
- final RtNetlinkRouteMessage routeMsg = (RtNetlinkRouteMessage) msg;
-
+ private void assertRtmRouteMessage(final RtNetlinkRouteMessage routeMsg) {
final StructNlMsgHdr hdr = routeMsg.getHeader();
assertNotNull(hdr);
assertEquals(136, hdr.nlmsg_len);
@@ -97,6 +89,18 @@ public class RtNetlinkRouteMessageTest {
assertEquals((Inet6Address) routeMsg.getGateway(), TEST_IPV6_LINK_LOCAL_GATEWAY);
}
+ @Test
+ public void testParseRtmRouteMessage() {
+ final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_HEX);
+ byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
+
+ final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
+ assertNotNull(msg);
+ assertTrue(msg instanceof RtNetlinkRouteMessage);
+ final RtNetlinkRouteMessage routeMsg = (RtNetlinkRouteMessage) msg;
+ assertRtmRouteMessage(routeMsg);
+ }
+
private static final String RTM_NEWROUTE_PACK_HEX =
"4C000000180000060000000000000000" // struct nlmsghr
+ "0A400000FC02000100000000" // struct rtmsg
@@ -143,7 +147,7 @@ public class RtNetlinkRouteMessageTest {
+ "08000400DF020000"; // RTA_OIF
@Test
- public void testParseRtmRouteAddress_IPv4MappedIPv6Gateway() {
+ public void testParseRtmRouteMessage_IPv4MappedIPv6Gateway() {
final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_IPV4_MAPPED_IPV6_GATEWAY_HEX);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
@@ -160,7 +164,7 @@ public class RtNetlinkRouteMessageTest {
+ "08000400DF020000"; // RTA_OIF
@Test
- public void testParseRtmRouteAddress_IPv4MappedIPv6Destination() {
+ public void testParseRtmRouteMessage_IPv4MappedIPv6Destination() {
final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_IPV4_MAPPED_IPV6_DST_HEX);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
@@ -169,6 +173,35 @@ public class RtNetlinkRouteMessageTest {
assertNull(msg);
}
+ // An example of the full RTM_NEWADDR message.
+ private static final String RTM_NEWADDR_HEX =
+ "48000000140000000000000000000000" // struct nlmsghr
+ + "0A4080FD1E000000" // struct ifaddrmsg
+ + "14000100FE800000000000002C415CFFFE096665" // IFA_ADDRESS
+ + "14000600100E0000201C00002A70000045700000" // IFA_CACHEINFO
+ + "0800080080000000"; // IFA_FLAGS
+
+ @Test
+ public void testParseMultipleRtmMessagesInOneByteBuffer() {
+ final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_HEX + RTM_NEWADDR_HEX);
+ byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
+
+ // Try to parse the RTM_NEWROUTE message.
+ NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
+ assertNotNull(msg);
+ assertTrue(msg instanceof RtNetlinkRouteMessage);
+ final RtNetlinkRouteMessage routeMsg = (RtNetlinkRouteMessage) msg;
+ assertRtmRouteMessage(routeMsg);
+
+ // Try to parse the RTM_NEWADDR message.
+ msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
+ // The current parse code does not advance to the end of the message if the
+ // entire message wasn't consumed, then the remaining attributes in the
+ // RTM_NEWROUTE message will be considered as another new rtm netlink message,
+ // that will return null.
+ assertNull(msg);
+ }
+
@Test
public void testToString() {
final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_HEX);