• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cts
18 
19 import android.Manifest
20 import android.net.util.NetworkStackUtils
21 import android.provider.DeviceConfig
22 import com.android.testutils.runAsShell
23 
24 /**
25  * Collection of utility methods for configuring network validation.
26  */
27 internal object NetworkValidationTestUtil {
28 
29     /**
30      * Clear the test network validation URLs.
31      */
clearValidationTestUrlsDeviceConfignull32     @JvmStatic fun clearValidationTestUrlsDeviceConfig() {
33         setHttpsUrlDeviceConfig(null)
34         setHttpUrlDeviceConfig(null)
35         setUrlExpirationDeviceConfig(null)
36     }
37 
38     /**
39      * Set the test validation HTTPS URL.
40      *
41      * @see NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTPS_URL
42      */
setHttpsUrlDeviceConfignull43     @JvmStatic fun setHttpsUrlDeviceConfig(url: String?) =
44             setConfig(NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTPS_URL, url)
45 
46     /**
47      * Set the test validation HTTP URL.
48      *
49      * @see NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTP_URL
50      */
51     @JvmStatic fun setHttpUrlDeviceConfig(url: String?) =
52             setConfig(NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTP_URL, url)
53 
54     /**
55      * Set the test validation URL expiration.
56      *
57      * @see NetworkStackUtils.TEST_URL_EXPIRATION_TIME
58      */
59     @JvmStatic fun setUrlExpirationDeviceConfig(timestamp: Long?) =
60             setConfig(NetworkStackUtils.TEST_URL_EXPIRATION_TIME, timestamp?.toString())
61 
62     private fun setConfig(configKey: String, value: String?) {
63         runAsShell(Manifest.permission.WRITE_DEVICE_CONFIG) {
64             DeviceConfig.setProperty(
65                     DeviceConfig.NAMESPACE_CONNECTIVITY, configKey, value, false /* makeDefault */)
66         }
67     }
68 }