Search in sources :

Example 11 with DigestInputStream

use of java.security.DigestInputStream in project lucene-solr by apache.

the class LicenseCheckTask method checkJarFile.

/**
   * Check a single JAR file.
   */
private boolean checkJarFile(File jarFile) {
    log("Scanning: " + jarFile.getPath(), verboseLevel);
    if (!skipChecksum) {
        boolean skipDueToSnapshot = skipSnapshotsChecksum && jarFile.getName().contains("-SNAPSHOT");
        if (!skipDueToSnapshot && !matchesRegexChecksum(jarFile, skipRegexChecksum)) {
            // validate the jar matches against our expected hash
            final File checksumFile = new File(licenseDirectory, jarFile.getName() + "." + CHECKSUM_TYPE);
            if (!(checksumFile.exists() && checksumFile.canRead())) {
                log("MISSING " + CHECKSUM_TYPE + " checksum file for: " + jarFile.getPath(), Project.MSG_ERR);
                log("EXPECTED " + CHECKSUM_TYPE + " checksum file : " + checksumFile.getPath(), Project.MSG_ERR);
                this.failures = true;
                return false;
            } else {
                final String expectedChecksum = readChecksumFile(checksumFile);
                try {
                    final MessageDigest md = MessageDigest.getInstance(CHECKSUM_TYPE);
                    byte[] buf = new byte[CHECKSUM_BUFFER_SIZE];
                    try {
                        FileInputStream fis = new FileInputStream(jarFile);
                        try {
                            DigestInputStream dis = new DigestInputStream(fis, md);
                            try {
                                while (dis.read(buf, 0, CHECKSUM_BUFFER_SIZE) != -1) {
                                // NOOP
                                }
                            } finally {
                                dis.close();
                            }
                        } finally {
                            fis.close();
                        }
                    } catch (IOException ioe) {
                        throw new BuildException("IO error computing checksum of file: " + jarFile, ioe);
                    }
                    final byte[] checksumBytes = md.digest();
                    final String checksum = createChecksumString(checksumBytes);
                    if (!checksum.equals(expectedChecksum)) {
                        log("CHECKSUM FAILED for " + jarFile.getPath() + " (expected: \"" + expectedChecksum + "\" was: \"" + checksum + "\")", Project.MSG_ERR);
                        this.failures = true;
                        return false;
                    }
                } catch (NoSuchAlgorithmException ae) {
                    throw new BuildException("Digest type " + CHECKSUM_TYPE + " not supported by your JVM", ae);
                }
            }
        } else if (skipDueToSnapshot) {
            log("Skipping jar because it is a SNAPSHOT : " + jarFile.getAbsolutePath(), Project.MSG_INFO);
        } else {
            log("Skipping jar because it matches regex pattern: " + jarFile.getAbsolutePath() + " pattern: " + skipRegexChecksum.pattern(), Project.MSG_INFO);
        }
    }
    // Get the expected license path base from the mapper and search for license files.
    Map<File, LicenseType> foundLicenses = new LinkedHashMap<>();
    List<File> expectedLocations = new ArrayList<>();
    outer: for (String mappedPath : licenseMapper.mapFileName(jarFile.getName())) {
        for (LicenseType licenseType : LicenseType.values()) {
            File licensePath = new File(licenseDirectory, mappedPath + licenseType.licenseFileSuffix());
            if (licensePath.exists()) {
                foundLicenses.put(licensePath, licenseType);
                log(" FOUND " + licenseType.name() + " license at " + licensePath.getPath(), verboseLevel);
                // We could continue scanning here to detect duplicate associations?
                break outer;
            } else {
                expectedLocations.add(licensePath);
            }
        }
    }
    // Check for NOTICE files.
    for (Map.Entry<File, LicenseType> e : foundLicenses.entrySet()) {
        LicenseType license = e.getValue();
        String licensePath = e.getKey().getName();
        String baseName = licensePath.substring(0, licensePath.length() - license.licenseFileSuffix().length());
        File noticeFile = new File(licenseDirectory, baseName + license.noticeFileSuffix());
        if (noticeFile.exists()) {
            log(" FOUND NOTICE file at " + noticeFile.getAbsolutePath(), verboseLevel);
        } else {
            if (license.isNoticeRequired()) {
                this.failures = true;
                log("MISSING NOTICE for the license file:\n  " + licensePath + "\n  Expected location below:\n  " + noticeFile.getAbsolutePath(), Project.MSG_ERR);
            }
        }
    }
    // In case there is something missing, complain.
    if (foundLicenses.isEmpty()) {
        this.failures = true;
        StringBuilder message = new StringBuilder();
        message.append("MISSING LICENSE for the following file:\n  " + jarFile.getAbsolutePath() + "\n  Expected locations below:\n");
        for (File location : expectedLocations) {
            message.append("  => ").append(location.getAbsolutePath()).append("\n");
        }
        log(message.toString(), Project.MSG_ERR);
        return false;
    }
    return true;
}
Also used : DigestInputStream(java.security.DigestInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap) BuildException(org.apache.tools.ant.BuildException) MessageDigest(java.security.MessageDigest) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with DigestInputStream

use of java.security.DigestInputStream in project robovm by robovm.

the class DigestInputStreamTest method testReadbyteArrayintint05.

