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