1 /* 2 * Copyright (C) 2023 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 android.credentials.cts.unittests.service; 18 19 import static android.credentials.cts.unittests.TestUtils.createTestBundle; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.testng.Assert.assertThrows; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.content.pm.SigningInfo; 28 import android.credentials.cts.unittests.TestUtils; 29 import android.platform.test.annotations.AppModeFull; 30 import android.service.credentials.BeginGetCredentialOption; 31 import android.service.credentials.BeginGetCredentialRequest; 32 import android.service.credentials.CallingAppInfo; 33 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 import androidx.test.filters.SmallTest; 36 import androidx.test.platform.app.InstrumentationRegistry; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.List; 43 44 @SmallTest 45 @AppModeFull(reason = "unit test") 46 @RunWith(AndroidJUnit4.class) 47 public class BeginGetCredentialsRequestTest { 48 49 private static final List<BeginGetCredentialOption> sCredOptions = List.of( 50 new BeginGetCredentialOption("id1", "type", createTestBundle()), 51 new BeginGetCredentialOption("id2", "type2", createTestBundle()), 52 new BeginGetCredentialOption("id3", "type3", createTestBundle())); 53 54 private CallingAppInfo mCallingAppInfo; 55 56 @Before setup()57 public void setup() throws PackageManager.NameNotFoundException { 58 final Context context = InstrumentationRegistry.getInstrumentation().getContext(); 59 final SigningInfo signingInfo = context.getPackageManager().getPackageInfo( 60 context.getOpPackageName(), PackageManager.PackageInfoFlags.of( 61 PackageManager.GET_SIGNING_CERTIFICATES)).signingInfo; 62 mCallingAppInfo = new CallingAppInfo(context.getOpPackageName(), signingInfo); 63 } 64 65 @Test testBuilder_setBeginGetCredentialOptions_null()66 public void testBuilder_setBeginGetCredentialOptions_null() { 67 assertThrows(NullPointerException.class, 68 () -> new BeginGetCredentialRequest.Builder().setBeginGetCredentialOptions(null)); 69 } 70 71 @Test testBuilder_setBeginGetCredentialOptions_empty()72 public void testBuilder_setBeginGetCredentialOptions_empty() { 73 assertThrows(IllegalArgumentException.class, 74 () -> new BeginGetCredentialRequest.Builder().setBeginGetCredentialOptions( 75 List.of())); 76 } 77 78 @Test testBuilder_setBeginGetCredentialOptions_nullOption()79 public void testBuilder_setBeginGetCredentialOptions_nullOption() { 80 assertThrows(NullPointerException.class, 81 () -> new BeginGetCredentialRequest.Builder().setBeginGetCredentialOptions( 82 List.of(null))); 83 } 84 85 @Test testBuilder_addBeginGetCredentialOption_null()86 public void testBuilder_addBeginGetCredentialOption_null() { 87 assertThrows(NullPointerException.class, 88 () -> new BeginGetCredentialRequest.Builder().addBeginGetCredentialOption(null)); 89 } 90 91 @Test testBuilder_setBeginGetCredentialOptions()92 public void testBuilder_setBeginGetCredentialOptions() { 93 final BeginGetCredentialRequest request = 94 new BeginGetCredentialRequest.Builder().setBeginGetCredentialOptions( 95 sCredOptions).build(); 96 97 assertThat(request.getBeginGetCredentialOptions()).isSameInstanceAs(sCredOptions); 98 } 99 100 @Test testBuilder_addBeginGetCredentialOption()101 public void testBuilder_addBeginGetCredentialOption() { 102 final BeginGetCredentialOption opt = sCredOptions.get(0); 103 final BeginGetCredentialRequest request = 104 new BeginGetCredentialRequest.Builder().addBeginGetCredentialOption(opt).build(); 105 106 assertThat(request.getBeginGetCredentialOptions().get(0)).isSameInstanceAs(opt); 107 } 108 109 @Test testWriteToParcel()110 public void testWriteToParcel() { 111 final BeginGetCredentialRequest request1 = 112 new BeginGetCredentialRequest.Builder().setBeginGetCredentialOptions( 113 sCredOptions).setCallingAppInfo(mCallingAppInfo).build(); 114 115 final BeginGetCredentialRequest request2 = TestUtils.cloneParcelable(request1); 116 assertThat(request2.getCallingAppInfo().getPackageName()).isEqualTo( 117 request1.getCallingAppInfo().getPackageName()); 118 119 final SigningInfo signing1 = request1.getCallingAppInfo().getSigningInfo(); 120 final SigningInfo signing2 = request2.getCallingAppInfo().getSigningInfo(); 121 assertThat(signing2.getApkContentsSigners()).isEqualTo(signing1.getApkContentsSigners()); 122 assertThat(signing2.getSigningCertificateHistory()).isEqualTo( 123 signing1.getSigningCertificateHistory()); 124 assertThat(signing2.hasPastSigningCertificates()).isEqualTo( 125 signing1.hasPastSigningCertificates()); 126 assertThat(signing2.hasMultipleSigners()).isEqualTo(signing2.hasMultipleSigners()); 127 } 128 } 129