• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.util
18 
19 import android.content.Context
20 import android.content.res.Resources
21 import android.net.ConnectivityResources
22 import android.net.NetworkCapabilities
23 import android.net.NetworkCapabilities.MAX_TRANSPORT
24 import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
25 import android.net.NetworkCapabilities.TRANSPORT_ETHERNET
26 import android.net.NetworkCapabilities.TRANSPORT_VPN
27 import android.net.NetworkCapabilities.TRANSPORT_WIFI
28 import android.os.Build
29 import androidx.test.filters.SmallTest
30 import com.android.internal.R
31 import com.android.testutils.DevSdkIgnoreRule
32 import com.android.testutils.DevSdkIgnoreRunner
33 import org.junit.After
34 import org.junit.Assert.assertArrayEquals
35 import org.junit.Assert.assertEquals
36 import org.junit.Assert.fail
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 import org.mockito.ArgumentMatchers.eq
40 import org.mockito.Mockito.any
41 import org.mockito.Mockito.doReturn
42 import org.mockito.Mockito.mock
43 
44 /**
45  * Tests for [KeepaliveUtils].
46  *
47  * Build, install and run with:
48  * atest android.net.util.KeepaliveUtilsTest
49  */
50 @RunWith(DevSdkIgnoreRunner::class)
51 @SmallTest
52 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
53 class KeepaliveUtilsTest {
54 
55     // Prepare mocked context with given resource strings.
getMockedContextWithStringArrayResnull56     private fun getMockedContextWithStringArrayRes(
57         id: Int,
58         name: String,
59         res: Array<out String?>?
60     ): Context {
61         val mockRes = mock(Resources::class.java)
62         doReturn(res).`when`(mockRes).getStringArray(eq(id))
63         doReturn(id).`when`(mockRes).getIdentifier(eq(name), any(), any())
64 
65         return mock(Context::class.java).apply {
66             doReturn(mockRes).`when`(this).getResources()
67             ConnectivityResources.setResourcesContextForTest(this)
68         }
69     }
70 
71     @After
tearDownnull72     fun tearDown() {
73         ConnectivityResources.setResourcesContextForTest(null)
74     }
75 
76     @Test
testGetSupportedKeepalivesnull77     fun testGetSupportedKeepalives() {
78         fun assertRunWithException(res: Array<out String?>?) {
79             try {
80                 val mockContext = getMockedContextWithStringArrayRes(
81                         R.array.config_networkSupportedKeepaliveCount,
82                         "config_networkSupportedKeepaliveCount", res)
83                 KeepaliveUtils.getSupportedKeepalives(mockContext)
84                 fail("Expected KeepaliveDeviceConfigurationException")
85             } catch (expected: KeepaliveUtils.KeepaliveDeviceConfigurationException) {
86             }
87         }
88 
89         // Check resource with various invalid format.
90         assertRunWithException(null)
91         assertRunWithException(arrayOf<String?>(null))
92         assertRunWithException(arrayOfNulls<String?>(10))
93         assertRunWithException(arrayOf(""))
94         assertRunWithException(arrayOf("3,ABC"))
95         assertRunWithException(arrayOf("6,3,3"))
96         assertRunWithException(arrayOf("5"))
97 
98         // Check resource with invalid slots value.
99         assertRunWithException(arrayOf("3,-1"))
100 
101         // Check resource with invalid transport type.
102         assertRunWithException(arrayOf("-1,3"))
103         assertRunWithException(arrayOf("10,3"))
104 
105         // Check valid customization generates expected array.
106         val validRes = arrayOf("0,3", "1,0", "4,4")
107         val expectedValidRes = intArrayOf(3, 0, 0, 0, 4, 0, 0, 0, 0)
108 
109         val mockContext = getMockedContextWithStringArrayRes(
110                 R.array.config_networkSupportedKeepaliveCount,
111                 "config_networkSupportedKeepaliveCount", validRes)
112         val actual = KeepaliveUtils.getSupportedKeepalives(mockContext)
113         assertArrayEquals(expectedValidRes, actual)
114     }
115 
116     @Test
testGetSupportedKeepalivesForNetworkCapabilitiesnull117     fun testGetSupportedKeepalivesForNetworkCapabilities() {
118         // Mock customized supported keepalives for each transport type, and assuming:
119         //   3 for cellular,
120         //   6 for wifi,
121         //   0 for others.
122         val cust = IntArray(MAX_TRANSPORT + 1).apply {
123             this[TRANSPORT_CELLULAR] = 3
124             this[TRANSPORT_WIFI] = 6
125         }
126 
127         val nc = NetworkCapabilities()
128         // Check supported keepalives with single transport type.
129         nc.transportTypes = intArrayOf(TRANSPORT_CELLULAR)
130         assertEquals(3, KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc))
131 
132         // Check supported keepalives with multiple transport types.
133         nc.transportTypes = intArrayOf(TRANSPORT_WIFI, TRANSPORT_VPN)
134         assertEquals(0, KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc))
135 
136         // Check supported keepalives with non-customized transport type.
137         nc.transportTypes = intArrayOf(TRANSPORT_ETHERNET)
138         assertEquals(0, KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc))
139 
140         // Check supported keepalives with undefined transport type.
141         nc.transportTypes = intArrayOf(MAX_TRANSPORT + 1)
142         try {
143             KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc)
144             fail("Expected ArrayIndexOutOfBoundsException")
145         } catch (expected: ArrayIndexOutOfBoundsException) {
146         }
147     }
148 }
149