jdk/test/javax/crypto/Cipher/TestGetInstance.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 294 61e9ed3ce684
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        c = Cipher.getInstance("des");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        c = Cipher.getInstance("des/cbc/pkcs5padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        c = Cipher.getInstance("des", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        c = Cipher.getInstance("des/cbc/pkcs5padding", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        c = Cipher.getInstance("des", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        c = Cipher.getInstance("des/cbc/pkcs5padding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        same(p, c.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            c = Cipher.getInstance("DES/XYZ/PKCS5Padding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            c = Cipher.getInstance("DES/XYZ/PKCS5Padding", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            c = Cipher.getInstance("DES/XYZ/PKCS5Padding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            c = Cipher.getInstance("DES/CBC/XYZPadding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            c = Cipher.getInstance("DES/CBC/XYZPadding", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            c = Cipher.getInstance("DES/CBC/XYZPadding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            c = Cipher.getInstance("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            c = Cipher.getInstance("foo", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            c = Cipher.getInstance("foo", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            c = Cipher.getInstance("foo", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            c = Cipher.getInstance("foo", Security.getProvider("SUN"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            c = Cipher.getInstance("foo", "bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        System.out.println("All Tests ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}