/**
     * Test #5 for <code>read(byte[],int,int)</code> method<br>
     *
     * Assertion: returns the number of bytes read<br>
     *
     * Assertion: put bytes read into specified array at specified offset<br>
     *
     * Assertion: does not update associated digest if
     * digest functionality is off<br>
     */
public final void testReadbyteArrayintint05() throws IOException {
    // check precondition
    assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE);
    for (int ii = 0; ii < algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            InputStream is = new ByteArrayInputStream(myMessage);
            DigestInputStream dis = new DigestInputStream(is, md);
            byte[] bArray = new byte[MY_MESSAGE_LEN];
            // turn digest off
            dis.on(false);
            for (int i = 0; i < MY_MESSAGE_LEN / CHUNK_SIZE; i++) {
                dis.read(bArray, i * CHUNK_SIZE, CHUNK_SIZE);
            }
            // check that digest has not been updated
            assertTrue(Arrays.equals(dis.getMessageDigest().digest(), MDGoldenData.getDigest(algorithmName[ii] + "_NU")));
            return;
        } catch (NoSuchAlgorithmException e) {
        // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 13 with DigestInputStream

use of java.security.DigestInputStream in project robovm by robovm.

the class DigestInputStreamTest method testReadbyteArrayintint03.

/**
     * Test #3 for <code>read(byte[],int,int)</code> method<br>
     *
     * Assertion: returns the number of bytes read<br>
     *
     * Assertion: put bytes read into specified array at specified offset<br>
     *
     * Assertion: updates associated digest<br>
     */
public final void testReadbyteArrayintint03() throws IOException {
    // check precondition
    assertTrue(MY_MESSAGE_LEN % (CHUNK_SIZE + 1) != 0);
    for (int ii = 0; ii < algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            InputStream is = new ByteArrayInputStream(myMessage);
            DigestInputStream dis = new DigestInputStream(is, md);
            byte[] bArray = new byte[MY_MESSAGE_LEN];
            for (int i = 0; i < MY_MESSAGE_LEN / (CHUNK_SIZE + 1); i++) {
                // check that read(byte[],int,int) returns valid value
                assertTrue("retval1", dis.read(bArray, i * (CHUNK_SIZE + 1), CHUNK_SIZE + 1) == CHUNK_SIZE + 1);
            }
            // check that last call returns right
            // number of remaining bytes
            assertTrue("retval2", dis.read(bArray, MY_MESSAGE_LEN / (CHUNK_SIZE + 1) * (CHUNK_SIZE + 1), MY_MESSAGE_LEN % (CHUNK_SIZE + 1)) == (MY_MESSAGE_LEN % (CHUNK_SIZE + 1)));
            // check that bArray has been filled properly
            assertTrue("bArray", Arrays.equals(myMessage, bArray));
            // check that associated digest has been updated properly
            assertTrue("update", Arrays.equals(dis.getMessageDigest().digest(), MDGoldenData.getDigest(algorithmName[ii])));
            return;
        } catch (NoSuchAlgorithmException e) {
        // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 14 with DigestInputStream

use of java.security.DigestInputStream in project robovm by robovm.

the class DigestInputStreamTest method testOn.

/**
     * Test for <code>on()</code> method<br>
     * Assertion: turns digest functionality on or off
     */
public final void testOn() throws IOException {
    for (int ii = 0; ii < algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            InputStream is = new ByteArrayInputStream(myMessage);
            DigestInputStream dis = new DigestInputStream(is, md);
            // turn digest off
            dis.on(false);
            for (int i = 0; i < MY_MESSAGE_LEN - 1; i++) {
                dis.read();
            }
            // turn digest on
            dis.on(true);
            // read remaining byte
            dis.read();
            byte[] digest = dis.getMessageDigest().digest();
            // check that digest value has been
            // updated by the last read() call
            assertFalse(Arrays.equals(digest, MDGoldenData.getDigest(algorithmName[ii])) || Arrays.equals(digest, MDGoldenData.getDigest(algorithmName[ii] + "_NU")));
            return;
        } catch (NoSuchAlgorithmException e) {
        // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 15 with DigestInputStream

use of java.security.DigestInputStream in project robovm by robovm.

the class DigestInputStreamTest method testGetMessageDigest.

/**
     * Test for <code>getMessageDigest()</code> method<br>
     *
     * Assertion: returns associated message digest<br>
     */
public final void testGetMessageDigest() {
    for (int ii = 0; ii < algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            DigestInputStream dis = new DigestInputStream(null, md);
            assertTrue(dis.getMessageDigest() == md);
            return;
        } catch (NoSuchAlgorithmException e) {
        // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

DigestInputStream (java.security.DigestInputStream)161 MessageDigest (java.security.MessageDigest)124 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)78 IOException (java.io.IOException)62 InputStream (java.io.InputStream)53 ByteArrayInputStream (java.io.ByteArrayInputStream)38 FileInputStream (java.io.FileInputStream)34 File (java.io.File)19 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileOutputStream (java.io.FileOutputStream)8 URL (java.net.URL)7 OutputStream (java.io.OutputStream)6 BigInteger (java.math.BigInteger)5 DigestOutputStream (java.security.DigestOutputStream)5 HashMap (java.util.HashMap)5 FileNotFoundException (java.io.FileNotFoundException)4 Formatter (java.util.Formatter)4 ByteUtil (com.zimbra.common.util.ByteUtil)3 Path (java.nio.file.Path)3