Detect discrepancies in subIds

The subIds in network capabilities are stored both in
the network specifier (for telephony) or transportInfo (for
wifi), and in a separate field in NC, which are seeded
differently. These should in principle never diverge if there
is no bug in the network factories code, but since this code
is OEM-specific, it's very hard to know whether it's the case.

Historically CarrierPrivilegeAuthenticator has been using the
specifier / info to find out about the sub id, but reading
from NetworkCapabilities#getSubscriptionIds() would be both
nicer and more future-proof. Log a terrible failure in case
they differ, so we can confidently switch to the latter if
we don't see any such log.

Change-Id: Ifdf67318ceb881349e322591632c18a4dbf218e1
Test: NetworkAgentTest
diff --git a/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java b/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java
index ebf6193..219d0ff 100644
--- a/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java
+++ b/service/src/com/android/server/connectivity/CarrierPrivilegeAuthenticator.java
@@ -224,6 +224,17 @@
         } else {
             subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
         }
+        if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
+                && !networkCapabilities.getSubscriptionIds().contains(subId)) {
+            // Ideally, the code above should just use networkCapabilities.getSubscriptionIds()
+            // for simplicity and future-proofing. However, this is not the historical behavior,
+            // and there is no enforcement that they do not differ, so log a terrible failure if
+            // they do not match to gain confidence this never happens.
+            // TODO : when there is confidence that this never happens, rewrite the code above
+            // with NetworkCapabilities#getSubscriptionIds.
+            Log.wtf(TAG, "NetworkCapabilities subIds are inconsistent between "
+                    + "specifier/transportInfo and mSubIds : " + networkCapabilities);
+        }
         if (SubscriptionManager.INVALID_SUBSCRIPTION_ID == subId) return false;
         return callingUid == getCarrierServiceUidForSubId(subId);
     }