001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.tests.java.security;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.security.DigestInputStream;
023: import java.security.MessageDigest;
024: import java.security.NoSuchAlgorithmException;
025:
026: public class DigestInputStream2Test extends junit.framework.TestCase {
027:
028: ByteArrayInputStream inStream;
029:
030: ByteArrayInputStream inStream1;
031:
032: MessageDigest digest;
033:
034: /**
035: * @tests java.security.DigestInputStream#DigestInputStream(java.io.InputStream,
036: * java.security.MessageDigest)
037: */
038: public void test_ConstructorLjava_io_InputStreamLjava_security_MessageDigest() {
039: // Test for method java.security.DigestInputStream(java.io.InputStream,
040: // java.security.MessageDigest)
041: DigestInputStream dis = new DigestInputStream(inStream, digest);
042: assertNotNull("Constructor returned null instance", dis);
043: }
044:
045: /**
046: * @tests java.security.DigestInputStream#getMessageDigest()
047: */
048: public void test_getMessageDigest() {
049: // Test for method java.security.MessageDigest
050: // java.security.DigestInputStream.getMessageDigest()
051: DigestInputStream dis = new DigestInputStream(inStream, digest);
052: assertEquals("getMessageDigest returned a bogus result",
053: digest, dis.getMessageDigest());
054: }
055:
056: /**
057: * @tests java.security.DigestInputStream#on(boolean)
058: */
059: public void test_onZ() throws Exception {
060: // Test for method void java.security.DigestInputStream.on(boolean)
061: MessageDigest originalDigest = (MessageDigest) (digest.clone());
062: MessageDigest noChangeDigest = (MessageDigest) (digest.clone());
063: DigestInputStream dis = new DigestInputStream(inStream,
064: noChangeDigest);
065: // turn off processing
066: dis.on(false);
067: // read some data
068: int c = dis.read();
069: assertEquals('T', c);
070:
071: // make sure the digest for the part where it was off has not
072: // changed
073: assertTrue(
074: "MessageDigest changed even though processing was off",
075: MessageDigest.isEqual(noChangeDigest.digest(),
076: originalDigest.digest()));
077: MessageDigest changeDigest = (MessageDigest) (digest.clone());
078: dis = new DigestInputStream(inStream, digest);
079:
080: // turn on processing
081: dis.on(true);
082: c = dis.read();
083: assertEquals('h', c);
084:
085: // make sure the digest has changed
086: assertTrue("MessageDigest did not change with processing on",
087: !MessageDigest.isEqual(digest.digest(), changeDigest
088: .digest()));
089: }
090:
091: /**
092: * @tests java.security.DigestInputStream#read()
093: */
094: public void test_read() throws IOException {
095: // Test for method int java.security.DigestInputStream.read()
096: DigestInputStream dis = new DigestInputStream(inStream, digest);
097:
098: // read and compare the data that the inStream has
099: int c;
100: while ((c = dis.read()) > -1) {
101: int d = inStream1.read();
102: assertEquals(d, c);
103: }// end while
104: }
105:
106: /**
107: * @tests java.security.DigestInputStream#read(byte[], int, int)
108: */
109: public void test_read$BII() throws IOException {
110: // Test for method int java.security.DigestInputStream.read(byte [],
111: // int, int)
112: DigestInputStream dis = new DigestInputStream(inStream, digest);
113: int bytesToRead = inStream.available();
114: byte buf1[] = new byte[bytesToRead + 5];
115: byte buf2[] = new byte[bytesToRead + 5];
116: // make sure we're actually reading some data
117: assertTrue("No data to read for this test", bytesToRead > 0);
118:
119: // read and compare the data that the inStream has
120: int bytesRead1 = dis.read(buf1, 5, bytesToRead);
121: int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
122: assertEquals("Didn't read the same from each stream",
123: bytesRead1, bytesRead2);
124: assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
125: // compare the arrays
126: boolean same = true;
127: for (int i = 0; i < bytesToRead + 5; i++) {
128: if (buf1[i] != buf2[i]) {
129: same = false;
130: }
131: }// end for
132: assertTrue("Didn't get the same data", same);
133: }
134:
135: /**
136: * @tests java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
137: */
138: public void test_setMessageDigestLjava_security_MessageDigest() {
139: // Test for method void
140: // java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
141: DigestInputStream dis = new DigestInputStream(inStream, null);
142:
143: // make sure the digest is null when it's not been set
144: assertNull(
145: "Uninitialised MessageDigest should have been returned as null",
146: dis.getMessageDigest());
147: dis.setMessageDigest(digest);
148: assertEquals("Wrong MessageDigest was returned.", digest, dis
149: .getMessageDigest());
150: }
151:
152: /**
153: * Sets up the fixture, for example, open a network connection. This method
154: * is called before a test is executed.
155: */
156: protected void setUp() {
157: // create a ByteArrayInputStream to perform digesting on
158: inStream = new ByteArrayInputStream(
159: "This is a test string for digesting".getBytes());
160: inStream1 = new ByteArrayInputStream(
161: "This is a test string for digesting".getBytes());
162: try {
163: digest = MessageDigest.getInstance("SHA-1");
164: } catch (NoSuchAlgorithmException e) {
165: fail("Unable to find SHA-1 algorithm");
166: }
167: }
168: }
|