jdk/test/com/sun/crypto/provider/CICO/PBEFunc/CICO_PBE_Test.java
author tyan
Wed, 23 Sep 2015 21:28:14 +0000
changeset 32766 2322e6c2a032
permissions -rw-r--r--
8048604: Tests for strong crypto ciphers Summary: SQE test co-location effort Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32766
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     1
/*
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     2
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     4
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     8
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    13
 * accompanied this code).
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    14
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    18
 *
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    21
 * questions.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    22
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    23
import java.io.ByteArrayInputStream;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    24
import java.io.IOException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    25
import java.security.GeneralSecurityException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    26
import javax.crypto.Cipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    27
import javax.crypto.CipherInputStream;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    28
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    29
/**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    30
 * This is an abstract class for CipherInputStream/CipherOutputStream PBE
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    31
 * functional tests.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    32
 */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    33
public abstract class CICO_PBE_Test {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    34
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    35
     * Sample string for byte buffer.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    36
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    37
    public static final String BYTE_ARR_BUFFER = "byteArrayBuffering";
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    38
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    39
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    40
     * Sample string for int buffer.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    41
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    42
    public static final String INT_BYTE_BUFFER = "intByteBuffering";
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    43
    public static final String PASS_PHRASE = "Some password phrase!";
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    44
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    45
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    46
     * Text string size.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    47
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    48
    public static final int TEXT_SIZE = 800;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    49
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    50
    protected final byte[] plainText;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    51
    private final Cipher encryptCipher, decryptCipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    52
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    53
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    54
     * An CipherInputStream for reading cipher and plain text.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    55
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    56
    private final CipherInputStream ciInput;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    57
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    58
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    59
     * Constructor by algorithm.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    60
     * @param pbeAlgo PBE algorithm to test.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    61
     * @throws GeneralSecurityException if any security error.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    62
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    63
    public CICO_PBE_Test(PBEAlgorithm pbeAlgo) throws GeneralSecurityException {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    64
        // Do initialization of the plainText
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    65
        plainText = TestUtilities.generateBytes(TEXT_SIZE);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    66
        // Do initialization of the ciphers
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    67
        AbstractPBEWrapper pbeWrap = AbstractPBEWrapper.createWrapper(pbeAlgo, PASS_PHRASE);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    68
        encryptCipher = pbeWrap.initCipher(Cipher.ENCRYPT_MODE);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    69
        decryptCipher = pbeWrap.initCipher(Cipher.DECRYPT_MODE);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    70
        // init cipher input stream
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    71
        ciInput = new CipherInputStream(new ByteArrayInputStream(plainText),
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    72
                encryptCipher);
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    73
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    74
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    75
    protected byte[] getPlainText() {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    76
        return plainText;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    77
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    78
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    79
    /**
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    80
     * The body of the test. Should be defined in subclasses.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    81
     * @param type byteArrayBuffering or intByteBuffering
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    82
     * @throws IOException I/O operation failed.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    83
     * @throws GeneralSecurityException all exceptions thrown.
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    84
     */
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    85
    protected abstract void proceedTest(String type)
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    86
            throws IOException, GeneralSecurityException;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    87
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    88
    protected Cipher getEncryptCipher() {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    89
        return encryptCipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    90
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    91
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    92
    public CipherInputStream getCiInput() {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    93
        return ciInput;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    94
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    95
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    96
    public Cipher getDecryptCipher() {
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    97
        return decryptCipher;
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    98
    }
2322e6c2a032 8048604: Tests for strong crypto ciphers
tyan
parents:
diff changeset
    99
}