summaryrefslogtreecommitdiff
path: root/core/java/android/util/HashedStringCache.java
diff options
context:
space:
mode:
authorSusi Kharraz-Post <susikp@google.com>2019-04-04 11:19:20 -0400
committerSusi Kharraz-Post <susikp@google.com>2019-04-04 15:15:33 -0400
commita7f614519e571f6ef6d72b3c36abba13de78138f (patch)
treeb2e1fdc02e37c117775d9c02b7e10e59cd4005b4 /core/java/android/util/HashedStringCache.java
parent0454f3b8aefdf1387468a96e6de60530fcf55f83 (diff)
Fast follow-on unit tests for HashedStringCache
Unit tests for HashedStringCache that was commited in earlier CL ag/6867725 . This is testing the various inputs and expected outputs. Testing also revealed some vulnerability for invalid input so added validation in the code under test. Bug: b/129870147 Test: This is the test file Change-Id: I7387f808df87a869f81339cd4aea99b23dfc06bd
Diffstat (limited to 'core/java/android/util/HashedStringCache.java')
-rw-r--r--core/java/android/util/HashedStringCache.java15
1 files changed, 10 insertions, 5 deletions
diff --git a/core/java/android/util/HashedStringCache.java b/core/java/android/util/HashedStringCache.java
index 8ce85148c7c2..1f2b95650288 100644
--- a/core/java/android/util/HashedStringCache.java
+++ b/core/java/android/util/HashedStringCache.java
@@ -22,6 +22,8 @@ import android.os.Environment;
import android.os.storage.StorageManager;
import android.text.TextUtils;
+import com.android.internal.annotations.VisibleForTesting;
+
import java.io.File;
import java.nio.charset.Charset;
import java.security.MessageDigest;
@@ -32,7 +34,6 @@ import java.security.SecureRandom;
* HashedStringCache provides hashing functionality with an underlying LRUCache and expiring salt.
* Salt and expiration time are being stored under the tag passed in by the calling package --
* intended usage is the calling package name.
- * TODO: Add unit tests b/129870147
* @hide
*/
public class HashedStringCache {
@@ -40,9 +41,12 @@ public class HashedStringCache {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final int HASH_CACHE_SIZE = 100;
private static final int HASH_LENGTH = 8;
- private static final String HASH_SALT = "_hash_salt";
- private static final String HASH_SALT_DATE = "_hash_salt_date";
- private static final String HASH_SALT_GEN = "_hash_salt_gen";
+ @VisibleForTesting
+ static final String HASH_SALT = "_hash_salt";
+ @VisibleForTesting
+ static final String HASH_SALT_DATE = "_hash_salt_date";
+ @VisibleForTesting
+ static final String HASH_SALT_GEN = "_hash_salt_gen";
// For privacy we need to rotate the salt regularly
private static final long DAYS_TO_MILLIS = 1000 * 60 * 60 * 24;
private static final int MAX_SALT_DAYS = 100;
@@ -94,7 +98,8 @@ public class HashedStringCache {
*/
public HashResult hashString(Context context, String tag, String clearText,
int saltExpirationDays) {
- if (TextUtils.isEmpty(clearText) || saltExpirationDays == -1) {
+ if (saltExpirationDays == -1 || context == null
+ || TextUtils.isEmpty(clearText) || TextUtils.isEmpty(tag)) {
return null;
}