Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7215585b authored by Chen Xu's avatar Chen Xu Committed by Android (Google) Code Review
Browse files

Merge "First Unit test on UiccCardApplication" into nyc-dev

parents 46428a1d 4145d45a
Loading
Loading
Loading
Loading
+23 −5
Original line number Diff line number Diff line
@@ -72,10 +72,10 @@ public class SimulatedCommands extends BaseCommands
    }

    private final static SimLockState INITIAL_LOCK_STATE = SimLockState.NONE;
    private final static String DEFAULT_SIM_PIN_CODE = "1234";
    public final static String DEFAULT_SIM_PIN_CODE = "1234";
    private final static String SIM_PUK_CODE = "12345678";
    private final static SimFdnState INITIAL_FDN_STATE = SimFdnState.NONE;
    private final static String DEFAULT_SIM_PIN2_CODE = "5678";
    public final static String DEFAULT_SIM_PIN2_CODE = "5678";
    private final static String SIM_PUK2_CODE = "87654321";
    public final static String FAKE_LONG_NAME = "Fake long name";
    public final static String FAKE_SHORT_NAME = "Fake short name";
@@ -84,6 +84,7 @@ public class SimulatedCommands extends BaseCommands
    public final static String FAKE_IMEISV = "99";
    public final static String FAKE_ESN = "1234";
    public final static String FAKE_MEID = "1234";
    public final static int DEFAULT_PIN1_ATTEMPT = 5;

    private String mImei;
    private String mImeiSv;
@@ -97,6 +98,7 @@ public class SimulatedCommands extends BaseCommands
    int mPinUnlockAttempts;
    int mPukUnlockAttempts;
    String mPinCode;
    int mPin1attemptsRemaining = DEFAULT_PIN1_ATTEMPT;
    SimFdnState mSimFdnEnabledState;
    boolean mSimFdnEnabled;
    int mPin2UnlockAttempts;
@@ -1762,7 +1764,17 @@ public class SimulatedCommands extends BaseCommands

    @Override
    public void supplyIccPinForApp(String pin, String aid, Message response) {
        unimplemented(response);
        SimulatedCommandsVerifier.getInstance().supplyIccPinForApp(pin, aid, response);
        if (mPinCode != null && mPinCode.equals(pin)) {
            resultSuccess(response, null);
            return;
        }

        Rlog.i(LOG_TAG, "[SimCmd] supplyIccPinForApp: pin failed!");
        CommandException ex = new CommandException(
                CommandException.Error.PASSWORD_INCORRECT);
        resultFail(response, new int[]{
                (--mPin1attemptsRemaining < 0) ? 0 : mPin1attemptsRemaining}, ex);
    }

    @Override
@@ -1782,7 +1794,9 @@ public class SimulatedCommands extends BaseCommands

    @Override
    public void changeIccPinForApp(String oldPin, String newPin, String aidPtr, Message response) {
        unimplemented(response);
        SimulatedCommandsVerifier.getInstance().changeIccPinForApp(oldPin, newPin, aidPtr,
                response);
        changeIccPin(oldPin, newPin, response);
    }

    @Override
@@ -2029,6 +2043,10 @@ public class SimulatedCommands extends BaseCommands
        mChannelId = channelId;
    }

    public void setPin1RemainingAttempt(int pin1attemptsRemaining) {
        mPin1attemptsRemaining = pin1attemptsRemaining;
    }

    private AtomicBoolean mAllowed = new AtomicBoolean(false);

    @Override
