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/** 19* @author Alexander Y. Kleymenov 20* @version $Revision$ 21*/ 22 23package org.apache.harmony.crypto.tests.javax.crypto; 24 25import java.io.ByteArrayInputStream; 26import java.security.InvalidKeyException; 27import java.security.Key; 28import java.security.NoSuchAlgorithmException; 29import java.security.SecureRandom; 30 31import javax.crypto.Cipher; 32import javax.crypto.KeyGenerator; 33import javax.crypto.NoSuchPaddingException; 34import javax.crypto.NullCipher; 35import javax.crypto.CipherInputStream; 36 37import junit.framework.TestCase; 38 39/** 40 */ 41public class CipherInputStream1Test extends TestCase { 42 43 private static class TestInputStream extends ByteArrayInputStream { 44 private boolean closed = false; 45 46 public TestInputStream(byte[] data) { 47 super(data); 48 } 49 50 public void close() { 51 closed = true; 52 } 53 54 public boolean wasClosed() { 55 return closed; 56 } 57 } 58 59 /** 60 * CipherInputStream(InputStream is) method testing. Tests that 61 * CipherInputStream uses NullCipher if Cipher is not specified 62 * in the constructor. 63 */ 64 public void testCipherInputStream() throws Exception { 65 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; 66 TestInputStream tis = new TestInputStream(data); 67 CipherInputStream cis = new CipherInputStream(tis){}; 68 69 for (int i = 0; i < data.length; i++) { 70 if ((byte) cis.read() != data[i]) { 71 fail("NullCipher should be used " 72 + "if Cipher is not specified."); 73 } 74 } 75 if (cis.read() != -1) { 76 fail("NullCipher should be used if Cipher is not specified."); 77 } 78 } 79 80 /** 81 * read() method testing. Tests that method returns the correct value 82 * (related to the InputStream) and that it returns -1 at the end of stream. 83 */ 84 public void testRead1() throws Exception { 85 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; 86 TestInputStream tis = new TestInputStream(data); 87 CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); 88 byte res; 89 for (int i = 0; i < data.length; i++) { 90 if ((res = (byte) cis.read()) != data[i]) { 91 fail("read() returned the incorrect value. " + "Expected: " 92 + data[i] + ", Got: " + res + "."); 93 } 94 } 95 if (cis.read() != -1) { 96 fail("read() should return -1 at the end of the stream."); 97 } 98 } 99 100 /** 101 * read(byte[] b) method testing. Tests that method returns the correct 102 * value (related to the InputStream) and that it returns -1 at the end of 103 * stream. 104 */ 105 public void testRead2() throws Exception { 106 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; 107 TestInputStream tis = new TestInputStream(data); 108 CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); 109 110 int expected = data.length; 111 byte[] result = new byte[expected]; 112 113 int ind = 0; // index into the data array (to check the got data) 114 int got = cis.read(result); // the number of got bytes 115 while (true) { 116 for (int j = 0; j < got - ind; j++) { 117 if (result[j] != data[ind + j]) { 118 fail("read(byte[] b) returned incorrect data."); 119 } 120 } 121 if (got == expected) { 122 break; 123 } else if (got > expected) { 124 fail("The data returned by read(byte[] b) " 125 + "is larger than expected."); 126 } else { 127 ind = got; 128 got += cis.read(result); 129 } 130 } 131 if (cis.read(result) != -1) { 132 fail("read(byte[] b) should return -1 " 133 + "at the end of the stream."); 134 } 135 } 136 137 /** 138 * read(byte[] b, int off, int len) method testing. Tests that method 139 * returns the correct value (related to the InputStream), that it discards 140 * bytes in the case of null buffer, and that it returns -1 at the end of 141 * stream. 142 */ 143 public void testRead3() throws Exception { 144 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; 145 TestInputStream tis = new TestInputStream(data); 146 CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); 147 148 int expected = data.length; 149 byte[] result = new byte[expected]; 150 151 int skip = 2; 152 int ind = skip; // index into the data array (to check the got data) 153 // should read and discard bytes; 154 cis.read(null, 0, skip); 155 int got = skip + cis.read(result, 0, 1); // the number of got bytes 156 while (true) { 157 for (int j = 0; j < got - ind; j++) { 158 assertEquals("read(byte[] b, int off, int len) " 159 + "returned incorrect data.", result[j], data[ind + j]); 160 } 161 if (got == expected) { 162 break; 163 } else if (got > expected) { 164 fail("The data returned by " 165 + "read(byte[] b, int off, int len) " 166 + "is larger than expected."); 167 } else { 168 ind = got; 169 got += cis.read(result, 0, 3); 170 } 171 } 172 if (cis.read(result, 0, 1) != -1) { 173 fail("read() should return -1 at the end of the stream."); 174 } 175 } 176 177 /** 178 * skip(long n) method testing. Tests that the method correctly skips the 179 * bytes. 180 */ 181 public void testSkip() throws Exception { 182 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; 183 TestInputStream tis = new TestInputStream(data); 184 CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); 185 int expected = data.length; 186 byte[] result = new byte[expected]; 187 188 int skipped = (int) cis.skip(2); 189 int ind = skipped; 190 int got = skipped + cis.read(result, 0, 1); // the number of got bytes 191 while (true) { 192 for (int j = 0; j < got - ind; j++) { 193 if (result[j] != data[ind + j]) { 194 fail("read(byte[] b, int off, int len) " 195 + "returned incorrect data: Expected " 196 + data[ind + j] + ", got: " + result[j]); 197 } 198 } 199 if (got == expected) { 200 break; 201 } else if (got > expected) { 202 fail("The data returned by " 203 + "read(byte[] b, int off, int len) " 204 + "is larger than expected."); 205 } else { 206 ind = got; 207 got += cis.read(result, 0, 1); 208 } 209 } 210 if ((got = cis.read(result, 0, 1)) != -1) { 211 fail("read() should return -1 at the end of the stream. " 212 + "Output is: " + got + "."); 213 } 214 } 215 216 /** 217 * available() method testing. Tests that the method always return 0. 218 */ 219 public void testAvailable() throws Exception { 220 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; 221 TestInputStream tis = new TestInputStream(data); 222 CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); 223 assertEquals("The returned by available() method value " 224 + "should be 0.", cis.available(), 0); 225 } 226 227 /** 228 * close() method testing. Tests that the method calls the close() 229 * method of the underlying input stream. 230 */ 231 public void testClose() throws Exception { 232 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 }; 233 TestInputStream tis = new TestInputStream(data); 234 CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); 235 cis.close(); 236 assertTrue("The close() method should call the close() method " 237 + "of its underlying input stream.", tis.wasClosed()); 238 } 239 240 /** 241 * markSupported() method testing. Tests that mark is not supported. 242 */ 243 public void testMarkSupported() { 244 byte[] data = new byte[] {-127, -100, -50, -10, -1, 0, 1, 10, 50, 127}; 245 TestInputStream tis = new TestInputStream(data); 246 CipherInputStream cis = new CipherInputStream(tis, new NullCipher()); 247 assertFalse("The returned by markSupported() method value " 248 + "should be false.", cis.markSupported()); 249 } 250 251 public void test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher () throws 252 NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { 253 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[100]); 254 255 KeyGenerator kg = KeyGenerator.getInstance("DES"); 256 kg.init(56, new SecureRandom()); 257 Key key = kg.generateKey(); 258 259 Cipher c = Cipher.getInstance("DES/CBC/NoPadding"); 260 c.init(Cipher.ENCRYPT_MODE, key); 261 262 CipherInputStream cis = new CipherInputStream(bais, c); 263 264 assertNotNull(cis); 265 } 266 267} 268 269