-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUID2ClientTest.kt
420 lines (353 loc) · 14.9 KB
/
UID2ClientTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package com.uid2
import com.uid2.data.IdentityRequest
import com.uid2.data.IdentityStatus
import com.uid2.data.TestData
import com.uid2.data.UID2Identity
import com.uid2.network.DataEnvelope
import com.uid2.network.NetworkRequest
import com.uid2.network.NetworkResponse
import com.uid2.network.NetworkSession
import com.uid2.utils.KeyUtils
import com.uid2.utils.Logger
import com.uid2.utils.TimeUtils
import io.mockk.every
import io.mockk.junit4.MockKRule
import io.mockk.mockk
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.runTest
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.security.KeyPair
import java.security.PublicKey
import javax.crypto.SecretKey
class UID2ClientTest {
@get:Rule
val mockkRule = MockKRule(this)
private val testDispatcher: TestDispatcher = StandardTestDispatcher()
private val networkSession = mockk<NetworkSession>()
private val packageName = "com.uid2.devapp"
private val dataEnvelope = mockk<DataEnvelope>(relaxed = true)
private val keyUtils = mockk<KeyUtils>()
private val timeUtils = mockk<TimeUtils>()
private val logger = mockk<Logger>(relaxed = true)
private val url = "https://test.dev"
private val refreshToken = "RefreshToken"
private val refreshKey = "RefreshKey"
private val keyPair = mockk<KeyPair>()
private val keyPairPublic = mockk<PublicKey>()
private val keyPairPublicEncoded = ByteArray(12)
private val SUBSCRIPTION_ID = "subscription_id"
private val PUBLIC_KEY = "public_key"
@Before
fun before() {
// By default, don't encrypt the data. Just convert it directly to a ByteArray.
every { dataEnvelope.encrypt(any(), any(), any(), any()) }.answers {
(secondArg() as String).toByteArray()
}
every { dataEnvelope.decrypt(any<String>(), any<String>(), any<Boolean>()) }.returns(null)
every { keyUtils.generateServerPublicKey(any()) }.returns(mockk<PublicKey>())
every { keyUtils.generateKeyPair() }.returns(keyPair)
every { keyUtils.generateSharedSecret(any(), any()) }.returns(mockk<SecretKey>(relaxed = true))
every { keyUtils.generateIv(any()) }.answers { ByteArray(firstArg() as Int) }
every { keyUtils.generateAad(any(), any()) }.returns("")
every { keyPairPublic.encoded }.returns(keyPairPublicEncoded)
every { keyPair.public }.returns(keyPairPublic)
every { timeUtils.now() }.returns(0)
}
//region generateIdentity
@Test
fun `test generate with invalid api url`() = runTest(testDispatcher) {
testInvalidClientApi { client ->
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
@Test
fun `test generate with public key failure`() = runTest(testDispatcher) {
val client = withClient()
// Mock the KeyUtils to fail to generate the required PublicKey.
every { keyUtils.generateServerPublicKey(any()) }.returns(null)
// Verify the expected CryptoException is thrown.
assertThrows(CryptoException::class.java) {
runTest(testDispatcher) {
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
}
@Test
fun `test generate with key pair failure`() = runTest(testDispatcher) {
val client = withClient()
// Mock the KeyUtils to fail to generate the required KeyPair.
every { keyUtils.generateKeyPair() }.returns(null)
// Verify the expected CryptoException is thrown.
assertThrows(CryptoException::class.java) {
runTest(testDispatcher) {
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
}
@Test
fun `test generate with shared secret failure`() = runTest(testDispatcher) {
val client = withClient()
// Mock the KeyUtils to fail to generate the required SharedSecret.
every { keyUtils.generateSharedSecret(any(), any()) }.returns(null)
// Verify the expected CryptoException is thrown.
assertThrows(CryptoException::class.java) {
runTest(testDispatcher) {
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
}
@Test
fun `test generate with encryption failure`() = runTest(testDispatcher) {
val client = withClient()
// Mock the DataEnvelope to fail to encrypt the given payload.
every { dataEnvelope.encrypt(any(), any(), any(), any()) }.returns(null)
// Verify the expected CryptoException is thrown.
assertThrows(CryptoException::class.java) {
runTest(testDispatcher) {
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
}
@Test
fun `test generate with network failure`() = runTest(testDispatcher) {
testNetworkFailure { client ->
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
@Test
fun `test generate with decryption failure`() = runTest(testDispatcher) {
val client = withClient()
// Mock the DataEnvelope to fail to decrypt the given payload.
every { networkSession.loadData(any(), any()) }.returns(NetworkResponse(200, "somedata"))
every { dataEnvelope.decrypt(any<ByteArray>(), any<String>(), any<Boolean>()) }.returns(null)
// Verify the expected CryptoException is thrown.
assertThrows(PayloadDecryptException::class.java) {
runTest(testDispatcher) {
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
}
@Test
fun `test generate success`() = runTest(testDispatcher) {
val client = withClient()
val unencrypted = JSONObject(TestData.REFRESH_TOKEN_SUCCESS_DECRYPTED)
every { dataEnvelope.decrypt(any<ByteArray>(), any<String>(), any<Boolean>()) }.returns(
unencrypted.toString().toByteArray(),
)
every { networkSession.loadData(any(), any()) }.returns(NetworkResponse(200, "some data"))
val response = client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
assertNotNull(response)
// Verify that the returned package has an identity that matches what we included in the body of the response.
val expectedIdentity = unencrypted.getJSONObject("body").let {
UID2Identity.fromJson(it)
}
assertEquals(expectedIdentity, response.identity)
}
//endregion
//region refreshIdentity
@Test
fun `test refresh with invalid api url`() = runTest(testDispatcher) {
testInvalidClientApi { client ->
client.refreshIdentity(refreshToken, refreshKey)
}
}
@Test
fun `test refresh with network failure`() = runTest(testDispatcher) {
testNetworkFailure { client ->
client.refreshIdentity(refreshToken, refreshKey)
}
}
@Test
fun `test refresh with invalid data failure`() = runTest(testDispatcher) {
testInvalidNetworkResponse { client ->
client.refreshIdentity(refreshToken, refreshKey)
}
}
@Test
fun `test refresh with invalid data key`() = runTest(testDispatcher) {
val client = withClient()
every { dataEnvelope.decrypt(any<String>(), any(), any()) }.returns(null)
every { networkSession.loadData(any(), any()) }.returns(NetworkResponse(200, "some data"))
// Verify that when an unexpected response is returned, the appropriate exception is thrown.
assertThrows(PayloadDecryptException::class.java) {
runBlocking { client.refreshIdentity(refreshToken, "This is not a key") }
}
}
@Test
fun `test successful refresh`() = runTest(testDispatcher) {
val client = withClient()
val unencrypted = JSONObject(TestData.REFRESH_TOKEN_SUCCESS_DECRYPTED)
every { dataEnvelope.decrypt(any<String>(), any(), any()) }.returns(unencrypted.toString().toByteArray())
every { networkSession.loadData(any(), any()) }.returns(NetworkResponse(200, "some data"))
// Verify that the payload was successfully decrypted, and parsed.
val identity = client.refreshIdentity(refreshToken, TestData.REFRESH_TOKEN_ENCRYPTED_SUCCESS_KEY)
assertEquals(IdentityStatus.REFRESHED, identity.status)
// Verify that the returned package has an identity that matches what we included in the body of the response.
val expectedIdentity = unencrypted.getJSONObject("body").let {
UID2Identity.fromJson(it)
}
assertEquals(expectedIdentity, identity.identity)
}
//endregion
@Test
fun `test successful opt-out`() = runBlocking {
val client = withClient()
// Configure the network session to return a valid payload.
val unencrypted = JSONObject(TestData.REFRESH_TOKEN_OPT_OUT_DECRYPTED)
every { dataEnvelope.decrypt(any<String>(), any(), any()) }.returns(unencrypted.toString().toByteArray())
every { networkSession.loadData(any(), any()) }.returns(NetworkResponse(200, "some data"))
// Verify that the payload was successfully decrypted, and parsed.
val identity = client.refreshIdentity(refreshToken, TestData.REFRESH_TOKEN_ENCRYPTED_OPT_OUT_KEY)
assertEquals(IdentityStatus.OPT_OUT, identity.status)
assertNull(identity.identity)
}
@Test
fun `test version info - generate identity`() {
testVersionInfo { client ->
val unencrypted = JSONObject(TestData.REFRESH_TOKEN_SUCCESS_DECRYPTED)
every { dataEnvelope.decrypt(any<ByteArray>(), any(), any()) }.returns(
unencrypted.toString().toByteArray(),
)
// Ask the Client to generate a new Identity, so it can make the appropriate request.
client.generateIdentity(
IdentityRequest.Email("test@test.com"),
SUBSCRIPTION_ID,
PUBLIC_KEY,
)
}
}
@Test
fun `test version info - refresh identity`() {
every { dataEnvelope.decrypt(any<String>(), any(), any()) }.returns(
TestData.REFRESH_TOKEN_SUCCESS_DECRYPTED.toByteArray(),
)
testVersionInfo { client ->
// Ask the Client to refresh the Identity, so it can make the appropriate request.
client.refreshIdentity(refreshToken, TestData.REFRESH_TOKEN_ENCRYPTED_SUCCESS_KEY)
}
}
/**
* Helper function to test that the given callback will result in the correct exception when the [UID2Client] is
* configured with an invalid API URL.
*/
private fun testInvalidClientApi(callback: suspend (client: UID2Client) -> Unit) {
val client = UID2Client(
"this is not a url",
networkSession,
packageName,
dataEnvelope,
timeUtils,
keyUtils,
logger,
)
// Verify that when we have configured the client with an invalid URL, that it throws the appropriate exception
// when we try to fetch the client details.
assertThrows(InvalidApiUrlException::class.java) {
runTest(testDispatcher) {
callback(client)
}
}
}
/**
* Helper function to test that a given callback will result in the correct exception when the [UID2Client]
* experiences a network failure.
*/
private fun testNetworkFailure(callback: suspend (client: UID2Client) -> Unit) {
val client = withClient()
// Configure the network session to report a failure.
every { networkSession.loadData(any(), any()) }.returns(NetworkResponse(400))
// Verify that when a network failure occurs, the appropriate exception is thrown.
assertThrows(RequestFailureException::class.java) {
runTest(testDispatcher) {
callback(client)
}
}
}
/**
* Helper function to test that a given callback will result in the correct exception when the [UID2Client]
* receives an unexpected response.
*/
private fun testInvalidNetworkResponse(callback: suspend (client: UID2Client) -> Unit) {
val client = withClient()
// Configure the network session to return an invalid response.
every { networkSession.loadData(any(), any()) }.returns(
NetworkResponse(200, "This is not encrypted"),
)
// Verify that when an unexpected response is received, the appropriate exception is thrown.
assertThrows(PayloadDecryptException::class.java) {
runTest(testDispatcher) {
callback(client)
}
}
}
/**
* Helper function to test whether the expected version headers are included in a specific request, with the
* request being invoked via a given callback.
*/
private fun testVersionInfo(callback: suspend (client: UID2Client) -> Unit) = runTest(testDispatcher) {
val client = withClient()
// Configure the network session to return a valid (encrypted) payload and allows us to capture the given
// NetworkRequest.
var networkRequest: NetworkRequest? = null
every { networkSession.loadData(any(), any()) }.answers {
networkRequest = secondArg() as NetworkRequest?
NetworkResponse(200, "some data")
}
callback(client)
// Verify that the Client included the expected Version header, and that it ended with our SDK version.
val reportedVersion = networkRequest?.headers?.get("X-UID2-Client-Version")
assertNotNull(reportedVersion)
assertTrue(reportedVersion?.endsWith(UID2.getVersion()) == true)
}
private fun withClient() = UID2Client(
url,
networkSession,
packageName,
dataEnvelope,
timeUtils,
keyUtils,
logger,
)
}