From c7bb93f7433c30b4c2df98a0fb0bea607b91382f Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 6 Jun 2016 07:43:18 +1000 Subject: [PATCH] Inject hash + hashType into install method. This makes testing of the function easier, as the method previously expected a real file to exist on disk for which it could then hash. This instead allows mock hash values to be inserted when under test. Other than this, the semantics remain exactly the same as before, and the expensive hashing is still done on a worker thread as part of the `IntentService`. --- .../fdroid/data/InstalledAppProviderService.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java b/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java index f1959596f..3c54f5519 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java +++ b/app/src/main/java/org/fdroid/fdroid/data/InstalledAppProviderService.java @@ -156,7 +156,10 @@ public class InstalledAppProviderService extends IntentService { String packageName = intent.getData().getSchemeSpecificPart(); final String action = intent.getAction(); if (ACTION_INSERT.equals(action)) { - insertAppIntoDb(this, packageName, (PackageInfo) intent.getParcelableExtra(EXTRA_PACKAGE_INFO)); + PackageInfo packageInfo = intent.getParcelableExtra(EXTRA_PACKAGE_INFO); + String hashType = "sha256"; + String hash = Utils.getBinaryHash(new File(packageInfo.applicationInfo.publicSourceDir), hashType); + insertAppIntoDb(this, packageName, packageInfo, hashType, hash); } else if (ACTION_DELETE.equals(action)) { deleteAppFromDb(this, packageName); } @@ -164,7 +167,13 @@ public class InstalledAppProviderService extends IntentService { } } - static void insertAppIntoDb(Context context, String packageName, PackageInfo packageInfo) { + /** + * @param hash Although the has could be calculated within this function, it is helpful to inject + * the hash so as to be able to use this method during testing. Otherwise, the + * hashing method will try to hash a non-existent .apk file and try to insert NULL + * into the database when under test. + */ + static void insertAppIntoDb(Context context, String packageName, PackageInfo packageInfo, String hashType, String hash) { if (packageInfo == null) { try { packageInfo = context.getPackageManager().getPackageInfo(packageName, @@ -185,8 +194,6 @@ public class InstalledAppProviderService extends IntentService { contentValues.put(InstalledAppProvider.DataColumns.SIGNATURE, getPackageSig(packageInfo)); contentValues.put(InstalledAppProvider.DataColumns.LAST_UPDATE_TIME, packageInfo.lastUpdateTime); - String hashType = "sha256"; - String hash = Utils.getBinaryHash(new File(packageInfo.applicationInfo.publicSourceDir), hashType); contentValues.put(InstalledAppProvider.DataColumns.HASH_TYPE, hashType); contentValues.put(InstalledAppProvider.DataColumns.HASH, hash);