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 18 package org.apache.harmony.security.tests.java.security; 19 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; 26 27 public class DigestInputStream2Test extends junit.framework.TestCase { 28 29 ByteArrayInputStream inStream; 30 31 ByteArrayInputStream inStream1; 32 33 MessageDigest digest; 34 35 /** 36 * 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 * 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 * 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 * 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 * 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 * java.security.DigestInputStream#read(byte[], int, int) 135 */ 136 public void test_read$BII_Exception() throws IOException { 137 DigestInputStream is = new DigestInputStream(inStream, digest); 138 byte[] buf = null; 139 try { 140 is.read(buf, -1, 0); 141 fail("Test 1: NullPointerException expected."); 142 } catch (NullPointerException e) { 143 // Expected. 144 } 145 146 buf = new byte[1000]; 147 try { 148 is.read(buf, -1, 0); 149 fail("Test 2: IndexOutOfBoundsException expected."); 150 } catch (IndexOutOfBoundsException e) { 151 // Expected. 152 } 153 154 try { 155 is.read(buf, 0, -1); 156 fail("Test 3: IndexOutOfBoundsException expected."); 157 } catch (IndexOutOfBoundsException e) { 158 // Expected. 159 } 160 161 try { 162 is.read(buf, -1, -1); 163 fail("Test 4: IndexOutOfBoundsException expected."); 164 } catch (IndexOutOfBoundsException e) { 165 // Expected. 166 } 167 168 try { 169 is.read(buf, 0, 1001); 170 fail("Test 5: IndexOutOfBoundsException expected."); 171 } catch (IndexOutOfBoundsException e) { 172 // Expected. 173 } 174 175 try { 176 is.read(buf, 1001, 0); 177 fail("Test 6: IndexOutOfBoundsException expected."); 178 } catch (IndexOutOfBoundsException e) { 179 // Expected. 180 } 181 182 try { 183 is.read(buf, 500, 501); 184 fail("Test 7: IndexOutOfBoundsException expected."); 185 } catch (IndexOutOfBoundsException e) { 186 // Expected. 187 } 188 189 is.close(); 190 191 Support_ASimpleInputStream sis = new Support_ASimpleInputStream(true); 192 is = new DigestInputStream(sis, digest); 193 try { 194 is.read(buf, 0, 100); 195 fail("Test 9: IOException expected."); 196 } catch (IOException e) { 197 // Expected. 198 } 199 sis.throwExceptionOnNextUse = false; 200 is.close(); 201 } 202 203 /** 204 * java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest) 205 */ 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); 210 211 // make sure the digest is null when it's not been set 212 assertNull( 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()); 218 } 219 220 /** 221 * Sets up the fixture, for example, open a network connection. This method 222 * is called before a test is executed. 223 */ 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()); 230 try { 231 digest = MessageDigest.getInstance("SHA-1"); 232 } catch (NoSuchAlgorithmException e) { 233 fail("Unable to find SHA-1 algorithm"); 234 } 235 } 236 } 237