OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / crypto / tests / javax / crypto / CipherOutputStream1Test.java
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
23 package org.apache.harmony.crypto.tests.javax.crypto;
24
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
29
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;
39
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;
45
46 import junit.framework.TestCase;
47
48 @TestTargetClass(CipherOutputStream.class)
49 /**
50  */
51
52 public class CipherOutputStream1Test extends TestCase {
53
54     private static class TestOutputStream extends ByteArrayOutputStream {
55         private boolean closed = false;
56
57         public void close() {
58             closed = true;
59         }
60
61         public boolean wasClosed() {
62             return closed;
63         }
64     }
65
66     /**
67      * CipherOutputStream(OutputStream os) method testing. Tests that
68      * CipherOutputStream uses NullCipher if Cipher is not specified
69      * in the constructor.
70      */
71     @TestTargetNew(
72         level = TestLevel.COMPLETE,
73         notes = "",
74         method = "CipherOutputStream",
75         args = {java.io.OutputStream.class}
76     )
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){};
81         cos.write(data);
82         cos.flush();
83         byte[] result = tos.toByteArray();
84         if (!Arrays.equals(result, data)) {
85             fail("NullCipher should be used " + "if Cipher is not specified.");
86         }
87     }
88
89     /**
90      * write(int b) method testing. Tests that method writes correct values to
91      * the underlying output stream.
92      */
93     @TestTargetNew(
94         level = TestLevel.COMPLETE,
95         notes = "Can not check IOException.",
96         method = "write",
97         args = {int.class}
98     )
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++) {
104             cos.write(data[i]);
105         }
106         cos.flush();
107         byte[] result = tos.toByteArray();
108         if (!Arrays.equals(result, data)) {
109             fail("CipherOutputStream wrote incorrect data.");
110         }
111     }
112
113     /**
114      * write(byte[] b) method testing. Tests that method writes correct values
115      * to the underlying output stream.
116      */
117     @TestTargetNew(
118         level = TestLevel.COMPLETE,
119         notes = "Can not check IOException.",
120         method = "write",
121         args = {byte[].class}
122     )
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());
127         cos.write(data);
128         cos.flush();
129         byte[] result = tos.toByteArray();
130         if (!Arrays.equals(result, data)) {
131             fail("CipherOutputStream wrote incorrect data.");
132         }
133
134         try {
135             cos.write(null);
136             fail("NullPointerException expected");
137         } catch (NullPointerException e) {
138             //expected
139         }
140     }
141
142     /**
143      * write(byte[] b, int off, int len) method testing.
144      */
145     @TestTargetNew(
146         level = TestLevel.COMPLETE,
147         notes = "Can not check IOException.",
148         method = "write",
149         args = {byte[].class, int.class, int.class}
150     )
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);
157         }
158         cos.flush();
159         byte[] result = tos.toByteArray();
160         if (!Arrays.equals(result, data)) {
161             fail("CipherOutputStream wrote incorrect data.");
162         }
163     }
164
165     /**
166      * @tests write(byte[] b, int off, int len)
167      */
168     @TestTargetNew(
169         level = TestLevel.PARTIAL,
170         notes = "Regression test. IllegalArgumentException checked.",
171         method = "write",
172         args = {byte[].class, int.class, int.class}
173     )
174     public void testWrite4() throws Exception {
175         //Regression for HARMONY-758
176         try {
177             new CipherOutputStream(new BufferedOutputStream((OutputStream) null), new NullCipher()).write(new byte[] {0}, 1, Integer.MAX_VALUE);
178         } catch (IllegalArgumentException e) {
179         }
180     }
181
182     /**
183      * @tests write(byte[] b, int off, int len)
184      */
185     @TestTargetNew(
186         level = TestLevel.COMPLETE,
187         notes = "Can not check IOException.",
188         method = "write",
189         args = {byte[].class, int.class, int.class}
190     )
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
200     }
201
202     /**
203      * flush() method testing. Tests that method flushes the data to the
204      * underlying output stream.
205      */
206     @TestTargetNew(
207         level = TestLevel.COMPLETE,
208         notes = "Can not check IOException.",
209         method = "flush",
210         args = {}
211     )
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){};
216         cos.write(data);
217         cos.flush();
218         byte[] result = tos.toByteArray();
219         if (!Arrays.equals(result, data)) {
220             fail("CipherOutputStream did not flush the data.");
221         }
222     }
223
224     /**
225      * close() method testing. Tests that the method calls the close() method of
226      * the underlying input stream.
227      */
228     @TestTargetNew(
229         level = TestLevel.COMPLETE,
230         notes = "Can not check IOException.",
231         method = "close",
232         args = {}
233     )
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){};
238         cos.write(data);
239         cos.close();
240         byte[] result = tos.toByteArray();
241         if (!Arrays.equals(result, data)) {
242             fail("CipherOutputStream did not flush the data.");
243         }
244         assertTrue("The close() method should call the close() method "
245                 + "of its underlying output stream.", tos.wasClosed());
246     }
247
248     @TestTargetNew(
249         level = TestLevel.COMPLETE,
250         notes = "",
251         method = "CipherOutputStream",
252         args = {java.io.OutputStream.class, javax.crypto.Cipher.class}
253     )
254     public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws
255     NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
256         ByteArrayOutputStream baos = new ByteArrayOutputStream();
257
258         KeyGenerator kg = KeyGenerator.getInstance("DES");
259         kg.init(56, new SecureRandom());
260         Key key = kg.generateKey();
261
262         Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
263         c.init(Cipher.ENCRYPT_MODE, key);
264
265         CipherOutputStream cos = new CipherOutputStream(baos, c);
266
267         assertNotNull(cos);
268     }
269 }
270