17
17
import static com .google .common .truth .Truth .assertThat ;
18
18
import static org .junit .Assert .assertThrows ;
19
19
import static org .mockito .ArgumentMatchers .any ;
20
+ import static org .mockito .ArgumentMatchers .anyInt ;
20
21
import static org .mockito .ArgumentMatchers .eq ;
22
+ import static org .mockito .Mockito .never ;
21
23
import static org .mockito .Mockito .verify ;
22
24
import static org .mockito .Mockito .when ;
23
25
34
36
import com .google .firebase .appcheck .internal .RetryManager ;
35
37
import java .io .IOException ;
36
38
import java .util .concurrent .ExecutorService ;
39
+ import java .util .concurrent .TimeoutException ;
40
+ import org .json .JSONObject ;
37
41
import org .junit .Before ;
38
42
import org .junit .Test ;
39
43
import org .junit .runner .RunWith ;
@@ -53,8 +57,6 @@ public class PlayIntegrityAppCheckProviderTest {
53
57
private static final String ATTESTATION_TOKEN = "token" ;
54
58
private static final String TIME_TO_LIVE = "3600s" ;
55
59
private static final String CHALLENGE = "testChallenge" ;
56
- private static final String CHALLENGE_JSON_RESPONSE =
57
- "{\" challenge\" :\" " + CHALLENGE + "\" ,\" ttl\" :\" 3600s\" }" ;
58
60
private static final String INTEGRITY_TOKEN = "integrityToken" ;
59
61
60
62
@ Mock private IntegrityManager mockIntegrityManager ;
@@ -88,7 +90,7 @@ public void testPublicConstructor_nullFirebaseApp_expectThrows() {
88
90
@ Test
89
91
public void getToken_onSuccess_setsTaskResult () throws Exception {
90
92
when (mockNetworkClient .generatePlayIntegrityChallenge (any (), eq (mockRetryManager )))
91
- .thenReturn (CHALLENGE_JSON_RESPONSE );
93
+ .thenReturn (createGeneratePlayIntegrityChallengeResponse () );
92
94
when (mockIntegrityManager .requestIntegrityToken (any ()))
93
95
.thenReturn (Tasks .forResult (mockIntegrityTokenResponse ));
94
96
when (mockNetworkClient .exchangeAttestationForAppCheckToken (
@@ -108,6 +110,8 @@ public void getToken_onSuccess_setsTaskResult() throws Exception {
108
110
assertThat (token ).isInstanceOf (DefaultAppCheckToken .class );
109
111
assertThat (token .getToken ()).isEqualTo (ATTESTATION_TOKEN );
110
112
113
+ verify (mockNetworkClient ).generatePlayIntegrityChallenge (any (), eq (mockRetryManager ));
114
+
111
115
verify (mockIntegrityManager ).requestIntegrityToken (integrityTokenRequestCaptor .capture ());
112
116
assertThat (integrityTokenRequestCaptor .getValue ().cloudProjectNumber ())
113
117
.isEqualTo (Long .parseLong (PROJECT_NUMBER ));
@@ -124,9 +128,59 @@ public void getToken_onSuccess_setsTaskResult() throws Exception {
124
128
}
125
129
126
130
@ Test
127
- public void getToken_tokenExchangeFailure_setsTaskException () throws Exception {
131
+ public void getToken_generateChallengeFails_setsTaskException () throws Exception {
128
132
when (mockNetworkClient .generatePlayIntegrityChallenge (any (), eq (mockRetryManager )))
129
- .thenReturn (CHALLENGE_JSON_RESPONSE );
133
+ .thenThrow (new IOException ());
134
+
135
+ PlayIntegrityAppCheckProvider provider =
136
+ new PlayIntegrityAppCheckProvider (
137
+ PROJECT_NUMBER ,
138
+ mockIntegrityManager ,
139
+ mockNetworkClient ,
140
+ backgroundExecutor ,
141
+ mockRetryManager );
142
+ Task <AppCheckToken > task = provider .getToken ();
143
+
144
+ assertThat (task .isSuccessful ()).isFalse ();
145
+ assertThat (task .getException ()).isInstanceOf (IOException .class );
146
+
147
+ verify (mockNetworkClient ).generatePlayIntegrityChallenge (any (), eq (mockRetryManager ));
148
+ verify (mockNetworkClient , never ()).exchangeAttestationForAppCheckToken (any (), anyInt (), any ());
149
+ verify (mockIntegrityManager , never ()).requestIntegrityToken (any ());
150
+ }
151
+
152
+ @ Test
153
+ public void getToken_requestIntegrityTokenFails_setsTaskException () throws Exception {
154
+ when (mockNetworkClient .generatePlayIntegrityChallenge (any (), eq (mockRetryManager )))
155
+ .thenReturn (createGeneratePlayIntegrityChallengeResponse ());
156
+ when (mockIntegrityManager .requestIntegrityToken (any ()))
157
+ .thenReturn (Tasks .forException (new TimeoutException ()));
158
+
159
+ PlayIntegrityAppCheckProvider provider =
160
+ new PlayIntegrityAppCheckProvider (
161
+ PROJECT_NUMBER ,
162
+ mockIntegrityManager ,
163
+ mockNetworkClient ,
164
+ backgroundExecutor ,
165
+ mockRetryManager );
166
+ Task <AppCheckToken > task = provider .getToken ();
167
+
168
+ assertThat (task .isSuccessful ()).isFalse ();
169
+ assertThat (task .getException ()).isInstanceOf (TimeoutException .class );
170
+
171
+ verify (mockNetworkClient ).generatePlayIntegrityChallenge (any (), eq (mockRetryManager ));
172
+ verify (mockNetworkClient , never ()).exchangeAttestationForAppCheckToken (any (), anyInt (), any ());
173
+
174
+ verify (mockIntegrityManager ).requestIntegrityToken (integrityTokenRequestCaptor .capture ());
175
+ assertThat (integrityTokenRequestCaptor .getValue ().cloudProjectNumber ())
176
+ .isEqualTo (Long .parseLong (PROJECT_NUMBER ));
177
+ assertThat (integrityTokenRequestCaptor .getValue ().nonce ()).isEqualTo (CHALLENGE );
178
+ }
179
+
180
+ @ Test
181
+ public void getToken_tokenExchangeFails_setsTaskException () throws Exception {
182
+ when (mockNetworkClient .generatePlayIntegrityChallenge (any (), eq (mockRetryManager )))
183
+ .thenReturn (createGeneratePlayIntegrityChallengeResponse ());
130
184
when (mockIntegrityManager .requestIntegrityToken (any ()))
131
185
.thenReturn (Tasks .forResult (mockIntegrityTokenResponse ));
132
186
when (mockNetworkClient .exchangeAttestationForAppCheckToken (
@@ -145,6 +199,8 @@ public void getToken_tokenExchangeFailure_setsTaskException() throws Exception {
145
199
assertThat (task .isSuccessful ()).isFalse ();
146
200
assertThat (task .getException ()).isInstanceOf (IOException .class );
147
201
202
+ verify (mockNetworkClient ).generatePlayIntegrityChallenge (any (), eq (mockRetryManager ));
203
+
148
204
verify (mockIntegrityManager ).requestIntegrityToken (integrityTokenRequestCaptor .capture ());
149
205
assertThat (integrityTokenRequestCaptor .getValue ().cloudProjectNumber ())
150
206
.isEqualTo (Long .parseLong (PROJECT_NUMBER ));
@@ -159,4 +215,12 @@ public void getToken_tokenExchangeFailure_setsTaskException() throws Exception {
159
215
new String (exchangePlayIntegrityTokenRequestCaptor .getValue ());
160
216
assertThat (exchangePlayIntegrityTokenRequestJsonString ).contains (INTEGRITY_TOKEN );
161
217
}
218
+
219
+ private static String createGeneratePlayIntegrityChallengeResponse () throws Exception {
220
+ JSONObject responseBodyJson = new JSONObject ();
221
+ responseBodyJson .put (GeneratePlayIntegrityChallengeResponse .CHALLENGE_KEY , CHALLENGE );
222
+ responseBodyJson .put (GeneratePlayIntegrityChallengeResponse .TIME_TO_LIVE_KEY , TIME_TO_LIVE );
223
+
224
+ return responseBodyJson .toString ();
225
+ }
162
226
}
0 commit comments