# ignite-117 : fix issue: add md5 checksum validation
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/dca51c3f Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/dca51c3f Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/dca51c3f Branch: refs/heads/sprint-1 Commit: dca51c3f24dfbcd16565099d98551e730285a149 Parents: 75544b2 Author: Artem SHutak <ashu...@gridgain.com> Authored: Tue Jan 27 17:42:02 2015 +0300 Committer: Artem SHutak <ashu...@gridgain.com> Committed: Tue Jan 27 17:42:02 2015 +0300 ---------------------------------------------------------------------- .../shmem/GridIpcSharedMemoryNativeLoader.java | 63 +++++++++++++++++--- ...GridIpcSharedMemoryNativeLoaderSelfTest.java | 32 +++++----- 2 files changed, 72 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dca51c3f/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoader.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoader.java b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoader.java index cb001cd..875d7ba 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoader.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoader.java @@ -17,14 +17,17 @@ package org.apache.ignite.internal.util.ipc.shmem; -import org.apache.ignite.*; -import org.apache.ignite.internal.*; -import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.internal.GridProductImpl; +import org.apache.ignite.internal.util.typedef.internal.U; import java.io.*; -import java.net.*; -import java.nio.channels.*; -import java.util.*; +import java.net.URL; +import java.nio.channels.FileLock; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Collection; +import java.util.LinkedList; /** * Shared memory native loader. @@ -209,7 +212,7 @@ public class GridIpcSharedMemoryNativeLoader { InputStream is = null; try { - if (!target.exists()) { + if (!target.exists() || ! haveEqualMD5(target, src)) { is = src.openStream(); if (is != null) { @@ -232,7 +235,7 @@ public class GridIpcSharedMemoryNativeLoader { return true; } - catch (IOException | UnsatisfiedLinkError | InterruptedException e) { + catch (IOException | UnsatisfiedLinkError | InterruptedException | NoSuchAlgorithmException e) { errs.add(e); } finally { @@ -242,4 +245,48 @@ public class GridIpcSharedMemoryNativeLoader { return false; } + + private static boolean haveEqualMD5(File target, URL src) throws NoSuchAlgorithmException, IOException { + String targetMD5 = calculateMD5(new FileInputStream(target)); + String srcMD5 = calculateMD5(src.openStream()); + + return targetMD5.equals(srcMD5); + } + + static byte[] calculateMD5Digest(InputStream input) throws NoSuchAlgorithmException, IOException { + MessageDigest md = MessageDigest.getInstance("MD5"); + InputStream fis = new BufferedInputStream(input); + byte[] dataBytes = new byte[1024]; + + int nread = 0; + + while ((nread = fis.read(dataBytes)) != -1) { + md.update(dataBytes, 0, nread); + }; + + byte[] md5Bytes = md.digest(); + + //convert the byte to hex format + StringBuffer sb = new StringBuffer(""); + for (int i = 0; i < md5Bytes.length; i++) { + sb.append(Integer.toString((md5Bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + + System.out.println("Digest(in hex format):: " + sb.toString()); + + return md5Bytes; + } + + static String calculateMD5(InputStream input) throws NoSuchAlgorithmException, IOException { + byte[] md5Bytes = calculateMD5Digest(input); + + //convert the byte to hex format + StringBuffer sb = new StringBuffer(""); + for (int i = 0; i < md5Bytes.length; i++) { + sb.append(Integer.toString((md5Bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + + return sb.toString(); + } + } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dca51c3f/modules/core/src/test/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoaderSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoaderSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoaderSelfTest.java index bf2396c..196be0a 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoaderSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/util/ipc/shmem/GridIpcSharedMemoryNativeLoaderSelfTest.java @@ -2,6 +2,7 @@ package org.apache.ignite.internal.util.ipc.shmem; import junit.framework.TestCase; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -9,28 +10,23 @@ import java.io.IOException; public class GridIpcSharedMemoryNativeLoaderSelfTest extends TestCase { private static final String DEFAULT_TMP_DIR = System.getProperty("java.io.tmpdir"); public static final String TMP_DIR_FOR_TEST = System.getProperty("user.home"); - public static final String LOADED_FILE_NAME = System.mapLibraryName(GridIpcSharedMemoryNativeLoader.libFileName()); - - @Override - public void setUp() throws Exception { - System.setProperty("java.io.tmpdir", TMP_DIR_FOR_TEST); - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - System.setProperty("java.io.tmpdir", DEFAULT_TMP_DIR); - } + public static final String LOADED_LIB_FILE_NAME = System.mapLibraryName(GridIpcSharedMemoryNativeLoader.libFileName()); //TODO linux specific public void testLoadIfLibFileWasCorrupted() throws Exception { - createCorruptedLibFile(); + try { + System.setProperty("java.io.tmpdir", TMP_DIR_FOR_TEST); - GridIpcSharedMemoryNativeLoader.load(); + createCorruptedLibFile(); + + GridIpcSharedMemoryNativeLoader.load(); + } finally { + System.setProperty("java.io.tmpdir", DEFAULT_TMP_DIR); + } } private void createCorruptedLibFile() throws IOException { - File loadedFile = new File(System.getProperty("java.io.tmpdir"), LOADED_FILE_NAME); + File loadedFile = new File(System.getProperty("java.io.tmpdir"), LOADED_LIB_FILE_NAME); if (loadedFile.exists()) assertTrue("Could not delete libggshem file.",loadedFile.delete()); @@ -42,4 +38,10 @@ public class GridIpcSharedMemoryNativeLoaderSelfTest extends TestCase { out.write("Corrupted information.\n".getBytes()); }; } + + public void testMD5Calculation() throws Exception { + String md5 = GridIpcSharedMemoryNativeLoader.calculateMD5(new ByteArrayInputStream("Corrupted information.".getBytes())); + + assertEquals("d7dbe555be2eee7fa658299850169fa1", md5); + } } \ No newline at end of file