• 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.NetworkCapabilities
22 import android.net.NetworkCapabilities.MAX_TRANSPORT
23 import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
24 import android.net.NetworkCapabilities.TRANSPORT_ETHERNET
25 import android.net.NetworkCapabilities.TRANSPORT_VPN
26 import android.net.NetworkCapabilities.TRANSPORT_WIFI
27 import androidx.test.filters.SmallTest
28 import com.android.internal.R
29 import org.junit.Assert.assertArrayEquals
30 import org.junit.Assert.assertEquals
31 import org.junit.Assert.fail
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.junit.runners.JUnit4
35 import org.mockito.ArgumentMatchers
36 import org.mockito.Mockito.doReturn
37 import org.mockito.Mockito.mock
38 
39 /**
40  * Tests for [KeepaliveUtils].
41  *
42  * Build, install and run with:
43  * atest android.net.util.KeepaliveUtilsTest
44  */
45 @RunWith(JUnit4::class)
46 @SmallTest
47 class KeepaliveUtilsTest {
48 
49     // Prepare mocked context with given resource strings.
getMockedContextWithStringArrayResnull50     private fun getMockedContextWithStringArrayRes(id: Int, res: Array<out String?>?): Context {
51         val mockRes = mock(Resources::class.java)
52         doReturn(res).`when`(mockRes).getStringArray(ArgumentMatchers.eq(id))
53 
54         return mock(Context::class.java).apply {
55             doReturn(mockRes).`when`(this).getResources()
56         }
57     }
58 
59     @Test
testGetSupportedKeepalivesnull60     fun testGetSupportedKeepalives() {
61         fun assertRunWithException(res: Array<out String?>?) {
62             try {
63                 val mockContext = getMockedContextWithStringArrayRes(
64                         R.array.config_networkSupportedKeepaliveCount, res)
65                 KeepaliveUtils.getSupportedKeepalives(mockContext)
66                 fail("Expected KeepaliveDeviceConfigurationException")
67             } catch (expected: KeepaliveUtils.KeepaliveDeviceConfigurationException) {
68             }
69         }
70 
71         // Check resource with various invalid format.
72         assertRunWithException(null)
73         assertRunWithException(arrayOf<String?>(null))
74         assertRunWithException(arrayOfNulls<String?>(10))
75         assertRunWithException(arrayOf(""))
76         assertRunWithException(arrayOf("3,ABC"))
77         assertRunWithException(arrayOf("6,3,3"))
78         assertRunWithException(arrayOf("5"))
79 
80         // Check resource with invalid slots value.
81         assertRunWithException(arrayOf("3,-1"))
82 
83         // Check resource with invalid transport type.
84         assertRunWithException(arrayOf("-1,3"))
85         assertRunWithException(arrayOf("10,3"))
86 
87         // Check valid customization generates expected array.
88         val validRes = arrayOf("0,3", "1,0", "4,4")
89         val expectedValidRes = intArrayOf(3, 0, 0, 0, 4, 0, 0, 0)
90 
91         val mockContext = getMockedContextWithStringArrayRes(
92                 R.array.config_networkSupportedKeepaliveCount, validRes)
93         val actual = KeepaliveUtils.getSupportedKeepalives(mockContext)
94         assertArrayEquals(expectedValidRes, actual)
95     }
96 
97     @Test
testGetSupportedKeepalivesForNetworkCapabilitiesnull98     fun testGetSupportedKeepalivesForNetworkCapabilities() {
99         // Mock customized supported keepalives for each transport type, and assuming:
100         //   3 for cellular,
101         //   6 for wifi,
102         //   0 for others.
103         val cust = IntArray(MAX_TRANSPORT + 1).apply {
104             this[TRANSPORT_CELLULAR] = 3
105             this[TRANSPORT_WIFI] = 6
106         }
107 
108         val nc = NetworkCapabilities()
109         // Check supported keepalives with single transport type.
110         nc.transportTypes = intArrayOf(TRANSPORT_CELLULAR)
111         assertEquals(3, KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc))
112 
113         // Check supported keepalives with multiple transport types.
114         nc.transportTypes = intArrayOf(TRANSPORT_WIFI, TRANSPORT_VPN)
115         assertEquals(0, KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc))
116 
117         // Check supported keepalives with non-customized transport type.
118         nc.transportTypes = intArrayOf(TRANSPORT_ETHERNET)
119         assertEquals(0, KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc))
120 
121         // Check supported keepalives with undefined transport type.
122         nc.transportTypes = intArrayOf(MAX_TRANSPORT + 1)
123         try {
124             KeepaliveUtils.getSupportedKeepalivesForNetworkCapabilities(cust, nc)
125             fail("Expected ArrayIndexOutOfBoundsException")
126         } catch (expected: ArrayIndexOutOfBoundsException) {
127         }
128     }
129 }
130