1 /* 2 * Copyright (C) 2024 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.nfc.cardemulation; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.ArgumentMatchers.anyString; 21 import static org.mockito.Mockito.doNothing; 22 import static org.mockito.Mockito.times; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import androidx.test.platform.app.InstrumentationRegistry; 29 30 import com.android.dx.mockito.inline.extended.ExtendedMockito; 31 import com.android.nfc.NfcService; 32 import com.android.nfc.cardemulation.RegisteredT3tIdentifiersCache; 33 import com.android.nfc.cardemulation.RegisteredT3tIdentifiersCache.T3tIdentifier; 34 35 import java.io.PrintWriter; 36 import java.util.ArrayList; 37 38 import org.junit.After; 39 import org.junit.Assert; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.mockito.ArgumentCaptor; 43 import org.mockito.Captor; 44 import org.mockito.Mock; 45 import org.mockito.Mockito; 46 import org.mockito.MockitoAnnotations; 47 import org.mockito.MockitoSession; 48 import org.mockito.quality.Strictness; 49 50 public class SystemCodeRoutingManagerTest { 51 private SystemCodeRoutingManager manager; 52 private MockitoSession mStaticMockSession; 53 private T3tIdentifier mIdentifier; 54 @Mock 55 private NfcService mNfcService; 56 @Mock 57 private PrintWriter mPw; 58 59 @Captor 60 private ArgumentCaptor<String> systemCodeCaptor; 61 @Captor 62 private ArgumentCaptor<String> nfcId2Captor; 63 @Captor 64 private ArgumentCaptor<String> t3tPmmCaptor; 65 66 private static final String IDENTIFIER_SYSTEM_CODE = "systemCode"; 67 private static final String IDENTIFIER_NFCID_2 = "nfcid2"; 68 private static final String IDENTIFIER_T3TPMM = "t3tpmm"; 69 70 @Before setUp()71 public void setUp() { 72 mStaticMockSession = ExtendedMockito.mockitoSession() 73 .mockStatic(NfcService.class) 74 .strictness(Strictness.LENIENT) 75 .startMocking(); 76 MockitoAnnotations.initMocks(this); 77 78 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 79 mIdentifier = new RegisteredT3tIdentifiersCache(context) 80 .new T3tIdentifier(IDENTIFIER_SYSTEM_CODE, IDENTIFIER_NFCID_2, IDENTIFIER_T3TPMM); 81 manager = new SystemCodeRoutingManager(); 82 83 when(NfcService.getInstance()).thenReturn(mNfcService); 84 doNothing().when(mNfcService).deregisterT3tIdentifier(anyString(), anyString(), anyString()); 85 doNothing().when(mNfcService).registerT3tIdentifier(anyString(), anyString(), anyString()); 86 } 87 88 @After tearDown()89 public void tearDown() { 90 mStaticMockSession.finishMocking(); 91 } 92 93 @Test testConfigureRoutingWithNoItemsAddedOrRemoved()94 public void testConfigureRoutingWithNoItemsAddedOrRemoved() { 95 boolean result = manager.configureRouting(new ArrayList<T3tIdentifier>()); 96 97 assertThat(result).isFalse(); 98 verify(mNfcService, never()).deregisterT3tIdentifier(anyString(), anyString(), anyString()); 99 verify(mNfcService, never()).registerT3tIdentifier(anyString(), anyString(), anyString()); 100 verify(mNfcService, never()).commitRouting(); 101 assertThat(manager.mConfiguredT3tIdentifiers).isEmpty(); 102 } 103 104 @Test testConfigureRoutingWithItemsAdded()105 public void testConfigureRoutingWithItemsAdded() { 106 ArrayList<T3tIdentifier> list = new ArrayList<>(); 107 list.add(mIdentifier); 108 109 boolean result = manager.configureRouting(list); 110 111 assertThat(result).isTrue(); 112 verify(mNfcService, never()).deregisterT3tIdentifier(anyString(), anyString(), anyString()); 113 verify(mNfcService).registerT3tIdentifier(systemCodeCaptor.capture(), 114 nfcId2Captor.capture(), 115 t3tPmmCaptor.capture()); 116 assertThat(systemCodeCaptor.getValue()).isEqualTo(IDENTIFIER_SYSTEM_CODE); 117 assertThat(nfcId2Captor.getValue()).isEqualTo(IDENTIFIER_NFCID_2); 118 assertThat(t3tPmmCaptor.getValue()).isEqualTo(IDENTIFIER_T3TPMM); 119 verify(mNfcService).commitRouting(); 120 assertThat(manager.mConfiguredT3tIdentifiers.size()).isEqualTo(1); 121 assertThat(manager.mConfiguredT3tIdentifiers.get(0)).isEqualTo(mIdentifier); 122 } 123 124 @Test testConfigureRoutingWithItemsRemoved()125 public void testConfigureRoutingWithItemsRemoved() { 126 manager.mConfiguredT3tIdentifiers.add(mIdentifier); 127 128 boolean result = manager.configureRouting(new ArrayList<T3tIdentifier>()); 129 130 assertThat(result).isTrue(); 131 verify(mNfcService).deregisterT3tIdentifier(systemCodeCaptor.capture(), 132 nfcId2Captor.capture(), 133 t3tPmmCaptor.capture()); 134 assertThat(systemCodeCaptor.getValue()).isEqualTo(IDENTIFIER_SYSTEM_CODE); 135 assertThat(nfcId2Captor.getValue()).isEqualTo(IDENTIFIER_NFCID_2); 136 assertThat(t3tPmmCaptor.getValue()).isEqualTo(IDENTIFIER_T3TPMM); 137 verify(mNfcService, never()).registerT3tIdentifier(anyString(), anyString(), anyString()); 138 verify(mNfcService).commitRouting(); 139 assertThat(manager.mConfiguredT3tIdentifiers).isEmpty(); 140 } 141 142 @Test testOnNfccRoutingTableCleared()143 public void testOnNfccRoutingTableCleared() { 144 manager.mConfiguredT3tIdentifiers.add(mIdentifier); 145 146 manager.onNfccRoutingTableCleared(); 147 148 verify(mNfcService).clearT3tIdentifiersCache(); 149 assertThat(manager.mConfiguredT3tIdentifiers).isEmpty(); 150 } 151 152 @Test testDump()153 public void testDump() { 154 manager.mConfiguredT3tIdentifiers.add(mIdentifier); 155 156 manager.dump(/* fd = */ null, mPw, /* args = */ null); 157 158 verify(mPw, times(2)).println(anyString()); 159 } 160 } 161