22
22
import static org .mockito .Mockito .when ;
23
23
24
24
import com .google .android .gms .tasks .Task ;
25
+ import com .google .android .gms .tasks .Tasks ;
26
+ import com .google .android .play .core .integrity .IntegrityManager ;
27
+ import com .google .android .play .core .integrity .IntegrityTokenRequest ;
28
+ import com .google .android .play .core .integrity .IntegrityTokenResponse ;
25
29
import com .google .common .util .concurrent .MoreExecutors ;
26
30
import com .google .firebase .appcheck .AppCheckToken ;
27
31
import com .google .firebase .appcheck .internal .AppCheckTokenResponse ;
33
37
import org .junit .Before ;
34
38
import org .junit .Test ;
35
39
import org .junit .runner .RunWith ;
40
+ import org .mockito .ArgumentCaptor ;
41
+ import org .mockito .Captor ;
36
42
import org .mockito .Mock ;
37
43
import org .mockito .MockitoAnnotations ;
38
44
import org .robolectric .RobolectricTestRunner ;
43
49
@ Config (manifest = Config .NONE )
44
50
public class PlayIntegrityAppCheckProviderTest {
45
51
52
+ private static final String PROJECT_NUMBER = "123456" ;
46
53
private static final String ATTESTATION_TOKEN = "token" ;
47
54
private static final String TIME_TO_LIVE = "3600s" ;
55
+ private static final String CHALLENGE = "testChallenge" ;
56
+ private static final String CHALLENGE_JSON_RESPONSE =
57
+ "{\" challenge\" :\" " + CHALLENGE + "\" ,\" ttl\" :\" 3600s\" }" ;
58
+ private static final String INTEGRITY_TOKEN = "integrityToken" ;
48
59
49
- private ExecutorService backgroundExecutor = MoreExecutors . newDirectExecutorService () ;
60
+ @ Mock private IntegrityManager mockIntegrityManager ;
50
61
@ Mock private NetworkClient mockNetworkClient ;
51
62
@ Mock private RetryManager mockRetryManager ;
63
+ @ Mock private IntegrityTokenResponse mockIntegrityTokenResponse ;
52
64
@ Mock private AppCheckTokenResponse mockAppCheckTokenResponse ;
53
65
66
+ @ Captor private ArgumentCaptor <IntegrityTokenRequest > integrityTokenRequestCaptor ;
67
+ @ Captor private ArgumentCaptor <byte []> exchangePlayIntegrityTokenRequestCaptor ;
68
+
69
+ private ExecutorService backgroundExecutor = MoreExecutors .newDirectExecutorService ();
70
+
54
71
@ Before
55
72
public void setup () {
56
73
MockitoAnnotations .initMocks (this );
74
+ when (mockIntegrityTokenResponse .token ()).thenReturn (INTEGRITY_TOKEN );
75
+ when (mockAppCheckTokenResponse .getAttestationToken ()).thenReturn (ATTESTATION_TOKEN );
76
+ when (mockAppCheckTokenResponse .getTimeToLive ()).thenReturn (TIME_TO_LIVE );
57
77
}
58
78
59
79
@ Test
@@ -67,41 +87,76 @@ public void testPublicConstructor_nullFirebaseApp_expectThrows() {
67
87
68
88
@ Test
69
89
public void getToken_onSuccess_setsTaskResult () throws Exception {
90
+ when (mockNetworkClient .generatePlayIntegrityChallenge (any (), eq (mockRetryManager )))
91
+ .thenReturn (CHALLENGE_JSON_RESPONSE );
92
+ when (mockIntegrityManager .requestIntegrityToken (any ()))
93
+ .thenReturn (Tasks .forResult (mockIntegrityTokenResponse ));
70
94
when (mockNetworkClient .exchangeAttestationForAppCheckToken (
71
95
any (), eq (NetworkClient .PLAY_INTEGRITY ), eq (mockRetryManager )))
72
96
.thenReturn (mockAppCheckTokenResponse );
73
- when (mockAppCheckTokenResponse .getAttestationToken ()).thenReturn (ATTESTATION_TOKEN );
74
- when (mockAppCheckTokenResponse .getTimeToLive ()).thenReturn (TIME_TO_LIVE );
75
97
76
98
PlayIntegrityAppCheckProvider provider =
77
- new PlayIntegrityAppCheckProvider (mockNetworkClient , backgroundExecutor , mockRetryManager );
99
+ new PlayIntegrityAppCheckProvider (
100
+ PROJECT_NUMBER ,
101
+ mockIntegrityManager ,
102
+ mockNetworkClient ,
103
+ backgroundExecutor ,
104
+ mockRetryManager );
78
105
Task <AppCheckToken > task = provider .getToken ();
79
106
80
- verify (mockNetworkClient )
81
- .exchangeAttestationForAppCheckToken (
82
- any (), eq (NetworkClient .PLAY_INTEGRITY ), eq (mockRetryManager ));
83
-
84
107
AppCheckToken token = task .getResult ();
85
108
assertThat (token ).isInstanceOf (DefaultAppCheckToken .class );
86
109
assertThat (token .getToken ()).isEqualTo (ATTESTATION_TOKEN );
110
+
111
+ verify (mockIntegrityManager ).requestIntegrityToken (integrityTokenRequestCaptor .capture ());
112
+ assertThat (integrityTokenRequestCaptor .getValue ().cloudProjectNumber ())
113
+ .isEqualTo (Long .parseLong (PROJECT_NUMBER ));
114
+ assertThat (integrityTokenRequestCaptor .getValue ().nonce ()).isEqualTo (CHALLENGE );
115
+
116
+ verify (mockNetworkClient )
117
+ .exchangeAttestationForAppCheckToken (
118
+ exchangePlayIntegrityTokenRequestCaptor .capture (),
119
+ eq (NetworkClient .PLAY_INTEGRITY ),
120
+ eq (mockRetryManager ));
121
+ String exchangePlayIntegrityTokenRequestJsonString =
122
+ new String (exchangePlayIntegrityTokenRequestCaptor .getValue ());
123
+ assertThat (exchangePlayIntegrityTokenRequestJsonString ).contains (INTEGRITY_TOKEN );
87
124
}
88
125
89
126
@ Test
90
- public void getToken_onFailure_setsTaskException () throws Exception {
127
+ public void getToken_tokenExchangeFailure_setsTaskException () throws Exception {
128
+ when (mockNetworkClient .generatePlayIntegrityChallenge (any (), eq (mockRetryManager )))
129
+ .thenReturn (CHALLENGE_JSON_RESPONSE );
130
+ when (mockIntegrityManager .requestIntegrityToken (any ()))
131
+ .thenReturn (Tasks .forResult (mockIntegrityTokenResponse ));
91
132
when (mockNetworkClient .exchangeAttestationForAppCheckToken (
92
133
any (), eq (NetworkClient .PLAY_INTEGRITY ), eq (mockRetryManager )))
93
134
.thenThrow (new IOException ());
94
135
95
136
PlayIntegrityAppCheckProvider provider =
96
- new PlayIntegrityAppCheckProvider (mockNetworkClient , backgroundExecutor , mockRetryManager );
137
+ new PlayIntegrityAppCheckProvider (
138
+ PROJECT_NUMBER ,
139
+ mockIntegrityManager ,
140
+ mockNetworkClient ,
141
+ backgroundExecutor ,
142
+ mockRetryManager );
97
143
Task <AppCheckToken > task = provider .getToken ();
98
144
145
+ assertThat (task .isSuccessful ()).isFalse ();
146
+ assertThat (task .getException ()).isInstanceOf (IOException .class );
147
+
148
+ verify (mockIntegrityManager ).requestIntegrityToken (integrityTokenRequestCaptor .capture ());
149
+ assertThat (integrityTokenRequestCaptor .getValue ().cloudProjectNumber ())
150
+ .isEqualTo (Long .parseLong (PROJECT_NUMBER ));
151
+ assertThat (integrityTokenRequestCaptor .getValue ().nonce ()).isEqualTo (CHALLENGE );
152
+
99
153
verify (mockNetworkClient )
100
154
.exchangeAttestationForAppCheckToken (
101
- any (), eq (NetworkClient .PLAY_INTEGRITY ), eq (mockRetryManager ));
102
-
103
- assertThat (task .isSuccessful ()).isFalse ();
104
- Exception exception = task .getException ();
105
- assertThat (exception ).isInstanceOf (IOException .class );
155
+ exchangePlayIntegrityTokenRequestCaptor .capture (),
156
+ eq (NetworkClient .PLAY_INTEGRITY ),
157
+ eq (mockRetryManager ));
158
+ String exchangePlayIntegrityTokenRequestJsonString =
159
+ new String (exchangePlayIntegrityTokenRequestCaptor .getValue ());
160
+ assertThat (exchangePlayIntegrityTokenRequestJsonString ).contains (INTEGRITY_TOKEN );
106
161
}
107
162
}
0 commit comments