1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19 * @author Alexander Y. Kleymenov 20 */ 21 22package org.apache.harmony.crypto.tests.javax.crypto.spec; 23 24import java.security.spec.MGF1ParameterSpec; 25import java.security.spec.AlgorithmParameterSpec; 26 27import javax.crypto.spec.OAEPParameterSpec; 28import javax.crypto.spec.PSource; 29 30import junit.framework.Test; 31import junit.framework.TestCase; 32import junit.framework.TestSuite; 33 34/** 35 */ 36 37public class OAEPParameterSpecTest extends TestCase { 38 39 /** 40 * OAEPParameterSpec(String mdName, String mgfName, AlgorithmParameterSpec 41 * mgfSpec, PSource pSrc) method testing. Tests that NullPointerException 42 * is thrown in the case of inappropriate constructor parameters and checks 43 * the value of DEFAULT field. 44 */ 45 public void testOAEPParameterSpec() { 46 // using init values for OAEPParameterSpec.DEFAULT 47 String mdName = "SHA-1"; 48 String mgfName = "MGF1"; 49 AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; 50 PSource pSrc = PSource.PSpecified.DEFAULT; 51 52 try { 53 new OAEPParameterSpec(null, mgfName, mgfSpec, pSrc); 54 fail("NullPointerException should be thrown in the case of " 55 + "null mdName."); 56 } catch (NullPointerException e) { 57 } 58 59 try { 60 new OAEPParameterSpec(mdName, null, mgfSpec, pSrc); 61 fail("NullPointerException should be thrown in the case of " 62 + "null mgfName."); 63 } catch (NullPointerException e) { 64 } 65 66 try { 67 new OAEPParameterSpec(mdName, mgfName, mgfSpec, null); 68 fail("NullPointerException should be thrown in the case of " 69 + "null pSrc."); 70 } catch (NullPointerException e) { 71 } 72 73 assertTrue("The message digest algorithm name of " 74 + "OAEPParameterSpec.DEFAULT field should be " + mdName, 75 OAEPParameterSpec.DEFAULT.getDigestAlgorithm().equals(mdName)); 76 77 assertTrue("The mask generation function algorithm name of " 78 + "OAEPParameterSpec.DEFAULT field should be " + mgfName, 79 OAEPParameterSpec.DEFAULT.getMGFAlgorithm().equals(mgfName)); 80 81 assertTrue("The mask generation function parameters of " 82 + "OAEPParameterSpec.DEFAULT field should be the same object " 83 + "as MGF1ParameterSpec.SHA1", 84 OAEPParameterSpec.DEFAULT.getMGFParameters() 85 == MGF1ParameterSpec.SHA1); 86 assertTrue("The source of the encoding input P of " 87 + "OAEPParameterSpec.DEFAULT field should be the same object " 88 + "PSource.PSpecified.DEFAULT", 89 OAEPParameterSpec.DEFAULT.getPSource() 90 == PSource.PSpecified.DEFAULT); 91 } 92 93 /** 94 * getDigestAlgorithm() method testing. 95 */ 96 public void testGetDigestAlgorithm() { 97 String mdName = "SHA-1"; 98 String mgfName = "MGF1"; 99 AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; 100 PSource pSrc = PSource.PSpecified.DEFAULT; 101 102 OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, 103 mgfSpec, pSrc); 104 assertTrue("The returned value does not equal to the " 105 + "value specified in the constructor.", 106 ps.getDigestAlgorithm().equals(mdName)); 107 } 108 109 /** 110 * getMGFAlgorithm() method testing. 111 */ 112 public void testGetMGFAlgorithm() { 113 String mdName = "SHA-1"; 114 String mgfName = "MGF1"; 115 AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; 116 PSource pSrc = PSource.PSpecified.DEFAULT; 117 118 OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, 119 mgfSpec, pSrc); 120 assertTrue("The returned value does not equal to the " 121 + "value specified in the constructor.", 122 ps.getMGFAlgorithm().equals(mgfName)); 123 } 124 125 /** 126 * getMGFParameters() method testing. 127 */ 128 public void testGetMGFParameters() { 129 String mdName = "SHA-1"; 130 String mgfName = "MGF1"; 131 AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; 132 PSource pSrc = PSource.PSpecified.DEFAULT; 133 134 OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, 135 mgfSpec, pSrc); 136 assertTrue("The returned value does not equal to the " 137 + "value specified in the constructor.", 138 ps.getMGFParameters() == mgfSpec); 139 } 140 141 /** 142 * getPSource() method testing. 143 */ 144 public void testGetPSource() { 145 String mdName = "SHA-1"; 146 String mgfName = "MGF1"; 147 AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1; 148 PSource pSrc = PSource.PSpecified.DEFAULT; 149 150 OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, 151 mgfSpec, pSrc); 152 assertTrue("The returned value does not equal to the " 153 + "value specified in the constructor.", 154 ps.getPSource() == pSrc); 155 } 156 157 public static Test suite() { 158 return new TestSuite(OAEPParameterSpecTest.class); 159 } 160 161} 162