Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47f5c9c5 authored by Annie Meng's avatar Annie Meng
Browse files

Validate restore of NetworkPolicy settings

We want to catch potential exceptions that occur when restoring
NetworkPolicy settings. Here, a DateTimeException can be thrown when
we try to read the recurrence rule from the backup data and cannot
parse a timezone.

We also add GTS tests to validate that these catches persist and that
we don't crash when given unexpected backup data to restore.

Bug: 73942796
Test: gts-tradefed run gts -m GtsBackupHostTestCases -t com.google.android.gts.backup.NetworkPolicyRestoreHostSideTest
Change-Id: I6f6ea09d2fff60b8d704c6160234e6f032321103
(cherry picked from commit 853097ca)
parent 3fbc54e6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -257,7 +257,7 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
    public static NetworkPolicy getNetworkPolicyFromBackup(DataInputStream in) throws IOException,
            BackupUtils.BadVersionException {
        final int version = in.readInt();
        if (version > VERSION_RAPID) {
        if (version < VERSION_INIT || version > VERSION_RAPID) {
            throw new BackupUtils.BadVersionException("Unknown backup version: " + version);
        }

+1 −0
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@ public class RecurrenceRule implements Parcelable {
                start = convertZonedDateTime(BackupUtils.readString(in));
                end = convertZonedDateTime(BackupUtils.readString(in));
                period = convertPeriod(BackupUtils.readString(in));
                break;
            default:
                throw new ProtocolException("Unknown version " + version);
        }
+3 −3
Original line number Diff line number Diff line
@@ -49,12 +49,11 @@ import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.DateTimeException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.zip.CRC32;
@@ -913,7 +912,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                }
                // Only set the policies if there was no error in the restore operation
                networkPolicyManager.setNetworkPolicies(policies);
            } catch (NullPointerException | IOException | BackupUtils.BadVersionException e) {
            } catch (NullPointerException | IOException | BackupUtils.BadVersionException
                    | DateTimeException e) {
                // NPE can be thrown when trying to instantiate a NetworkPolicy
                Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage());
            }