Merge "Handle getNetworkWatchlistConfigHash() without config case" into pi-dev

This commit is contained in:
Ricky Wai
2018-04-03 19:32:27 +00:00
committed by Android (Google) Code Review
6 changed files with 26 additions and 8 deletions

View 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(

View 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);
}

View 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;

View 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 + " < ?";

View File

@@ -86,7 +86,7 @@ class WatchlistSettings {
}
}
public void reloadSettings() {
private void reloadSettings() {
if (!mXmlFile.exists()) {
// No settings config
return;

View 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,12 +115,18 @@ 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"));