Search in sources :

Example 1 with CorruptChecksumException

use of gov.loc.repository.bagit.exceptions.CorruptChecksumException in project bagit-java by LibraryOfCongress.

the class BagitSuiteComplanceTest method testInvalidOperatingSystemSpecificBags.

@Test
public void testInvalidOperatingSystemSpecificBags() {
    int errorCount = 0;
    Bag bag;
    List<Path> osSpecificInvalidPaths = visitor.getLinuxOnlyTestCases();
    ConcurrentMap<Class<? extends Exception>, AtomicLong> map = new ConcurrentHashMap<>();
    if (TestUtils.isExecutingOnWindows()) {
        osSpecificInvalidPaths = visitor.getWindowsOnlyTestCases();
    }
    for (Path invalidBagDir : osSpecificInvalidPaths) {
        try {
            bag = reader.read(invalidBagDir);
            verifier.isValid(bag, true);
        } catch (InvalidBagitFileFormatException | IOException | UnparsableVersionException | MissingPayloadManifestException | MissingBagitFileException | MissingPayloadDirectoryException | FileNotInPayloadDirectoryException | InterruptedException | MaliciousPathException | CorruptChecksumException | VerificationException | UnsupportedAlgorithmException e) {
            logger.info("Found invalid os specific bag with message: {}", e.getMessage());
            map.putIfAbsent(e.getClass(), new AtomicLong(0));
            map.get(e.getClass()).incrementAndGet();
            errorCount++;
        }
    }
    assertEquals("every test case should throw an error", osSpecificInvalidPaths.size(), errorCount);
    logger.debug("Count of all errors found in os specific invalid cases: {}", map);
}
Also used : Path(java.nio.file.Path) MissingPayloadManifestException(gov.loc.repository.bagit.exceptions.MissingPayloadManifestException) MaliciousPathException(gov.loc.repository.bagit.exceptions.MaliciousPathException) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) Bag(gov.loc.repository.bagit.domain.Bag) IOException(java.io.IOException) FileNotInPayloadDirectoryException(gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) UnparsableVersionException(gov.loc.repository.bagit.exceptions.UnparsableVersionException) MaliciousPathException(gov.loc.repository.bagit.exceptions.MaliciousPathException) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException) UnsupportedAlgorithmException(gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException) IOException(java.io.IOException) MissingPayloadManifestException(gov.loc.repository.bagit.exceptions.MissingPayloadManifestException) MissingPayloadDirectoryException(gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) FileNotInPayloadDirectoryException(gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException) AtomicLong(java.util.concurrent.atomic.AtomicLong) MissingPayloadDirectoryException(gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException) UnparsableVersionException(gov.loc.repository.bagit.exceptions.UnparsableVersionException) UnsupportedAlgorithmException(gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) BeforeClass(org.junit.BeforeClass) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 2 with CorruptChecksumException

use of gov.loc.repository.bagit.exceptions.CorruptChecksumException in project bagit-java by LibraryOfCongress.

the class CheckManifestHashesTask method run.

@Override
public void run() {
    try {
        final MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        checkManifestEntry(entry, messageDigest, algorithm);
    } catch (IOException | CorruptChecksumException | NoSuchAlgorithmException e) {
        exceptions.add(e);
    }
    latch.countDown();
}
Also used : CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 3 with CorruptChecksumException

use of gov.loc.repository.bagit.exceptions.CorruptChecksumException in project bagit-java by LibraryOfCongress.

the class BagVerifier method checkHashes.

/*
   * Check the supplied checksum hashes against the generated checksum hashes
   */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException {
    final CountDownLatch latch = new CountDownLatch(manifest.getFileToChecksumMap().size());
    // TODO maybe return all of these at some point...
    final Collection<Exception> exceptions = Collections.synchronizedCollection(new ArrayList<>());
    for (final Entry<Path, String> entry : manifest.getFileToChecksumMap().entrySet()) {
        executor.execute(new CheckManifestHashesTask(entry, manifest.getAlgorithm().getMessageDigestName(), latch, exceptions));
    }
    latch.await();
    if (!exceptions.isEmpty()) {
        final Exception e = exceptions.iterator().next();
        if (e instanceof CorruptChecksumException) {
            logger.debug(messages.getString("checksums_not_matching_error"), exceptions.size());
            throw (CorruptChecksumException) e;
        }
        throw new VerificationException(e);
    }
}
Also used : Path(java.nio.file.Path) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) CountDownLatch(java.util.concurrent.CountDownLatch) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) MaliciousPathException(gov.loc.repository.bagit.exceptions.MaliciousPathException) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException) UnsupportedAlgorithmException(gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException) PayloadOxumDoesNotExistException(gov.loc.repository.bagit.exceptions.PayloadOxumDoesNotExistException) IOException(java.io.IOException) MissingPayloadManifestException(gov.loc.repository.bagit.exceptions.MissingPayloadManifestException) MissingPayloadDirectoryException(gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) FileNotInPayloadDirectoryException(gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) InvalidPayloadOxumException(gov.loc.repository.bagit.exceptions.InvalidPayloadOxumException)

