Home | History | Annotate | Download | only in watchlist
      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.assertNotEquals;
     21 
     22 import android.content.Context;
     23 import android.support.test.InstrumentationRegistry;
     24 import android.support.test.filters.SmallTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 
     27 import com.android.internal.util.HexDump;
     28 
     29 import org.junit.After;
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 
     34 import java.io.BufferedReader;
     35 import java.io.File;
     36 import java.io.FileWriter;
     37 import java.io.IOException;
     38 import java.io.InputStreamReader;
     39 
     40 /**
     41  * runtest frameworks-services -c com.android.server.net.watchlist.WatchlistSettingsTests
     42  */
     43 @RunWith(AndroidJUnit4.class)
     44 @SmallTest
     45 public class WatchlistSettingsTests {
     46 
     47     private static final String TEST_XML_1 = "NetworkWatchlistTest/watchlist_settings_test1.xml";
     48     private static final String TEST_XML_2 = "NetworkWatchlistTest/watchlist_settings_test2.xml";
     49     private static final String HARD_CODED_SECRET_KEY = "1234567890ABCDEF1234567890ABCDEF"
     50             + "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF";
     51 
     52     private Context mContext;
     53     private File mTestXmlFile;
     54 
     55     @Before
     56     public void setUp() throws Exception {
     57         mContext = InstrumentationRegistry.getContext();
     58         mTestXmlFile = new File(mContext.getFilesDir(), "test_settings_config.xml");
     59         mTestXmlFile.delete();
     60     }
     61 
     62     @After
     63     public void tearDown() throws Exception {
     64         mTestXmlFile.delete();
     65     }
     66 
     67     @Test
     68     public void testWatchlistSettings_parsing() throws Exception {
     69         copyWatchlistSettingsXml(mContext, TEST_XML_1, mTestXmlFile);
     70         WatchlistSettings settings = new WatchlistSettings(mTestXmlFile);
     71         assertEquals(HARD_CODED_SECRET_KEY, HexDump.toHexString(settings.getPrivacySecretKey()));
     72         // Try again.
     73         assertEquals(HARD_CODED_SECRET_KEY, HexDump.toHexString(settings.getPrivacySecretKey()));
     74     }
     75 
     76     @Test
     77     public void testWatchlistSettings_parsingWithoutKey() throws Exception {
     78         copyWatchlistSettingsXml(mContext, TEST_XML_2, mTestXmlFile);
     79         WatchlistSettings settings = new WatchlistSettings(mTestXmlFile);
     80         final String tmpKey1 = HexDump.toHexString(settings.getPrivacySecretKey());
     81         assertNotEquals(HARD_CODED_SECRET_KEY, tmpKey1);
     82         assertEquals(96, tmpKey1.length());
     83         // Try again to make sure it's the same.
     84         assertEquals(tmpKey1, HexDump.toHexString(settings.getPrivacySecretKey()));
     85         // Create new settings object again to make sure it can get the new saved key.
     86         settings = new WatchlistSettings(mTestXmlFile);
     87         assertEquals(tmpKey1, HexDump.toHexString(settings.getPrivacySecretKey()));
     88     }
     89 
     90     @Test
     91     public void testWatchlistSettings_noExistingXml() throws Exception {
     92         WatchlistSettings settings = new WatchlistSettings(mTestXmlFile);
     93         final String tmpKey1 = HexDump.toHexString(settings.getPrivacySecretKey());
     94         assertNotEquals(HARD_CODED_SECRET_KEY, tmpKey1);
     95         assertEquals(96, tmpKey1.length());
     96         // Try again to make sure it's the same.
     97         assertEquals(tmpKey1, HexDump.toHexString(settings.getPrivacySecretKey()));
     98         // Create new settings object again to make sure it can get the new saved key.
     99         settings = new WatchlistSettings(mTestXmlFile);
    100         assertEquals(tmpKey1, HexDump.toHexString(settings.getPrivacySecretKey()));
    101         // Delete xml and generate key again, to make sure key is randomly generated.
    102         mTestXmlFile.delete();
    103         settings = new WatchlistSettings(mTestXmlFile);
    104         final String tmpKey2 = HexDump.toHexString(settings.getPrivacySecretKey());
    105         assertNotEquals(HARD_CODED_SECRET_KEY, tmpKey2);
    106         assertNotEquals(tmpKey1, tmpKey2);
    107         assertEquals(96, tmpKey2.length());
    108     }
    109 
    110     private static void copyWatchlistSettingsXml(Context context, String xmlAsset, File outFile)
    111             throws IOException {
    112         writeToFile(outFile, readAsset(context, xmlAsset));
    113     }
    114 
    115     private static String readAsset(Context context, String assetPath) throws IOException {
    116         final StringBuilder sb = new StringBuilder();
    117         try (BufferedReader br = new BufferedReader(
    118                 new InputStreamReader(
    119                         context.getResources().getAssets().open(assetPath)))) {
    120             String line;
    121             while ((line = br.readLine()) != null) {
    122                 sb.append(line);
    123                 sb.append(System.lineSeparator());
    124             }
    125         }
    126         return sb.toString();
    127     }
    128 
    129     private static void writeToFile(File path, String content)
    130             throws IOException {
    131         path.getParentFile().mkdirs();
    132 
    133         try (FileWriter writer = new FileWriter(path)) {
    134             writer.write(content);
    135         }
    136     }
    137 }
    138