1 /* 2 * Copyright (C) 2012 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 17 package org.apache.harmony.security.tests; 18 19 import org.apache.harmony.security.utils.AlgNameMapper; 20 21 import java.io.ByteArrayOutputStream; 22 import java.io.PrintStream; 23 import java.util.Locale; 24 25 import junit.framework.TestCase; 26 27 public class AlgNameMapperTest extends TestCase { 28 private final String[][] HARDCODED_ALIASES = { 29 {"1.2.840.10040.4.1", "DSA"}, 30 {"1.2.840.10040.4.3", "SHA1withDSA"}, 31 {"1.2.840.113549.1.1.1", "RSA"}, 32 {"1.2.840.113549.1.1.4", "MD5withRSA"}, 33 {"1.2.840.113549.1.1.5", "SHA1withRSA"}, 34 {"1.2.840.113549.1.3.1", "DiffieHellman"}, 35 {"1.2.840.113549.1.5.3", "pbeWithMD5AndDES-CBC"}, 36 {"1.2.840.113549.1.12.1.3", "pbeWithSHAAnd3-KeyTripleDES-CBC"}, 37 {"1.2.840.113549.1.12.1.6", "pbeWithSHAAnd40BitRC2-CBC"}, 38 {"1.2.840.113549.3.2", "RC2-CBC"}, 39 {"1.2.840.113549.3.3", "RC2-EBC"}, 40 {"1.2.840.113549.3.4", "RC4"}, 41 {"1.2.840.113549.3.5", "RC4WithMAC"}, 42 {"1.2.840.113549.3.6", "DESx-CBC"}, 43 {"1.2.840.113549.3.7", "TripleDES-CBC"}, 44 {"1.2.840.113549.3.8", "rc5CBC"}, 45 {"1.2.840.113549.3.9", "RC5-CBC"}, 46 {"1.2.840.113549.3.10", "DESCDMF"}, 47 {"2.23.42.9.11.4.1", "ECDSA"}, 48 }; 49 50 public void testHardcodedAliases() throws Exception { 51 final ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); 52 PrintStream out = new PrintStream(errBuffer); 53 54 for (int i = 0; i < HARDCODED_ALIASES.length; i++) { 55 try { 56 assertEquals(HARDCODED_ALIASES[i][1].toUpperCase(Locale.US), 57 AlgNameMapper.map2AlgName(HARDCODED_ALIASES[i][0]).toUpperCase(Locale.US)); 58 59 assertEquals(HARDCODED_ALIASES[i][0], 60 AlgNameMapper.map2OID(HARDCODED_ALIASES[i][1])); 61 62 assertEquals(HARDCODED_ALIASES[i][1].toUpperCase(Locale.US), 63 AlgNameMapper.getStandardName(HARDCODED_ALIASES[i][1] 64 .toUpperCase(Locale.US)).toUpperCase(Locale.US)); 65 66 assertTrue(AlgNameMapper.isOID(HARDCODED_ALIASES[i][0])); 67 } catch (Throwable e) { 68 out.append("Error encountered checking " + HARDCODED_ALIASES[i][1] + "\n"); 69 e.printStackTrace(out); 70 } 71 } 72 73 out.flush(); 74 if (errBuffer.size() > 0) { 75 throw new Exception("Errors encountered:\n\n" + errBuffer.toString() + "\n\n"); 76 } 77 } 78 79 private final String[][] NON_HARDCODED_ALIASES = { 80 {"2.16.840.1.101.3.4.2.3", "SHA512"}, // This isn't currently hardcoded in AlgNameMapper 81 {"1.2.840.10045.3.1.7", "prime256v1"}, // No provider provides EC curves 82 }; 83 84 public void testNon_Hardcoded_Aliases_Exist() throws Exception { 85 final ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); 86 PrintStream out = new PrintStream(errBuffer); 87 88 for (int i = 0; i < NON_HARDCODED_ALIASES.length; i++) { 89 try { 90 String algName = AlgNameMapper.map2AlgName(NON_HARDCODED_ALIASES[i][0]); 91 assertNotNull(algName); 92 assertEquals(NON_HARDCODED_ALIASES[i][1].toUpperCase(Locale.US), 93 algName.toUpperCase(Locale.US)); 94 95 String oid = AlgNameMapper.map2OID(algName); 96 assertNotNull(oid); 97 assertEquals(NON_HARDCODED_ALIASES[i][0], oid); 98 } catch (Throwable e) { 99 out.append("Error encountered checking " + HARDCODED_ALIASES[i][1] + "\n"); 100 e.printStackTrace(out); 101 } 102 } 103 104 out.flush(); 105 if (errBuffer.size() > 0) { 106 throw new Exception("Errors encountered:\n\n" + errBuffer.toString() + "\n\n"); 107 } 108 } 109 } 110