blob: 372ffcd057b4bb7258709d39e1f7b77b140b7f8e [file] [log] [blame]
markchien46f41d42018-12-27 22:49:51 +08001/*
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
17package android.net;
18
19import static org.junit.Assert.assertArrayEquals;
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.fail;
22
23import android.net.SocketKeepalive.InvalidPacketException;
markchien46f41d42018-12-27 22:49:51 +080024
25import com.android.internal.util.TestUtils;
26
markchien46f41d42018-12-27 22:49:51 +080027import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32import java.net.InetAddress;
33import java.nio.ByteBuffer;
34
35@RunWith(JUnit4.class)
36public final class TcpKeepalivePacketDataTest {
markchien0fcb0782019-03-19 21:25:33 +080037 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};
markchien46f41d42018-12-27 22:49:51 +080039
40 @Before
41 public void setUp() {}
42
43 @Test
markchien0fcb0782019-03-19 21:25:33 +080044 public void testV4TcpKeepalivePacket() throws Exception {
markchien46f41d42018-12-27 22:49:51 +080045 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;
markchien0fcb0782019-03-19 21:25:33 +080052 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;
markchien46f41d42018-12-27 22:49:51 +080061 try {
62 resultData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
63 } catch (InvalidPacketException e) {
64 fail("InvalidPacketException: " + e);
65 }
66
markchien0fcb0782019-03-19 21:25:33 +080067 assertEquals(InetAddress.getByAddress(testInfo.srcAddress), resultData.srcAddress);
68 assertEquals(InetAddress.getByAddress(testInfo.dstAddress), resultData.dstAddress);
markchien46f41d42018-12-27 22:49:51 +080069 assertEquals(testInfo.srcPort, resultData.srcPort);
70 assertEquals(testInfo.dstPort, resultData.dstPort);
71 assertEquals(testInfo.seq, resultData.tcpSeq);
72 assertEquals(testInfo.ack, resultData.tcpAck);
markchien0fcb0782019-03-19 21:25:33 +080073 assertEquals(testInfo.rcvWnd, resultData.tcpWnd);
markchien46f41d42018-12-27 22:49:51 +080074 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);
markchien0fcb0782019-03-19 21:25:33 +080086 assertArrayEquals(ip, IPV4_KEEPALIVE_SRC_ADDR);
markchien46f41d42018-12-27 22:49:51 +080087 // Destination IP address.
88 buf = ByteBuffer.wrap(packet, 16, 4);
89 buf.get(ip);
markchien0fcb0782019-03-19 21:25:33 +080090 assertArrayEquals(ip, IPV4_KEEPALIVE_DST_ADDR);
markchien46f41d42018-12-27 22:49:51 +080091
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
junyulai8c1586e2019-01-30 19:11:45 +0800107
108 @Test
109 public void testParcel() throws Exception {
junyulai8c1586e2019-01-30 19:11:45 +0800110 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;
markchien0fcb0782019-03-19 21:25:33 +0800116 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;
junyulai8c1586e2019-01-30 19:11:45 +0800125 TcpKeepalivePacketData testData = null;
126 TcpKeepalivePacketDataParcelable resultData = null;
junyulai8c1586e2019-01-30 19:11:45 +0800127 testData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
128 resultData = testData.toStableParcelable();
markchien0fcb0782019-03-19 21:25:33 +0800129 assertArrayEquals(resultData.srcAddress, IPV4_KEEPALIVE_SRC_ADDR);
130 assertArrayEquals(resultData.dstAddress, IPV4_KEEPALIVE_DST_ADDR);
junyulai8c1586e2019-01-30 19:11:45 +0800131 assertEquals(resultData.srcPort, srcPort);
132 assertEquals(resultData.dstPort, dstPort);
133 assertEquals(resultData.seq, sequence);
134 assertEquals(resultData.ack, ack);
markchien0fcb0782019-03-19 21:25:33 +0800135 assertEquals(resultData.rcvWnd, wnd);
136 assertEquals(resultData.rcvWndScale, wndScale);
junyulai8c1586e2019-01-30 19:11:45 +0800137 }
markchien46f41d42018-12-27 22:49:51 +0800138}