From 55aa8e9aa6f3625582087f37c2ba1e2bc42ec497 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 14 Dec 2017 12:22:03 +0100 Subject: [PATCH] use standard buffer size for Utils.getBinaryHash() While a large buffer might make things slightly faster, the smaller buffer size should play much nicer when F-Droid is doing things in the background. Since calculating the hash is part of the update procedure, which can now happen in the background, this method will be often running in the background. The tests showed no difference in time between the large and small buffer. --- app/src/main/java/org/fdroid/fdroid/Utils.java | 2 +- .../test/java/org/fdroid/fdroid/UtilsTest.java | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/Utils.java b/app/src/main/java/org/fdroid/fdroid/Utils.java index 52a8b5441..9c16d4674 100644 --- a/app/src/main/java/org/fdroid/fdroid/Utils.java +++ b/app/src/main/java/org/fdroid/fdroid/Utils.java @@ -409,7 +409,7 @@ public final class Utils { fis = new FileInputStream(apk); BufferedInputStream bis = new BufferedInputStream(fis); - byte[] dataBytes = new byte[524288]; + byte[] dataBytes = new byte[8192]; int nread; while ((nread = bis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); diff --git a/app/src/test/java/org/fdroid/fdroid/UtilsTest.java b/app/src/test/java/org/fdroid/fdroid/UtilsTest.java index 116d399c4..bb9589da0 100644 --- a/app/src/test/java/org/fdroid/fdroid/UtilsTest.java +++ b/app/src/test/java/org/fdroid/fdroid/UtilsTest.java @@ -2,7 +2,6 @@ package org.fdroid.fdroid; import android.content.Context; - import org.fdroid.fdroid.views.AppDetailsRecyclerViewAdapter; import org.junit.Test; import org.junit.runner.RunWith; @@ -10,6 +9,8 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import java.io.File; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -174,6 +175,21 @@ public class UtilsTest { } } + @Test + public void testGetBinaryHash() { + File f = TestUtils.copyResourceToTempFile("largeRepo.xml"); + assertEquals("df1754aa4b56c86c06d7842dfd02064f0781c1f740f489d3fc158bb541c8d197", + Utils.getBinaryHash(f, "sha256")); + f = TestUtils.copyResourceToTempFile("masterKeyIndex.jar"); + assertEquals("625d5aedcd0499fe04ebab81f3c7ae30c236cee653a914ffb587d890198f3aba", + Utils.getBinaryHash(f, "sha256")); + f = TestUtils.copyResourceToTempFile("index.fdroid.2016-10-30.jar"); + assertEquals("c138b503c6475aa749585d0e3ad4dba3546b6d33ec485efd8ac8bd603d93fedb", + Utils.getBinaryHash(f, "sha256")); + f = TestUtils.copyResourceToTempFile("index.fdroid.2016-11-10.jar"); + assertEquals("93bea45814fd8955cabb957e7a3f8790d6c568eaa16fa30425c2d26c60490bde", + Utils.getBinaryHash(f, "sha256")); + } // TODO write tests that work with a Certificate }