/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Vladimir N. Molotkov
*/
package org.apache.harmony.security.tests.java.security;
import java.security.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import org.apache.harmony.security.tests.support.MDGoldenData;
import org.apache.harmony.security.tests.support.MyMessageDigest1;
import junit.framework.TestCase;
/**
* Tests for fields and methods of class DigestInputStream
*
*/
public class DigestOutputStreamTest extends TestCase {
/**
* Message digest algorithm name used during testing
*/
private static final String algorithmName[] = {
"SHA-1",
"SHA",
"SHA1",
"SHA-256",
"SHA-384",
"SHA-512",
"MD5",
};
/**
* Chunk size for read(byte, off, len) tests
*/
private static final int CHUNK_SIZE = 32;
/**
* Test message for digest computations
*/
private static final byte[] myMessage = MDGoldenData.getMessage();
/**
* The length of test message
*/
private static final int MY_MESSAGE_LEN = myMessage.length;
/**
* Constructor for DigestInputStreamTest.
* @param name
*/
public DigestOutputStreamTest(String name) {
super(name);
}
//
// Tests
//
/**
* @tests java.security.DigestOutputStream#DigestOutputStream(java.io.OutputStream,
* java.security.MessageDigest)
*/
public void test_CtorLjava_io_OutputStreamLjava_security_MessageDigest() {
// non-null parameters
MessageDigest md = new MyMessageDigest1();
MyOutputStream out = new MyOutputStream();
MyDigestOutputStream dos = new MyDigestOutputStream(out, md);
assertSame(out, dos.myOutputStream());
assertSame(md, dos.myMessageDigest());
// null parameters
dos = new MyDigestOutputStream(null, null);
assertNull(dos.myOutputStream());
assertNull(dos.myMessageDigest());
}
/**
* @tests java.security.DigestOutputStream#getMessageDigest()
*/
public void test_getMessageDigest() {
MessageDigest digest = new MyMessageDigest1();
OutputStream out = new MyOutputStream();
// non-null parameter
DigestOutputStream dos = new DigestOutputStream(out, digest);
assertSame(digest, dos.getMessageDigest());
// null parameter
dos = new DigestOutputStream(out, null);
assertNull("getMessageDigest should have returned null", dos
.getMessageDigest());
}
/**
* @tests java.security.DigestOutputStream#setMessageDigest(MessageDigest)
*/
public void test_setMessageDigestLjava_security_MessageDigest() {
MessageDigest digest = new MyMessageDigest1();
OutputStream out = new MyOutputStream();
DigestOutputStream dos = new DigestOutputStream(out, null);
// non-null parameter
dos.setMessageDigest(digest);
assertSame(digest, dos.getMessageDigest());
// null parameter
dos.setMessageDigest(null);
assertNull("getMessageDigest should have returned null", dos
.getMessageDigest());
}
/**
* Test #1 for write(int)
method
*
* Assertion: writes the byte to the output stream
* Assertion: updates associated digest
*/
public final void testWriteint01()
throws IOException {
for (int k=0; kwrite(int) method
* Test #1 for on(boolean)
method
*
* Assertion: write(int)
must not update digest if it is off
* Assertion: on(boolean)
turns digest functionality on
* if true
passed as a parameter or off if false
* passed
*/
public final void testWriteint02()
throws IOException {
for (int k=0; kwrite(int) method
*
* Assertion: broken DigestOutputStream
instance:
* OutputStream
not set. write(int)
must
* not work
*/
public final void testWriteint03() throws IOException {
for (int k=0; kwrite(int) method
*
* Assertion: broken DigestOutputStream
instance:
* associated MessageDigest
not set.
* write(int)
must not work when digest
* functionality is on
*/
public final void testWriteint04() throws IOException {
OutputStream os = new ByteArrayOutputStream(MY_MESSAGE_LEN);
DigestOutputStream dos = new DigestOutputStream(os, null);
// must result in an exception
try {
for (int i=0; iwrite(int) method
* Test #2 for on(boolean)
method
*
* Assertion: broken DigestOutputStream
instance:
* associated MessageDigest
not set.
* write(int)
must work when digest
* functionality is off
*/
public final void testWriteint05() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
DigestOutputStream dos = new DigestOutputStream(bos, null);
// set digest functionality to off
dos.on(false);
// the following must pass without any exception
for (int i=0; iwrite(byte[],int,int) method
*
* Assertion: put bytes into output stream
*
* Assertion: updates associated digest
*/
public final void testWritebyteArrayintint01()
throws IOException {
for (int k=0; kwrite(byte[],int,int) method
*
* Assertion: put bytes into output stream
*
* Assertion: updates associated digest
*/
public final void testWritebyteArrayintint02()
throws IOException {
// check precondition
assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE);
for (int k=0; kwrite(byte[],int,int) method
*
* Assertion: put bytes into output stream
*
* Assertion: updates associated digest
*/
public final void testWritebyteArrayintint03()
throws NoSuchAlgorithmException,
IOException {
// check precondition
assertTrue(MY_MESSAGE_LEN % (CHUNK_SIZE+1) != 0);
for (int k=0; kwrite(byte[],int,int) method
*
* Assertion: put bytes into output stream
*
* Assertion: does not update associated digest if digest
* functionality is off
*/
public final void testWritebyteArrayintint04()
throws NoSuchAlgorithmException,
IOException {
// check precondition
assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE);
for (int k=0; k buf.length
try {
dig.write(bytes, 0, bytes.length + 1);
fail("No expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// offset < 0
try {
dig.write(bytes, -1, 1);
fail("No expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
}
// len < 0
try {
dig.write(bytes, 0, -1);
fail("No expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
}
}
/**
* Test for on()
method
* Assertion: turns digest functionality on or off
*/
public final void testOn() throws IOException {
for (int k=0; ktoString() method
* Assertion: returns String
representation of this object
*/
public final void testToString() throws NoSuchAlgorithmException {
for (int k=0; k