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
18 
19 import android.net.InetAddresses.parseNumericAddress
20 import android.net.TetheredClient.AddressInfo
21 import android.net.TetheringManager.TETHERING_BLUETOOTH
22 import android.net.TetheringManager.TETHERING_USB
23 import android.system.OsConstants.RT_SCOPE_UNIVERSE
24 import androidx.test.filters.SmallTest
25 import androidx.test.runner.AndroidJUnit4
26 import com.android.testutils.assertParcelSane
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import kotlin.test.assertEquals
30 import kotlin.test.assertNotEquals
31 
32 private val TEST_MACADDR = MacAddress.fromBytes(byteArrayOf(12, 23, 34, 45, 56, 67))
33 private val TEST_OTHER_MACADDR = MacAddress.fromBytes(byteArrayOf(23, 34, 45, 56, 67, 78))
34 private val TEST_ADDR1 = makeLinkAddress("192.168.113.3", prefixLength = 24, expTime = 123L)
35 private val TEST_ADDR2 = makeLinkAddress("fe80::1:2:3", prefixLength = 64, expTime = 456L)
36 private val TEST_HOSTNAME = "test_hostname"
37 private val TEST_OTHER_HOSTNAME = "test_other_hostname"
38 private val TEST_ADDRINFO1 = AddressInfo(TEST_ADDR1, TEST_HOSTNAME)
39 private val TEST_ADDRINFO2 = AddressInfo(TEST_ADDR2, null)
40 
makeLinkAddressnull41 private fun makeLinkAddress(addr: String, prefixLength: Int, expTime: Long) = LinkAddress(
42         parseNumericAddress(addr),
43         prefixLength,
44         0 /* flags */,
45         RT_SCOPE_UNIVERSE,
46         expTime /* deprecationTime */,
47         expTime /* expirationTime */)
48 
49 @RunWith(AndroidJUnit4::class)
50 @SmallTest
51 class TetheredClientTest {
52     @Test
53     fun testParceling() {
54         assertParcelSane(TEST_ADDRINFO1, fieldCount = 2)
55         assertParcelSane(makeTestClient(), fieldCount = 3)
56     }
57 
58     @Test
59     fun testEquals() {
60         assertEquals(makeTestClient(), makeTestClient())
61 
62         // Different mac address
63         assertNotEquals(makeTestClient(), TetheredClient(
64                 TEST_OTHER_MACADDR,
65                 listOf(TEST_ADDRINFO1, TEST_ADDRINFO2),
66                 TETHERING_BLUETOOTH))
67 
68         // Different hostname
69         assertNotEquals(makeTestClient(), TetheredClient(
70                 TEST_MACADDR,
71                 listOf(AddressInfo(TEST_ADDR1, TEST_OTHER_HOSTNAME), TEST_ADDRINFO2),
72                 TETHERING_BLUETOOTH))
73 
74         // Null hostname
75         assertNotEquals(makeTestClient(), TetheredClient(
76                 TEST_MACADDR,
77                 listOf(AddressInfo(TEST_ADDR1, null), TEST_ADDRINFO2),
78                 TETHERING_BLUETOOTH))
79 
80         // Missing address
81         assertNotEquals(makeTestClient(), TetheredClient(
82                 TEST_MACADDR,
83                 listOf(TEST_ADDRINFO2),
84                 TETHERING_BLUETOOTH))
85 
86         // Different type
87         assertNotEquals(makeTestClient(), TetheredClient(
88                 TEST_MACADDR,
89                 listOf(TEST_ADDRINFO1, TEST_ADDRINFO2),
90                 TETHERING_USB))
91     }
92 
93     @Test
94     fun testAddAddresses() {
95         val client1 = TetheredClient(TEST_MACADDR, listOf(TEST_ADDRINFO1), TETHERING_USB)
96         val client2 = TetheredClient(TEST_OTHER_MACADDR, listOf(TEST_ADDRINFO2), TETHERING_USB)
97         assertEquals(TetheredClient(
98                 TEST_MACADDR,
99                 listOf(TEST_ADDRINFO1, TEST_ADDRINFO2),
100                 TETHERING_USB), client1.addAddresses(client2))
101     }
102 
103     @Test
104     fun testGetters() {
105         assertEquals(TEST_MACADDR, makeTestClient().macAddress)
106         assertEquals(listOf(TEST_ADDRINFO1, TEST_ADDRINFO2), makeTestClient().addresses)
107         assertEquals(TETHERING_BLUETOOTH, makeTestClient().tetheringType)
108     }
109 
110     @Test
111     fun testAddressInfo_Getters() {
112         assertEquals(TEST_ADDR1, TEST_ADDRINFO1.address)
113         assertEquals(TEST_ADDR2, TEST_ADDRINFO2.address)
114         assertEquals(TEST_HOSTNAME, TEST_ADDRINFO1.hostname)
115         assertEquals(null, TEST_ADDRINFO2.hostname)
116     }
117 
118     private fun makeTestClient() = TetheredClient(
119             TEST_MACADDR,
120             listOf(TEST_ADDRINFO1, TEST_ADDRINFO2),
121             TETHERING_BLUETOOTH)
122 }