summaryrefslogtreecommitdiff
path: root/service/javatests/src/com/android/server/deviceconfig/SimPinReplayManagerTest.java
blob: f56a6ddc51381f82ac208dcb474bfde4c6423c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.android.server.deviceconfig;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.test.filters.SmallTest;

import org.junit.Before;
import org.junit.Test;

@SmallTest
public class SimPinReplayManagerTest {

  private static final String TAG = "SimPinReplayManagerTest";

  // A copy of the hidden field CarrierConfigManager#KEY_STORE_SIM_PIN_FOR_UNATTENDED_REBOOT_BOOL.
  public static final String CARRIER_ENABLE_SIM_PIN_STORAGE_KEY =
      "store_sim_pin_for_unattended_reboot_bool";

  SimPinReplayManager mSimPinReplayManager;
  SubscriptionManager mSubscriptionManager;
  TelephonyManager mTelephonyManager;
  CarrierConfigManager mCarrierConfigManager;

  private Context mContext;

  @Before
  public void setUp() {

    mSubscriptionManager = mock(SubscriptionManager.class);
    mTelephonyManager = mock(TelephonyManager.class);
    mCarrierConfigManager = mock(CarrierConfigManager.class);

    mContext =
        new ContextWrapper(getInstrumentation().getTargetContext()) {
          @Override
          public Object getSystemService(String name) {
            if (name.equals(Context.TELEPHONY_SUBSCRIPTION_SERVICE)) {
              return mSubscriptionManager;
            } else if (name.equals(Context.TELEPHONY_SERVICE)) {
              return mTelephonyManager;
            } else if (name.equals(Context.CARRIER_CONFIG_SERVICE)) {
              return mCarrierConfigManager;
            }
            return super.getSystemService(name);
          }
        };

    mSimPinReplayManager = new SimPinReplayManager(mContext);
  }

  @Test
  public void prepareSimPinReplay_success() {
    Log.i(TAG, "prepareSimPinReplay_success");
    when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
    TelephonyManager subIdManager = mock(TelephonyManager.class);
    when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
    when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
    PersistableBundle config = new PersistableBundle(); // empty carrier config
    when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
        .thenReturn(config);
    when(mTelephonyManager.prepareForUnattendedReboot())
        .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_SUCCESS);

    boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();

    assertTrue(isPrepared);
  }

  @Test
  public void prepareSimPinReplay_noSim() {
    Log.i(TAG, "prepareSimPinReplay_noSim");
    when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {}); // no sim

    boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();

    assertTrue(isPrepared);
  }

  @Test
  public void prepareSimPinReplay_noSimPin() {
    Log.i(TAG, "prepareSimPinReplay_noSimPin");
    when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
    TelephonyManager subIdManager = mock(TelephonyManager.class);
    when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
    when(subIdManager.isIccLockEnabled()).thenReturn(false); // no pin

    boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();

    assertTrue(isPrepared);
  }

  @Test
  public void prepareSimPinReplay_carrierDisableSimPin() {
    Log.i(TAG, "prepareSimPinReplay_carrierDisableSimPin");
    when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
    TelephonyManager subIdManager = mock(TelephonyManager.class);
    when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
    when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
    PersistableBundle config = new PersistableBundle();
    config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, false); // carrier disabled
    when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
        .thenReturn(config);

    boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();

    assertFalse(isPrepared);
  }

  @Test
  public void prepareSimPinReplay_carrierEnabled() {
    Log.i(TAG, "prepareSimPinReplay_carrierEnabled");
    when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
    TelephonyManager subIdManager = mock(TelephonyManager.class);
    when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
    when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
    PersistableBundle config = new PersistableBundle();
    config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, true); // carrier enabled
    when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
        .thenReturn(config);
    when(mTelephonyManager.prepareForUnattendedReboot())
        .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_SUCCESS);

    boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();

    assertTrue(isPrepared);
  }

  @Test
  public void prepareSimPinReplay_prepareError() {
    Log.i(TAG, "prepareSimPinReplay_prepareError");
    when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
    TelephonyManager subIdManager = mock(TelephonyManager.class);
    when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
    when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
    PersistableBundle config = new PersistableBundle();
    config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, true); // carrier enabled
    when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
        .thenReturn(config);
    when(mTelephonyManager.prepareForUnattendedReboot())
        .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_ERROR);

    boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();

    assertFalse(isPrepared);
  }

  @Test
  public void prepareSimPinReplay_preparePinRequired() {
    Log.i(TAG, "prepareSimPinReplay_preparePinRequired");
    when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1}); // has sim
    TelephonyManager subIdManager = mock(TelephonyManager.class);
    when(mTelephonyManager.createForSubscriptionId(1)).thenReturn(subIdManager);
    when(subIdManager.isIccLockEnabled()).thenReturn(true); // has pin
    PersistableBundle config = new PersistableBundle();
    config.putBoolean(CARRIER_ENABLE_SIM_PIN_STORAGE_KEY, true); // carrier enabled
    when(mCarrierConfigManager.getConfigForSubId(1, CARRIER_ENABLE_SIM_PIN_STORAGE_KEY))
        .thenReturn(config);
    when(mTelephonyManager.prepareForUnattendedReboot())
        .thenReturn(TelephonyManager.PREPARE_UNATTENDED_REBOOT_PIN_REQUIRED);

    boolean isPrepared = mSimPinReplayManager.prepareSimPinReplay();

    assertFalse(isPrepared);
  }
}