1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.net.module.util.netlink; 18 19 import static android.system.OsConstants.AF_INET; 20 import static android.system.OsConstants.AF_INET6; 21 22 import static org.junit.Assert.assertArrayEquals; 23 import static org.junit.Assert.assertEquals; 24 25 import android.net.InetAddresses; 26 27 import androidx.test.filters.SmallTest; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.net.Inet4Address; 34 import java.net.Inet6Address; 35 import java.net.InetSocketAddress; 36 import java.nio.ByteBuffer; 37 38 @RunWith(AndroidJUnit4.class) 39 @SmallTest 40 public class StructInetDiagSockIdTest { 41 private static final Inet4Address IPV4_SRC_ADDR = 42 (Inet4Address) InetAddresses.parseNumericAddress("192.0.2.1"); 43 private static final Inet4Address IPV4_DST_ADDR = 44 (Inet4Address) InetAddresses.parseNumericAddress("198.51.100.1"); 45 private static final Inet6Address IPV6_SRC_ADDR = 46 (Inet6Address) InetAddresses.parseNumericAddress("2001:db8::1"); 47 private static final Inet6Address IPV6_DST_ADDR = 48 (Inet6Address) InetAddresses.parseNumericAddress("2001:db8::2"); 49 private static final int SRC_PORT = 65297; 50 private static final int DST_PORT = 443; 51 private static final int IF_INDEX = 7; 52 private static final long COOKIE = 561; 53 54 private static final byte[] INET_DIAG_SOCKET_ID_IPV4 = 55 new byte[] { 56 // src port, dst port 57 (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb, 58 // src address 59 (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x01, 60 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 61 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 62 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 63 // dst address 64 (byte) 0xc6, (byte) 0x33, (byte) 0x64, (byte) 0x01, 65 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 66 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 67 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 68 // if index 69 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 70 // cookie 71 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 72 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff 73 }; 74 75 private static final byte[] INET_DIAG_SOCKET_ID_IPV4_IF_COOKIE = 76 new byte[] { 77 // src port, dst port 78 (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb, 79 // src address 80 (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x01, 81 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 82 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 83 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 84 // dst address 85 (byte) 0xc6, (byte) 0x33, (byte) 0x64, (byte) 0x01, 86 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 87 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 88 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 89 // if index 90 (byte) 0x07, (byte) 0x00, (byte) 0x00, (byte) 0x00, 91 // cookie 92 (byte) 0x31, (byte) 0x02, (byte) 0x00, (byte) 0x00, 93 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 94 }; 95 96 private static final byte[] INET_DIAG_SOCKET_ID_IPV6 = 97 new byte[] { 98 // src port, dst port 99 (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb, 100 // src address 101 (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 102 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 103 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 104 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, 105 // dst address 106 (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 107 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 108 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 109 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, 110 // if index 111 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 112 // cookie 113 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 114 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff 115 }; 116 117 private static final byte[] INET_DIAG_SOCKET_ID_IPV6_IF_COOKIE = 118 new byte[] { 119 // src port, dst port 120 (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb, 121 // src address 122 (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 123 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 124 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 125 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, 126 // dst address 127 (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 128 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 129 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 130 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, 131 // if index 132 (byte) 0x07, (byte) 0x00, (byte) 0x00, (byte) 0x00, 133 // cookie 134 (byte) 0x31, (byte) 0x02, (byte) 0x00, (byte) 0x00, 135 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 136 }; 137 138 @Test testPackStructInetDiagSockIdWithIpv4()139 public void testPackStructInetDiagSockIdWithIpv4() { 140 final InetSocketAddress srcAddr = new InetSocketAddress(IPV4_SRC_ADDR, SRC_PORT); 141 final InetSocketAddress dstAddr = new InetSocketAddress(IPV4_DST_ADDR, DST_PORT); 142 final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr); 143 final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE); 144 sockId.pack(buffer); 145 assertArrayEquals(INET_DIAG_SOCKET_ID_IPV4, buffer.array()); 146 } 147 148 @Test testPackStructInetDiagSockIdWithIpv6()149 public void testPackStructInetDiagSockIdWithIpv6() { 150 final InetSocketAddress srcAddr = new InetSocketAddress(IPV6_SRC_ADDR, SRC_PORT); 151 final InetSocketAddress dstAddr = new InetSocketAddress(IPV6_DST_ADDR, DST_PORT); 152 final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr); 153 final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE); 154 sockId.pack(buffer); 155 assertArrayEquals(INET_DIAG_SOCKET_ID_IPV6, buffer.array()); 156 } 157 158 @Test testPackStructInetDiagSockIdWithIpv4IfIndexCookie()159 public void testPackStructInetDiagSockIdWithIpv4IfIndexCookie() { 160 final InetSocketAddress srcAddr = new InetSocketAddress(IPV4_SRC_ADDR, SRC_PORT); 161 final InetSocketAddress dstAddr = new InetSocketAddress(IPV4_DST_ADDR, DST_PORT); 162 final StructInetDiagSockId sockId = 163 new StructInetDiagSockId(srcAddr, dstAddr, IF_INDEX, COOKIE); 164 final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE); 165 sockId.pack(buffer); 166 assertArrayEquals(INET_DIAG_SOCKET_ID_IPV4_IF_COOKIE, buffer.array()); 167 } 168 169 @Test testPackStructInetDiagSockIdWithIpv6IfIndexCookie()170 public void testPackStructInetDiagSockIdWithIpv6IfIndexCookie() { 171 final InetSocketAddress srcAddr = new InetSocketAddress(IPV6_SRC_ADDR, SRC_PORT); 172 final InetSocketAddress dstAddr = new InetSocketAddress(IPV6_DST_ADDR, DST_PORT); 173 final StructInetDiagSockId sockId = 174 new StructInetDiagSockId(srcAddr, dstAddr, IF_INDEX, COOKIE); 175 final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE); 176 sockId.pack(buffer); 177 assertArrayEquals(INET_DIAG_SOCKET_ID_IPV6_IF_COOKIE, buffer.array()); 178 } 179 180 @Test testParseStructInetDiagSockIdWithIpv4()181 public void testParseStructInetDiagSockIdWithIpv4() { 182 final ByteBuffer buffer = ByteBuffer.wrap(INET_DIAG_SOCKET_ID_IPV4_IF_COOKIE); 183 final StructInetDiagSockId sockId = StructInetDiagSockId.parse(buffer, (byte) AF_INET); 184 185 assertEquals(SRC_PORT, sockId.locSocketAddress.getPort()); 186 assertEquals(IPV4_SRC_ADDR, sockId.locSocketAddress.getAddress()); 187 assertEquals(DST_PORT, sockId.remSocketAddress.getPort()); 188 assertEquals(IPV4_DST_ADDR, sockId.remSocketAddress.getAddress()); 189 assertEquals(IF_INDEX, sockId.ifIndex); 190 assertEquals(COOKIE, sockId.cookie); 191 } 192 193 @Test testParseStructInetDiagSockIdWithIpv6()194 public void testParseStructInetDiagSockIdWithIpv6() { 195 final ByteBuffer buffer = ByteBuffer.wrap(INET_DIAG_SOCKET_ID_IPV6_IF_COOKIE); 196 final StructInetDiagSockId sockId = StructInetDiagSockId.parse(buffer, (byte) AF_INET6); 197 198 assertEquals(SRC_PORT, sockId.locSocketAddress.getPort()); 199 assertEquals(IPV6_SRC_ADDR, sockId.locSocketAddress.getAddress()); 200 assertEquals(DST_PORT, sockId.remSocketAddress.getPort()); 201 assertEquals(IPV6_DST_ADDR, sockId.remSocketAddress.getAddress()); 202 assertEquals(IF_INDEX, sockId.ifIndex); 203 assertEquals(COOKIE, sockId.cookie); 204 } 205 206 @Test testToStringStructInetDiagSockIdWithIpv4()207 public void testToStringStructInetDiagSockIdWithIpv4() { 208 final InetSocketAddress srcAddr = new InetSocketAddress(IPV4_SRC_ADDR, SRC_PORT); 209 final InetSocketAddress dstAddr = new InetSocketAddress(IPV4_DST_ADDR, DST_PORT); 210 final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr); 211 assertEquals("StructInetDiagSockId{ idiag_sport{65297}, idiag_dport{443}," 212 + " idiag_src{192.0.2.1}, idiag_dst{198.51.100.1}, idiag_if{0}," 213 + " idiag_cookie{INET_DIAG_NOCOOKIE}}", sockId.toString()); 214 } 215 216 @Test testToStringStructInetDiagSockIdWithIpv6()217 public void testToStringStructInetDiagSockIdWithIpv6() { 218 final InetSocketAddress srcAddr = new InetSocketAddress(IPV6_SRC_ADDR, SRC_PORT); 219 final InetSocketAddress dstAddr = new InetSocketAddress(IPV6_DST_ADDR, DST_PORT); 220 final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr); 221 assertEquals("StructInetDiagSockId{ idiag_sport{65297}, idiag_dport{443}," 222 + " idiag_src{2001:db8::1}, idiag_dst{2001:db8::2}, idiag_if{0}," 223 + " idiag_cookie{INET_DIAG_NOCOOKIE}}", sockId.toString()); 224 } 225 } 226