jdk/test/javax/crypto/Cipher/TestGetInstance.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 5506 202f599c92aa
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 294
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 294
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 294
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 294
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4898428
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test that the new getInstance() implementation works correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class TestGetInstance {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static void same(Provider p1, Provider p2) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        if (p1 != p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
           throw new Exception("not same object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Provider p = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Cipher c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
294
61e9ed3ce684 6681652: Two new regression test failures in pkcs11 code
valeriep
parents: 2
diff changeset
    49
        c = Cipher.getInstance("PBEWithMD5AndTripleDES");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        c = Cipher.getInstance("des", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        c = Cipher.getInstance("des/cbc/pkcs5padding", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        c = Cipher.getInstance("des", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        c = Cipher.getInstance("des/cbc/pkcs5padding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            c = Cipher.getInstance("DES/XYZ/PKCS5Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            c = Cipher.getInstance("DES/XYZ/PKCS5Padding", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            c = Cipher.getInstance("DES/XYZ/PKCS5Padding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            c = Cipher.getInstance("DES/CBC/XYZPadding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            c = Cipher.getInstance("DES/CBC/XYZPadding", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            c = Cipher.getInstance("DES/CBC/XYZPadding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            c = Cipher.getInstance("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            c = Cipher.getInstance("foo", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            c = Cipher.getInstance("foo", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            c = Cipher.getInstance("foo", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            c = Cipher.getInstance("foo", Security.getProvider("SUN"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            c = Cipher.getInstance("foo", "bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        System.out.println("All Tests ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}