jdk/test/com/sun/crypto/provider/Cipher/PBE/TestCipherPBECons.java
author vinnie
Wed, 15 Jul 2015 18:12:48 +0100
changeset 31721 7dfa3c302b12
child 41350 1b73adfc5a6a
permissions -rw-r--r--
8041787: Need new regressions tests for buffer handling for PBE algorithms Reviewed-by: vinnie Contributed-by: rhalade <rajan.halade@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31721
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     1
/*
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     4
 *
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     8
 *
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    13
 * accompanied this code).
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    14
 *
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    18
 *
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    21
 * questions.
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    22
 */
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    23
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    24
import java.io.PrintStream;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    25
import java.security.NoSuchAlgorithmException;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    26
import java.security.Provider;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    27
import java.security.Security;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    28
import javax.crypto.Cipher;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    29
import javax.crypto.NoSuchPaddingException;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    30
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    31
/**
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    32
 * @test
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    33
 * @bug 8041787
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    34
 * @summary Verify that for PBEWithMD5AndDES cipher, only CBC mode and
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    35
 * PKCS#5Padding is allowed
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    36
 * @author Yun Ke
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    37
 * @author Bill Situ
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    38
 * @author Yu-Ching (Valerie) PENG
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    39
 * @run main TestCipherKeyWrapperPBEKey
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    40
 */
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    41
public class TestCipherPBECons {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    42
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    43
    private static final String[] PBEAlgorithms = {"pbeWithMD5ANDdes",
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    44
        "PBEWithMD5AndTripleDES"};
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    45
    private static final String[] cipherModes = {"ECb", "cbC", "cFB", "Cfb32",
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    46
        "OfB", "oFb64", "pCbC"};
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    47
    private static final String[] cipherPaddings = {"Pkcs5Padding", "NoPaDDing"};
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    48
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    49
    public static void main(String[] args) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    50
        TestCipherPBECons test = new TestCipherPBECons();
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    51
        Provider sunjce = Security.getProvider("SunJCE");
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    52
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    53
        if (!test.runAll(sunjce, System.out)) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    54
            throw new RuntimeException("One or more tests have failed....");
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    55
        }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    56
    }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    57
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    58
    public boolean runAll(Provider p, PrintStream out) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    59
        boolean finalResult = true;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    60
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    61
        for (String algorithm : PBEAlgorithms) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    62
            for (String mode : cipherModes) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    63
                for (String padding : cipherPaddings) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    64
                    out.println("Running test with " + algorithm
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    65
                            + "/" + mode + "/" + padding);
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    66
                    try {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    67
                        if (!runTest(p, algorithm, mode, padding, out)) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    68
                            finalResult = false;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    69
                            out.println("STATUS: Failed");
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    70
                        } else {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    71
                            out.println("STATUS: Passed");
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    72
                        }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    73
                    } catch (Exception ex) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    74
                        finalResult = false;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    75
                        ex.printStackTrace(out);
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    76
                        out.println("STATUS:Failed");
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    77
                    }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    78
                }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    79
            }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    80
        }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    81
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    82
        return finalResult;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    83
    }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    84
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    85
    public boolean runTest(Provider p, String algo, String mo, String pad,
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    86
            PrintStream out) throws Exception {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    87
        try {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    88
            // Initialization
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    89
            Cipher ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    90
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    91
            // No exception thrown, must be of the right mode and right
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    92
            // padding scheme
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    93
            return (mo.equalsIgnoreCase("CBC"))
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    94
                    && (pad.equalsIgnoreCase("PKCS5Padding"));
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    95
        } catch (NoSuchAlgorithmException ex) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    96
            if (p.getName().compareTo("SunJCE") == 0) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    97
                if (!(mo.equalsIgnoreCase("CBC")
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    98
                        && pad.equalsIgnoreCase("PKCS5Padding"))) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
    99
                    out.println("NoSuchAlgorithmException is as expected");
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   100
                    return true;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   101
                }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   102
            }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   103
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   104
            out.println("Caught exception: " + ex.getMessage());
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   105
            throw ex;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   106
        } catch (NoSuchPaddingException ex) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   107
            if (mo.equalsIgnoreCase("CBC")
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   108
                    && pad.equalsIgnoreCase("NoPadding")) {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   109
                out.println("NoSuchPaddingException is as expected");
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   110
                return true;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   111
            } else {
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   112
                out.println("Caught unexpected exception: " + ex.getMessage());
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   113
                return false;
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   114
            }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   115
        }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   116
    }
7dfa3c302b12 8041787: Need new regressions tests for buffer handling for PBE algorithms
vinnie
parents:
diff changeset
   117
}