Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package org.dataone.hashstore.exceptions;

import java.util.Map;

/**
* An exception thrown when a checksum does not match what is expected.
*/

public class NonMatchingChecksumException extends IllegalArgumentException {

public NonMatchingChecksumException(String message) {
private final Map<String, String> hexDigests;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check code to make sure if the hexDigests can be null or not?


public NonMatchingChecksumException(String message, Map<String, String> checksumMap) {
super(message);
this.hexDigests = checksumMap;
}

public Map<String, String> getHexDigests() {
return hexDigests;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ public void deleteIfInvalidObject(
+ ". Actual checksum calculated: " + digestFromHexDigests + " (algorithm: "
+ checksumAlgorithm + ")";
logFileHashStore.error(errMsg);
throw new NonMatchingChecksumException(errMsg);
throw new NonMatchingChecksumException(errMsg, hexDigests);
}
// Validate size
if (objInfoRetrievedSize != objSize) {
Expand Down Expand Up @@ -1258,6 +1258,7 @@ protected void validateTmpObject(
}

if (compareChecksum) {
FileHashStoreUtility.ensureNotNull(hexDigests, "hexDigests");
logFileHashStore.info("Validating object, checksum arguments supplied and valid.");
String digestFromHexDigests = hexDigests.get(checksumAlgorithm);
if (digestFromHexDigests == null) {
Expand All @@ -1269,7 +1270,7 @@ protected void validateTmpObject(
String errMsg = baseErrMsg + ". Failed to delete tmpFile: " + tmpFile + ". "
+ ge.getMessage();
logFileHashStore.error(errMsg);
throw new NonMatchingChecksumException(errMsg);
throw new NonMatchingChecksumException(errMsg, hexDigests);
}
String errMsg = baseErrMsg + ". tmpFile has been deleted: " + tmpFile;
logFileHashStore.error(errMsg);
Expand All @@ -1285,12 +1286,12 @@ protected void validateTmpObject(
String errMsg = baseErrMsg + ". Failed to delete tmpFile: " + tmpFile + ". "
+ ge.getMessage();
logFileHashStore.error(errMsg);
throw new NonMatchingChecksumException(errMsg);
throw new NonMatchingChecksumException(errMsg, hexDigests);
}

String errMsg = baseErrMsg + ". tmpFile has been deleted: " + tmpFile;
logFileHashStore.error(errMsg);
throw new NonMatchingChecksumException(errMsg);
throw new NonMatchingChecksumException(errMsg, hexDigests);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ public ObjectMetadata storeHardLink(

try (InputStream fileStream = Files.newInputStream(filePath)) {
Map<String, String> hexDigests = generateChecksums(fileStream, checksumAlgorithm);
FileHashStoreUtility.ensureNotNull(hexDigests, "hexDigests");
String checksumToMatch = hexDigests.get(checksumAlgorithm);
if (!checksum.equalsIgnoreCase(checksumToMatch)) {
String errMsg = "Checksum supplied: " + checksum + " does not match what has been"
+ " calculated: " + checksumToMatch + " for pid: " + pid + " and checksum"
+ " algorithm: " + checksumAlgorithm;
logFileHashStoreLinks.error(errMsg);
throw new NonMatchingChecksumException(errMsg);
throw new NonMatchingChecksumException(errMsg, hexDigests);
}

// Gather the elements to form the permanent address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public void storeObject_correctChecksumValue() throws Exception {
*/
@Test
public void storeObject_incorrectChecksumValue() {
assertThrows(IllegalArgumentException.class, () -> {
assertThrows(NonMatchingChecksumException.class, () -> {
// Get test file to "upload"
String pid = "jtao.1700.1";
Path testDataFile = testData.getTestFile(pid);
Expand All @@ -379,6 +379,39 @@ public void storeObject_incorrectChecksumValue() {
});
}

/**
* Verify exception contains hexDigests when NonMatchingChecksumException is thrown
*/
@Test
public void storeObject_nonMatchingChecksumException_hexDigestsIncluded() throws Exception {
// Get test file to "upload"
String pid = "jtao.1700.1";
Path testDataFile = testData.getTestFile(pid);

String checksumIncorrect =
"aaf9b6c88f1f458e410c30c351c6384ea42ac1b5ee1f8430d3e365e43b78a38a";

try (InputStream dataStream = Files.newInputStream(testDataFile)) {
try {
fileHashStore.storeObject(dataStream, pid, null, checksumIncorrect, "SHA-256", -1);

} catch (NonMatchingChecksumException nmce) {
Map<String, String> hexDigestsRetrieved = nmce.getHexDigests();

String md5 = testData.pidData.get(pid).get("md5");
String sha1 = testData.pidData.get(pid).get("sha1");
String sha256 = testData.pidData.get(pid).get("sha256");
String sha384 = testData.pidData.get(pid).get("sha384");
String sha512 = testData.pidData.get(pid).get("sha512");
assertEquals(md5, hexDigestsRetrieved.get("MD5"));
assertEquals(sha1, hexDigestsRetrieved.get("SHA-1"));
assertEquals(sha256, hexDigestsRetrieved.get("SHA-256"));
assertEquals(sha384, hexDigestsRetrieved.get("SHA-384"));
assertEquals(sha512, hexDigestsRetrieved.get("SHA-512"));
}
}
}

/**
* Verify exception thrown when checksum is empty and algorithm supported
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ public void validateTmpObject_validationRequested_matchingChecksum() throws Exce
}

/**
* Confirm validateTmpObject does not throw exception when requested to validate checksums with
* good values, and that the tmpFile passed is deleted.
* Confirm validateTmpObject throws exception when requested to validate a bad checksum,
* and that the tmpFile passed is deleted.
*/
@Test
public void validateTmpObject_validationRequested_nonMatchingChecksum() throws Exception {
Expand Down Expand Up @@ -676,6 +676,18 @@ public void validateTmpObject_validationRequested_algoNotFound() throws Exceptio
assertFalse(Files.exists(tmpFile.toPath()));
}

/**
* Confirm validateTmpObject throws exception when hexDigests provided is null
*/
@Test
public void validateTmpObject_validationRequested_hexDigestsNull() throws Exception {
File tmpFile = generateTemporaryFile();

assertThrows(IllegalArgumentException.class,
() -> fileHashStore.validateTmpObject(true, "md2Digest", "MD2", tmpFile, null,
-1));
}

/**
* Check algorithm support for supported algorithm
*/
Expand Down
Loading