Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/CICOChainingTest.java
41155 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.IOException;25import java.io.InputStream;26import java.io.OutputStream;27import java.io.PipedInputStream;28import java.io.PipedOutputStream;29import java.util.Arrays;30import javax.crypto.CipherInputStream;31import javax.crypto.CipherOutputStream;3233/*34* @test35* @bug 804860436* @summary This test verifies the assertion "The chaining feature of37* Filter streams should be supported." for feature "CipherInputStream &38* CipherOutputStream"39* @run main CICOChainingTest40*/41public class CICOChainingTest {42/**43* Plain text length.44*/45private static final int PLAIN_TEXT_LENGTH = 200;4647public static void main(String argv[]) throws Exception {48CICOChainingTest test = new CICOChainingTest();49test.chainTest(true);50test.chainTest(false);51}5253/**54* Chain CipherInputStream/CipherOutputStream with other stream, encrypt55* the text and decrypt it, recovered text is supposed to be same as56* original text.57* @param useInt true if read byte by byte false if read with buffer.58* @throws IOException any I/O operation failed.59*/60public void chainTest(boolean useInt) throws IOException {61byte[] plainText = TestUtilities.generateBytes(PLAIN_TEXT_LENGTH);62byte[] recoveredText = new byte[plainText.length];63// Do initialization64try (MyNullCipherInputStream ciInput1 = new MyNullCipherInputStream(65new ByteArrayInputStream(plainText));66PipedOutputStream piOut = new PipedOutputStream();67MyNullCipherInputStream ciInput2 = new MyNullCipherInputStream(68new PipedInputStream(piOut));69MyNullCipherOutputStream ciOut = new MyNullCipherOutputStream(70piOut);) {71if (useInt) {72int buffer = ciInput1.read();73while (buffer != -1) {74piOut.write(buffer);75buffer = ciInput1.read();76}77} else {78byte[] buffer = new byte[20];79int len = ciInput1.read(buffer);80while (len != -1) {81ciOut.write(buffer, 0, len);82len = ciInput1.read(buffer);83}84}85ciOut.flush();86piOut.flush();87// Get the output88ciInput2.read(recoveredText);89if (ciInput2.available() > 0) {90throw new RuntimeException("Expected no data from ciInput2, but"91+ " ciInput2.available() = " + ciInput2.available());92}93}94// Verify output is same to input.95if (!Arrays.equals(plainText, recoveredText)) {96throw new RuntimeException("plainText:" + new String(plainText)97+ " recoveredText:" + new String(recoveredText)98+ " Test failed due to result compare fail");99}100}101}102103class MyNullCipherInputStream extends CipherInputStream {104105public MyNullCipherInputStream(InputStream is) {106super(is);107}108}109110class MyNullCipherOutputStream extends CipherOutputStream {111112public MyNullCipherOutputStream(OutputStream os) {113super(os);114}115}116117118