blob: fc739fbfac617507b48a23d94e2801a4d446f589 [file] [log] [blame]
Remi NGUYEN VAN31022d62021-05-11 13:37:06 +00001/*
2 * Copyright (C) 2020 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 com.android.testutils.ParcelUtils.assertParcelingIsLossless;
20
21import static org.junit.Assert.assertArrayEquals;
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertTrue;
24import static org.junit.Assert.fail;
25
26import android.net.util.KeepalivePacketDataUtil;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31import org.junit.runners.JUnit4;
32
33import java.net.InetAddress;
34import java.nio.ByteBuffer;
35
36@RunWith(JUnit4.class)
37public final class KeepalivePacketDataUtilTest {
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};
40
41 @Before
42 public void setUp() {}
43
44 @Test
45 public void testFromTcpKeepaliveStableParcelable() throws Exception {
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;
52 final int tos = 4;
53 final int ttl = 64;
54 TcpKeepalivePacketData resultData = null;
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;
64 testInfo.tos = tos;
65 testInfo.ttl = ttl;
66 try {
67 resultData = KeepalivePacketDataUtil.fromStableParcelable(testInfo);
68 } catch (InvalidPacketException e) {
69 fail("InvalidPacketException: " + e);
70 }
71
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());
76 assertEquals(testInfo.seq, resultData.tcpSeq);
77 assertEquals(testInfo.ack, resultData.tcpAck);
78 assertEquals(testInfo.rcvWnd, resultData.tcpWindow);
79 assertEquals(testInfo.rcvWndScale, resultData.tcpWindowScale);
80 assertEquals(testInfo.tos, resultData.ipTos);
81 assertEquals(testInfo.ttl, resultData.ipTtl);
82
83 assertParcelingIsLossless(resultData);
84
85 final byte[] packet = resultData.getPacket();
86 // IP version and IHL
87 assertEquals(packet[0], 0x45);
88 // TOS
89 assertEquals(packet[1], tos);
90 // TTL
91 assertEquals(packet[8], ttl);
92 // Source IP address.
93 byte[] ip = new byte[4];
94 ByteBuffer buf = ByteBuffer.wrap(packet, 12, 4);
95 buf.get(ip);
96 assertArrayEquals(ip, IPV4_KEEPALIVE_SRC_ADDR);
97 // Destination IP address.
98 buf = ByteBuffer.wrap(packet, 16, 4);
99 buf.get(ip);
100 assertArrayEquals(ip, IPV4_KEEPALIVE_DST_ADDR);
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
117
118 @Test
119 public void testToTcpKeepaliveStableParcelable() throws Exception {
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;
126 final int tos = 4;
127 final int ttl = 64;
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;
137 testInfo.tos = tos;
138 testInfo.ttl = ttl;
139 TcpKeepalivePacketData testData = null;
140 TcpKeepalivePacketDataParcelable resultData = null;
141 testData = KeepalivePacketDataUtil.fromStableParcelable(testInfo);
142 resultData = KeepalivePacketDataUtil.toStableParcelable(testData);
143 assertArrayEquals(resultData.srcAddress, IPV4_KEEPALIVE_SRC_ADDR);
144 assertArrayEquals(resultData.dstAddress, IPV4_KEEPALIVE_DST_ADDR);
145 assertEquals(resultData.srcPort, srcPort);
146 assertEquals(resultData.dstPort, dstPort);
147 assertEquals(resultData.seq, sequence);
148 assertEquals(resultData.ack, ack);
149 assertEquals(resultData.rcvWnd, wnd);
150 assertEquals(resultData.rcvWndScale, wndScale);
151 assertEquals(resultData.tos, tos);
152 assertEquals(resultData.ttl, ttl);
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());
159 }
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 }
205}