Skip to content

Commit bc6b467

Browse files
committed
fix: update CryptoUtilTest mocks for refactored exception handling
1 parent f79f8f1 commit bc6b467

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

auth0/src/main/java/com/auth0/android/authentication/storage/CryptoUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ private byte[] tryMigrateLegacyAESKey() {
617617

618618
Log.d(TAG, "Legacy AES key migrated successfully");
619619
return decryptedAESKey;
620-
} catch (Exception e) {
620+
} catch (CryptoException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException |
621+
BadPaddingException | IllegalBlockSizeException | IllegalArgumentException e) {
621622
Log.e(TAG, "Could not migrate legacy AES key. Will generate new key.", e);
622623
deleteAESKeys();
623624
return null;

auth0/src/test/java/com/auth0/android/authentication/storage/CryptoUtilTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public class CryptoUtilTest {
119119
public void setUp() throws Exception {
120120
PowerMockito.mockStatic(Log.class);
121121
PowerMockito.mockStatic(TextUtils.class);
122-
PowerMockito.when(TextUtils.isEmpty(anyString())).then((Answer<Boolean>) invocation -> {
122+
PowerMockito.when(TextUtils.isEmpty(nullable(String.class))).then((Answer<Boolean>) invocation -> {
123123
String input = invocation.getArgument(0, String.class);
124124
return input == null || input.isEmpty();
125125
});
@@ -1781,14 +1781,14 @@ public void shouldHandleKeyStoreErrorDuringMigration() throws Exception {
17811781
when(keyGenerator.generateKey()).thenReturn(mockSecretKey);
17821782

17831783
byte[] encryptedNewKey = new byte[]{30, 31, 32, 33};
1784-
doReturn(encryptedNewKey).when(cryptoUtil).RSAEncrypt(newAESKey);
1784+
doReturn(encryptedNewKey).when(cryptoUtil).RSAEncrypt(any(byte[].class));
17851785
String encodedNewKey = "new_generated_key";
17861786
PowerMockito.when(Base64.encode(encryptedNewKey, Base64.DEFAULT))
17871787
.thenReturn(encodedNewKey.getBytes(StandardCharsets.UTF_8));
17881788

17891789
byte[] result = cryptoUtil.getAESKey();
17901790

1791-
Mockito.verify(storage, times(2)).remove(KEY_ALIAS);
1791+
Mockito.verify(storage, times(1)).remove(KEY_ALIAS);
17921792

17931793
assertThat(result, is(newAESKey));
17941794
Mockito.verify(storage).store(KEY_ALIAS, encodedNewKey);
@@ -1930,14 +1930,15 @@ public void shouldGenerateNewKeyWhenMigrationFails() throws Exception {
19301930
when(keyGenerator.generateKey()).thenReturn(mockSecretKey);
19311931

19321932
byte[] encryptedNewKey = new byte[]{40, 41, 42};
1933-
doReturn(encryptedNewKey).when(cryptoUtil).RSAEncrypt(newAESKey);
1933+
doReturn(encryptedNewKey).when(cryptoUtil).RSAEncrypt(any(byte[].class));
19341934
String encodedNewKey = "fresh_key";
19351935
PowerMockito.when(Base64.encode(encryptedNewKey, Base64.DEFAULT))
19361936
.thenReturn(encodedNewKey.getBytes(StandardCharsets.UTF_8));
19371937
byte[] result = cryptoUtil.getAESKey();
19381938
assertThat(result, is(newAESKey));
19391939
Mockito.verify(storage).store(KEY_ALIAS, encodedNewKey);
1940-
Mockito.verify(storage, times(2)).remove(KEY_ALIAS);
1941-
Mockito.verify(storage, times(2)).remove(OLD_KEY_ALIAS);
1940+
// deleteAESKeys() is called once in tryMigrateLegacyAESKey when getRSAKeyEntry throws
1941+
Mockito.verify(storage, times(1)).remove(KEY_ALIAS);
1942+
Mockito.verify(storage, times(1)).remove(OLD_KEY_ALIAS);
19421943
}
19431944
}

0 commit comments

Comments
 (0)