1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package org.apache.harmony.security.tests.java.security;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.UnsupportedEncodingException;
23import java.security.DigestInputStream;
24import java.security.MessageDigest;
25import java.security.NoSuchAlgorithmException;
26
27public class DigestInputStream2Test extends junit.framework.TestCase {
28
29	ByteArrayInputStream inStream;
30
31	ByteArrayInputStream inStream1;
32
33	MessageDigest digest;
34
35	/**
36	 * @tests java.security.DigestInputStream#DigestInputStream(java.io.InputStream,
37	 *        java.security.MessageDigest)
38	 */
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);
44	}
45
46	/**
47	 * @tests java.security.DigestInputStream#getMessageDigest()
48	 */
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
54				.getMessageDigest());
55	}
56
57	/**
58	 * @tests java.security.DigestInputStream#on(boolean)
59	 */
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
66        dis.on(false);
67        // read some data
68        int c = dis.read();
69        assertEquals('T', c);
70
71        // make sure the digest for the part where it was off has not
72        // changed
73        assertTrue("MessageDigest changed even though processing was off",
74                MessageDigest.isEqual(noChangeDigest.digest(), originalDigest
75                        .digest()));
76        MessageDigest changeDigest = (MessageDigest) (digest.clone());
77        dis = new DigestInputStream(inStream, digest);
78
79        // turn on processing
80        dis.on(true);
81        c = dis.read();
82        assertEquals('h', c);
83
84        // make sure the digest has changed
85        assertTrue("MessageDigest did not change with processing on",
86                !MessageDigest.isEqual(digest.digest(), changeDigest.digest()));
87    }
88
89	/**
90	 * @tests java.security.DigestInputStream#read()
91	 */
92    public void test_read() throws IOException {
93        // Test for method int java.security.DigestInputStream.read()
94        DigestInputStream dis = new DigestInputStream(inStream, digest);
95
96        // read and compare the data that the inStream has
97        int c;
98        while ((c = dis.read()) > -1) {
99            int d = inStream1.read();
100            assertEquals(d, c);
101        }// end while
102    }
103
104	/**
105	 * @tests java.security.DigestInputStream#read(byte[], int, int)
106	 */
107	public void test_read$BII() throws IOException {
108		// Test for method int java.security.DigestInputStream.read(byte [],
109		// int, int)
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);
116
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,
121                bytesRead2);
122        assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
123        // compare the arrays
124        boolean same = true;
125        for (int i = 0; i < bytesToRead + 5; i++) {
126            if (buf1[i] != buf2[i]) {
127                same = false;
128            }
129        }// end for
130        assertTrue("Didn't get the same data", same);
131	}
132
133	/**
134	 * @tests java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
135	 */
136	public void test_setMessageDigestLjava_security_MessageDigest() {
137		// Test for method void
138		// java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
139		DigestInputStream dis = new DigestInputStream(inStream, null);
140
141		// make sure the digest is null when it's not been set
142		assertNull(
143				"Uninitialised MessageDigest should have been returned as null",
144				dis.getMessageDigest());
145		dis.setMessageDigest(digest);
146		assertEquals("Wrong MessageDigest was returned.", digest, dis
147				.getMessageDigest());
148	}
149
150	/**
151	 * Sets up the fixture, for example, open a network connection. This method
152	 * is called before a test is executed.
153	 * @throws UnsupportedEncodingException
154	 */
155	protected void setUp() throws UnsupportedEncodingException {
156		// create a ByteArrayInputStream to perform digesting on
157		inStream = new ByteArrayInputStream(
158				"This is a test string for digesting".getBytes("UTF-8"));
159		inStream1 = new ByteArrayInputStream(
160				"This is a test string for digesting".getBytes("UTF-8"));
161		try {
162			digest = MessageDigest.getInstance("SHA-1");
163		} catch (NoSuchAlgorithmException e) {
164			fail("Unable to find SHA-1 algorithm");
165		}
166	}
167}