OSDN Git Service

Handle getNetworkWatchlistConfigHash() without config case
authorRicky Wai <rickywai@google.com>
Thu, 29 Mar 2018 10:47:27 +0000 (11:47 +0100)
committerRicky Wai <rickywai@google.com>
Tue, 3 Apr 2018 10:38:55 +0000 (10:38 +0000)
Follow ConnectivityManager.getNetworkWatchlistConfigHash() javadoc,
it should return null when watchlist config file does not exist

Bug: 76212923
Test: NULL
Change-Id: Ice2de0732cb07f5159d1e068fb8f95b979191d1a

services/core/java/com/android/server/net/watchlist/ReportEncoder.java
services/core/java/com/android/server/net/watchlist/WatchlistConfig.java
services/core/java/com/android/server/net/watchlist/WatchlistLoggingHandler.java
services/core/java/com/android/server/net/watchlist/WatchlistReportDbHelper.java
services/core/java/com/android/server/net/watchlist/WatchlistSettings.java
services/tests/servicestests/src/com/android/server/net/watchlist/WatchlistConfigTests.java

index 2a8f4d5..a482e05 100644 (file)
@@ -49,6 +49,7 @@ class ReportEncoder {
      * Apply DP on watchlist results, and generate a serialized watchlist report ready to store
      * in DropBox.
      */
+    @Nullable
     static byte[] encodeWatchlistReport(WatchlistConfig config, byte[] userSecret,
             List<String> appDigestList, WatchlistReportDbHelper.AggregatedResult aggregatedResult) {
         Map<String, Boolean> resultMap = PrivacyUtils.createDpEncodedReportMap(
index d793842..8352ca6 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.android.server.net.watchlist;
 
+import android.annotation.Nullable;
 import android.os.FileUtils;
 import android.util.AtomicFile;
 import android.util.Log;
@@ -55,9 +56,6 @@ class WatchlistConfig {
     private static final String NETWORK_WATCHLIST_DB_FOR_TEST_PATH =
             "/data/misc/network_watchlist/network_watchlist_for_test.xml";
 
-    // Hash for null / unknown config, a 32 byte array filled with content 0x00
-    private static final byte[] UNKNOWN_CONFIG_HASH = new byte[32];
-
     private static class XmlTags {
         private static final String WATCHLIST_CONFIG = "watchlist-config";
         private static final String SHA256_DOMAIN = "sha256-domain";
@@ -228,16 +226,21 @@ class WatchlistConfig {
         return mIsSecureConfig;
     }
 
+    @Nullable
+    /**
+     * Get watchlist config SHA-256 digest.
+     * Return null if watchlist config does not exist.
+     */
     public byte[] getWatchlistConfigHash() {
         if (!mXmlFile.exists()) {
-            return UNKNOWN_CONFIG_HASH;
+            return null;
         }
         try {
             return DigestUtils.getSha256Hash(mXmlFile);
         } catch (IOException | NoSuchAlgorithmException e) {
             Log.e(TAG, "Unable to get watchlist config hash", e);
         }
-        return UNKNOWN_CONFIG_HASH;
+        return null;
     }
 
     /**
@@ -271,8 +274,10 @@ class WatchlistConfig {
     }
 
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
-        pw.println("Watchlist config hash: " + HexDump.toHexString(getWatchlistConfigHash()));
+        final byte[] hash = getWatchlistConfigHash();
+        pw.println("Watchlist config hash: " + (hash != null ? HexDump.toHexString(hash) : null));
         pw.println("Domain CRC32 digest list:");
+        // mDomainDigests won't go from non-null to null so it's safe
         if (mDomainDigests != null) {
             mDomainDigests.crc32Digests.dump(fd, pw, args);
         }
@@ -281,6 +286,7 @@ class WatchlistConfig {
             mDomainDigests.sha256Digests.dump(fd, pw, args);
         }
         pw.println("Ip CRC32 digest list:");
+        // mIpDigests won't go from non-null to null so it's safe
         if (mIpDigests != null) {
             mIpDigests.crc32Digests.dump(fd, pw, args);
         }
index b331b9c..864ce5d 100644 (file)
@@ -346,6 +346,7 @@ class WatchlistLoggingHandler extends Handler {
      * @param ipAddresses Ip address that you want to search in watchlist.
      * @return Ip address that exists in watchlist, null if it does not match anything.
      */
+    @Nullable
     private String searchIpInWatchlist(String[] ipAddresses) {
         for (String ipAddress : ipAddresses) {
             if (isIpInWatchlist(ipAddress)) {
@@ -377,6 +378,7 @@ class WatchlistLoggingHandler extends Handler {
      * @param host Host that we want to search.
      * @return Domain that exists in watchlist, null if it does not match anything.
      */
+    @Nullable
     private String searchAllSubDomainsInWatchlist(String host) {
         if (host == null) {
             return null;
@@ -392,6 +394,7 @@ class WatchlistLoggingHandler extends Handler {
 
     /** Get all sub-domains in a host */
     @VisibleForTesting
+    @Nullable
     static String[] getAllSubDomains(String host) {
         if (host == null) {
             return null;
index 632ab81..c69934a 100644 (file)
@@ -144,6 +144,7 @@ class WatchlistReportDbHelper extends SQLiteOpenHelper {
      * Aggregate all records in database before input timestamp, and return a
      * rappor encoded result.
      */
+    @Nullable
     public AggregatedResult getAggregatedRecords(long untilTimestamp) {
         final String selectStatement = WhiteListReportContract.TIMESTAMP + " < ?";
 
index e20a510..c2f3ba0 100644 (file)
@@ -86,7 +86,7 @@ class WatchlistSettings {
         }
     }
 
-    public void reloadSettings() {
+    private void reloadSettings() {
         if (!mXmlFile.exists()) {
             // No settings config
             return;
index 654acc2..678f018 100644 (file)
@@ -19,6 +19,7 @@ package com.android.server.net.watchlist;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
 
 import android.content.Context;
 import android.support.test.InstrumentationRegistry;
@@ -114,13 +115,19 @@ public class WatchlistConfigTests {
     }
 
     @Test
-    public void testWatchlistConfig_getWatchlistConfigHash() throws Exception {
+    public void testWatchlistConfig_getWatchlistConfigHash_hasConfig() throws Exception {
         copyWatchlistConfigXml(mContext, TEST_XML_1, mTestXmlFile);
         WatchlistConfig config = new WatchlistConfig(mTestXmlFile);
         assertEquals(TEST_XML_1_HASH, HexDump.toHexString(config.getWatchlistConfigHash()));
     }
 
     @Test
+    public void testWatchlistConfig_getWatchlistConfigHash_withoutConfig() throws Exception {
+        WatchlistConfig config = new WatchlistConfig(mTestXmlFile);
+        assertNull(config.getWatchlistConfigHash());
+    }
+
+    @Test
     public void testWatchlistConfig_testDumpDoesNotCrash() throws Exception {
         WatchlistConfig config = new WatchlistConfig(new File("/not_exist_path.xml"));
         ByteArrayOutputStream bs = new ByteArrayOutputStream(2048);