1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.uwb.secure.provisioning; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.ArgumentMatchers.eq; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.os.test.TestLooper; 25 26 import com.android.server.uwb.secure.SecureElementChannel; 27 import com.android.server.uwb.secure.csml.FiRaCommand; 28 import com.android.server.uwb.secure.iso7816.ResponseApdu; 29 import com.android.server.uwb.secure.iso7816.StatusWord; 30 import com.android.server.uwb.util.ObjectIdentifier; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 37 import java.io.IOException; 38 import java.util.UUID; 39 40 public class ProvisioningManagerTest { 41 @Mock 42 private SecureElementChannel mSecureElementChannel; 43 @Mock 44 private ProvisioningManager.ProvisioningCallback mProvisioningCallback; 45 @Mock 46 private ProvisioningManager.DeleteAdfCallback mDeleteAdfCallback; 47 48 private final TestLooper mTestLooper = new TestLooper(); 49 50 private ProvisioningManager mUnderTest; 51 52 private static final UUID SERVICE_INSTANCE_ID = UUID.fromString("1-2-3-4-5"); 53 private static final ObjectIdentifier ADF_OID = 54 ObjectIdentifier.fromBytes(new byte[] {(byte) 1}); 55 56 @Before setup()57 public void setup() { 58 MockitoAnnotations.initMocks(this); 59 mUnderTest = new ProvisioningManager(mSecureElementChannel, mTestLooper.getLooper()); 60 } 61 62 @Test provisioningException()63 public void provisioningException() { 64 mUnderTest.provisioningAdf(SERVICE_INSTANCE_ID, new byte[0], mProvisioningCallback); 65 66 mTestLooper.dispatchAll(); 67 68 verify(mProvisioningCallback).onFail(eq(SERVICE_INSTANCE_ID)); 69 } 70 71 @Test deleteAdfSuccess()72 public void deleteAdfSuccess() throws IOException { 73 when(mSecureElementChannel.openChannel()).thenReturn(true); 74 when(mSecureElementChannel.transmit(any(FiRaCommand.class))) 75 .thenReturn(ResponseApdu.fromStatusWord(StatusWord.SW_NO_ERROR)); 76 77 mUnderTest.deleteAdf(SERVICE_INSTANCE_ID, ADF_OID, mDeleteAdfCallback); 78 mTestLooper.dispatchAll(); 79 80 verify(mDeleteAdfCallback).onSuccess(eq(SERVICE_INSTANCE_ID), eq(ADF_OID)); 81 } 82 83 @Test deleteAdfFail()84 public void deleteAdfFail() throws IOException { 85 when(mSecureElementChannel.openChannel()).thenReturn(true); 86 when(mSecureElementChannel.transmit(any(FiRaCommand.class))) 87 .thenReturn(ResponseApdu.fromStatusWord(StatusWord.SW_COMMAND_NOT_ALLOWED)); 88 89 mUnderTest.deleteAdf(SERVICE_INSTANCE_ID, ADF_OID, mDeleteAdfCallback); 90 mTestLooper.dispatchAll(); 91 92 verify(mDeleteAdfCallback).onFail(eq(SERVICE_INSTANCE_ID), eq(ADF_OID)); 93 } 94 95 @Test deleteAdfWithException()96 public void deleteAdfWithException() throws IOException { 97 when(mSecureElementChannel.openChannel()).thenReturn(true); 98 when(mSecureElementChannel.transmit(any(FiRaCommand.class))) 99 .thenThrow(new IOException("error")); 100 mUnderTest.deleteAdf(SERVICE_INSTANCE_ID, ADF_OID, mDeleteAdfCallback); 101 mTestLooper.dispatchAll(); 102 103 verify(mDeleteAdfCallback).onFail(eq(SERVICE_INSTANCE_ID), eq(ADF_OID)); 104 } 105 } 106