/* * 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.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.security.DigestInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import org.apache.harmony.security.tests.support.MDGoldenData; import junit.framework.TestCase; /** * Tests for fields and methods of class DigestInputStream * */ public class DigestInputStreamTest 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 DigestInputStreamTest(String name) { super(name); } // // Tests // /** * Test #1 for DigestInputStream constructor
* * Assertion: creates new DigestInputStream instance * using valid parameters (both non null) * * @throws NoSuchAlgorithmException */ public final void testDigestInputStream01() { for (int i=0; iDigestInputStream constructor
* * Assertion: creates new DigestInputStream instance * using valid parameters (both null) */ public final void testDigestInputStream02() { InputStream dis = new DigestInputStream(null, null); assertTrue(dis instanceof DigestInputStream); } /** * Test #1 for read() method
* * Assertion: returns the byte read
* Assertion: updates associated digest
*/ public final void testRead01() throws IOException { for (int ii=0; iiread() method
* * Assertion: returns -1 if EOS had been * reached but not read before method call
* * Assertion: must not update digest if EOS had been * reached but not read before method call
*/ public final void testRead02() throws IOException { for (int ii=0; iiread() method
* Test #1 for on(boolean) method
* * Assertion: read() 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 testRead03() throws IOException { for (int ii=0; iiread() method
* * Assertion: broken DigestInputStreaminstance: * InputStream not set. read() must * not work */ public final void testRead04() throws IOException { for (int ii=0; iiread() method
* * Assertion: broken DigestInputStreaminstance: * associated MessageDigest not set. * read() must not work when digest * functionality is on */ public final void testRead05() { InputStream is = new ByteArrayInputStream(myMessage); DigestInputStream dis = new DigestInputStream(is, null); // must result in an exception try { for (int i=0; iread() method
* Test #2 for on(boolean) method
* * Assertion: broken DigestInputStreaminstance: * associated MessageDigest not set. * read() 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; iread(byte[],int,int) method
* * Assertion: returns the number of bytes read
* * Assertion: put bytes read into specified array at specified offset
* * Assertion: updates associated digest
*/ public final void testReadbyteArrayintint01() throws IOException { for (int ii=0; iiread(byte[],int,int) method
* * Assertion: returns the number of bytes read
* * Assertion: put bytes read into specified array at specified offset
* * Assertion: updates associated digest
*/ public final void testReadbyteArrayintint02() throws IOException { // check precondition assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE); for (int ii=0; iiread(byte[],int,int) method
* * Assertion: returns the number of bytes read
* * Assertion: put bytes read into specified array at specified offset
* * Assertion: updates associated digest
*/ public final void testReadbyteArrayintint03() throws IOException { // check precondition assertTrue(MY_MESSAGE_LEN % (CHUNK_SIZE+1) != 0); for (int ii=0; iiread(byte[],int,int) method
* * Assertion: returns the number of bytes read
* * Assertion: updates associated digest
*/ public final void testReadbyteArrayintint04() throws IOException { for (int ii=0; iiread(byte[],int,int) method
* * Assertion: returns the number of bytes read
* * Assertion: put bytes read into specified array at specified offset
* * Assertion: does not update associated digest if * digest functionality is off
*/ public final void testReadbyteArrayintint05() throws IOException { // check precondition assertEquals(0, MY_MESSAGE_LEN % CHUNK_SIZE); for (int ii=0; iigetMessageDigest() method
* * Assertion: returns associated message digest
*/ public final void testGetMessageDigest() { for (int ii=0; iisetMessageDigest() method
* * Assertion: set associated message digest
*/ public final void testSetMessageDigest() { for (int ii=0; iion() method
* Assertion: turns digest functionality on or off */ public final void testOn() throws IOException { for (int ii=0; iitoString() method
* Assertion: returns String representation of this object */ public final void testToString() { for (int ii=0; ii