markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 android.net; |
| 18 | |
| 19 | import static org.junit.Assert.assertArrayEquals; |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.fail; |
| 22 | |
| 23 | import android.net.SocketKeepalive.InvalidPacketException; |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 24 | |
| 25 | import com.android.internal.util.TestUtils; |
| 26 | |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.JUnit4; |
| 31 | |
| 32 | import java.net.InetAddress; |
| 33 | import java.nio.ByteBuffer; |
| 34 | |
| 35 | @RunWith(JUnit4.class) |
| 36 | public final class TcpKeepalivePacketDataTest { |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 37 | private static final byte[] IPV4_KEEPALIVE_SRC_ADDR = {10, 0, 0, 1}; |
| 38 | private static final byte[] IPV4_KEEPALIVE_DST_ADDR = {10, 0, 0, 5}; |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 39 | |
| 40 | @Before |
| 41 | public void setUp() {} |
| 42 | |
| 43 | @Test |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 44 | public void testV4TcpKeepalivePacket() throws Exception { |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 45 | final int srcPort = 1234; |
| 46 | final int dstPort = 4321; |
| 47 | final int seq = 0x11111111; |
| 48 | final int ack = 0x22222222; |
| 49 | final int wnd = 8000; |
| 50 | final int wndScale = 2; |
| 51 | TcpKeepalivePacketData resultData = null; |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 52 | final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable(); |
| 53 | testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR; |
| 54 | testInfo.srcPort = srcPort; |
| 55 | testInfo.dstAddress = IPV4_KEEPALIVE_DST_ADDR; |
| 56 | testInfo.dstPort = dstPort; |
| 57 | testInfo.seq = seq; |
| 58 | testInfo.ack = ack; |
| 59 | testInfo.rcvWnd = wnd; |
| 60 | testInfo.rcvWndScale = wndScale; |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 61 | try { |
| 62 | resultData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo); |
| 63 | } catch (InvalidPacketException e) { |
| 64 | fail("InvalidPacketException: " + e); |
| 65 | } |
| 66 | |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 67 | assertEquals(InetAddress.getByAddress(testInfo.srcAddress), resultData.srcAddress); |
| 68 | assertEquals(InetAddress.getByAddress(testInfo.dstAddress), resultData.dstAddress); |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 69 | assertEquals(testInfo.srcPort, resultData.srcPort); |
| 70 | assertEquals(testInfo.dstPort, resultData.dstPort); |
| 71 | assertEquals(testInfo.seq, resultData.tcpSeq); |
| 72 | assertEquals(testInfo.ack, resultData.tcpAck); |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 73 | assertEquals(testInfo.rcvWnd, resultData.tcpWnd); |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 74 | assertEquals(testInfo.rcvWndScale, resultData.tcpWndScale); |
| 75 | |
| 76 | TestUtils.assertParcelingIsLossless(resultData, TcpKeepalivePacketData.CREATOR); |
| 77 | |
| 78 | final byte[] packet = resultData.getPacket(); |
| 79 | // IP version and TOS. |
| 80 | ByteBuffer buf = ByteBuffer.wrap(packet); |
| 81 | assertEquals(buf.getShort(), 0x4500); |
| 82 | // Source IP address. |
| 83 | byte[] ip = new byte[4]; |
| 84 | buf = ByteBuffer.wrap(packet, 12, 4); |
| 85 | buf.get(ip); |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 86 | assertArrayEquals(ip, IPV4_KEEPALIVE_SRC_ADDR); |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 87 | // Destination IP address. |
| 88 | buf = ByteBuffer.wrap(packet, 16, 4); |
| 89 | buf.get(ip); |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 90 | assertArrayEquals(ip, IPV4_KEEPALIVE_DST_ADDR); |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 91 | |
| 92 | buf = ByteBuffer.wrap(packet, 20, 12); |
| 93 | // Source port. |
| 94 | assertEquals(buf.getShort(), srcPort); |
| 95 | // Destination port. |
| 96 | assertEquals(buf.getShort(), dstPort); |
| 97 | // Sequence number. |
| 98 | assertEquals(buf.getInt(), seq); |
| 99 | // Ack. |
| 100 | assertEquals(buf.getInt(), ack); |
| 101 | // Window size. |
| 102 | buf = ByteBuffer.wrap(packet, 34, 2); |
| 103 | assertEquals(buf.getShort(), wnd >> wndScale); |
| 104 | } |
| 105 | |
| 106 | //TODO: add ipv6 test when ipv6 supported |
junyulai | 8c1586e | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 107 | |
| 108 | @Test |
| 109 | public void testParcel() throws Exception { |
junyulai | 8c1586e | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 110 | final int srcPort = 1234; |
| 111 | final int dstPort = 4321; |
| 112 | final int sequence = 0x11111111; |
| 113 | final int ack = 0x22222222; |
| 114 | final int wnd = 48_000; |
| 115 | final int wndScale = 2; |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 116 | final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable(); |
| 117 | testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR; |
| 118 | testInfo.srcPort = srcPort; |
| 119 | testInfo.dstAddress = IPV4_KEEPALIVE_DST_ADDR; |
| 120 | testInfo.dstPort = dstPort; |
| 121 | testInfo.seq = sequence; |
| 122 | testInfo.ack = ack; |
| 123 | testInfo.rcvWnd = wnd; |
| 124 | testInfo.rcvWndScale = wndScale; |
junyulai | 8c1586e | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 125 | TcpKeepalivePacketData testData = null; |
| 126 | TcpKeepalivePacketDataParcelable resultData = null; |
junyulai | 8c1586e | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 127 | testData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo); |
| 128 | resultData = testData.toStableParcelable(); |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 129 | assertArrayEquals(resultData.srcAddress, IPV4_KEEPALIVE_SRC_ADDR); |
| 130 | assertArrayEquals(resultData.dstAddress, IPV4_KEEPALIVE_DST_ADDR); |
junyulai | 8c1586e | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 131 | assertEquals(resultData.srcPort, srcPort); |
| 132 | assertEquals(resultData.dstPort, dstPort); |
| 133 | assertEquals(resultData.seq, sequence); |
| 134 | assertEquals(resultData.ack, ack); |
markchien | 0fcb078 | 2019-03-19 21:25:33 +0800 | [diff] [blame^] | 135 | assertEquals(resultData.rcvWnd, wnd); |
| 136 | assertEquals(resultData.rcvWndScale, wndScale); |
junyulai | 8c1586e | 2019-01-30 19:11:45 +0800 | [diff] [blame] | 137 | } |
markchien | 46f41d4 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 138 | } |