Fix dumpsys network_watchlist NPE when watchlist xml is not ready yet
Bug: 63908748 Test: dumpsys network_watchlist won't cause NPE anymore Change-Id: I4e15516cc3b0efa113a00acc90179f7008fc1525
This commit is contained in:
@@ -161,7 +161,7 @@ class WatchlistConfig {
|
||||
public boolean containsDomain(String domain) {
|
||||
final CrcShaDigests domainDigests = mDomainDigests;
|
||||
if (domainDigests == null) {
|
||||
Slog.wtf(TAG, "domainDigests should not be null");
|
||||
// mDomainDigests is not initialized
|
||||
return false;
|
||||
}
|
||||
// First it does a quick CRC32 check.
|
||||
@@ -177,7 +177,7 @@ class WatchlistConfig {
|
||||
public boolean containsIp(String ip) {
|
||||
final CrcShaDigests ipDigests = mIpDigests;
|
||||
if (ipDigests == null) {
|
||||
Slog.wtf(TAG, "ipDigests should not be null");
|
||||
// mIpDigests is not initialized
|
||||
return false;
|
||||
}
|
||||
// First it does a quick CRC32 check.
|
||||
@@ -234,12 +234,20 @@ class WatchlistConfig {
|
||||
|
||||
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
|
||||
pw.println("Domain CRC32 digest list:");
|
||||
mDomainDigests.crc32Digests.dump(fd, pw, args);
|
||||
if (mDomainDigests != null) {
|
||||
mDomainDigests.crc32Digests.dump(fd, pw, args);
|
||||
}
|
||||
pw.println("Domain SHA256 digest list:");
|
||||
mDomainDigests.sha256Digests.dump(fd, pw, args);
|
||||
if (mDomainDigests != null) {
|
||||
mDomainDigests.sha256Digests.dump(fd, pw, args);
|
||||
}
|
||||
pw.println("Ip CRC32 digest list:");
|
||||
mIpDigests.crc32Digests.dump(fd, pw, args);
|
||||
if (mIpDigests != null) {
|
||||
mIpDigests.crc32Digests.dump(fd, pw, args);
|
||||
}
|
||||
pw.println("Ip SHA256 digest list:");
|
||||
mIpDigests.sha256Digests.dump(fd, pw, args);
|
||||
if (mIpDigests != null) {
|
||||
mIpDigests.sha256Digests.dump(fd, pw, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,12 +33,15 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/**
|
||||
* runtest frameworks-services -c com.android.server.net.watchlist.WatchlistConfigTests
|
||||
*/
|
||||
@@ -117,6 +120,15 @@ public class WatchlistConfigTests {
|
||||
assertEquals(TEST_XML_1_HASH, HexDump.toHexString(config.getWatchlistConfigHash()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWatchlistConfig_testDumpDoesNotCrash() throws Exception {
|
||||
WatchlistConfig config = new WatchlistConfig(new File("/not_exist_path.xml"));
|
||||
ByteArrayOutputStream bs = new ByteArrayOutputStream(2048);
|
||||
PrintWriter pw = new PrintWriter(bs);
|
||||
// Make sure dump still works even watchlist does not exist
|
||||
config.dump(null, pw, null);
|
||||
}
|
||||
|
||||
private static void copyWatchlistConfigXml(Context context, String xmlAsset, File outFile)
|
||||
throws IOException {
|
||||
writeToFile(outFile, readAsset(context, xmlAsset));
|
||||
|
||||
Reference in New Issue
Block a user