• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.server.deviceconfig;
2 
3 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8 
9 import android.content.Context;
10 import android.content.ContextWrapper;
11 import android.os.PersistableBundle;
12 import android.telephony.CarrierConfigManager;
13 import android.telephony.SubscriptionManager;
14 import android.telephony.TelephonyManager;
15 import android.util.Log;
16 
17 import androidx.test.filters.SmallTest;
18 
19 import org.junit.Before;
20 import org.junit.Test;
21 
22 @SmallTest
23 public class SimPinReplayManagerTest {
24 
25   private static final String TAG = "SimPinReplayManagerTest";
26 
27   // A copy of the hidden field CarrierConfigManager#KEY_STORE_SIM_PIN_FOR_UNATTENDED_REBOOT_BOOL.
28   public static final String CARRIER_ENABLE_SIM_PIN_STORAGE_KEY =
29       "store_sim_pin_for_unattended_reboot_bool";
30 
31   SimPinReplayManager mSimPinReplayManager;
32   SubscriptionManager mSubscriptionManager;
33   TelephonyManager mTelephonyManager;
34   CarrierConfigManager mCarrierConfigManager;
35 
36   private Context mContext;
37 
38   @Before
setUp()39   public void setUp() {
40 
41     mSubscriptionManager = mock(SubscriptionManager.class);
42     mTelephonyManager = mock(TelephonyManager.class);
43     mCarrierConfigManager = mock(CarrierConfigManager.class);
44 
45     mContext =
46         new ContextWrapper(getInstrumentation().getTargetContext()) {
47           @Override
48           public Object getSystemService(String name) {
49             if (name.equals(Context.TELEPHONY_SUBSCRIPTION_SERVICE)) {
50               return mSubscriptionManager;
51             } else if (name.equals(Context.TELEPHONY_SERVICE)) {
52               return mTelephonyManager;
53             } else if (name.equals(Context.CARRIER_CONFIG_SERVICE)) {
54               return mCarrierConfigManager;
55             }
56             return super.getSystemService(name);
57           }
58         };
59 
60     mSimPinReplayManager = new SimPinReplayManager(mContext);
61   }
62 
63   @Test
prepareSimPinReplay_success()64   public void prepareSimPinReplay_success() {
65     Log.i(TAG, "prepareSimPinReplay_success");
66     when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
67     TelephonyManager subIdManager = mock(TelephonyManager.class);
68     when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
69     when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
70     PersistableBundle config = new PersistableBundle(); // empty carrier config
71     when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
72         .thenReturn(config);
73     when(mTelephonyManager.prepareForUnattendedReboot())
74         .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_SUCCESS);
75 
76     boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();
77 
78     assertTrue(isPrepared);
79   }
80 
81   @Test
prepareSimPinReplay_noSim()82   public void prepareSimPinReplay_noSim() {
83     Log.i(TAG, "prepareSimPinReplay_noSim");
84     when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {}); // no sim
85 
86     boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();
87 
88     assertTrue(isPrepared);
89   }
90 
91   @Test
prepareSimPinReplay_noSimPin()92   public void prepareSimPinReplay_noSimPin() {
93     Log.i(TAG, "prepareSimPinReplay_noSimPin");
94     when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
95     TelephonyManager subIdManager = mock(TelephonyManager.class);
96     when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
97     when(subIdManager.isIccLockEnabled()).thenReturn(false); // no pin
98 
99     boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();
100 
101     assertTrue(isPrepared);
102   }
103 
104   @Test
prepareSimPinReplay_carrierDisableSimPin()105   public void prepareSimPinReplay_carrierDisableSimPin() {
106     Log.i(TAG, "prepareSimPinReplay_carrierDisableSimPin");
107     when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
108     TelephonyManager subIdManager = mock(TelephonyManager.class);
109     when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
110     when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
111     PersistableBundle config = new PersistableBundle();
112     config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, false); // carrier disabled
113     when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
114         .thenReturn(config);
115 
116     boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();
117 
118     assertFalse(isPrepared);
119   }
120 
121   @Test
prepareSimPinReplay_carrierEnabled()122   public void prepareSimPinReplay_carrierEnabled() {
123     Log.i(TAG, "prepareSimPinReplay_carrierEnabled");
124     when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
125     TelephonyManager subIdManager = mock(TelephonyManager.class);
126     when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
127     when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
128     PersistableBundle config = new PersistableBundle();
129     config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, true); // carrier enabled
130     when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
131         .thenReturn(config);
132     when(mTelephonyManager.prepareForUnattendedReboot())
133         .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_SUCCESS);
134 
135     boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();
136 
137     assertTrue(isPrepared);
138   }
139 
140   @Test
prepareSimPinReplay_prepareError()141   public void prepareSimPinReplay_prepareError() {
142     Log.i(TAG, "prepareSimPinReplay_prepareError");
143     when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
144     TelephonyManager subIdManager = mock(TelephonyManager.class);
145     when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
146     when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
147     PersistableBundle config = new PersistableBundle();
148     config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, true); // carrier enabled
149     when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
150         .thenReturn(config);
151     when(mTelephonyManager.prepareForUnattendedReboot())
152         .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_ERROR);
153 
154     boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();
155 
156     assertFalse(isPrepared);
157   }
158 
159   @Test
prepareSimPinReplay_preparePinRequired()160   public void prepareSimPinReplay_preparePinRequired() {
161     Log.i(TAG, "prepareSimPinReplay_preparePinRequired");
162     when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
163     TelephonyManager subIdManager = mock(TelephonyManager.class);
164     when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
165     when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
166     PersistableBundle config = new PersistableBundle();
167     config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, true); // carrier enabled
168     when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
169         .thenReturn(config);
170     when(mTelephonyManager.prepareForUnattendedReboot())
171         .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_PIN_REQUIRED);
172 
173     boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();
174 
175     assertFalse(isPrepared);
176   }
177 }
178