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.Assume.assumeTrue; 23 24 import android.content.Context; 25 import android.net.ConnectivityManager; 26 import android.os.FileUtils; 27 import android.support.test.filters.SmallTest; 28 import android.support.test.runner.AndroidJUnit4; 29 import android.support.test.InstrumentationRegistry; 30 31 import com.android.compatibility.common.util.ApiLevelUtil; 32 import com.android.compatibility.common.util.SystemUtil; 33 34 import org.junit.After; 35 import org.junit.Assert; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 41 import java.io.File; 42 import java.io.IOException; 43 import java.io.InputStream; 44 import java.util.Formatter; 45 46 @SmallTest 47 @RunWith(AndroidJUnit4.class) 48 public class NetworkWatchlistTest { 49 50 private static final String TEST_WATCHLIST_XML = "assets/network_watchlist_config_for_test.xml"; 51 private static final String TEST_EMPTY_WATCHLIST_XML = 52 "assets/network_watchlist_config_empty_for_test.xml"; 53 private static final String SDCARD_CONFIG_PATH = 54 "/sdcard/network_watchlist_config_for_test.xml"; 55 private static final String TMP_CONFIG_PATH = 56 "/data/local/tmp/network_watchlist_config_for_test.xml"; 57 // Generated from sha256sum network_watchlist_config_for_test.xml 58 private static final String TEST_WATCHLIST_CONFIG_HASH = 59 "B5FC4636994180D54E1E912F78178AB1D8BD2BE71D90CA9F5BBC3284E4D04ED4"; 60 61 private ConnectivityManager mConnectivityManager; 62 private boolean mHasFeature; 63 64 @Before 65 public void setUp() throws Exception { 66 mHasFeature = isAtLeastP(); 67 mConnectivityManager = 68 (ConnectivityManager) InstrumentationRegistry.getContext().getSystemService( 69 Context.CONNECTIVITY_SERVICE); 70 assumeTrue(mHasFeature); 71 // Set empty watchlist test config before testing 72 setWatchlistConfig(TEST_EMPTY_WATCHLIST_XML); 73 // Verify test watchlist config is not set before testing 74 byte[] result = mConnectivityManager.getNetworkWatchlistConfigHash(); 75 assertNotEquals(TEST_WATCHLIST_CONFIG_HASH, byteArrayToHexString(result)); 76 } 77 78 @After 79 public void tearDown() throws Exception { 80 if (mHasFeature) { 81 // Set empty watchlist test config after testing 82 setWatchlistConfig(TEST_EMPTY_WATCHLIST_XML); 83 } 84 } 85 86 private void cleanup() throws Exception { 87 runCommand("rm " + SDCARD_CONFIG_PATH); 88 runCommand("rm " + TMP_CONFIG_PATH); 89 } 90 91 private boolean isAtLeastP() throws Exception { 92 // TODO: replace with ApiLevelUtil.isAtLeast(Build.VERSION_CODES.P) when the P API level 93 // constant is defined. 94 return ApiLevelUtil.getCodename().compareToIgnoreCase("P") >= 0; 95 } 96 97 /** 98 * Test if ConnectivityManager.getNetworkWatchlistConfigHash() correctly 99 * returns the hash of config we set. 100 */ 101 @Test 102 public void testGetWatchlistConfigHash() throws Exception { 103 // Set watchlist config file for test 104 setWatchlistConfig(TEST_WATCHLIST_XML); 105 // Test if watchlist config hash value is correct 106 byte[] result = mConnectivityManager.getNetworkWatchlistConfigHash(); 107 Assert.assertEquals(TEST_WATCHLIST_CONFIG_HASH, byteArrayToHexString(result)); 108 } 109 110 private static String byteArrayToHexString(byte[] bytes) { 111 Formatter formatter = new Formatter(); 112 for (byte b : bytes) { 113 formatter.format("%02X", b); 114 } 115 return formatter.toString(); 116 } 117 118 private void saveResourceToFile(String res, String filePath) throws IOException { 119 InputStream in = getClass().getClassLoader().getResourceAsStream(res); 120 FileUtils.copyToFileOrThrow(in, new File(filePath)); 121 } 122 123 private static String runCommand(String command) throws IOException { 124 return SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(), command); 125 } 126 127 private void setWatchlistConfig(String watchlistConfigFile) throws Exception { 128 cleanup(); 129 // Save test watchlist config to sdcard as app can't access /data/local/tmp 130 saveResourceToFile(watchlistConfigFile, SDCARD_CONFIG_PATH); 131 // Copy test watchlist config from sdcard to /data/local/tmp as system service 132 // can't access /sdcard 133 runCommand("cp " + SDCARD_CONFIG_PATH + " " + TMP_CONFIG_PATH); 134 // Set test watchlist config to system 135 final String cmdResult = runCommand( 136 "cmd network_watchlist set-test-config " + TMP_CONFIG_PATH).trim(); 137 assertThat(cmdResult).contains("Success"); 138 cleanup(); 139 } 140 } 141