Example 4 with CorruptChecksumException

use of gov.loc.repository.bagit.exceptions.CorruptChecksumException in project bagit-java by LibraryOfCongress.

the class BagitSuiteComplanceTest method testInvalidBags.

@Test
public void testInvalidBags() {
    int errorCount = 0;
    Bag bag;
    ConcurrentMap<Class<? extends Exception>, AtomicLong> map = new ConcurrentHashMap<>();
    for (Path invalidBagDir : visitor.getInvalidTestCases()) {
        try {
            bag = reader.read(invalidBagDir);
            verifier.isValid(bag, true);
            System.err.println(bag.getRootDir() + " should have failed but didn't!");
        } catch (InvalidBagitFileFormatException | IOException | UnparsableVersionException | MissingPayloadManifestException | MissingBagitFileException | MissingPayloadDirectoryException | FileNotInPayloadDirectoryException | InterruptedException | MaliciousPathException | CorruptChecksumException | VerificationException | UnsupportedAlgorithmException e) {
            logger.info("Found invalid os specific bag with message: {}", e.getMessage());
            map.putIfAbsent(e.getClass(), new AtomicLong(0));
            map.get(e.getClass()).incrementAndGet();
            errorCount++;
        }
    }
    assertEquals("every test case should throw an error", visitor.getInvalidTestCases().size(), errorCount);
    logger.debug("Count of all errors found in generic invalid cases: {}", map);
}
Also used : Path(java.nio.file.Path) MissingPayloadManifestException(gov.loc.repository.bagit.exceptions.MissingPayloadManifestException) MaliciousPathException(gov.loc.repository.bagit.exceptions.MaliciousPathException) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) Bag(gov.loc.repository.bagit.domain.Bag) IOException(java.io.IOException) FileNotInPayloadDirectoryException(gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) UnparsableVersionException(gov.loc.repository.bagit.exceptions.UnparsableVersionException) MaliciousPathException(gov.loc.repository.bagit.exceptions.MaliciousPathException) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException) UnsupportedAlgorithmException(gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException) IOException(java.io.IOException) MissingPayloadManifestException(gov.loc.repository.bagit.exceptions.MissingPayloadManifestException) MissingPayloadDirectoryException(gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) FileNotInPayloadDirectoryException(gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException) AtomicLong(java.util.concurrent.atomic.AtomicLong) MissingPayloadDirectoryException(gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException) UnparsableVersionException(gov.loc.repository.bagit.exceptions.UnparsableVersionException) UnsupportedAlgorithmException(gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) BeforeClass(org.junit.BeforeClass) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

CorruptChecksumException (gov.loc.repository.bagit.exceptions.CorruptChecksumException)4 IOException (java.io.IOException)4 FileNotInPayloadDirectoryException (gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException)3 InvalidBagitFileFormatException (gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)3 MaliciousPathException (gov.loc.repository.bagit.exceptions.MaliciousPathException)3 MissingBagitFileException (gov.loc.repository.bagit.exceptions.MissingBagitFileException)3 MissingPayloadDirectoryException (gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException)3 MissingPayloadManifestException (gov.loc.repository.bagit.exceptions.MissingPayloadManifestException)3 UnsupportedAlgorithmException (gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException)3 VerificationException (gov.loc.repository.bagit.exceptions.VerificationException)3 Path (java.nio.file.Path)3 Bag (gov.loc.repository.bagit.domain.Bag)2 UnparsableVersionException (gov.loc.repository.bagit.exceptions.UnparsableVersionException)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 BeforeClass (org.junit.BeforeClass)2 Test (org.junit.Test)2 InvalidPayloadOxumException (gov.loc.repository.bagit.exceptions.InvalidPayloadOxumException)1 PayloadOxumDoesNotExistException (gov.loc.repository.bagit.exceptions.PayloadOxumDoesNotExistException)1 MessageDigest (java.security.MessageDigest)1