Skip to content

Commit 1e4a4ee

Browse files
committed
[MOB-9235] Make encryptor support old versions
1 parent d1d2dff commit 1e4a4ee

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

iterableapi/src/test/java/com/iterable/iterableapi/IterableDataEncryptorTest.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -304,28 +304,28 @@ public void testDecryptionAfterKeyLoss() {
304304
@Test
305305
public void testEncryptionAcrossApiLevels() {
306306
String testData = "test data for cross-version compatibility";
307-
307+
308308
// Test API 16 (Legacy)
309309
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
310310
String encryptedOnApi16 = encryptor.encrypt(testData);
311-
311+
312312
// Test API 18 (Legacy)
313313
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_MR2);
314314
String encryptedOnApi18 = encryptor.encrypt(testData);
315315
assertEquals("Legacy decryption should work on API 18", testData, encryptor.decrypt(encryptedOnApi16));
316-
316+
317317
// Test API 19 (Modern - First version with GCM support)
318318
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.KITKAT);
319319
String encryptedOnApi19 = encryptor.encrypt(testData);
320320
assertEquals("Should decrypt legacy data on API 19", testData, encryptor.decrypt(encryptedOnApi16));
321321
assertEquals("Should decrypt legacy data on API 19", testData, encryptor.decrypt(encryptedOnApi18));
322-
322+
323323
// Test API 23 (Modern with KeyStore)
324324
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.M);
325325
String encryptedOnApi23 = encryptor.encrypt(testData);
326326
assertEquals("Should decrypt legacy data on API 23", testData, encryptor.decrypt(encryptedOnApi16));
327327
assertEquals("Should decrypt API 19 data on API 23", testData, encryptor.decrypt(encryptedOnApi19));
328-
328+
329329
// Test that modern encryption fails on legacy devices
330330
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
331331
try {
@@ -347,13 +347,13 @@ public void testEncryptionAcrossApiLevels() {
347347
@Test
348348
public void testEncryptionMethodFlag() {
349349
String testData = "test data for encryption method verification";
350-
350+
351351
// Test legacy encryption flag (API 16)
352352
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
353353
String legacyEncrypted = encryptor.encrypt(testData);
354354
byte[] legacyBytes = Base64.decode(legacyEncrypted, Base64.NO_WRAP);
355355
assertEquals("Legacy encryption should have flag 0", 0, legacyBytes[0]);
356-
356+
357357
// Test modern encryption flag (API 19)
358358
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.KITKAT);
359359
String modernEncrypted = encryptor.encrypt(testData);
@@ -366,11 +366,11 @@ public void testDecryptCorruptData() {
366366
String testData = "test data";
367367
String encrypted = encryptor.encrypt(testData);
368368
byte[] bytes = Base64.decode(encrypted, Base64.NO_WRAP);
369-
369+
370370
// Corrupt the data portion
371371
bytes[bytes.length - 1] ^= 0xFF;
372372
String corrupted = Base64.encodeToString(bytes, Base64.NO_WRAP);
373-
373+
374374
try {
375375
encryptor.decrypt(corrupted);
376376
fail("Should throw exception for corrupted data");
@@ -385,11 +385,11 @@ public void testDecryptManipulatedIV() {
385385
String testData = "test data";
386386
String encrypted = encryptor.encrypt(testData);
387387
byte[] bytes = Base64.decode(encrypted, Base64.NO_WRAP);
388-
388+
389389
// Manipulate the IV
390390
bytes[1] ^= 0xFF; // First byte after version flag
391391
String manipulated = Base64.encodeToString(bytes, Base64.NO_WRAP);
392-
392+
393393
try {
394394
encryptor.decrypt(manipulated);
395395
fail("Should throw exception for manipulated IV");
@@ -403,15 +403,15 @@ public void testDecryptManipulatedIV() {
403403
public void testDecryptManipulatedVersionFlag() {
404404
// Test on API 16 device
405405
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
406-
406+
407407
String testData = "test data";
408408
String encrypted = encryptor.encrypt(testData);
409409
byte[] bytes = Base64.decode(encrypted, Base64.NO_WRAP);
410-
410+
411411
// Change version flag from legacy (0) to modern (1)
412412
bytes[0] = 1;
413413
String manipulated = Base64.encodeToString(bytes, Base64.NO_WRAP);
414-
414+
415415
try {
416416
encryptor.decrypt(manipulated);
417417
fail("Should throw exception for manipulated version flag");
@@ -425,26 +425,26 @@ public void testDecryptManipulatedVersionFlag() {
425425
public void testLegacyEncryptionAndDecryption() {
426426
// Set to API 16 (Legacy)
427427
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
428-
428+
429429
String testData = "test data for legacy encryption";
430430
String encrypted = encryptor.encrypt(testData);
431431
String decrypted = encryptor.decrypt(encrypted);
432-
432+
433433
assertEquals("Legacy encryption/decryption should work on API 16", testData, decrypted);
434-
434+
435435
// Verify it's using legacy encryption
436436
byte[] encryptedBytes = Base64.decode(encrypted, Base64.NO_WRAP);
437437
assertEquals("Should use legacy encryption flag", 0, encryptedBytes[0]);
438-
438+
439439
// Test on API 18
440440
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_MR2);
441441
String decryptedOnApi18 = encryptor.decrypt(encrypted);
442442
assertEquals("Legacy data should be decryptable on API 18", testData, decryptedOnApi18);
443-
443+
444444
String encryptedOnApi18 = encryptor.encrypt(testData);
445445
String decryptedFromApi18 = encryptor.decrypt(encryptedOnApi18);
446446
assertEquals("API 18 encryption/decryption should work", testData, decryptedFromApi18);
447-
447+
448448
// Verify API 18 also uses legacy encryption
449449
byte[] api18EncryptedBytes = Base64.decode(encryptedOnApi18, Base64.NO_WRAP);
450450
assertEquals("Should use legacy encryption flag on API 18", 0, api18EncryptedBytes[0]);
@@ -453,25 +453,25 @@ public void testLegacyEncryptionAndDecryption() {
453453
@Test
454454
public void testModernEncryptionAndDecryption() {
455455
String testData = "test data for modern encryption";
456-
456+
457457
// Test on API 19 (First modern version)
458458
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.KITKAT);
459459
String encryptedOnApi19 = encryptor.encrypt(testData);
460460
String decryptedOnApi19 = encryptor.decrypt(encryptedOnApi19);
461461
assertEquals("Modern encryption should work on API 19", testData, decryptedOnApi19);
462-
462+
463463
byte[] api19EncryptedBytes = Base64.decode(encryptedOnApi19, Base64.NO_WRAP);
464464
assertEquals("Should use modern encryption flag on API 19", 1, api19EncryptedBytes[0]);
465-
465+
466466
// Test on API 23
467467
setFinalStatic(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.M);
468468
String decryptedOnApi23 = encryptor.decrypt(encryptedOnApi19);
469469
assertEquals("API 19 data should be decryptable on API 23", testData, decryptedOnApi23);
470-
470+
471471
String encryptedOnApi23 = encryptor.encrypt(testData);
472472
String decryptedFromApi23 = encryptor.decrypt(encryptedOnApi23);
473473
assertEquals("API 23 encryption/decryption should work", testData, decryptedFromApi23);
474-
474+
475475
byte[] api23EncryptedBytes = Base64.decode(encryptedOnApi23, Base64.NO_WRAP);
476476
assertEquals("Should use modern encryption flag on API 23", 1, api23EncryptedBytes[0]);
477477
}
@@ -480,11 +480,11 @@ private static void setFinalStatic(Class<?> clazz, String fieldName, Object newV
480480
try {
481481
Field field = clazz.getDeclaredField(fieldName);
482482
field.setAccessible(true);
483-
483+
484484
Field modifiersField = Field.class.getDeclaredField("modifiers");
485485
modifiersField.setAccessible(true);
486486
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
487-
487+
488488
field.set(null, newValue);
489489
} catch (Exception e) {
490490
throw new RuntimeException(e);

0 commit comments

Comments
 (0)