1 /*
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 
17 package android.net.dhcp
18 
19 import android.net.InetAddresses.parseNumericAddress
20 import android.net.MacAddress
21 import com.android.net.module.util.Inet4AddressUtils.intToInet4AddressHTH
22 import androidx.test.filters.SmallTest
23 import androidx.test.runner.AndroidJUnit4
24 import com.android.testutils.assertFieldCountEquals
25 import org.junit.Assert.assertArrayEquals
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import java.net.Inet4Address
29 import kotlin.test.assertEquals
30 import kotlin.test.assertNotEquals
31 
32 @RunWith(AndroidJUnit4::class)
33 @SmallTest
34 class DhcpLeaseTest {
35     companion object {
36         private val TEST_CLIENT_ID = byteArrayOf(0, 1, 2, 127)
37         private val TEST_HWADDR = MacAddress.fromString("01:23:45:67:8F:9A")
38         private val TEST_INETADDR = parseNumericAddress("192.168.42.123") as Inet4Address
39         private val TEST_PREFIXLEN = 23
40         private val TEST_EXPTIME = 1234L
41         private val TEST_HOSTNAME = "test_hostname"
42     }
43 
44     @Test
testToParcelablenull45     fun testToParcelable() {
46         val lease = DhcpLease(TEST_CLIENT_ID, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN,
47                 TEST_EXPTIME, TEST_HOSTNAME)
48 
49         assertParcelEquals(lease, lease.toParcelable())
50     }
51 
52     @Test
testToParcelable_NullFieldsnull53     fun testToParcelable_NullFields() {
54         val lease = DhcpLease(null, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN, TEST_EXPTIME, null)
55         assertParcelEquals(lease, lease.toParcelable())
56     }
57 
58     @Test
testEqualsnull59     fun testEquals() {
60         val lease = DhcpLease(TEST_CLIENT_ID, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN,
61                 TEST_EXPTIME, TEST_HOSTNAME)
62         assertEquals(lease, DhcpLease(TEST_CLIENT_ID, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN,
63                 TEST_EXPTIME, TEST_HOSTNAME))
64 
65         // Change client ID
66         assertNotEquals(lease, DhcpLease(null, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN,
67                 TEST_EXPTIME, TEST_HOSTNAME))
68         assertNotEquals(lease, DhcpLease(byteArrayOf(42), TEST_HWADDR, TEST_INETADDR,
69                 TEST_PREFIXLEN, TEST_EXPTIME, TEST_HOSTNAME))
70 
71         // Change mac address
72         assertNotEquals(lease, DhcpLease(TEST_CLIENT_ID, MacAddress.fromString("12:34:56:78:9A:0B"),
73                 TEST_INETADDR, TEST_PREFIXLEN, TEST_EXPTIME, TEST_HOSTNAME))
74 
75         // Change address
76         assertNotEquals(lease, DhcpLease(TEST_CLIENT_ID, TEST_HWADDR,
77                 parseNumericAddress("192.168.43.43") as Inet4Address, TEST_PREFIXLEN, TEST_EXPTIME,
78                 TEST_HOSTNAME))
79 
80         // Change prefix length
81         assertNotEquals(lease, DhcpLease(TEST_CLIENT_ID, TEST_HWADDR, TEST_INETADDR, 24,
82                 TEST_EXPTIME, TEST_HOSTNAME))
83 
84         // Change expiry time
85         assertNotEquals(lease, DhcpLease(TEST_CLIENT_ID, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN,
86                 4567L, TEST_HOSTNAME))
87 
88         // Change hostname
89         assertNotEquals(lease, DhcpLease(TEST_CLIENT_ID, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN,
90                 TEST_EXPTIME, null))
91         assertNotEquals(lease, DhcpLease(TEST_CLIENT_ID, TEST_HWADDR, TEST_INETADDR, TEST_PREFIXLEN,
92                 TEST_EXPTIME, "other_hostname"))
93 
94         assertFieldCountEquals(6, DhcpLease::class.java)
95     }
96 
assertParcelEqualsnull97     private fun assertParcelEquals(expected: DhcpLease, p: DhcpLeaseParcelable) {
98         assertArrayEquals(expected.clientId, p.clientId)
99         assertEquals(expected.hwAddr, MacAddress.fromBytes(p.hwAddr))
100         assertEquals(expected.netAddr, intToInet4AddressHTH(p.netAddr))
101         assertEquals(expected.prefixLength, p.prefixLength)
102         assertEquals(expected.expTime, p.expTime)
103         assertEquals(expected.hostname, p.hostname)
104 
105         assertFieldCountEquals(6, DhcpLease::class.java)
106     }
107 }