Search in sources :

Example 21 with DigestInputStream

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

the class DigestInputStreamTest method testDigestInputStream01.

//
// Tests
//
/**
     * Test #1 for <code>DigestInputStream</code> constructor<br>
     *
     * Assertion: creates new <code>DigestInputStream</code> instance
     * using valid parameters (both non <code>null</code>)
     *
     * @throws NoSuchAlgorithmException
     */
public final void testDigestInputStream01() {
    for (int i = 0; i < algorithmName.length; i++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[i]);
            InputStream is = new ByteArrayInputStream(myMessage);
            InputStream dis = new DigestInputStream(is, md);
            assertTrue(dis instanceof DigestInputStream);
            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 22 with DigestInputStream

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

the class DigestInputStreamTest method testRead06.

/**
     * Test #6 for <code>read()</code> method<br>
     * Test #2 for <code>on(boolean)</code> method<br>
     *
     * Assertion: broken <code>DigestInputStream</code>instance:
     * associated <code>MessageDigest</code> not set.
     * <code>read()</code> must work when digest
     * functionality is off
     */
public final void testRead06() throws IOException {
    InputStream is = new ByteArrayInputStream(myMessage);
    // construct object without digest
    DigestInputStream dis = new DigestInputStream(is, null);
    // set digest functionality to off
    dis.on(false);
    // the following must pass without any exception
    for (int i = 0; i < MY_MESSAGE_LEN; i++) {
        assertTrue((byte) dis.read() == myMessage[i]);
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream)

Example 23 with DigestInputStream

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

the class DigestInputStreamTest method testToString.

/**
     * Test for <code>toString()</code> method<br>
     * Assertion: returns <code>String</code> representation of this object
     */
public final void testToString() {
    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);
            assertNotNull(dis.toString());
            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 24 with DigestInputStream

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

the class FilterInputStreamNullSourceTest method testDigestInputStream.

public void testDigestInputStream() throws IOException, NoSuchAlgorithmException {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    assertReadsFailWithNullPointerException(new DigestInputStream(null, md5));
}
Also used : DigestInputStream(java.security.DigestInputStream) MessageDigest(java.security.MessageDigest)

Example 25 with DigestInputStream

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

the class DigestInputStream2Test method test_read$BII.

/**
     * java.security.DigestInputStream#read(byte[], int, int)
     */
public void test_read$BII() throws IOException {
    // Test for method int java.security.DigestInputStream.read(byte [],
    // int, int)
    DigestInputStream dis = new DigestInputStream(inStream, digest);
    int bytesToRead = inStream.available();
    byte[] buf1 = new byte[bytesToRead + 5];
    byte[] buf2 = new byte[bytesToRead + 5];
    // make sure we're actually reading some data
    assertTrue("No data to read for this test", bytesToRead > 0);
    // read and compare the data that the inStream has
    int bytesRead1 = dis.read(buf1, 5, bytesToRead);
    int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
    assertEquals("Didn't read the same from each stream", bytesRead1, bytesRead2);
    assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
    // compare the arrays
    boolean same = true;
    for (int i = 0; i < bytesToRead + 5; i++) {
        if (buf1[i] != buf2[i]) {
            same = false;
        }
    }
    // end for
    assertTrue("Didn't get the same data", same);
}
Also used : DigestInputStream(java.security.DigestInputStream)

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