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.ip
18 
19 import android.net.ipmemorystore.NetworkAttributes
20 import android.net.ipmemorystore.OnNetworkAttributesRetrievedListener
21 import android.net.ipmemorystore.Status
22 import android.net.ipmemorystore.Status.SUCCESS
23 import android.util.ArrayMap
24 import android.util.Pair
25 import org.mockito.ArgumentCaptor
26 import org.mockito.Mockito.any
27 import org.mockito.Mockito.doAnswer
28 import org.mockito.Mockito.eq
29 import org.mockito.Mockito.never
30 import org.mockito.Mockito.timeout
31 import org.mockito.Mockito.verify
32 
33 /**
34  * Tests for IpClient, run with signature permissions.
35  */
36 class IpClientSignatureTest : IpClientIntegrationTestCommon() {
37     companion object {
38         private val TAG = IpClientSignatureTest::class.java.simpleName
39     }
40 
41     private val DEFAULT_NUD_SOLICIT_NUM_POST_ROAM = 5
42     private val DEFAULT_NUD_SOLICIT_NUM_STEADY_STATE = 10
43 
44     private val mDeviceConfigProperties = ArrayMap<String, String>()
45 
46     override fun makeIIpClient(ifaceName: String, cb: IIpClientCallbacks): IIpClient {
47         return mIpc.makeConnector()
48     }
49 
50     override fun useNetworkStackSignature() = true
51 
52     override fun isFeatureEnabled(name: String): Boolean {
53         return FEATURE_ENABLED.equals(getDeviceConfigProperty(name))
54     }
55 
56     override fun isFeatureNotChickenedOut(name: String): Boolean {
57         return !FEATURE_DISABLED.equals(getDeviceConfigProperty(name))
58     }
59 
60     override fun setDeviceConfigProperty(name: String, value: String) {
61         mDeviceConfigProperties.put(name, value)
62     }
63 
64     override fun getDeviceConfigProperty(name: String): String? {
65         return mDeviceConfigProperties.get(name)
66     }
67 
68     override fun getStoredNetworkAttributes(l2Key: String, timeout: Long): NetworkAttributes {
69         val networkAttributesCaptor = ArgumentCaptor.forClass(NetworkAttributes::class.java)
70 
71         verify(mIpMemoryStore, timeout(timeout))
72                 .storeNetworkAttributes(eq(l2Key), networkAttributesCaptor.capture(), any())
73         return networkAttributesCaptor.value
74     }
75 
76     override fun getStoredNetworkEventCount(
77             cluster: String,
78             sinceTimes: LongArray,
79             eventType: IntArray,
80             timeout: Long
81     ): IntArray {
82         val counts = IntArray(sinceTimes.size)
83         val eventTypesSet = eventType.toSet() // Convert eventType to Set for faster contains check
84 
85         sinceTimes.forEachIndexed { index, sinceTime ->
86             var count = 0
87             mNetworkEvents.forEach { event ->
88                 val key = event.first
89                 val value = event.second
90                 if (key == cluster && eventTypesSet.contains(value.second) &&
91                         sinceTime <= value.first) {
92                     count++
93                 }
94             }
95             counts[index] = count
96         }
97         return counts
98     }
99 
100     override fun assertIpMemoryNeverStoreNetworkAttributes(l2Key: String, timeout: Long) {
101         verify(mIpMemoryStore, never()).storeNetworkAttributes(eq(l2Key), any(), any())
102     }
103 
104     override fun storeNetworkAttributes(l2Key: String, na: NetworkAttributes) {
105         doAnswer { inv ->
106             val listener = inv.getArgument<OnNetworkAttributesRetrievedListener>(1)
107             listener.onNetworkAttributesRetrieved(Status(SUCCESS), l2Key, na)
108             true
109         }.`when`(mIpMemoryStore).retrieveNetworkAttributes(eq(l2Key), any())
110     }
111 
112     override fun storeNetworkEvent(cluster: String, now: Long, expiry: Long, eventType: Int) {
113         val event = Pair(cluster, Pair(now, eventType))
114         mNetworkEvents.add(event)
115     }
116 
117     override fun readNudSolicitNumInSteadyStateFromResource(): Int {
118         return DEFAULT_NUD_SOLICIT_NUM_STEADY_STATE
119     }
120 
121     override fun readNudSolicitNumPostRoamingFromResource(): Int {
122         return DEFAULT_NUD_SOLICIT_NUM_POST_ROAM
123     }
124 }
125