23
23
import static android .net .NetworkCapabilities .TRANSPORT_WIFI ;
24
24
import static android .net .vcn .VcnManager .VCN_STATUS_CODE_ACTIVE ;
25
25
import static android .net .vcn .VcnManager .VCN_STATUS_CODE_SAFE_MODE ;
26
+ import static android .telephony .SubscriptionManager .INVALID_SUBSCRIPTION_ID ;
26
27
import static android .telephony .TelephonyManager .CARRIER_PRIVILEGE_STATUS_HAS_ACCESS ;
27
28
import static android .telephony .TelephonyManager .CARRIER_PRIVILEGE_STATUS_NO_ACCESS ;
28
29
@@ -276,7 +277,6 @@ private void setupMockedCarrierPrivilege(boolean isPrivileged, String pkg) {
276
277
@ Test
277
278
public void testSystemReady () throws Exception {
278
279
mVcnMgmtSvc .systemReady ();
279
- mTestLooper .dispatchAll ();
280
280
281
281
verify (mConnMgr ).registerNetworkProvider (any (VcnNetworkProvider .class ));
282
282
verify (mSubscriptionTracker ).register ();
@@ -494,8 +494,10 @@ public void testTelephonyNetworkTrackerCallbackStopsInstances() throws Exception
494
494
mVcnMgmtSvc .addVcnUnderlyingNetworkPolicyListener (mMockPolicyListener );
495
495
496
496
triggerSubscriptionTrackerCbAndGetSnapshot (null , Collections .emptySet ());
497
- mTestLooper .dispatchAll ();
498
497
498
+ // Verify teardown after delay
499
+ mTestLooper .moveTimeForward (VcnManagementService .CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS );
500
+ mTestLooper .dispatchAll ();
499
501
verify (vcn ).teardownAsynchronously ();
500
502
verify (mMockPolicyListener ).onPolicyChanged ();
501
503
}
@@ -521,6 +523,92 @@ public void testTelephonyNetworkTrackerCallbackSwitchToNewSubscriptionImmediatel
521
523
assertEquals (0 , mVcnMgmtSvc .getAllVcns ().size ());
522
524
}
523
525
526
+ /**
527
+ * Tests an intermediate state where carrier privileges are marked as lost before active data
528
+ * subId changes during a SIM ejection.
529
+ *
530
+ * <p>The expected outcome is that the VCN is torn down after a delay, as opposed to
531
+ * immediately.
532
+ */
533
+ @ Test
534
+ public void testTelephonyNetworkTrackerCallbackLostCarrierPrivilegesBeforeActiveDataSubChanges ()
535
+ throws Exception {
536
+ setupActiveSubscription (TEST_UUID_2 );
537
+
538
+ final TelephonySubscriptionTrackerCallback cb = getTelephonySubscriptionTrackerCallback ();
539
+ final Vcn vcn = startAndGetVcnInstance (TEST_UUID_2 );
540
+
541
+ // Simulate privileges lost
542
+ triggerSubscriptionTrackerCbAndGetSnapshot (
543
+ TEST_SUBSCRIPTION_ID ,
544
+ TEST_UUID_2 ,
545
+ Collections .emptySet (),
546
+ Collections .emptyMap (),
547
+ false /* hasCarrierPrivileges */ );
548
+
549
+ // Verify teardown after delay
550
+ mTestLooper .moveTimeForward (VcnManagementService .CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS );
551
+ mTestLooper .dispatchAll ();
552
+ verify (vcn ).teardownAsynchronously ();
553
+ }
554
+
555
+ @ Test
556
+ public void testTelephonyNetworkTrackerCallbackSimSwitchesDoNotKillVcnInstances ()
557
+ throws Exception {
558
+ setupActiveSubscription (TEST_UUID_2 );
559
+
560
+ final TelephonySubscriptionTrackerCallback cb = getTelephonySubscriptionTrackerCallback ();
561
+ final Vcn vcn = startAndGetVcnInstance (TEST_UUID_2 );
562
+
563
+ // Simulate SIM unloaded
564
+ triggerSubscriptionTrackerCbAndGetSnapshot (
565
+ INVALID_SUBSCRIPTION_ID ,
566
+ null /* activeDataSubscriptionGroup */ ,
567
+ Collections .emptySet (),
568
+ Collections .emptyMap (),
569
+ false /* hasCarrierPrivileges */ );
570
+
571
+ // Simulate new SIM loaded right during teardown delay.
572
+ mTestLooper .moveTimeForward (
573
+ VcnManagementService .CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS / 2 );
574
+ mTestLooper .dispatchAll ();
575
+ triggerSubscriptionTrackerCbAndGetSnapshot (TEST_UUID_2 , Collections .singleton (TEST_UUID_2 ));
576
+
577
+ // Verify that even after the full timeout duration, the VCN instance is not torn down
578
+ mTestLooper .moveTimeForward (VcnManagementService .CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS );
579
+ mTestLooper .dispatchAll ();
580
+ verify (vcn , never ()).teardownAsynchronously ();
581
+ }
582
+
583
+ @ Test
584
+ public void testTelephonyNetworkTrackerCallbackDoesNotKillNewVcnInstances () throws Exception {
585
+ setupActiveSubscription (TEST_UUID_2 );
586
+
587
+ final TelephonySubscriptionTrackerCallback cb = getTelephonySubscriptionTrackerCallback ();
588
+ final Vcn oldInstance = startAndGetVcnInstance (TEST_UUID_2 );
589
+
590
+ // Simulate SIM unloaded
591
+ triggerSubscriptionTrackerCbAndGetSnapshot (null , Collections .emptySet ());
592
+
593
+ // Config cleared, SIM reloaded & config re-added right before teardown delay, staring new
594
+ // vcnInstance.
595
+ mTestLooper .moveTimeForward (
596
+ VcnManagementService .CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS / 2 );
597
+ mTestLooper .dispatchAll ();
598
+ mVcnMgmtSvc .clearVcnConfig (TEST_UUID_2 , TEST_PACKAGE_NAME );
599
+ triggerSubscriptionTrackerCbAndGetSnapshot (TEST_UUID_2 , Collections .singleton (TEST_UUID_2 ));
600
+ final Vcn newInstance = startAndGetVcnInstance (TEST_UUID_2 );
601
+
602
+ // Verify that new instance was different, and the old one was torn down
603
+ assertTrue (oldInstance != newInstance );
604
+ verify (oldInstance ).teardownAsynchronously ();
605
+
606
+ // Verify that even after the full timeout duration, the new VCN instance is not torn down
607
+ mTestLooper .moveTimeForward (VcnManagementService .CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS );
608
+ mTestLooper .dispatchAll ();
609
+ verify (newInstance , never ()).teardownAsynchronously ();
610
+ }
611
+
524
612
@ Test
525
613
public void testPackageChangeListenerRegistered () throws Exception {
526
614
verify (mMockContext ).registerReceiver (any (BroadcastReceiver .class ), argThat (filter -> {
@@ -910,8 +998,6 @@ private void setupSubscriptionAndStartVcn(int subId, ParcelUuid subGrp, boolean
910
998
private void setupSubscriptionAndStartVcn (
911
999
int subId , ParcelUuid subGrp , boolean isVcnActive , boolean hasCarrierPrivileges ) {
912
1000
mVcnMgmtSvc .systemReady ();
913
- mTestLooper .dispatchAll ();
914
-
915
1001
triggerSubscriptionTrackerCbAndGetSnapshot (
916
1002
subGrp ,
917
1003
Collections .singleton (subGrp ),
@@ -1007,7 +1093,6 @@ public void testGetUnderlyingNetworkPolicyVcnWifi_safeMode() throws Exception {
1007
1093
1008
1094
private void setupTrackedCarrierWifiNetwork (NetworkCapabilities caps ) {
1009
1095
mVcnMgmtSvc .systemReady ();
1010
- mTestLooper .dispatchAll ();
1011
1096
1012
1097
final ArgumentCaptor <NetworkCallback > captor =
1013
1098
ArgumentCaptor .forClass (NetworkCallback .class );
@@ -1252,14 +1337,15 @@ public void testRegisterVcnStatusCallback_VcnInactive() throws Exception {
1252
1337
true /* isActive */ ,
1253
1338
true /* hasCarrierPrivileges */ );
1254
1339
1255
- // VCN is currently active. Lose carrier privileges for TEST_PACKAGE so the VCN goes
1256
- // inactive.
1340
+ // VCN is currently active. Lose carrier privileges for TEST_PACKAGE and hit teardown
1341
+ // timeout so the VCN goes inactive.
1257
1342
final TelephonySubscriptionSnapshot snapshot =
1258
1343
triggerSubscriptionTrackerCbAndGetSnapshot (
1259
1344
TEST_UUID_1 ,
1260
1345
Collections .singleton (TEST_UUID_1 ),
1261
1346
Collections .singletonMap (TEST_SUBSCRIPTION_ID , TEST_UUID_1 ),
1262
1347
false /* hasCarrierPrivileges */ );
1348
+ mTestLooper .moveTimeForward (VcnManagementService .CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS );
1263
1349
mTestLooper .dispatchAll ();
1264
1350
1265
1351
// Giving TEST_PACKAGE privileges again will restart the VCN (which will indicate ACTIVE
0 commit comments