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 */
23import java.io.ByteArrayInputStream;
24import java.io.IOException;
25import java.security.GeneralSecurityException;
26import javax.crypto.Cipher;
27import javax.crypto.CipherInputStream;
28
29/**
30 * This is an abstract class for CipherInputStream/CipherOutputStream PBE
31 * functional tests.
32 */
33public abstract class CICO_PBE_Test {
34    /**
35     * Sample string for byte buffer.
36     */
37    public static final String BYTE_ARR_BUFFER = "byteArrayBuffering";
38
39    /**
40     * Sample string for int buffer.
41     */
42    public static final String INT_BYTE_BUFFER = "intByteBuffering";
43    public static final String PASS_PHRASE = "Some password phrase!";
44
45    /**
46     * Text string size.
47     */
48    public static final int TEXT_SIZE = 800;
49
50    protected final byte[] plainText;
51    private final Cipher encryptCipher, decryptCipher;
52
53    /**
54     * An CipherInputStream for reading cipher and plain text.
55     */
56    private final CipherInputStream ciInput;
57
58    /**
59     * Constructor by algorithm.
60     * @param pbeAlgo PBE algorithm to test.
61     * @throws GeneralSecurityException if any security error.
62     */
63    public CICO_PBE_Test(PBEAlgorithm pbeAlgo) throws GeneralSecurityException {
64        // Do initialization of the plainText
65        plainText = TestUtilities.generateBytes(TEXT_SIZE);
66        // Do initialization of the ciphers
67        AbstractPBEWrapper pbeWrap = AbstractPBEWrapper.createWrapper(pbeAlgo, PASS_PHRASE);
68        encryptCipher = pbeWrap.initCipher(Cipher.ENCRYPT_MODE);
69        decryptCipher = pbeWrap.initCipher(Cipher.DECRYPT_MODE);
70        // init cipher input stream
71        ciInput = new CipherInputStream(new ByteArrayInputStream(plainText),
72                encryptCipher);
73    }
74
75    protected byte[] getPlainText() {
76        return plainText;
77    }
78
79    /**
80     * The body of the test. Should be defined in subclasses.
81     * @param type byteArrayBuffering or intByteBuffering
82     * @throws IOException I/O operation failed.
83     * @throws GeneralSecurityException all exceptions thrown.
84     */
85    protected abstract void proceedTest(String type)
86            throws IOException, GeneralSecurityException;
87
88    protected Cipher getEncryptCipher() {
89        return encryptCipher;
90    }
91
92    public CipherInputStream getCiInput() {
93        return ciInput;
94    }
95
96    public Cipher getDecryptCipher() {
97        return decryptCipher;
98    }
99}
100