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: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.io.BufferedOutputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.OutputStream;
026: import java.util.Arrays;
027: import javax.crypto.NullCipher;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: */
033:
034: public class CipherOutputStreamTest extends TestCase {
035:
036: private static class TestOutputStream extends ByteArrayOutputStream {
037: private boolean closed = false;
038:
039: public void close() {
040: closed = true;
041: }
042:
043: public boolean wasClosed() {
044: return closed;
045: }
046: }
047:
048: /**
049: * CipherOutputStream(OutputStream os) method testing. Tests that
050: * CipherOutputStream uses NullCipher if Cipher is not specified
051: * in the constructor.
052: */
053: public void testCipherOutputStream() throws Exception {
054: byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
055: 50, 127 };
056: TestOutputStream tos = new TestOutputStream();
057: CipherOutputStream cos = new CipherOutputStream(tos);
058: cos.write(data);
059: cos.flush();
060: byte[] result = tos.toByteArray();
061: if (!Arrays.equals(result, data)) {
062: fail("NullCipher should be used "
063: + "if Cipher is not specified.");
064: }
065: }
066:
067: /**
068: * write(int b) method testing. Tests that method writes correct values to
069: * the underlying output stream.
070: */
071: public void testWrite1() throws Exception {
072: byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
073: 50, 127 };
074: TestOutputStream tos = new TestOutputStream();
075: CipherOutputStream cos = new CipherOutputStream(tos,
076: new NullCipher());
077: for (int i = 0; i < data.length; i++) {
078: cos.write(data[i]);
079: }
080: cos.flush();
081: byte[] result = tos.toByteArray();
082: if (!Arrays.equals(result, data)) {
083: fail("CipherOutputStream wrote incorrect data.");
084: }
085: }
086:
087: /**
088: * write(byte[] b) method testing. Tests that method writes correct values
089: * to the underlying output stream.
090: */
091: public void testWrite2() throws Exception {
092: byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
093: 50, 127 };
094: TestOutputStream tos = new TestOutputStream();
095: CipherOutputStream cos = new CipherOutputStream(tos,
096: new NullCipher());
097: cos.write(data);
098: cos.flush();
099: byte[] result = tos.toByteArray();
100: if (!Arrays.equals(result, data)) {
101: fail("CipherOutputStream wrote incorrect data.");
102: }
103: }
104:
105: /**
106: * write(byte[] b, int off, int len) method testing.
107: */
108: public void testWrite3() throws Exception {
109: byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
110: 50, 127 };
111: TestOutputStream tos = new TestOutputStream();
112: CipherOutputStream cos = new CipherOutputStream(tos,
113: new NullCipher());
114: for (int i = 0; i < data.length; i++) {
115: cos.write(data, i, 1);
116: }
117: cos.flush();
118: byte[] result = tos.toByteArray();
119: if (!Arrays.equals(result, data)) {
120: fail("CipherOutputStream wrote incorrect data.");
121: }
122: }
123:
124: /**
125: * @tests write(byte[] b, int off, int len)
126: */
127: public void testWrite4() throws Exception {
128: //Regression for HARMONY-758
129: try {
130: new CipherOutputStream(new BufferedOutputStream(
131: (OutputStream) null), new NullCipher()).write(
132: new byte[] { 0 }, 1, Integer.MAX_VALUE);
133: } catch (IllegalArgumentException e) {
134: }
135: }
136:
137: /**
138: * @tests write(byte[] b, int off, int len)
139: */
140: public void testWrite5() throws Exception {
141: //Regression for HARMONY-758
142: Cipher cf = Cipher.getInstance("DES/CBC/PKCS5Padding");
143: NullCipher nc = new NullCipher();
144: CipherOutputStream stream1 = new CipherOutputStream(
145: new BufferedOutputStream((OutputStream) null), nc);
146: CipherOutputStream stream2 = new CipherOutputStream(stream1, cf);
147: CipherOutputStream stream3 = new CipherOutputStream(stream2, nc);
148: stream3.write(new byte[] { 0 }, 0, 0);
149: //no exception expected
150: }
151:
152: /**
153: * flush() method testing. Tests that method flushes the data to the
154: * underlying output stream.
155: */
156: public void testFlush() throws Exception {
157: byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
158: 50, 127 };
159: TestOutputStream tos = new TestOutputStream();
160: CipherOutputStream cos = new CipherOutputStream(tos);
161: cos.write(data);
162: cos.flush();
163: byte[] result = tos.toByteArray();
164: if (!Arrays.equals(result, data)) {
165: fail("CipherOutputStream did not flush the data.");
166: }
167: }
168:
169: /**
170: * close() method testing. Tests that the method calls the close() method of
171: * the underlying input stream.
172: */
173: public void testClose() throws Exception {
174: byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10,
175: 50, 127 };
176: TestOutputStream tos = new TestOutputStream();
177: CipherOutputStream cos = new CipherOutputStream(tos);
178: cos.write(data);
179: cos.close();
180: byte[] result = tos.toByteArray();
181: if (!Arrays.equals(result, data)) {
182: fail("CipherOutputStream did not flush the data.");
183: }
184: assertTrue("The close() method should call the close() method "
185: + "of its underlying output stream.", tos.wasClosed());
186: }
187: }
|