use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testSetMessageDigest.
/**
* Test for <code>setMessageDigest()</code> method<br>
*
* Assertion: set associated message digest<br>
*/
public final void testSetMessageDigest() {
for (int ii = 0; ii < algorithmName.length; ii++) {
try {
DigestInputStream dis = new DigestInputStream(null, null);
MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
dis.setMessageDigest(md);
assertTrue(dis.getMessageDigest() == md);
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testRead01.
/**
* Test #1 for <code>read()</code> method<br>
*
* Assertion: returns the byte read<br>
* Assertion: updates associated digest<br>
*/
public final void testRead01() 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);
for (int i = 0; i < MY_MESSAGE_LEN; i++) {
// check that read() returns valid values
assertTrue("retval", ((byte) dis.read() == myMessage[i]));
}
// 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");
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testReadbyteArrayintint02.
/**
* Test #2 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 testReadbyteArrayintint02() 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];
for (int i = 0; i < MY_MESSAGE_LEN / CHUNK_SIZE; i++) {
// check that read(byte[],int,int) returns valid value
assertTrue("retval", dis.read(bArray, i * CHUNK_SIZE, CHUNK_SIZE) == CHUNK_SIZE);
}
// 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");
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testRead03.
/**
* Test #3 for <code>read()</code> method<br>
* Test #1 for <code>on(boolean)</code> method<br>
*
* Assertion: <code>read()</code> must not update digest if it is off<br>
* Assertion: <code>on(boolean)</code> turns digest functionality on
* (if <code>true</code> passed as a parameter) or off (if <code>false</code>
* passed)
*/
public final void testRead03() 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; i++) {
dis.read();
}
// check that digest value has not been updated by read()
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");
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testReadbyteArrayintint04.
/**
* Test #4 for <code>read(byte[],int,int)</code> method<br>
*
* Assertion: returns the number of bytes read<br>
*
* Assertion: updates associated digest<br>
*/
public final void testReadbyteArrayintint04() 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);
byte[] bArray = new byte[MY_MESSAGE_LEN];
// read all but EOS
dis.read(bArray, 0, bArray.length);
// check that subsequent read(byte[],int,int) calls return -1 (EOS)
assertEquals("retval1", -1, dis.read(bArray, 0, 1));
assertEquals("retval2", -1, dis.read(bArray, 0, bArray.length));
assertEquals("retval3", -1, dis.read(bArray, 0, 1));
// check that 3 previous read() calls did not update digest
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");
}
Aggregations