Skip to content

Commit

Permalink
# ignite-117 : fix issue: add md5 checksum validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutakGG committed Jan 27, 2015
1 parent 75544b2 commit dca51c3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
Expand Up @@ -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.
Expand Down Expand Up @@ -209,7 +212,7 @@ private static boolean extract(Collection<Throwable> errs, URL src, File target)
InputStream is = null;

try {
if (!target.exists()) {
if (!target.exists() || ! haveEqualMD5(target, src)) {
is = src.openStream();

if (is != null) {
Expand All @@ -232,7 +235,7 @@ private static boolean extract(Collection<Throwable> errs, URL src, File target)

return true;
}
catch (IOException | UnsatisfiedLinkError | InterruptedException e) {
catch (IOException | UnsatisfiedLinkError | InterruptedException | NoSuchAlgorithmException e) {
errs.add(e);
}
finally {
Expand All @@ -242,4 +245,48 @@ private static boolean extract(Collection<Throwable> errs, URL src, File target)

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();
}

}
Expand Up @@ -2,35 +2,31 @@

import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
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());
Expand All @@ -42,4 +38,10 @@ private void createCorruptedLibFile() throws IOException {
out.write("Corrupted information.\n".getBytes());
};
}

public void testMD5Calculation() throws Exception {
String md5 = GridIpcSharedMemoryNativeLoader.calculateMD5(new ByteArrayInputStream("Corrupted information.".getBytes()));

assertEquals("d7dbe555be2eee7fa658299850169fa1", md5);
}
}

0 comments on commit dca51c3

Please sign in to comment.