blob: 7a18bb08faa8eda347f471e79617305cd9a3a88c [file] [log] [blame]
Remi NGUYEN VAN678277c2021-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 android.net.InetAddresses.parseNumericAddress
20import android.os.Build
21import com.android.testutils.DevSdkIgnoreRule
22import com.android.testutils.DevSdkIgnoreRunner
23import com.android.testutils.assertFieldCountEquals
24import com.android.testutils.assertParcelSane
25import org.junit.Test
26import org.junit.runner.RunWith
27import java.net.InetAddress
28import kotlin.test.assertEquals
29import kotlin.test.assertNotEquals
30import kotlin.test.assertTrue
31
32@RunWith(DevSdkIgnoreRunner::class)
33@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) // TcpKeepalivePacketData added to SDK in S
34class TcpKeepalivePacketDataTest {
35 private fun makeData(
36 srcAddress: InetAddress = parseNumericAddress("192.0.2.123"),
37 srcPort: Int = 1234,
38 dstAddress: InetAddress = parseNumericAddress("192.0.2.231"),
39 dstPort: Int = 4321,
40 data: ByteArray = byteArrayOf(1, 2, 3),
41 tcpSeq: Int = 135,
42 tcpAck: Int = 246,
43 tcpWnd: Int = 1234,
44 tcpWndScale: Int = 2,
45 ipTos: Int = 0x12,
46 ipTtl: Int = 10
47 ) = TcpKeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, data, tcpSeq, tcpAck,
48 tcpWnd, tcpWndScale, ipTos, ipTtl)
49
50 @Test
51 fun testEquals() {
52 val data1 = makeData()
53 val data2 = makeData()
54 assertEquals(data1, data2)
55 assertEquals(data1.hashCode(), data2.hashCode())
56 }
57
58 @Test
59 fun testNotEquals() {
60 assertNotEquals(makeData(srcAddress = parseNumericAddress("192.0.2.124")), makeData())
61 assertNotEquals(makeData(srcPort = 1235), makeData())
62 assertNotEquals(makeData(dstAddress = parseNumericAddress("192.0.2.232")), makeData())
63 assertNotEquals(makeData(dstPort = 4322), makeData())
64 // .equals does not test .packet, as it should be generated from the other fields
65 assertNotEquals(makeData(tcpSeq = 136), makeData())
66 assertNotEquals(makeData(tcpAck = 247), makeData())
67 assertNotEquals(makeData(tcpWnd = 1235), makeData())
68 assertNotEquals(makeData(tcpWndScale = 3), makeData())
69 assertNotEquals(makeData(ipTos = 0x14), makeData())
70 assertNotEquals(makeData(ipTtl = 11), makeData())
71
72 // Update above assertions if field is added
73 assertFieldCountEquals(5, KeepalivePacketData::class.java)
74 assertFieldCountEquals(6, TcpKeepalivePacketData::class.java)
75 }
76
77 @Test
78 fun testParcelUnparcel() {
79 assertParcelSane(makeData(), fieldCount = 6) { a, b ->
80 // .equals() does not verify .packet
81 a == b && a.packet contentEquals b.packet
82 }
83 }
84
85 @Test
86 fun testToString() {
87 val data = makeData()
88 val str = data.toString()
89
90 assertTrue(str.contains(data.srcAddress.hostAddress))
91 assertTrue(str.contains(data.srcPort.toString()))
92 assertTrue(str.contains(data.dstAddress.hostAddress))
93 assertTrue(str.contains(data.dstPort.toString()))
94 // .packet not included in toString()
95 assertTrue(str.contains(data.getTcpSeq().toString()))
96 assertTrue(str.contains(data.getTcpAck().toString()))
97 assertTrue(str.contains(data.getTcpWindow().toString()))
98 assertTrue(str.contains(data.getTcpWindowScale().toString()))
99 assertTrue(str.contains(data.getIpTos().toString()))
100 assertTrue(str.contains(data.getIpTtl().toString()))
101
102 // Update above assertions if field is added
103 assertFieldCountEquals(5, KeepalivePacketData::class.java)
104 assertFieldCountEquals(6, TcpKeepalivePacketData::class.java)
105 }
106}