1 /* 2 * Copyright 2017 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 com.android.server.net.watchlist; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.assertNull; 23 24 import android.content.Context; 25 import android.support.test.InstrumentationRegistry; 26 import android.support.test.filters.SmallTest; 27 import android.support.test.runner.AndroidJUnit4; 28 29 import com.android.internal.util.HexDump; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.io.BufferedReader; 37 import java.io.ByteArrayOutputStream; 38 import java.io.File; 39 import java.io.FileWriter; 40 import java.io.IOException; 41 import java.io.InputStreamReader; 42 import java.io.PrintWriter; 43 import java.util.Arrays; 44 45 46 /** 47 * runtest frameworks-services -c com.android.server.net.watchlist.WatchlistConfigTests 48 */ 49 @RunWith(AndroidJUnit4.class) 50 @SmallTest 51 public class WatchlistConfigTests { 52 53 private static final String TEST_XML_1 = "NetworkWatchlistTest/watchlist_config_test1.xml"; 54 private static final String TEST_XML_1_HASH = 55 "C99F27A08B1FDB15B101098E12BB2A0AA0D474E23C50F24920A52AB2322BFD94"; 56 private static final String TEST_CC_DOMAIN = "test-cc-domain.com"; 57 private static final String TEST_CC_IP = "127.0.0.2"; 58 private static final String TEST_NOT_EXIST_CC_DOMAIN = "test-not-exist-cc-domain.com"; 59 private static final String TEST_NOT_EXIST_CC_IP = "1.2.3.4"; 60 private static final String TEST_SHA256_ONLY_DOMAIN = "test-cc-match-sha256-only.com"; 61 private static final String TEST_SHA256_ONLY_IP = "127.0.0.3"; 62 private static final String TEST_CRC32_ONLY_DOMAIN = "test-cc-match-crc32-only.com"; 63 private static final String TEST_CRC32_ONLY_IP = "127.0.0.4"; 64 65 private static final String TEST_NEW_CC_DOMAIN = "test-new-cc-domain.com"; 66 private static final byte[] TEST_NEW_CC_DOMAIN_SHA256 = HexDump.hexStringToByteArray( 67 "B86F9D37425340B635F43D6BC2506630761ADA71F5E6BBDBCA4651C479F9FB43"); 68 private static final byte[] TEST_NEW_CC_DOMAIN_CRC32 = HexDump.hexStringToByteArray("76795BD3"); 69 70 private static final String TEST_NEW_CC_IP = "1.1.1.2"; 71 private static final byte[] TEST_NEW_CC_IP_SHA256 = HexDump.hexStringToByteArray( 72 "721BAB5E313CF0CC76B10F9592F18B9D1B8996497501A3306A55B3AE9F1CC87C"); 73 private static final byte[] TEST_NEW_CC_IP_CRC32 = HexDump.hexStringToByteArray("940B8BEE"); 74 75 private Context mContext; 76 private File mTestXmlFile; 77 78 @Before 79 public void setUp() throws Exception { 80 mContext = InstrumentationRegistry.getContext(); 81 mTestXmlFile = new File(mContext.getFilesDir(), "test_watchlist_config.xml"); 82 mTestXmlFile.delete(); 83 } 84 85 @After 86 public void tearDown() throws Exception { 87 mTestXmlFile.delete(); 88 } 89 90 @Test 91 public void testWatchlistConfig_parsing() throws Exception { 92 copyWatchlistConfigXml(mContext, TEST_XML_1, mTestXmlFile); 93 WatchlistConfig config = new WatchlistConfig(mTestXmlFile); 94 assertTrue(config.containsDomain(TEST_CC_DOMAIN)); 95 assertTrue(config.containsIp(TEST_CC_IP)); 96 assertFalse(config.containsDomain(TEST_NOT_EXIST_CC_DOMAIN)); 97 assertFalse(config.containsIp(TEST_NOT_EXIST_CC_IP)); 98 assertFalse(config.containsDomain(TEST_SHA256_ONLY_DOMAIN)); 99 assertFalse(config.containsIp(TEST_SHA256_ONLY_IP)); 100 assertFalse(config.containsDomain(TEST_CRC32_ONLY_DOMAIN)); 101 assertFalse(config.containsIp(TEST_CRC32_ONLY_IP)); 102 } 103 104 @Test 105 public void testWatchlistConfig_noXml() throws Exception { 106 WatchlistConfig config = new WatchlistConfig(mTestXmlFile); 107 assertFalse(config.containsDomain(TEST_CC_DOMAIN)); 108 assertFalse(config.containsIp(TEST_CC_IP)); 109 assertFalse(config.containsDomain(TEST_NOT_EXIST_CC_DOMAIN)); 110 assertFalse(config.containsIp(TEST_NOT_EXIST_CC_IP)); 111 assertFalse(config.containsDomain(TEST_SHA256_ONLY_DOMAIN)); 112 assertFalse(config.containsIp(TEST_SHA256_ONLY_IP)); 113 assertFalse(config.containsDomain(TEST_CRC32_ONLY_DOMAIN)); 114 assertFalse(config.containsIp(TEST_CRC32_ONLY_IP)); 115 } 116 117 @Test 118 public void testWatchlistConfig_getWatchlistConfigHash_hasConfig() throws Exception { 119 copyWatchlistConfigXml(mContext, TEST_XML_1, mTestXmlFile); 120 WatchlistConfig config = new WatchlistConfig(mTestXmlFile); 121 assertEquals(TEST_XML_1_HASH, HexDump.toHexString(config.getWatchlistConfigHash())); 122 } 123 124 @Test 125 public void testWatchlistConfig_getWatchlistConfigHash_withoutConfig() throws Exception { 126 WatchlistConfig config = new WatchlistConfig(mTestXmlFile); 127 assertNull(config.getWatchlistConfigHash()); 128 } 129 130 @Test 131 public void testWatchlistConfig_testDumpDoesNotCrash() throws Exception { 132 WatchlistConfig config = new WatchlistConfig(new File("/not_exist_path.xml")); 133 ByteArrayOutputStream bs = new ByteArrayOutputStream(2048); 134 PrintWriter pw = new PrintWriter(bs); 135 // Make sure dump still works even watchlist does not exist 136 config.dump(null, pw, null); 137 } 138 139 private static void copyWatchlistConfigXml(Context context, String xmlAsset, File outFile) 140 throws IOException { 141 writeToFile(outFile, readAsset(context, xmlAsset)); 142 } 143 144 private static String readAsset(Context context, String assetPath) throws IOException { 145 final StringBuilder sb = new StringBuilder(); 146 try (BufferedReader br = new BufferedReader( 147 new InputStreamReader( 148 context.getResources().getAssets().open(assetPath)))) { 149 String line; 150 while ((line = br.readLine()) != null) { 151 sb.append(line); 152 sb.append(System.lineSeparator()); 153 } 154 } 155 return sb.toString(); 156 } 157 158 private static void writeToFile(File path, String content) 159 throws IOException { 160 path.getParentFile().mkdirs(); 161 162 try (FileWriter writer = new FileWriter(path)) { 163 writer.write(content); 164 } 165 } 166 } 167