+3 −3
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ public class UiccCardApplication {
    private RegistrantList mPinLockedRegistrants = new RegistrantList();
    private RegistrantList mNetworkLockedRegistrants = new RegistrantList();

    UiccCardApplication(UiccCard uiccCard,
    public UiccCardApplication(UiccCard uiccCard,
                        IccCardApplicationStatus as,
                        Context c,
                        CommandsInterface ci) {
@@ -115,7 +115,7 @@ public class UiccCardApplication {
        mCi.registerForNotAvailable(mHandler, EVENT_RADIO_UNAVAILABLE, null);
    }

    void update (IccCardApplicationStatus as, Context c, CommandsInterface ci) {
    public void update (IccCardApplicationStatus as, Context c, CommandsInterface ci) {
        synchronized (mLock) {
            if (mDestroyed) {
                loge("Application updated after destroyed! Fix me!");
@@ -207,7 +207,7 @@ public class UiccCardApplication {
    }

    /** Assumes mLock is held. */
    void queryFdn() {
    public void queryFdn() {
        //This shouldn't change run-time. So needs to be called only once.
        int serviceClassX;

+226 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.internal.telephony;
import android.os.AsyncResult;
import android.os.HandlerThread;

import com.android.internal.telephony.test.SimulatedCommands;
import com.android.internal.telephony.uicc.IccCardApplicationStatus;
import com.android.internal.telephony.uicc.UiccCard;
import com.android.internal.telephony.uicc.UiccCardApplication;
import com.android.internal.telephony.uicc.IccCardStatus;

import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import org.junit.Test;
import org.mockito.Mock;
import android.test.suitebuilder.annotation.SmallTest;
import android.os.Handler;
import android.os.Message;

public class UiccCardApplicationTest extends TelephonyTest {
    private UiccCardApplication mUiccCardApplication;
    @Mock
    private IccCardApplicationStatus mUiccCardAppStatus;
    @Mock
    private UiccCard mUiccCard;
    private Handler mHandler;
    private UiccCardAppTestHandlerThread mTestHandlerThread;
    private int mAttemptsRemaining = -1;
    private CommandException mException = null;
    private static final int UICCCARDAPP_UPDATE_EVENT = 1;
    private static final int UICCCARDAPP_ENABLE_FDN_EVENT = 2;
    private static final int UICCCARDAPP_ENABLE_LOCK_EVENT = 3;
    private static final int UICCCARDAPP_CHANGE_PSW_EVENT = 4;
    private static final int UICCCARDAPP_SUPPLY_PIN_EVENT = 5;
    private class UiccCardAppTestHandlerThread extends HandlerThread {

        private UiccCardAppTestHandlerThread(String name) {
            super(name);
        }
        @Override
        public void onLooperPrepared() {
            mUiccCardApplication = new UiccCardApplication(mUiccCard, mUiccCardAppStatus,
                    mContext, mSimulatedCommands);
            mHandler = new Handler(mTestHandlerThread.getLooper()) {
                @Override
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case UICCCARDAPP_UPDATE_EVENT:
                            logd("Update UiccCardApplication Status");
                            mUiccCardApplication.update(mUiccCardAppStatus,
                                    mContext,
                                    mSimulatedCommands);
                            setReady(true);
                            break;
                        case UICCCARDAPP_SUPPLY_PIN_EVENT:
                        case UICCCARDAPP_CHANGE_PSW_EVENT:
                        case UICCCARDAPP_ENABLE_LOCK_EVENT:
                        case UICCCARDAPP_ENABLE_FDN_EVENT:
                            mAttemptsRemaining = msg.arg1;
                            mException = (CommandException) ((AsyncResult) msg.obj).exception;
                            if (mAttemptsRemaining != -1) {
                                logd("remaining Attempt:" + mAttemptsRemaining);
                            }
                            setReady(true);
                            break;
                        default:
                            logd("Unknown Event " + msg.what);
                    }
                }
            };
            setReady(true);
        }
    }

    @Before
    public void setUp() throws Exception {
        super.setUp(this.getClass().getSimpleName());
        //set initial state of app status
        mUiccCardAppStatus.app_type = IccCardApplicationStatus.AppType.APPTYPE_SIM;
        mUiccCardAppStatus.aid = TAG;
        mUiccCardAppStatus.app_state = IccCardApplicationStatus.AppState.APPSTATE_PIN;
        mUiccCardAppStatus.pin1 = IccCardStatus.PinState.PINSTATE_ENABLED_NOT_VERIFIED;
        mUiccCardAppStatus.pin2 = IccCardStatus.PinState.PINSTATE_ENABLED_VERIFIED;

        mTestHandlerThread = new UiccCardAppTestHandlerThread(TAG);
        mTestHandlerThread.start();

        waitUntilReady();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    @SmallTest
    public void testGetSetAppType() {
        assertEquals(IccCardApplicationStatus.AppType.APPTYPE_SIM, mUiccCardApplication.getType());
        mUiccCardAppStatus.app_type = IccCardApplicationStatus.AppType.APPTYPE_USIM;
        Message mCardAppUpdate = mHandler.obtainMessage(UICCCARDAPP_UPDATE_EVENT);
        setReady(false);
        mCardAppUpdate.sendToTarget();

        waitUntilReady();
        assertEquals(IccCardApplicationStatus.AppType.APPTYPE_USIM, mUiccCardApplication.getType());
    }

    @Test
    @SmallTest
    public void testGetSetAppState() {
        assertEquals(IccCardApplicationStatus.AppState.APPSTATE_PIN,
                mUiccCardApplication.getState());
        mUiccCardAppStatus.app_state = IccCardApplicationStatus.AppState.APPSTATE_PUK;
        Message mCardAppUpdate = mHandler.obtainMessage(UICCCARDAPP_UPDATE_EVENT);
        setReady(false);
        mCardAppUpdate.sendToTarget();

        waitUntilReady();
        assertEquals(IccCardApplicationStatus.AppState.APPSTATE_PUK,
                mUiccCardApplication.getState());
    }

    @Test
    @SmallTest
    public void testGetSetIccFdnEnabled() {
        assertFalse(mUiccCardApplication.getIccFdnEnabled());
        //enable FDN
        Message mFDNenabled = mHandler.obtainMessage(UICCCARDAPP_ENABLE_FDN_EVENT);
        //wrong PIN2Code
        setReady(false);
        mUiccCardApplication.setIccFdnEnabled(true, "XXXX", mFDNenabled);
        waitUntilReady();
        assertFalse(mUiccCardApplication.getIccFdnEnabled());

        setReady(false);
        mFDNenabled = mHandler.obtainMessage(UICCCARDAPP_ENABLE_FDN_EVENT);
        mUiccCardApplication.setIccFdnEnabled(true, mSimulatedCommands.DEFAULT_SIM_PIN2_CODE,
                mFDNenabled);
        waitUntilReady();
        assertTrue(mUiccCardApplication.getIccFdnEnabled());
    }

    @Test
    @SmallTest
    public void testGetSetIccLockedEnabled() {
        assertFalse(mUiccCardApplication.getIccLockEnabled());
        Message mLockEnabled = mHandler.obtainMessage(UICCCARDAPP_ENABLE_LOCK_EVENT);
        setReady(false);
        mUiccCardApplication.setIccLockEnabled(true, "XXXX", mLockEnabled);
        waitUntilReady();
        assertFalse(mUiccCardApplication.getIccLockEnabled());

        mLockEnabled = mHandler.obtainMessage(UICCCARDAPP_ENABLE_LOCK_EVENT);
        setReady(false);
        mUiccCardApplication.setIccLockEnabled(true, mSimulatedCommands.DEFAULT_SIM_PIN_CODE,
                mLockEnabled);
        waitUntilReady();
        assertTrue(mUiccCardApplication.getIccLockEnabled());
    }

    @Test
    @SmallTest
    public void testChangeIccLockPassword() {
        Message mChangePsw = mHandler.obtainMessage(UICCCARDAPP_CHANGE_PSW_EVENT);
        setReady(false);
        mUiccCardApplication.changeIccLockPassword(mSimulatedCommands.DEFAULT_SIM_PIN_CODE,
                "1111", mChangePsw);
        waitUntilReady();
        verify(mSimulatedCommandsVerifier).changeIccPinForApp(
                eq(mSimulatedCommands.DEFAULT_SIM_PIN_CODE), eq("1111"), eq(TAG), (Message) any());
        assertNull(mException);
    }

    @Test
    @SmallTest
    public void testSupplyPin() {
        //Supply with default PIN1
        Message mSupplyPin = mHandler.obtainMessage(UICCCARDAPP_SUPPLY_PIN_EVENT);
        setReady(false);
        mUiccCardApplication.supplyPin(mSimulatedCommands.DEFAULT_SIM_PIN_CODE, mSupplyPin);
        waitUntilReady();
        assertEquals(-1, mAttemptsRemaining);
        verify(mSimulatedCommandsVerifier).supplyIccPinForApp(
                eq(SimulatedCommands.DEFAULT_SIM_PIN_CODE), eq(TAG), (Message) any());

        //Supply with wrong PIN1
        mSupplyPin = mHandler.obtainMessage(UICCCARDAPP_SUPPLY_PIN_EVENT);
        setReady(false);
        mUiccCardApplication.supplyPin("1111", mSupplyPin);
        waitUntilReady();
        assertEquals(mSimulatedCommands.DEFAULT_PIN1_ATTEMPT - 1, mAttemptsRemaining);
        assertNotNull(mException);
        assertEquals(CommandException.Error.PASSWORD_INCORRECT, mException.getCommandError());

        testChangeIccLockPassword();
        //Supply with the updated PIN1
        mSupplyPin = mHandler.obtainMessage(UICCCARDAPP_SUPPLY_PIN_EVENT);
        setReady(false);
        mUiccCardApplication.supplyPin("1111", mSupplyPin);
        waitUntilReady();
        assertEquals(-1, mAttemptsRemaining);
    }
}