Supported traffic descriptor based network capability

Added traffic descriptor based network capability support.
Allowed multiple network requests having same TD based
capability, but with different differentiators.

Disallowed any real package name to be passed as app id.

Fix: 214399881
Test: atest FrameworksTelephonyTests
Change-Id: Ie1b17b10fd52400b709f8376ec971fc49a58b037
diff --git a/tests/tests/telephony/current/src/android/telephony/cts/DataCallResponseTest.java b/tests/tests/telephony/current/src/android/telephony/cts/DataCallResponseTest.java
index 6b8499d..d874280 100644
--- a/tests/tests/telephony/current/src/android/telephony/cts/DataCallResponseTest.java
+++ b/tests/tests/telephony/current/src/android/telephony/cts/DataCallResponseTest.java
@@ -68,7 +68,11 @@
                 .setMappedHplmnSliceServiceType(TEST_HPLMN_SLICE_SERVICE_TYPE)
                 .build();
     private static final String DNN = "DNN";
-    private static final byte[] OS_APP_ID = {1, 2, 3, 4};
+    // 97a498e3fc925c9489860333d06e4e470a454e5445525052495345.
+    // [OsAppId.ANDROID_OS_ID, "ENTERPRISE", 1]
+    private static final byte[] OS_APP_ID = {-105, -92, -104, -29, -4, -110, 92,
+            -108, -119, -122, 3, 51, -48, 110, 78, 71, 10, 69, 78, 84, 69,
+            82, 80, 82, 73, 83, 69};
     private static final List<TrafficDescriptor> TRAFFIC_DESCRIPTORS =
             Arrays.asList(new TrafficDescriptor(DNN, OS_APP_ID));
 
diff --git a/tests/tests/telephony/current/src/android/telephony/cts/DataProfileTest.java b/tests/tests/telephony/current/src/android/telephony/cts/DataProfileTest.java
index 6feec7f..da2e01c 100644
--- a/tests/tests/telephony/current/src/android/telephony/cts/DataProfileTest.java
+++ b/tests/tests/telephony/current/src/android/telephony/cts/DataProfileTest.java
@@ -216,7 +216,11 @@
                 .setAlwaysOn(IS_ALWAYS_ON)
                 .build();
 
-        byte[] osAppId = {1, 2, 3, 4};
+        // 97a498e3fc925c9489860333d06e4e470a454e5445525052495345.
+        // [OsAppId.ANDROID_OS_ID, "ENTERPRISE", 1]
+        byte[] osAppId = {-105, -92, -104, -29, -4, -110, 92,
+                -108, -119, -122, 3, 51, -48, 110, 78, 71, 10, 69, 78, 84, 69,
+                82, 80, 82, 73, 83, 69};
         TrafficDescriptor td = new TrafficDescriptor.Builder()
                 .setDataNetworkName(APN)
                 .setOsAppId(osAppId)
@@ -267,7 +271,11 @@
 
     @Test
     public void testGetTrafficDescriptor() {
-        byte[] osAppId = {1, 2, 3, 4};
+        // 97a498e3fc925c9489860333d06e4e470a454e5445525052495345.
+        // [OsAppId.ANDROID_OS_ID, "ENTERPRISE", 1]
+        byte[] osAppId = {-105, -92, -104, -29, -4, -110, 92,
+                -108, -119, -122, 3, 51, -48, 110, 78, 71, 10, 69, 78, 84, 69,
+                82, 80, 82, 73, 83, 69};
         TrafficDescriptor td = new TrafficDescriptor.Builder()
                 .setDataNetworkName(APN)
                 .setOsAppId(osAppId)
@@ -282,7 +290,11 @@
 
     @Test
     public void testNullApnSetting() {
-        byte[] osAppId = {1, 2, 3, 4};
+        // 97a498e3fc925c9489860333d06e4e470a454e5445525052495345.
+        // [OsAppId.ANDROID_OS_ID, "ENTERPRISE", 1]
+        byte[] osAppId = {-105, -92, -104, -29, -4, -110, 92,
+                -108, -119, -122, 3, 51, -48, 110, 78, 71, 10, 69, 78, 84, 69,
+                82, 80, 82, 73, 83, 69};
         TrafficDescriptor td = new TrafficDescriptor.Builder()
                 .setDataNetworkName(APN)
                 .setOsAppId(osAppId)
diff --git a/tests/tests/telephony/current/src/android/telephony/cts/TrafficDescriptorTest.java b/tests/tests/telephony/current/src/android/telephony/cts/TrafficDescriptorTest.java
index 4431520..a5b256c 100644
--- a/tests/tests/telephony/current/src/android/telephony/cts/TrafficDescriptorTest.java
+++ b/tests/tests/telephony/current/src/android/telephony/cts/TrafficDescriptorTest.java
@@ -18,14 +18,34 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.junit.Assert.assertThrows;
+
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
 import android.os.Parcel;
 import android.telephony.data.TrafficDescriptor;
+import android.telephony.data.TrafficDescriptor.OsAppId;
 
+import androidx.test.InstrumentationRegistry;
+
+import org.junit.Before;
 import org.junit.Test;
 
 public class TrafficDescriptorTest {
     private static final String DNN = "DNN";
-    private static final byte[] OS_APP_ID = {1, 2, 3, 4};
+    // 97a498e3fc925c9489860333d06e4e470a454e5445525052495345.
+    // [OsAppId.ANDROID_OS_ID, "ENTERPRISE", 1]
+    private static final byte[] OS_APP_ID = {-105, -92, -104, -29, -4, -110, 92,
+            -108, -119, -122, 3, 51, -48, 110, 78, 71, 10, 69, 78, 84, 69,
+            82, 80, 82, 73, 83, 69};
+
+    private PackageManager mPackageManager;
+
+    @Before
+    public void setUp() {
+        mPackageManager = InstrumentationRegistry.getInstrumentation()
+                .getContext().getPackageManager();
+    }
 
     @Test
     public void testConstructorAndGetters() {
@@ -44,8 +64,7 @@
     @Test
     public void testNotEquals() {
         TrafficDescriptor td = new TrafficDescriptor(DNN, OS_APP_ID);
-        byte[] notOsAppId = {5, 6, 7, 8};
-        TrafficDescriptor notEqualsTd = new TrafficDescriptor("NOT_DNN", notOsAppId);
+        TrafficDescriptor notEqualsTd = new TrafficDescriptor("NOT_DNN", OS_APP_ID);
         assertThat(td).isNotEqualTo(notEqualsTd);
         assertThat(td).isNotEqualTo(null);
     }
@@ -71,4 +90,19 @@
         assertThat(td.getDataNetworkName()).isEqualTo(DNN);
         assertThat(td.getOsAppId()).isEqualTo(OS_APP_ID);
     }
+
+    // The purpose of this test is to ensure that no real package names are used as app id.
+    // Traffic descriptor should throw exception for any app id not in the allowed list.
+    @Test
+    public void testRealAppIdThrowException() {
+        for (PackageInfo packageInfo : mPackageManager.getInstalledPackages(0 /*flags*/)) {
+            OsAppId osAppId = new OsAppId(OsAppId.ANDROID_OS_ID, packageInfo.packageName, 1);
+            // IllegalArgumentException is expected when using a real package name as app id.
+            assertThrows(IllegalArgumentException.class,
+                    () -> new TrafficDescriptor.Builder()
+                            .setDataNetworkName(DNN)
+                            .setOsAppId(osAppId.getBytes())
+                            .build());
+        }
+    }
 }