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