jdk/test/javax/crypto/Mac/TestGetInstance.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4898428
       
    27  * @summary test that the new getInstance() implementation works correctly
       
    28  * @author Andreas Sterbenz
       
    29  */
       
    30 
       
    31 import java.security.*;
       
    32 
       
    33 import javax.crypto.*;
       
    34 
       
    35 public class TestGetInstance {
       
    36 
       
    37     private static void same(Object o1, Object o2) throws Exception {
       
    38         if (o1 != o2) {
       
    39             throw new Exception("not same object");
       
    40         }
       
    41     }
       
    42 
       
    43     public static void main(String[] args) throws Exception {
       
    44         long start = System.currentTimeMillis();
       
    45 
       
    46         Provider p = Security.getProvider("SunJCE");
       
    47 
       
    48         Mac mac;
       
    49 
       
    50         mac = Mac.getInstance("hmacmd5");
       
    51         System.out.println("Default: " + mac.getProvider().getName());
       
    52         mac = Mac.getInstance("hmacmd5", "SunJCE");
       
    53         same(p, mac.getProvider());
       
    54         mac = Mac.getInstance("hmacmd5", p);
       
    55         same(p, mac.getProvider());
       
    56 
       
    57         try {
       
    58             mac = Mac.getInstance("foo");
       
    59             throw new AssertionError();
       
    60         } catch (NoSuchAlgorithmException e) {
       
    61             System.out.println(e);
       
    62         }
       
    63         try {
       
    64             mac = Mac.getInstance("foo", "SunJCE");
       
    65             throw new AssertionError();
       
    66         } catch (NoSuchAlgorithmException e) {
       
    67             System.out.println(e);
       
    68         }
       
    69         try {
       
    70             mac = Mac.getInstance("foo", p);
       
    71             throw new AssertionError();
       
    72         } catch (NoSuchAlgorithmException e) {
       
    73             System.out.println(e);
       
    74         }
       
    75 
       
    76         try {
       
    77             mac = Mac.getInstance("foo", "SUN");
       
    78             throw new AssertionError();
       
    79         } catch (NoSuchAlgorithmException e) {
       
    80             System.out.println(e);
       
    81         }
       
    82         try {
       
    83             mac = Mac.getInstance("foo", Security.getProvider("SUN"));
       
    84             throw new AssertionError();
       
    85         } catch (NoSuchAlgorithmException e) {
       
    86             System.out.println(e);
       
    87         }
       
    88         try {
       
    89             mac = Mac.getInstance("foo", "foo");
       
    90             throw new AssertionError();
       
    91         } catch (NoSuchProviderException e) {
       
    92             System.out.println(e);
       
    93         }
       
    94         long stop = System.currentTimeMillis();
       
    95         System.out.println("Done (" + (stop - start) + " ms).");
       
    96     }
       
    97 }