• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertNotEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assume.assumeTrue;
24 
25 import android.app.UiAutomation;
26 import android.content.Context;
27 import android.net.ConnectivityManager;
28 import android.os.FileUtils;
29 import android.os.ParcelFileDescriptor;
30 import android.platform.test.annotations.AppModeFull;
31 import android.util.Log;
32 
33 import androidx.test.InstrumentationRegistry;
34 import androidx.test.filters.SmallTest;
35 import androidx.test.runner.AndroidJUnit4;
36 
37 import com.android.compatibility.common.util.ApiLevelUtil;
38 import com.android.compatibility.common.util.SystemUtil;
39 
40 import org.junit.After;
41 import org.junit.Assert;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 
46 import java.io.FileOutputStream;
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.util.Formatter;
50 
51 @SmallTest
52 @RunWith(AndroidJUnit4.class)
53 public class NetworkWatchlistTest {
54 
55     private static final String TEST_WATCHLIST_XML = "assets/network_watchlist_config_for_test.xml";
56     private static final String TEST_EMPTY_WATCHLIST_XML =
57             "assets/network_watchlist_config_empty_for_test.xml";
58     private static final String TMP_CONFIG_PATH =
59             "/data/local/tmp/network_watchlist_config_for_test.xml";
60     // Generated from sha256sum network_watchlist_config_for_test.xml
61     private static final String TEST_WATCHLIST_CONFIG_HASH =
62             "B5FC4636994180D54E1E912F78178AB1D8BD2BE71D90CA9F5BBC3284E4D04ED4";
63 
64     private ConnectivityManager mConnectivityManager;
65     private boolean mHasFeature;
66 
67     @Before
setUp()68     public void setUp() throws Exception {
69         mHasFeature = isAtLeastP();
70         mConnectivityManager =
71                 (ConnectivityManager) InstrumentationRegistry.getContext().getSystemService(
72                         Context.CONNECTIVITY_SERVICE);
73         assumeTrue(mHasFeature);
74         // Set empty watchlist test config before testing
75         setWatchlistConfig(TEST_EMPTY_WATCHLIST_XML);
76         // Verify test watchlist config is not set before testing
77         byte[] result = mConnectivityManager.getNetworkWatchlistConfigHash();
78         assertNotNull("Watchlist config does not exist", result);
79         assertNotEquals(TEST_WATCHLIST_CONFIG_HASH, byteArrayToHexString(result));
80     }
81 
82     @After
tearDown()83     public void tearDown() throws Exception {
84         if (mHasFeature) {
85             // Set empty watchlist test config after testing
86             setWatchlistConfig(TEST_EMPTY_WATCHLIST_XML);
87         }
88     }
89 
cleanup()90     private void cleanup() throws IOException {
91         runCommand("rm " + TMP_CONFIG_PATH);
92     }
93 
isAtLeastP()94     private boolean isAtLeastP() throws Exception {
95         // TODO: replace with ApiLevelUtil.isAtLeast(Build.VERSION_CODES.P) when the P API level
96         // constant is defined.
97         return ApiLevelUtil.getCodename().compareToIgnoreCase("P") >= 0;
98     }
99 
100     /**
101      * Test if ConnectivityManager.getNetworkWatchlistConfigHash() correctly
102      * returns the hash of config we set.
103      */
104     @Test
105     @AppModeFull(reason = "Cannot access resource file in instant app mode")
testGetWatchlistConfigHash()106     public void testGetWatchlistConfigHash() throws Exception {
107         // Set watchlist config file for test
108         setWatchlistConfig(TEST_WATCHLIST_XML);
109         // Test if watchlist config hash value is correct
110         byte[] result = mConnectivityManager.getNetworkWatchlistConfigHash();
111         Assert.assertEquals(TEST_WATCHLIST_CONFIG_HASH, byteArrayToHexString(result));
112     }
113 
byteArrayToHexString(byte[] bytes)114     private static String byteArrayToHexString(byte[] bytes) {
115         Formatter formatter = new Formatter();
116         for (byte b : bytes) {
117             formatter.format("%02X", b);
118         }
119         return formatter.toString();
120     }
121 
setWatchlistConfig(String watchlistConfigFile)122     private void setWatchlistConfig(String watchlistConfigFile) throws Exception {
123         Log.w("NetworkWatchlistTest", "Setting watchlist config " + watchlistConfigFile
124                 + " in " + Thread.currentThread().getName());
125         cleanup();
126         saveResourceToFile(watchlistConfigFile, TMP_CONFIG_PATH);
127         final String cmdResult = runCommand(
128                 "cmd network_watchlist set-test-config " + TMP_CONFIG_PATH).trim();
129         assertThat(cmdResult).contains("Success");
130         cleanup();
131     }
132 
saveResourceToFile(String res, String filePath)133     private void saveResourceToFile(String res, String filePath) throws IOException {
134         final UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation()
135                 .getUiAutomation();
136         // App can't access /data/local/tmp directly, so we pipe resource to file through stdin.
137         // Not all devices have symlink for /dev/stdin, so use /proc/self/fd/0 directly.
138         // /dev/stdin maps to /proc/self/fd/0.
139         final ParcelFileDescriptor[] fileDescriptors = uiAutomation.executeShellCommandRw(
140                 "cp /proc/self/fd/0 " + filePath);
141 
142         ParcelFileDescriptor stdin = fileDescriptors[1];
143         ParcelFileDescriptor stdout = fileDescriptors[0];
144 
145         pipeResourceToFileDescriptor(res, stdin);
146 
147         // Wait for the process to close its stdout - which should mean it has completed.
148         consumeFile(stdout);
149     }
150 
consumeFile(ParcelFileDescriptor pfd)151     private void consumeFile(ParcelFileDescriptor pfd) throws IOException {
152         try (InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(pfd)) {
153             for (;;) {
154                 if (stream.read() == -1) {
155                     return;
156                 }
157             }
158         }
159     }
160 
pipeResourceToFileDescriptor(String res, ParcelFileDescriptor pfd)161     private void pipeResourceToFileDescriptor(String res, ParcelFileDescriptor pfd)
162             throws IOException {
163         try (InputStream resStream = getClass().getClassLoader().getResourceAsStream(res);
164                 FileOutputStream fdStream = new ParcelFileDescriptor.AutoCloseOutputStream(pfd)) {
165             FileUtils.copy(resStream, fdStream);
166         }
167     }
168 
runCommand(String command)169     private static String runCommand(String command) throws IOException {
170         return SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(), command);
171     }
172 }
173