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.
19 * @author Alexander Y. Kleymenov
23 package org.apache.harmony.crypto.tests.javax.crypto;
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
30 import java.io.BufferedOutputStream;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.OutputStream;
34 import java.security.InvalidKeyException;
35 import java.security.Key;
36 import java.security.NoSuchAlgorithmException;
37 import java.security.SecureRandom;
38 import java.util.Arrays;
40 import javax.crypto.KeyGenerator;
41 import javax.crypto.NoSuchPaddingException;
42 import javax.crypto.NullCipher;
43 import javax.crypto.CipherOutputStream;
44 import javax.crypto.Cipher;
46 import junit.framework.TestCase;
48 @TestTargetClass(CipherOutputStream.class)
52 public class CipherOutputStream1Test extends TestCase {
54 private static class TestOutputStream extends ByteArrayOutputStream {
55 private boolean closed = false;
61 public boolean wasClosed() {
67 * CipherOutputStream(OutputStream os) method testing. Tests that
68 * CipherOutputStream uses NullCipher if Cipher is not specified
72 level = TestLevel.COMPLETE,
74 method = "CipherOutputStream",
75 args = {java.io.OutputStream.class}
77 public void testCipherOutputStream() throws Exception {
78 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
79 TestOutputStream tos = new TestOutputStream();
80 CipherOutputStream cos = new CipherOutputStream(tos){};
83 byte[] result = tos.toByteArray();
84 if (!Arrays.equals(result, data)) {
85 fail("NullCipher should be used " + "if Cipher is not specified.");
90 * write(int b) method testing. Tests that method writes correct values to
91 * the underlying output stream.
94 level = TestLevel.COMPLETE,
95 notes = "Can not check IOException.",
99 public void testWrite1() throws Exception {
100 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
101 TestOutputStream tos = new TestOutputStream();
102 CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
103 for (int i = 0; i < data.length; i++) {
107 byte[] result = tos.toByteArray();
108 if (!Arrays.equals(result, data)) {
109 fail("CipherOutputStream wrote incorrect data.");
114 * write(byte[] b) method testing. Tests that method writes correct values
115 * to the underlying output stream.
118 level = TestLevel.COMPLETE,
119 notes = "Can not check IOException.",
121 args = {byte[].class}
123 public void testWrite2() throws Exception {
124 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
125 TestOutputStream tos = new TestOutputStream();
126 CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
129 byte[] result = tos.toByteArray();
130 if (!Arrays.equals(result, data)) {
131 fail("CipherOutputStream wrote incorrect data.");
136 fail("NullPointerException expected");
137 } catch (NullPointerException e) {
143 * write(byte[] b, int off, int len) method testing.
146 level = TestLevel.COMPLETE,
147 notes = "Can not check IOException.",
149 args = {byte[].class, int.class, int.class}
151 public void testWrite3() throws Exception {
152 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
153 TestOutputStream tos = new TestOutputStream();
154 CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
155 for (int i = 0; i < data.length; i++) {
156 cos.write(data, i, 1);
159 byte[] result = tos.toByteArray();
160 if (!Arrays.equals(result, data)) {
161 fail("CipherOutputStream wrote incorrect data.");
166 * @tests write(byte[] b, int off, int len)
169 level = TestLevel.PARTIAL,
170 notes = "Regression test. IllegalArgumentException checked.",
172 args = {byte[].class, int.class, int.class}
174 public void testWrite4() throws Exception {
175 //Regression for HARMONY-758
177 new CipherOutputStream(new BufferedOutputStream((OutputStream) null), new NullCipher()).write(new byte[] {0}, 1, Integer.MAX_VALUE);
178 } catch (IllegalArgumentException e) {
183 * @tests write(byte[] b, int off, int len)
186 level = TestLevel.COMPLETE,
187 notes = "Can not check IOException.",
189 args = {byte[].class, int.class, int.class}
191 public void testWrite5() throws Exception {
192 //Regression for HARMONY-758
193 Cipher cf = Cipher.getInstance("DES/CBC/PKCS5Padding");
194 NullCipher nc = new NullCipher();
195 CipherOutputStream stream1 = new CipherOutputStream(new BufferedOutputStream((OutputStream) null), nc);
196 CipherOutputStream stream2 = new CipherOutputStream(stream1, cf);
197 CipherOutputStream stream3 = new CipherOutputStream(stream2, nc);
198 stream3.write(new byte[] {0}, 0, 0);
199 //no exception expected
203 * flush() method testing. Tests that method flushes the data to the
204 * underlying output stream.
207 level = TestLevel.COMPLETE,
208 notes = "Can not check IOException.",
212 public void testFlush() throws Exception {
213 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
214 TestOutputStream tos = new TestOutputStream();
215 CipherOutputStream cos = new CipherOutputStream(tos){};
218 byte[] result = tos.toByteArray();
219 if (!Arrays.equals(result, data)) {
220 fail("CipherOutputStream did not flush the data.");
225 * close() method testing. Tests that the method calls the close() method of
226 * the underlying input stream.
229 level = TestLevel.COMPLETE,
230 notes = "Can not check IOException.",
234 public void testClose() throws Exception {
235 byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
236 TestOutputStream tos = new TestOutputStream();
237 CipherOutputStream cos = new CipherOutputStream(tos){};
240 byte[] result = tos.toByteArray();
241 if (!Arrays.equals(result, data)) {
242 fail("CipherOutputStream did not flush the data.");
244 assertTrue("The close() method should call the close() method "
245 + "of its underlying output stream.", tos.wasClosed());
249 level = TestLevel.COMPLETE,
251 method = "CipherOutputStream",
252 args = {java.io.OutputStream.class, javax.crypto.Cipher.class}
254 public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws
255 NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
256 ByteArrayOutputStream baos = new ByteArrayOutputStream();
258 KeyGenerator kg = KeyGenerator.getInstance("DES");
259 kg.init(56, new SecureRandom());
260 Key key = kg.generateKey();
262 Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
263 c.init(Cipher.ENCRYPT_MODE, key);
265 CipherOutputStream cos = new CipherOutputStream(baos, c);