summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunyu Lai <junyulai@google.com>2021-12-10 22:30:19 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-12-10 22:30:19 +0000
commit895f993439b4aff446b9823fec74841a5f940ce8 (patch)
tree2c700b6f0aeb6bfcf4f3eab99408da5c8cf80ff0
parent6d0a6803c919b57d9d4d5b6ce6994911791bef91 (diff)
parent40eb40302d0840a4637ddb264edcff84276e1a7b (diff)
downloadbase-temp-sc-sam.tar.gz
Merge "[MS01] Move NetworkTemplate cloud backup functions to NetworkPolicy" am: 40eb40302dtemp-sc-sam
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1878037 Change-Id: I86a23381b25fba2512c2ad1d3ffeb60d12947202
-rw-r--r--core/java/android/net/NetworkPolicy.java84
-rw-r--r--core/tests/coretests/Android.bp1
-rw-r--r--core/tests/coretests/src/android/net/NetworkPolicyTest.kt56
-rw-r--r--packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java73
4 files changed, 139 insertions, 75 deletions
diff --git a/core/java/android/net/NetworkPolicy.java b/core/java/android/net/NetworkPolicy.java
index 8a0211c2ec2f..70fe5d677b4f 100644
--- a/core/java/android/net/NetworkPolicy.java
+++ b/core/java/android/net/NetworkPolicy.java
@@ -16,11 +16,19 @@
package android.net;
+import static android.net.NetworkStats.METERED_ALL;
+import static android.net.NetworkStats.METERED_YES;
+import static android.net.NetworkTemplate.MATCH_CARRIER;
+import static android.net.NetworkTemplate.MATCH_MOBILE;
+import static android.net.NetworkTemplate.SUBSCRIBER_ID_MATCH_RULE_EXACT;
+
+import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.BackupUtils;
+import android.util.Log;
import android.util.Range;
import android.util.RecurrenceRule;
@@ -42,10 +50,25 @@ import java.util.Objects;
* @hide
*/
public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
+ private static final String TAG = NetworkPolicy.class.getSimpleName();
private static final int VERSION_INIT = 1;
private static final int VERSION_RULE = 2;
private static final int VERSION_RAPID = 3;
+ /**
+ * Initial Version of the NetworkTemplate backup serializer.
+ */
+ private static final int TEMPLATE_BACKUP_VERSION_1_INIT = 1;
+ /**
+ * Version of the NetworkTemplate backup serializer that added carrier template support.
+ */
+ private static final int TEMPLATE_BACKUP_VERSION_2_SUPPORT_CARRIER_TEMPLATE = 2;
+ /**
+ * Latest Version of the NetworkTemplate Backup Serializer.
+ */
+ private static final int TEMPLATE_BACKUP_VERSION_LATEST =
+ TEMPLATE_BACKUP_VERSION_2_SUPPORT_CARRIER_TEMPLATE;
+
public static final int CYCLE_NONE = -1;
public static final long WARNING_DISABLED = -1;
public static final long LIMIT_DISABLED = -1;
@@ -255,7 +278,7 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
DataOutputStream out = new DataOutputStream(baos);
out.writeInt(VERSION_RAPID);
- out.write(template.getBytesForBackup());
+ out.write(getNetworkTemplateBytesForBackup());
cycleRule.writeToStream(out);
out.writeLong(warningBytes);
out.writeLong(limitBytes);
@@ -274,7 +297,7 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
throw new BackupUtils.BadVersionException("Unknown backup version: " + version);
}
- final NetworkTemplate template = NetworkTemplate.getNetworkTemplateFromBackup(in);
+ final NetworkTemplate template = getNetworkTemplateFromBackup(in);
final RecurrenceRule cycleRule;
if (version >= VERSION_RULE) {
cycleRule = new RecurrenceRule(in);
@@ -298,4 +321,61 @@ public class NetworkPolicy implements Parcelable, Comparable<NetworkPolicy> {
return new NetworkPolicy(template, cycleRule, warningBytes, limitBytes, lastWarningSnooze,
lastLimitSnooze, lastRapidSnooze, metered, inferred);
}
+
+ @NonNull
+ private byte[] getNetworkTemplateBytesForBackup() throws IOException {
+ if (!template.isPersistable()) {
+ Log.wtf(TAG, "Trying to backup non-persistable template: " + this);
+ }
+
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final DataOutputStream out = new DataOutputStream(baos);
+
+ out.writeInt(TEMPLATE_BACKUP_VERSION_LATEST);
+
+ out.writeInt(template.getMatchRule());
+ BackupUtils.writeString(out, template.getSubscriberId());
+ BackupUtils.writeString(out, template.getNetworkId());
+ out.writeInt(template.getMeteredness());
+ out.writeInt(template.getSubscriberIdMatchRule());
+
+ return baos.toByteArray();
+ }
+
+ @NonNull
+ private static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
+ throws IOException, BackupUtils.BadVersionException {
+ int version = in.readInt();
+ if (version < TEMPLATE_BACKUP_VERSION_1_INIT || version > TEMPLATE_BACKUP_VERSION_LATEST) {
+ throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
+ }
+
+ int matchRule = in.readInt();
+ final String subscriberId = BackupUtils.readString(in);
+ final String networkId = BackupUtils.readString(in);
+
+ final int metered;
+ final int subscriberIdMatchRule;
+ if (version >= TEMPLATE_BACKUP_VERSION_2_SUPPORT_CARRIER_TEMPLATE) {
+ metered = in.readInt();
+ subscriberIdMatchRule = in.readInt();
+ } else {
+ // For backward compatibility, fill the missing filters from match rules.
+ metered = (matchRule == MATCH_MOBILE
+ || matchRule == NetworkTemplate.MATCH_MOBILE_WILDCARD
+ || matchRule == MATCH_CARRIER) ? METERED_YES : METERED_ALL;
+ subscriberIdMatchRule = SUBSCRIBER_ID_MATCH_RULE_EXACT;
+ }
+
+ try {
+ return new NetworkTemplate(matchRule,
+ subscriberId, new String[]{subscriberId},
+ networkId, metered, NetworkStats.ROAMING_ALL,
+ NetworkStats.DEFAULT_NETWORK_ALL, NetworkTemplate.NETWORK_TYPE_ALL,
+ NetworkTemplate.OEM_MANAGED_ALL, subscriberIdMatchRule);
+ } catch (IllegalArgumentException e) {
+ throw new BackupUtils.BadVersionException(
+ "Restored network template contains unknown match rule " + matchRule, e);
+ }
+ }
}
diff --git a/core/tests/coretests/Android.bp b/core/tests/coretests/Android.bp
index 93e4a294a2cb..05ec00fe84f0 100644
--- a/core/tests/coretests/Android.bp
+++ b/core/tests/coretests/Android.bp
@@ -46,6 +46,7 @@ android_test {
"androidx.test.ext.junit",
"androidx.test.runner",
"androidx.test.rules",
+ "kotlin-test",
"mockito-target-minus-junit4",
"ub-uiautomator",
"platform-test-annotations",
diff --git a/core/tests/coretests/src/android/net/NetworkPolicyTest.kt b/core/tests/coretests/src/android/net/NetworkPolicyTest.kt
new file mode 100644
index 000000000000..d936cad15689
--- /dev/null
+++ b/core/tests/coretests/src/android/net/NetworkPolicyTest.kt
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net
+
+import android.text.format.Time.TIMEZONE_UTC
+import androidx.test.runner.AndroidJUnit4
+import org.junit.Test
+import org.junit.runner.RunWith
+import java.io.ByteArrayInputStream
+import java.io.DataInputStream
+import java.time.ZoneId
+import kotlin.test.assertEquals
+
+private const val TEST_IMSI1 = "TESTIMSI1"
+private const val TEST_SSID1 = "TESTISSID1"
+
+@RunWith(AndroidJUnit4::class)
+class NetworkPolicyTest {
+ @Test
+ fun testTemplateBackupRestore() {
+ assertPolicyBackupRestore(createTestPolicyForTemplate(
+ NetworkTemplate.buildTemplateWifi(TEST_SSID1)))
+ assertPolicyBackupRestore(createTestPolicyForTemplate(
+ NetworkTemplate.buildTemplateMobileAll(TEST_IMSI1)))
+ assertPolicyBackupRestore(createTestPolicyForTemplate(
+ NetworkTemplate.buildTemplateCarrierMetered(TEST_IMSI1)))
+ }
+
+ private fun createTestPolicyForTemplate(template: NetworkTemplate): NetworkPolicy {
+ return NetworkPolicy(template, NetworkPolicy.buildRule(5, ZoneId.of(TIMEZONE_UTC)),
+ NetworkPolicy.WARNING_DISABLED, NetworkPolicy.LIMIT_DISABLED,
+ NetworkPolicy.SNOOZE_NEVER, NetworkPolicy.SNOOZE_NEVER, NetworkPolicy.SNOOZE_NEVER,
+ /*metered*/ false, /*inferred*/ true)
+ }
+
+ private fun assertPolicyBackupRestore(policy: NetworkPolicy) {
+ val bytes = policy.bytesForBackup
+ val stream = DataInputStream(ByteArrayInputStream(bytes))
+ val restored = NetworkPolicy.getNetworkPolicyFromBackup(stream)
+ assertEquals(policy, restored)
+ }
+} \ No newline at end of file
diff --git a/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java b/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java
index c906a13bf41b..8b9c14df7dc8 100644
--- a/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java
+++ b/packages/ConnectivityT/framework-t/src/android/net/NetworkTemplate.java
@@ -44,16 +44,10 @@ import android.os.Parcelable;
import android.telephony.Annotation.NetworkType;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
-import android.util.BackupUtils;
-import android.util.Log;
import com.android.internal.util.ArrayUtils;
import com.android.net.module.util.NetworkIdentityUtils;
-import java.io.ByteArrayOutputStream;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
@@ -69,19 +63,6 @@ import java.util.Objects;
public class NetworkTemplate implements Parcelable {
private static final String TAG = "NetworkTemplate";
- /**
- * Initial Version of the backup serializer.
- */
- public static final int BACKUP_VERSION_1_INIT = 1;
- /**
- * Version of the backup serializer that added carrier template support.
- */
- public static final int BACKUP_VERSION_2_SUPPORT_CARRIER_TEMPLATE = 2;
- /**
- * Current Version of the Backup Serializer.
- */
- private static final int BACKUP_VERSION = BACKUP_VERSION_2_SUPPORT_CARRIER_TEMPLATE;
-
public static final int MATCH_MOBILE = 1;
public static final int MATCH_WIFI = 4;
public static final int MATCH_ETHERNET = 5;
@@ -849,58 +830,4 @@ public class NetworkTemplate implements Parcelable {
return new NetworkTemplate[size];
}
};
-
- public byte[] getBytesForBackup() throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream out = new DataOutputStream(baos);
-
- if (!isPersistable()) {
- Log.wtf(TAG, "Trying to backup non-persistable template: " + this);
- }
-
- out.writeInt(BACKUP_VERSION);
-
- out.writeInt(mMatchRule);
- BackupUtils.writeString(out, mSubscriberId);
- BackupUtils.writeString(out, mNetworkId);
- out.writeInt(mMetered);
- out.writeInt(mSubscriberIdMatchRule);
-
- return baos.toByteArray();
- }
-
- public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
- throws IOException, BackupUtils.BadVersionException {
- int version = in.readInt();
- if (version < BACKUP_VERSION_1_INIT || version > BACKUP_VERSION) {
- throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
- }
-
- int matchRule = in.readInt();
- String subscriberId = BackupUtils.readString(in);
- String networkId = BackupUtils.readString(in);
-
- final int metered;
- final int subscriberIdMatchRule;
- if (version >= BACKUP_VERSION_2_SUPPORT_CARRIER_TEMPLATE) {
- metered = in.readInt();
- subscriberIdMatchRule = in.readInt();
- } else {
- // For backward compatibility, fill the missing filters from match rules.
- metered = (matchRule == MATCH_MOBILE || matchRule == MATCH_MOBILE_WILDCARD
- || matchRule == MATCH_CARRIER) ? METERED_YES : METERED_ALL;
- subscriberIdMatchRule = SUBSCRIBER_ID_MATCH_RULE_EXACT;
- }
-
- try {
- return new NetworkTemplate(matchRule,
- subscriberId, new String[] { subscriberId },
- networkId, metered, NetworkStats.ROAMING_ALL,
- NetworkStats.DEFAULT_NETWORK_ALL, NetworkTemplate.NETWORK_TYPE_ALL,
- NetworkTemplate.OEM_MANAGED_ALL, subscriberIdMatchRule);
- } catch (IllegalArgumentException e) {
- throw new BackupUtils.BadVersionException(
- "Restored network template contains unknown match rule " + matchRule, e);
- }
- }
}