2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.security.tests.java.security;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.security.DigestInputStream;
23 import java.security.MessageDigest;
24 import java.security.NoSuchAlgorithmException;
25 import tests.support.Support_ASimpleInputStream;
27 public class DigestInputStream2Test extends junit.framework.TestCase {
29 ByteArrayInputStream inStream;
31 ByteArrayInputStream inStream1;
36 * java.security.DigestInputStream#DigestInputStream(java.io.InputStream,
37 * java.security.MessageDigest)
39 public void test_ConstructorLjava_io_InputStreamLjava_security_MessageDigest() {
40 // Test for method java.security.DigestInputStream(java.io.InputStream,
41 // java.security.MessageDigest)
42 DigestInputStream dis = new DigestInputStream(inStream, digest);
43 assertNotNull("Constructor returned null instance", dis);
47 * java.security.DigestInputStream#getMessageDigest()
49 public void test_getMessageDigest() {
50 // Test for method java.security.MessageDigest
51 // java.security.DigestInputStream.getMessageDigest()
52 DigestInputStream dis = new DigestInputStream(inStream, digest);
53 assertEquals("getMessageDigest returned a bogus result", digest, dis
58 * java.security.DigestInputStream#on(boolean)
60 public void test_onZ() throws Exception {
61 // Test for method void java.security.DigestInputStream.on(boolean)
62 MessageDigest originalDigest = (MessageDigest) (digest.clone());
63 MessageDigest noChangeDigest = (MessageDigest) (digest.clone());
64 DigestInputStream dis = new DigestInputStream(inStream, noChangeDigest);
65 // turn off processing
71 // make sure the digest for the part where it was off has not
73 assertTrue("MessageDigest changed even though processing was off",
74 MessageDigest.isEqual(noChangeDigest.digest(), originalDigest
76 MessageDigest changeDigest = (MessageDigest) (digest.clone());
77 dis = new DigestInputStream(inStream, digest);
84 // make sure the digest has changed
85 assertTrue("MessageDigest did not change with processing on",
86 !MessageDigest.isEqual(digest.digest(), changeDigest.digest()));
90 * java.security.DigestInputStream#read()
92 public void test_read() throws IOException {
93 // Test for method int java.security.DigestInputStream.read()
94 DigestInputStream dis = new DigestInputStream(inStream, digest);
96 // read and compare the data that the inStream has
98 while ((c = dis.read()) > -1) {
99 int d = inStream1.read();
105 * java.security.DigestInputStream#read(byte[], int, int)
107 public void test_read$BII() throws IOException {
108 // Test for method int java.security.DigestInputStream.read(byte [],
110 DigestInputStream dis = new DigestInputStream(inStream, digest);
111 int bytesToRead = inStream.available();
112 byte buf1[] = new byte[bytesToRead + 5];
113 byte buf2[] = new byte[bytesToRead + 5];
114 // make sure we're actually reading some data
115 assertTrue("No data to read for this test", bytesToRead>0);
117 // read and compare the data that the inStream has
118 int bytesRead1 = dis.read(buf1, 5, bytesToRead);
119 int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
120 assertEquals("Didn't read the same from each stream", bytesRead1,
122 assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
123 // compare the arrays
125 for (int i = 0; i < bytesToRead + 5; i++) {
126 if (buf1[i] != buf2[i]) {
130 assertTrue("Didn't get the same data", same);
134 * java.security.DigestInputStream#read(byte[], int, int)
136 public void test_read$BII_Exception() throws IOException {
137 DigestInputStream is = new DigestInputStream(inStream, digest);
141 fail("Test 1: NullPointerException expected.");
142 } catch (NullPointerException e) {
146 buf = new byte[1000];
149 fail("Test 2: IndexOutOfBoundsException expected.");
150 } catch (IndexOutOfBoundsException e) {
156 fail("Test 3: IndexOutOfBoundsException expected.");
157 } catch (IndexOutOfBoundsException e) {
162 is.read(buf, -1, -1);
163 fail("Test 4: IndexOutOfBoundsException expected.");
164 } catch (IndexOutOfBoundsException e) {
169 is.read(buf, 0, 1001);
170 fail("Test 5: IndexOutOfBoundsException expected.");
171 } catch (IndexOutOfBoundsException e) {
176 is.read(buf, 1001, 0);
177 fail("Test 6: IndexOutOfBoundsException expected.");
178 } catch (IndexOutOfBoundsException e) {
183 is.read(buf, 500, 501);
184 fail("Test 7: IndexOutOfBoundsException expected.");
185 } catch (IndexOutOfBoundsException e) {
191 Support_ASimpleInputStream sis = new Support_ASimpleInputStream(true);
192 is = new DigestInputStream(sis, digest);
194 is.read(buf, 0, 100);
195 fail("Test 9: IOException expected.");
196 } catch (IOException e) {
199 sis.throwExceptionOnNextUse = false;
204 * java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
206 public void test_setMessageDigestLjava_security_MessageDigest() {
207 // Test for method void
208 // java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
209 DigestInputStream dis = new DigestInputStream(inStream, null);
211 // make sure the digest is null when it's not been set
213 "Uninitialised MessageDigest should have been returned as null",
214 dis.getMessageDigest());
215 dis.setMessageDigest(digest);
216 assertEquals("Wrong MessageDigest was returned.", digest, dis
217 .getMessageDigest());
221 * Sets up the fixture, for example, open a network connection. This method
222 * is called before a test is executed.
224 protected void setUp() {
225 // create a ByteArrayInputStream to perform digesting on
226 inStream = new ByteArrayInputStream(
227 "This is a test string for digesting".getBytes());
228 inStream1 = new ByteArrayInputStream(
229 "This is a test string for digesting".getBytes());
231 digest = MessageDigest.getInstance("SHA-1");
232 } catch (NoSuchAlgorithmException e) {
233 fail("Unable to find SHA-1 algorithm");