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