markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 1 | /* |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 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 android.net; |
| 18 | |
Chalard Jean | 39175f2 | 2020-06-26 00:41:26 +0900 | [diff] [blame] | 19 | import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame] | 20 | |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 21 | import static org.junit.Assert.assertArrayEquals; |
| 22 | import static org.junit.Assert.assertEquals; |
Remi NGUYEN VAN | 0956b8a | 2020-11-10 13:11:06 +0900 | [diff] [blame] | 23 | import static org.junit.Assert.assertTrue; |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 24 | import static org.junit.Assert.fail; |
| 25 | |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 26 | import android.net.util.KeepalivePacketDataUtil; |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame^] | 27 | import android.os.Build; |
Remi NGUYEN VAN | b397b56 | 2021-06-11 08:52:51 +0900 | [diff] [blame] | 28 | import android.util.Log; |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 29 | |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame^] | 30 | import com.android.testutils.DevSdkIgnoreRule; |
| 31 | import com.android.testutils.DevSdkIgnoreRunner; |
| 32 | |
Remi NGUYEN VAN | b397b56 | 2021-06-11 08:52:51 +0900 | [diff] [blame] | 33 | import org.junit.After; |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 37 | |
| 38 | import java.net.InetAddress; |
| 39 | import java.nio.ByteBuffer; |
| 40 | |
Remi NGUYEN VAN | 154cf1d | 2021-06-29 17:16:28 +0900 | [diff] [blame^] | 41 | @RunWith(DevSdkIgnoreRunner.class) |
| 42 | @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 43 | public final class KeepalivePacketDataUtilTest { |
markchien | 458c95b | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 44 | private static final byte[] IPV4_KEEPALIVE_SRC_ADDR = {10, 0, 0, 1}; |
| 45 | private static final byte[] IPV4_KEEPALIVE_DST_ADDR = {10, 0, 0, 5}; |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 46 | |
Remi NGUYEN VAN | b397b56 | 2021-06-11 08:52:51 +0900 | [diff] [blame] | 47 | private Log.TerribleFailureHandler mOriginalHandler; |
| 48 | |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 49 | @Before |
Remi NGUYEN VAN | b397b56 | 2021-06-11 08:52:51 +0900 | [diff] [blame] | 50 | public void setUp() { |
| 51 | // Terrible failures are logged when using deprecated methods on newer platforms |
| 52 | mOriginalHandler = Log.setWtfHandler((tag, what, sys) -> |
| 53 | Log.e(tag, "Terrible failure in test", what)); |
| 54 | } |
| 55 | |
| 56 | @After |
| 57 | public void tearDown() { |
| 58 | Log.setWtfHandler(mOriginalHandler); |
| 59 | } |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 60 | |
| 61 | @Test |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 62 | public void testFromTcpKeepaliveStableParcelable() throws Exception { |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 63 | final int srcPort = 1234; |
| 64 | final int dstPort = 4321; |
| 65 | final int seq = 0x11111111; |
| 66 | final int ack = 0x22222222; |
| 67 | final int wnd = 8000; |
| 68 | final int wndScale = 2; |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 69 | final int tos = 4; |
| 70 | final int ttl = 64; |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 71 | TcpKeepalivePacketData resultData = null; |
markchien | 458c95b | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 72 | final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable(); |
| 73 | testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR; |
| 74 | testInfo.srcPort = srcPort; |
| 75 | testInfo.dstAddress = IPV4_KEEPALIVE_DST_ADDR; |
| 76 | testInfo.dstPort = dstPort; |
| 77 | testInfo.seq = seq; |
| 78 | testInfo.ack = ack; |
| 79 | testInfo.rcvWnd = wnd; |
| 80 | testInfo.rcvWndScale = wndScale; |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 81 | testInfo.tos = tos; |
| 82 | testInfo.ttl = ttl; |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 83 | try { |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 84 | resultData = KeepalivePacketDataUtil.fromStableParcelable(testInfo); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 85 | } catch (InvalidPacketException e) { |
| 86 | fail("InvalidPacketException: " + e); |
| 87 | } |
| 88 | |
Aaron Huang | 005f9d1 | 2020-03-18 19:24:31 +0800 | [diff] [blame] | 89 | assertEquals(InetAddress.getByAddress(testInfo.srcAddress), resultData.getSrcAddress()); |
| 90 | assertEquals(InetAddress.getByAddress(testInfo.dstAddress), resultData.getDstAddress()); |
| 91 | assertEquals(testInfo.srcPort, resultData.getSrcPort()); |
| 92 | assertEquals(testInfo.dstPort, resultData.getDstPort()); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 93 | assertEquals(testInfo.seq, resultData.tcpSeq); |
| 94 | assertEquals(testInfo.ack, resultData.tcpAck); |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 95 | assertEquals(testInfo.rcvWnd, resultData.tcpWindow); |
| 96 | assertEquals(testInfo.rcvWndScale, resultData.tcpWindowScale); |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 97 | assertEquals(testInfo.tos, resultData.ipTos); |
| 98 | assertEquals(testInfo.ttl, resultData.ipTtl); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 99 | |
Chalard Jean | af71836 | 2019-05-30 17:11:14 +0900 | [diff] [blame] | 100 | assertParcelingIsLossless(resultData); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 101 | |
| 102 | final byte[] packet = resultData.getPacket(); |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 103 | // IP version and IHL |
| 104 | assertEquals(packet[0], 0x45); |
| 105 | // TOS |
| 106 | assertEquals(packet[1], tos); |
| 107 | // TTL |
| 108 | assertEquals(packet[8], ttl); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 109 | // Source IP address. |
| 110 | byte[] ip = new byte[4]; |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 111 | ByteBuffer buf = ByteBuffer.wrap(packet, 12, 4); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 112 | buf.get(ip); |
markchien | 458c95b | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 113 | assertArrayEquals(ip, IPV4_KEEPALIVE_SRC_ADDR); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 114 | // Destination IP address. |
| 115 | buf = ByteBuffer.wrap(packet, 16, 4); |
| 116 | buf.get(ip); |
markchien | 458c95b | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 117 | assertArrayEquals(ip, IPV4_KEEPALIVE_DST_ADDR); |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 118 | |
| 119 | buf = ByteBuffer.wrap(packet, 20, 12); |
| 120 | // Source port. |
| 121 | assertEquals(buf.getShort(), srcPort); |
| 122 | // Destination port. |
| 123 | assertEquals(buf.getShort(), dstPort); |
| 124 | // Sequence number. |
| 125 | assertEquals(buf.getInt(), seq); |
| 126 | // Ack. |
| 127 | assertEquals(buf.getInt(), ack); |
| 128 | // Window size. |
| 129 | buf = ByteBuffer.wrap(packet, 34, 2); |
| 130 | assertEquals(buf.getShort(), wnd >> wndScale); |
| 131 | } |
| 132 | |
| 133 | //TODO: add ipv6 test when ipv6 supported |
junyulai | 80d0356 | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 134 | |
| 135 | @Test |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 136 | public void testToTcpKeepaliveStableParcelable() throws Exception { |
junyulai | 80d0356 | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 137 | final int srcPort = 1234; |
| 138 | final int dstPort = 4321; |
| 139 | final int sequence = 0x11111111; |
| 140 | final int ack = 0x22222222; |
| 141 | final int wnd = 48_000; |
| 142 | final int wndScale = 2; |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 143 | final int tos = 4; |
| 144 | final int ttl = 64; |
markchien | 458c95b | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 145 | final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable(); |
| 146 | testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR; |
| 147 | testInfo.srcPort = srcPort; |
| 148 | testInfo.dstAddress = IPV4_KEEPALIVE_DST_ADDR; |
| 149 | testInfo.dstPort = dstPort; |
| 150 | testInfo.seq = sequence; |
| 151 | testInfo.ack = ack; |
| 152 | testInfo.rcvWnd = wnd; |
| 153 | testInfo.rcvWndScale = wndScale; |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 154 | testInfo.tos = tos; |
| 155 | testInfo.ttl = ttl; |
junyulai | 80d0356 | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 156 | TcpKeepalivePacketData testData = null; |
| 157 | TcpKeepalivePacketDataParcelable resultData = null; |
Remi NGUYEN VAN | 5e6b51b | 2020-11-10 15:29:01 +0900 | [diff] [blame] | 158 | testData = KeepalivePacketDataUtil.fromStableParcelable(testInfo); |
| 159 | resultData = KeepalivePacketDataUtil.toStableParcelable(testData); |
markchien | 458c95b | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 160 | assertArrayEquals(resultData.srcAddress, IPV4_KEEPALIVE_SRC_ADDR); |
| 161 | assertArrayEquals(resultData.dstAddress, IPV4_KEEPALIVE_DST_ADDR); |
junyulai | 80d0356 | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 162 | assertEquals(resultData.srcPort, srcPort); |
| 163 | assertEquals(resultData.dstPort, dstPort); |
| 164 | assertEquals(resultData.seq, sequence); |
| 165 | assertEquals(resultData.ack, ack); |
markchien | 458c95b | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 166 | assertEquals(resultData.rcvWnd, wnd); |
| 167 | assertEquals(resultData.rcvWndScale, wndScale); |
markchien | b5a2b80 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 168 | assertEquals(resultData.tos, tos); |
| 169 | assertEquals(resultData.ttl, ttl); |
Lorenzo Colitti | 88987d4 | 2020-11-18 19:02:00 +0900 | [diff] [blame] | 170 | |
| 171 | final String expected = "" |
| 172 | + "android.net.TcpKeepalivePacketDataParcelable{srcAddress: [10, 0, 0, 1]," |
| 173 | + " srcPort: 1234, dstAddress: [10, 0, 0, 5], dstPort: 4321, seq: 286331153," |
| 174 | + " ack: 572662306, rcvWnd: 48000, rcvWndScale: 2, tos: 4, ttl: 64}"; |
| 175 | assertEquals(expected, resultData.toString()); |
junyulai | 80d0356 | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 176 | } |
Remi NGUYEN VAN | 0956b8a | 2020-11-10 13:11:06 +0900 | [diff] [blame] | 177 | |
| 178 | @Test |
| 179 | public void testParseTcpKeepalivePacketData() throws Exception { |
| 180 | final int srcPort = 1234; |
| 181 | final int dstPort = 4321; |
| 182 | final int sequence = 0x11111111; |
| 183 | final int ack = 0x22222222; |
| 184 | final int wnd = 4800; |
| 185 | final int wndScale = 2; |
| 186 | final int tos = 4; |
| 187 | final int ttl = 64; |
| 188 | final TcpKeepalivePacketDataParcelable testParcel = new TcpKeepalivePacketDataParcelable(); |
| 189 | testParcel.srcAddress = IPV4_KEEPALIVE_SRC_ADDR; |
| 190 | testParcel.srcPort = srcPort; |
| 191 | testParcel.dstAddress = IPV4_KEEPALIVE_DST_ADDR; |
| 192 | testParcel.dstPort = dstPort; |
| 193 | testParcel.seq = sequence; |
| 194 | testParcel.ack = ack; |
| 195 | testParcel.rcvWnd = wnd; |
| 196 | testParcel.rcvWndScale = wndScale; |
| 197 | testParcel.tos = tos; |
| 198 | testParcel.ttl = ttl; |
| 199 | |
| 200 | final KeepalivePacketData testData = |
| 201 | KeepalivePacketDataUtil.fromStableParcelable(testParcel); |
| 202 | final TcpKeepalivePacketDataParcelable parsedParcelable = |
| 203 | KeepalivePacketDataUtil.parseTcpKeepalivePacketData(testData); |
| 204 | final TcpKeepalivePacketData roundTripData = |
| 205 | KeepalivePacketDataUtil.fromStableParcelable(parsedParcelable); |
| 206 | |
| 207 | // Generated packet is the same, but rcvWnd / wndScale will differ if scale is non-zero |
| 208 | assertTrue(testData.getPacket().length > 0); |
| 209 | assertArrayEquals(testData.getPacket(), roundTripData.getPacket()); |
| 210 | |
| 211 | testParcel.rcvWndScale = 0; |
| 212 | final KeepalivePacketData noScaleTestData = |
| 213 | KeepalivePacketDataUtil.fromStableParcelable(testParcel); |
| 214 | final TcpKeepalivePacketDataParcelable noScaleParsedParcelable = |
| 215 | KeepalivePacketDataUtil.parseTcpKeepalivePacketData(noScaleTestData); |
| 216 | final TcpKeepalivePacketData noScaleRoundTripData = |
| 217 | KeepalivePacketDataUtil.fromStableParcelable(noScaleParsedParcelable); |
| 218 | assertEquals(noScaleTestData, noScaleRoundTripData); |
| 219 | assertTrue(noScaleTestData.getPacket().length > 0); |
| 220 | assertArrayEquals(noScaleTestData.getPacket(), noScaleRoundTripData.getPacket()); |
| 221 | } |
markchien | e5591ce | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 222 | } |