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;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.slice.Slice;
22 import android.content.pm.SigningInfo;
23 import android.credentials.CredentialDescription;
24 import android.credentials.CredentialOption;
25 import android.os.Bundle;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.os.SystemClock;
29 import android.service.credentials.Action;
30 import android.service.credentials.BeginCreateCredentialRequest;
31 import android.service.credentials.CallingAppInfo;
32 import android.service.credentials.CreateEntry;
33 import android.service.credentials.CredentialEntry;
34 import android.service.credentials.RemoteEntry;
35 
36 import java.util.Random;
37 
38 public class TestUtils {
39     private static final Random sRandom = new Random(SystemClock.uptimeMillis());
40 
assertEquals(CredentialOption a, CredentialOption b)41     public static void assertEquals(CredentialOption a, CredentialOption b) {
42         assertThat(a.getType()).isEqualTo(b.getType());
43         assertEquals(a.getCandidateQueryData(), b.getCandidateQueryData());
44         assertEquals(a.getCredentialRetrievalData(), b.getCredentialRetrievalData());
45         assertThat(a.isSystemProviderRequired()).isEqualTo(b.isSystemProviderRequired());
46         assertThat(a.getAllowedProviders()).containsExactlyElementsIn(b.getAllowedProviders());
47     }
48 
assertEquals(BeginCreateCredentialRequest a, BeginCreateCredentialRequest b)49     public static void assertEquals(BeginCreateCredentialRequest a,
50             BeginCreateCredentialRequest b) {
51         assertThat(a.getType()).isEqualTo(b.getType());
52         assertEquals(a.getData(), b.getData());
53         assertEquals(a.getCallingAppInfo(), b.getCallingAppInfo());
54     }
55 
assertEquals(CredentialDescription a, CredentialDescription b)56     public static void assertEquals(CredentialDescription a, CredentialDescription b) {
57         assertThat(a).isEqualTo(b);
58         assertThat(a.getCredentialEntries()).hasSize(b.getCredentialEntries().size());
59     }
60 
assertEquals(Slice a, Slice b)61     public static void assertEquals(Slice a, Slice b) {
62         assertThat(a.getUri()).isEqualTo(b.getUri());
63     }
64 
assertEquals(CredentialEntry a, CredentialEntry b)65     public static void assertEquals(CredentialEntry a, CredentialEntry b) {
66         assertThat(a.getType()).isEqualTo(b.getType());
67         assertEquals(a.getSlice(), b.getSlice());
68     }
69 
assertEquals(CreateEntry a, CreateEntry b)70     public static void assertEquals(CreateEntry a, CreateEntry b) {
71         assertEquals(a.getSlice(), b.getSlice());
72     }
73 
assertEquals(RemoteEntry a, RemoteEntry b)74     public static void assertEquals(RemoteEntry a, RemoteEntry b) {
75         assertEquals(a.getSlice(), b.getSlice());
76     }
77 
assertEquals(Action a, Action b)78     public static void assertEquals(Action a, Action b) {
79         assertEquals(a.getSlice(), b.getSlice());
80     }
81 
assertEquals(Bundle a, Bundle b)82     public static void assertEquals(Bundle a, Bundle b) {
83         assertThat(a.size()).isEqualTo(b.size());
84 
85         for (String key : a.keySet()) {
86             assertThat(a.get(key)).isEqualTo(b.get(key));
87         }
88 
89         for (String key : b.keySet()) {
90             assertThat(b.get(key)).isEqualTo(a.get(key));
91         }
92     }
93 
assertEquals(SigningInfo a, SigningInfo b)94     public static void assertEquals(SigningInfo a, SigningInfo b) {
95         assertThat(b.getApkContentsSigners()).isEqualTo(a.getApkContentsSigners());
96         assertThat(b.getSigningCertificateHistory()).isEqualTo(
97                 a.getSigningCertificateHistory());
98         assertThat(b.hasPastSigningCertificates()).isEqualTo(
99                 a.hasPastSigningCertificates());
100         assertThat(b.hasMultipleSigners()).isEqualTo(a.hasMultipleSigners());
101     }
102 
assertEquals(CallingAppInfo a, CallingAppInfo b)103     public static void assertEquals(CallingAppInfo a, CallingAppInfo b) {
104         assertThat(a.getPackageName()).isEqualTo(b.getPackageName());
105         assertEquals(a.getSigningInfo(), b.getSigningInfo());
106     }
107 
cloneParcelable(T obj)108     public static <T extends Parcelable> T cloneParcelable(T obj) {
109         final Parcel parcel = Parcel.obtain();
110         obj.writeToParcel(parcel, 0);
111         parcel.setDataPosition(0);
112 
113         try {
114             final Parcelable.Creator<T> creator = (Parcelable.Creator<T>) obj.getClass().getField(
115                     "CREATOR").get(null);
116             return creator.createFromParcel(parcel);
117         } catch (IllegalAccessException e) {
118             throw new IllegalArgumentException("Parcelable CREATOR field must be public");
119         } catch (NoSuchFieldException e) {
120             throw new IllegalArgumentException("Parcelable must have a static CREATOR field");
121         }
122     }
123 
createTestBundle()124     public static Bundle createTestBundle() {
125         final Bundle bundle = new Bundle();
126         bundle.putInt("key" + sRandom.nextInt(), sRandom.nextInt());
127         return bundle;
128     }
129 }
130