1 /* 2 * Copyright (C) 2007 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.security.interfaces; 17 18 import junit.framework.TestCase; 19 20 import java.math.BigInteger; 21 import java.security.KeyFactory; 22 import java.security.interfaces.RSAKey; 23 import java.security.spec.RSAPrivateKeySpec; 24 import java.security.spec.RSAPublicKeySpec; 25 26 public class RSAKeyTest extends TestCase { 27 28 /** 29 * java.security.interfaces.RSAKey 30 * #getModulus() 31 * test covers following use cases 32 * Case 1: check private key 33 * Case 2: check public key 34 */ 35 public void test_getModulus() throws Exception { 36 KeyFactory gen = KeyFactory.getInstance("RSA"); 37 final BigInteger n = BigInteger.valueOf(3233); 38 final BigInteger d = BigInteger.valueOf(2753); 39 final BigInteger e = BigInteger.valueOf(17); 40 RSAKey key = null; 41 42 // Case 1: check private key 43 key = (RSAKey) gen.generatePrivate(new RSAPrivateKeySpec(n, d)); 44 assertEquals("invalid modulus", n, key.getModulus()); 45 46 // Case 2: check public key 47 key = (RSAKey) gen.generatePublic(new RSAPublicKeySpec(n, e)); 48 assertEquals("invalid modulus", n, key.getModulus()); 49 } 50 } 51