1/*
2 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.io.ByteArrayOutputStream;
25import java.io.IOException;
26import java.security.GeneralSecurityException;
27
28import javax.crypto.CipherOutputStream;
29
30/**
31 * CICO PBE Read/Write functional test.
32 *
33 * Verifies for the given PBE algorithm if the encrypt/decrypt mechanism is
34 * performed correctly for CipherInputStream and CipherOutputStream.
35 *
36 * Test scenario:
37 * 1. initializes plain text with random generated data.
38 * 2. for the given PBE algorithm instantiates encrypt and decrypt Ciphers.
39 * 3. instantiates CipherInputStream with the encrypt Cipher.
40 * 4. instantiates CipherOutputStream with the decrypt Cipher.
41 * 5. performs reading from the CipherInputStream (encryption data) and writing
42 *    to the CipherOutputStream (decryption). As a result the output of the
43 *    CipherOutputStream should be the same as an original plain text.
44 * 6. compares if the original plain text is the same as the output of the
45 *    CipherOutputStream.
46 *
47 * The test implements 2 test cases in accordance with buffering type:
48 * 1. byte array buffering
49 * 2. int buffering
50 */
51public class CICO_PBE_RW_Test extends CICO_PBE_Test {
52
53    public CICO_PBE_RW_Test(PBEAlgorithm pbeAlgo)
54            throws GeneralSecurityException {
55        super(pbeAlgo);
56    }
57
58    /**
59     * The CICO PBE RW test specific part of the super.doTest(). Implements the
60     * scenario in accordance to the class description.
61     * @param type byteArrayBuffering or intByteBuffering
62     * @throws IOException  any I/O operation failed.
63     * @throws GeneralSecurityException any security error.
64     */
65    @Override
66    public void proceedTest(String type) throws IOException,
67            GeneralSecurityException {
68        ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
69        try (CipherOutputStream ciOutput = new CipherOutputStream(baOutput,
70                getDecryptCipher())) {
71            if (type.equals(CICO_PBE_Test.BYTE_ARR_BUFFER)) {
72                proceedTestUsingByteArrayBuffer(ciOutput);
73            } else {
74                proceedTestUsingIntBuffer(ciOutput);
75            }
76            ciOutput.flush();
77        }
78        // Compare input and output
79        if (!TestUtilities.equalsBlock(plainText, baOutput.toByteArray(), TEXT_SIZE)) {
80            throw new RuntimeException("outputText not same with expectedText"
81                    + " when test " + type);
82        }
83    }
84
85    /**
86     * Implements byte array buffering type test case of the CICO PBE RW test.
87     * @param ciOutput  output stream for data written.
88     * @throws java.io.IOException any I/O operation failed.
89     */
90    public void proceedTestUsingByteArrayBuffer(
91            CipherOutputStream ciOutput) throws IOException {
92        byte[] buffer = new byte[TEXT_SIZE];
93        int len = getCiInput().read(buffer);
94        while (len != -1) {
95            ciOutput.write(buffer, 0, len);
96            len = getCiInput().read(buffer);
97        }
98    }
99
100    /**
101     * Implements int buffering type test case.
102     * @param ciOutput output stream for data written.
103     * @throws java.io.IOException any I/O operation failed.
104     */
105    public void proceedTestUsingIntBuffer(CipherOutputStream ciOutput)
106            throws IOException {
107        int buffer = getCiInput().read();
108        while (buffer != -1) {
109            ciOutput.write(buffer);
110            buffer = getCiInput().read();
111        }
112    }
113}
114