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 */
16package tests.targets.security;
17
18import dalvik.annotation.AndroidOnly;
19import java.security.MessageDigest;
20import java.security.NoSuchAlgorithmException;
21import java.security.Provider;
22import java.security.Security;
23import junit.framework.TestCase;
24
25public class MessageDigestTestMD2 extends TestCase {
26
27    @AndroidOnly("Android doesn't include MD2 message digest algorithm")
28    public void testMessageDigest1() throws Exception{
29        try {
30            MessageDigest digest = MessageDigest.getInstance("MD2");
31            fail("MD2 MessageDigest algorithm must not be supported");
32        } catch (NoSuchAlgorithmException e) {
33            // expected
34        }
35
36        try {
37            MessageDigest digest = MessageDigest.getInstance(
38                    "1.2.840.113549.2.2");
39            fail("MD2 MessageDigest algorithm must not be supported");
40        } catch (NoSuchAlgorithmException e) {
41            // expected
42        }
43    }
44
45    @AndroidOnly("Android allows usage of MD2 in third party providers")
46    public void testMessageDigest2() throws Exception{
47
48        Provider provider  = new MyProvider();
49        Security.addProvider(provider);
50
51        try {
52            MessageDigest digest = MessageDigest.getInstance("MD2");
53
54            digest = MessageDigest.getInstance("1.2.840.113549.2.2");
55        } finally {
56            Security.removeProvider(provider.getName());
57        }
58    }
59
60    public final class MyProvider extends Provider {
61        public MyProvider() {
62            super("MessageDigestMD2Test", 1.00, "TestProvider");
63            put("MessageDigest.MD2",
64                    "tests.targets.security.MessageDigestTestMD2$MD2");
65            put("Alg.Alias.MessageDigest.1.2.840.113549.2.2", "MD2");
66        }
67    }
68
69    public static class MD2 extends MessageDigest {
70
71        public MD2() {
72            super("MD2");
73        }
74
75        protected MD2(String algorithm) {
76            super(algorithm);
77        }
78
79        @Override
80        protected byte[] engineDigest() {
81            return null;
82        }
83
84        @Override
85        protected void engineReset() {
86        }
87
88        @Override
89        protected void engineUpdate(byte input) {
90        }
91
92        @Override
93        protected void engineUpdate(byte[] input, int offset, int len) {
94        }
95    }
96}
97