From 8352ecd00641d4df238ec6f103c75b97efe37648 Mon Sep 17 00:00:00 2001 From: aozarov Date: Wed, 27 May 2015 10:50:31 -0700 Subject: [PATCH] add a checksum verification --- .../gcloud/datastore/LocalGcdHelper.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java index 47cd63b19ab0..7ed9a4e830cf 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java @@ -20,6 +20,7 @@ import com.google.common.base.Strings; +import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; @@ -31,6 +32,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.math.BigInteger; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; @@ -42,6 +44,8 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.Locale; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -59,6 +63,7 @@ public class LocalGcdHelper { public static final int PORT = 8080; private static final String GCD = "gcd-v1beta2-rev1-2.1.2b"; private static final String GCD_FILENAME = GCD + ".zip"; + private static final String MD5_CHECKSUM = "d84384cdfa8658e1204f4f8be51300e8"; private static final URL GCD_URL; static { @@ -69,7 +74,6 @@ public class LocalGcdHelper { } } - private static class ProcessStreamReader extends Thread { private final Process process; @@ -134,7 +138,7 @@ public void start() throws IOException, InterruptedException { // check if we already have a local copy of the gcd utility and download it if not. File gcdZipFile = new File(System.getProperty("java.io.tmpdir"), GCD_FILENAME); - if (!gcdZipFile.exists()) { + if (!gcdZipFile.exists() || !MD5_CHECKSUM.equals(md5(gcdZipFile))) { ReadableByteChannel rbc = Channels.newChannel(GCD_URL.openStream()); FileOutputStream fos = new FileOutputStream(gcdZipFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); @@ -188,6 +192,22 @@ public void start() throws IOException, InterruptedException { processReader = ProcessStreamReader.start(temp, "Dev App Server is now running"); } + private static String md5(File gcdZipFile) throws IOException { + try { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + try (InputStream is = new BufferedInputStream(new FileInputStream(gcdZipFile))) { + byte[] bytes = new byte[4 * 1024 * 1024]; + int len; + while ((len = is.read(bytes)) >= 0) { + md5.update(bytes, 0, len); + } + } + return String.format("%032x",new BigInteger(1, md5.digest())); + } catch (NoSuchAlgorithmException e) { + throw new IOException(e); + } + } + private static boolean isWindows() { return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).indexOf("windows") > -1; }