Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package tests.targets.security;
     17 
     18 import java.security.MessageDigest;
     19 import java.security.NoSuchAlgorithmException;
     20 import java.security.Provider;
     21 import java.security.Security;
     22 import junit.framework.TestCase;
     23 
     24 public class MessageDigestTestMD2 extends TestCase {
     25 
     26     public void testMessageDigest1() throws Exception{
     27         try {
     28             MessageDigest digest = MessageDigest.getInstance("MD2");
     29             fail("MD2 MessageDigest algorithm must not be supported");
     30         } catch (NoSuchAlgorithmException e) {
     31             // expected
     32         }
     33 
     34         try {
     35             MessageDigest digest = MessageDigest.getInstance(
     36                     "1.2.840.113549.2.2");
     37             fail("MD2 MessageDigest algorithm must not be supported");
     38         } catch (NoSuchAlgorithmException e) {
     39             // expected
     40         }
     41     }
     42 
     43     public void testMessageDigest2() throws Exception{
     44 
     45         Provider provider  = new MyProvider();
     46         Security.addProvider(provider);
     47 
     48         try {
     49             MessageDigest digest = MessageDigest.getInstance("MD2");
     50 
     51             digest = MessageDigest.getInstance("1.2.840.113549.2.2");
     52         } finally {
     53             Security.removeProvider(provider.getName());
     54         }
     55     }
     56 
     57     public final class MyProvider extends Provider {
     58         public MyProvider() {
     59             super("MessageDigestMD2Test", 1.00, "TestProvider");
     60             put("MessageDigest.MD2",
     61                     "tests.targets.security.MessageDigestTestMD2$MD2");
     62             put("Alg.Alias.MessageDigest.1.2.840.113549.2.2", "MD2");
     63         }
     64     }
     65 
     66     public static class MD2 extends MessageDigest {
     67 
     68         public MD2() {
     69             super("MD2");
     70         }
     71 
     72         protected MD2(String algorithm) {
     73             super(algorithm);
     74         }
     75 
     76         @Override
     77         protected byte[] engineDigest() {
     78             return null;
     79         }
     80 
     81         @Override
     82         protected void engineReset() {
     83         }
     84 
     85         @Override
     86         protected void engineUpdate(byte input) {
     87         }
     88 
     89         @Override
     90         protected void engineUpdate(byte[] input, int offset, int len) {
     91         }
     92     }
     93 }
     94