-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathIterableKeychainTest.kt
More file actions
377 lines (302 loc) · 13.9 KB
/
IterableKeychainTest.kt
File metadata and controls
377 lines (302 loc) · 13.9 KB
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
package com.iterable.iterableapi
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import android.util.Base64
import org.junit.Before
import org.junit.After
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.Mockito.times
import org.mockito.MockitoAnnotations
import org.junit.Assert.*
import org.mockito.ArgumentMatchers.eq
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.isNull
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.MockedStatic
import org.mockito.Mockito.mockStatic
import org.mockito.Mockito.doThrow
import org.mockito.Mockito.mock
import org.mockito.Mockito.clearInvocations
import org.mockito.ArgumentMatchers.matches
import org.mockito.Mockito.never
class IterableKeychainTest {
@Mock private lateinit var mockContext: Context
@Mock private lateinit var mockSharedPrefs: SharedPreferences
@Mock private lateinit var mockEditor: SharedPreferences.Editor
@Mock private lateinit var mockDecryptionFailureHandler: IterableDecryptionFailureHandler
@Mock private lateinit var mockEncryptor: IterableDataEncryptor
private lateinit var keychain: IterableKeychain
private lateinit var mockedLog: MockedStatic<Log>
private lateinit var mockedBase64: MockedStatic<Base64>
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
// Mock Android Log
mockedLog = mockStatic(Log::class.java)
// Mock Android Base64
mockedBase64 = mockStatic(Base64::class.java)
`when`(Base64.encodeToString(any(), anyInt())).thenReturn("mocked_base64_string")
`when`(mockContext.getSharedPreferences(
any<String>(),
eq(Context.MODE_PRIVATE)
)).thenReturn(mockSharedPrefs)
`when`(mockSharedPrefs.edit()).thenReturn(mockEditor)
`when`(mockEditor.putString(any<String>(), any())).thenReturn(mockEditor)
`when`(mockEditor.remove(any<String>())).thenReturn(mockEditor)
`when`(mockEditor.putBoolean(any<String>(), anyBoolean())).thenReturn(mockEditor)
// Mock migration-related SharedPreferences calls
`when`(mockSharedPrefs.contains(any<String>())).thenReturn(false)
`when`(mockSharedPrefs.getBoolean(any<String>(), anyBoolean())).thenReturn(false)
`when`(mockSharedPrefs.getString(any<String>(), any())).thenReturn(null)
// Mock editor.apply() to do nothing
Mockito.doNothing().`when`(mockEditor).apply()
keychain = IterableKeychain(
mockContext,
mockDecryptionFailureHandler
)
// Directly set the mock encryptor
keychain.encryptor = mockEncryptor
// Setup encrypt/decrypt behavior
`when`(mockEncryptor.encrypt(any())).thenAnswer { invocation ->
val input = invocation.arguments[0] as String?
input?.let { "encrypted_$it" }
}
`when`(mockEncryptor.decrypt(any())).thenAnswer { invocation ->
val encrypted = invocation.arguments[0] as String?
if (encrypted == null) {
null
} else if (encrypted.startsWith("encrypted_")) {
encrypted.substring("encrypted_".length)
} else {
throw IterableDataEncryptor.DecryptionException("Invalid encrypted value")
}
}
// Reset the verification count after setup
Mockito.clearInvocations(mockEditor)
}
@After
fun tearDown() {
mockedLog.close()
mockedBase64.close()
}
@Test
fun testSaveAndGetEmail() {
val testEmail = "test@example.com"
// Update mock to return the encrypted value
`when`(mockSharedPrefs.getString(eq("iterable-email"), isNull()))
.thenReturn("encrypted_$testEmail")
keychain.saveEmail(testEmail)
verify(mockEditor).putString(eq("iterable-email"), eq("encrypted_$testEmail"))
verify(mockEditor).apply()
val retrievedEmail = keychain.getEmail()
assertEquals(testEmail, retrievedEmail)
}
@Test
fun testSaveAndGetUserId() {
val testUserId = "user123"
// Update mock to return the encrypted value
`when`(mockSharedPrefs.getString(eq("iterable-user-id"), isNull()))
.thenReturn("encrypted_$testUserId")
keychain.saveUserId(testUserId)
verify(mockEditor).putString(eq("iterable-user-id"), eq("encrypted_$testUserId"))
verify(mockEditor).apply()
val retrievedUserId = keychain.getUserId()
assertEquals(testUserId, retrievedUserId)
}
@Test
fun testSaveAndGetAuthToken() {
val testToken = "auth-token-123"
// Update mock to return the encrypted value
`when`(mockSharedPrefs.getString(eq("iterable-auth-token"), isNull()))
.thenReturn("encrypted_$testToken")
keychain.saveAuthToken(testToken)
verify(mockEditor).putString(eq("iterable-auth-token"), eq("encrypted_$testToken"))
verify(mockEditor).apply()
val retrievedToken = keychain.getAuthToken()
assertEquals(testToken, retrievedToken)
}
@Test
fun testDecryptionFailure() {
// Setup mock to throw runtime exception instead
`when`(mockEncryptor.decrypt(any())).thenAnswer {
throw RuntimeException("Test decryption failed")
}
`when`(mockSharedPrefs.getString(eq("iterable-email"), isNull()))
.thenReturn("any_encrypted_value")
val result = keychain.getEmail()
// Verify data was cleared
verify(mockEditor).remove("iterable-email")
verify(mockEditor).remove("iterable-user-id")
verify(mockEditor).remove("iterable-auth-token")
verify(mockEditor).apply()
// Verify failure handler was called with any exception
verify(mockDecryptionFailureHandler).onDecryptionFailed(any())
// Verify encryptor keys were reset
verify(mockEncryptor).resetKeys()
assertNull(result)
}
@Test
fun testDecryptionFailureForAllOperations() {
// Setup mock to throw runtime exception
`when`(mockEncryptor.decrypt(any())).thenAnswer {
throw RuntimeException("Test decryption failed")
}
`when`(mockSharedPrefs.getString(any(), isNull())).thenReturn("any_encrypted_value")
// Test all getter methods
assertNull(keychain.getEmail())
assertNull(keychain.getUserId())
assertNull(keychain.getAuthToken())
// Verify failure handler was called exactly once for each operation
verify(mockDecryptionFailureHandler, times(3)).onDecryptionFailed(any())
// Verify keys were reset for each failure
verify(mockEncryptor, times(3)).resetKeys()
}
@Test
fun testSaveNullValues() {
keychain.saveEmail(null)
keychain.saveUserId(null)
keychain.saveAuthToken(null)
// Verify remove calls for both key and plaintext flag
verify(mockEditor, times(6)).remove(any()) // 2 removes per save (key + plaintext flag)
verify(mockEditor, times(3)).remove(matches(".*_plaintext"))
// Verify exactly one apply call for each save operation
verify(mockEditor, times(3)).apply()
}
@Test
fun testConcurrentAccess() {
val testEmail = "test@example.com"
val threads = mutableListOf<Thread>()
val exceptions = mutableListOf<Exception>()
// Simulate multiple threads accessing keychain
for (i in 1..5) {
threads.add(Thread {
try {
keychain.saveEmail(testEmail)
assertEquals(testEmail, keychain.getEmail())
} catch (e: Exception) {
exceptions.add(e)
}
})
}
threads.forEach { it.start() }
threads.forEach { it.join() }
assertTrue("Concurrent access caused exceptions: $exceptions", exceptions.isEmpty())
}
@Test
fun testMigrationFailure() {
// Mock migration to throw exception
val mockMigrator = mock(IterableKeychainEncryptedDataMigrator::class.java)
val migrationException = RuntimeException("Test migration failed")
doThrow(migrationException).`when`(mockMigrator).attemptMigration()
// Create new keychain with mock migrator
keychain = IterableKeychain(
mockContext,
mockDecryptionFailureHandler,
mockMigrator
)
// Verify data was cleared
verify(mockEditor).remove(eq("iterable-email"))
verify(mockEditor).remove(eq("iterable-user-id"))
verify(mockEditor).remove(eq("iterable-auth-token"))
verify(mockEditor).apply()
// Verify failure handler was called
verify(mockDecryptionFailureHandler).onDecryptionFailed(migrationException)
}
@Test
fun testMigrationOnlyAttemptedOnce() {
// Create mock migrator
val mockMigrator = mock(IterableKeychainEncryptedDataMigrator::class.java)
// First check returns false, subsequent checks return true
`when`(mockMigrator.isMigrationCompleted())
.thenReturn(false) // first call
// First initialization
keychain = IterableKeychain(
mockContext,
mockDecryptionFailureHandler,
mockMigrator
)
`when`(mockMigrator.isMigrationCompleted())
.thenReturn(true) // subsequent calls
// Second initialization
keychain = IterableKeychain(
mockContext,
mockDecryptionFailureHandler,
mockMigrator
)
// Verify attemptMigration was called exactly once
verify(mockMigrator, times(1)).attemptMigration()
}
@Test
fun testEncryptionAndDecryptionFailure() {
// Setup encryption and decryption to fail
`when`(mockEncryptor.encrypt(any())).thenThrow(RuntimeException("Simulated encryption failure"))
`when`(mockEncryptor.decrypt(any())).thenThrow(RuntimeException("Simulated decryption failure"))
val testData = "test data for encryption failure"
// Save data - should fall back to plaintext
keychain.saveUserId(testData)
// Verify plaintext save operations
verify(mockEditor).putString(eq("iterable-user-id"), eq(testData))
verify(mockEditor).putBoolean(eq("iterable-user-id_plaintext"), eq(true))
// Setup SharedPreferences to return the saved data
`when`(mockSharedPrefs.getString(eq("iterable-user-id"), isNull())).thenReturn(testData)
`when`(mockSharedPrefs.getBoolean(eq("iterable-user-id_plaintext"), eq(false))).thenReturn(true)
// Verify data can be retrieved
assertEquals(testData, keychain.getUserId())
// Verify encryption was attempted and failed
verify(mockEncryptor).encrypt(eq(testData))
verify(mockEncryptor, never()).decrypt(any()) // Should not attempt decryption for plaintext
// Test null handling
clearInvocations(mockEditor)
keychain.saveUserId(null)
verify(mockEditor).remove(eq("iterable-user-id"))
verify(mockEditor).remove(eq("iterable-user-id_plaintext"))
// Verify no more encryption attempts for null
verify(mockEncryptor, never()).encrypt(isNull())
verify(mockEncryptor, never()).decrypt(isNull())
}
@Test
fun testEncryptionDisabled() {
// Create a new keychain with encryption disabled
val plaintextKeychain = IterableKeychain(
mockContext,
mockDecryptionFailureHandler,
null,
true
)
val testEmail = "test@example.com"
val testUserId = "user123"
val testToken = "auth-token-123"
// Mock the SharedPreferences to return plaintext values
`when`(mockSharedPrefs.getString(eq("iterable-email"), isNull())).thenReturn(testEmail)
`when`(mockSharedPrefs.getString(eq("iterable-user-id"), isNull())).thenReturn(testUserId)
`when`(mockSharedPrefs.getString(eq("iterable-auth-token"), isNull())).thenReturn(testToken)
// Mock plaintext flag checks to return true when encryption is disabled
`when`(mockSharedPrefs.getBoolean(eq("iterable-email_plaintext"), eq(false))).thenReturn(true)
`when`(mockSharedPrefs.getBoolean(eq("iterable-user-id_plaintext"), eq(false))).thenReturn(true)
`when`(mockSharedPrefs.getBoolean(eq("iterable-auth-token_plaintext"), eq(false))).thenReturn(true)
// Test save operations
plaintextKeychain.saveEmail(testEmail)
plaintextKeychain.saveUserId(testUserId)
plaintextKeychain.saveAuthToken(testToken)
// Verify values are stored as plaintext
verify(mockEditor).putString(eq("iterable-email"), eq(testEmail))
verify(mockEditor).putString(eq("iterable-user-id"), eq(testUserId))
verify(mockEditor).putString(eq("iterable-auth-token"), eq(testToken))
// Verify plaintext suffix flags are set for better compatibility
verify(mockEditor).putBoolean(eq("iterable-email_plaintext"), eq(true))
verify(mockEditor).putBoolean(eq("iterable-user-id_plaintext"), eq(true))
verify(mockEditor).putBoolean(eq("iterable-auth-token_plaintext"), eq(true))
// Test get operations
assertEquals(testEmail, plaintextKeychain.getEmail())
assertEquals(testUserId, plaintextKeychain.getUserId())
assertEquals(testToken, plaintextKeychain.getAuthToken())
// Verify IterableDataEncryptor was never created
assertNull(plaintextKeychain.encryptor)
}
}