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 Vladimir N. Molotkov 20 * @version $Revision$ 21 */ 22 23 package tests.security.spec; 24 25 import junit.framework.TestCase; 26 27 import java.math.BigInteger; 28 import java.security.spec.ECFieldFp; 29 30 /** 31 * Tests for <code>ECFieldFp</code> class fields and methods. 32 * 33 */ 34 public class ECFieldFpTest extends TestCase { 35 36 // 37 // Tests 38 // 39 40 /** 41 * Test #1 for <code>ECFieldFp</code> constructor 42 * 43 * Assertion: creates new object of <code>ECFieldFp</code> class 44 * using valid <code>p</code> (odd prime) 45 */ 46 public final void testECFieldFp01() { 47 new ECFieldFp(BigInteger.valueOf(23L)); 48 } 49 50 /** 51 * Test #2 for <code>ECFieldFp</code> constructor 52 * 53 * Assertion: creates new object of <code>ECFieldFp</code> class 54 * using valid <code>p</code> (odd but not prime) 55 */ 56 public final void testECFieldFp02() { 57 new ECFieldFp(BigInteger.valueOf(21L)); 58 } 59 60 /** 61 * Test #3 for <code>ECFieldFp</code> constructor 62 * 63 * Assertion: IllegalArgumentException if <code>p</code> is not positive 64 */ 65 public final void testECFieldFp03() { 66 try { 67 new ECFieldFp(BigInteger.valueOf(-1L)); 68 fail(getName() + 69 " FAILED: expected exception has not been thrown"); 70 } catch (IllegalArgumentException e) { 71 } 72 } 73 74 /** 75 * Test #4 for <code>ECFieldFp</code> constructor 76 * 77 * Assertion: IllegalArgumentException if <code>p</code> is not positive 78 */ 79 public final void testECFieldFp04() { 80 try { 81 new ECFieldFp(BigInteger.valueOf(0L)); 82 fail(getName() + 83 " FAILED: expected exception has not been thrown"); 84 } catch (IllegalArgumentException e) { 85 } 86 } 87 88 /** 89 * Test #4 for <code>ECFieldFp</code> constructor 90 * 91 * Assertion: NullPointerException if <code>p</code> is null 92 */ 93 public final void testECFieldFp05() { 94 try { 95 new ECFieldFp(null); 96 fail(getName() + 97 " FAILED: expected exception has not been thrown"); 98 } catch (NullPointerException e) { 99 } 100 } 101 102 /** 103 * Test #1 for <code>hashCode()</code> method.<br> 104 * 105 * Assertion: must return the same value if invoked 106 * repeatedly on the same object. 107 */ 108 public final void testHashCode01() { 109 ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L)); 110 int hc = f.hashCode(); 111 assertTrue(hc == f.hashCode() && 112 hc == f.hashCode() && 113 hc == f.hashCode() && 114 hc == f.hashCode() && 115 hc == f.hashCode() && 116 hc == f.hashCode() && 117 hc == f.hashCode() && 118 hc == f.hashCode()); 119 } 120 121 /** 122 * Test #2 for <code>hashCode()</code> method.<br> 123 * 124 * Assertion: must return the same value if invoked 125 * on equal (according to the <code>equals(Object)</code> method) objects. 126 */ 127 public final void testHashCode02() { 128 assertTrue(new ECFieldFp(BigInteger.valueOf(23L)).hashCode() == 129 new ECFieldFp(BigInteger.valueOf(23L)).hashCode()); 130 } 131 132 /** 133 * Test for <code>getFieldSize()()</code> method.<br> 134 * 135 * Assertion: returns field size in bits which is prime size 136 */ 137 public final void testGetFieldSize() { 138 assertEquals(5, new ECFieldFp(BigInteger.valueOf(23L)).getFieldSize()); 139 } 140 141 /** 142 * Test for <code>getP()</code> method.<br> 143 * 144 * Assertion: returns prime 145 */ 146 public final void testGetP() { 147 BigInteger p = BigInteger.valueOf(23L); 148 assertTrue(p.equals(new ECFieldFp(p).getP())); 149 } 150 151 /** 152 * Test #1 for <code>equals()</code> method.<br> 153 * 154 * Assertion: object equals to itself. 155 */ 156 public final void testEqualsObject01() { 157 ECFieldFp obj = new ECFieldFp(BigInteger.valueOf(23L)); 158 assertTrue(obj.equals(obj)); 159 } 160 161 /** 162 * Test #2 for <code>equals(Object obj)</code> method.<br> 163 * 164 * Assertion: returns false if <code>obj</code> is <code>null</code> 165 */ 166 public final void testEqualsObject02() { 167 assertFalse(new ECFieldFp(BigInteger.valueOf(23L)).equals(null)); 168 } 169 170 /** 171 * Test #3 for <code>equals(Object obj)</code> method.<br> 172 * 173 * Assertion: returns false if <code>obj</code> 174 * is not instance of <code>ECFieldFp</code> 175 */ 176 public final void testEqualsObject03() { 177 assertFalse(new ECFieldFp(BigInteger.valueOf(23L)).equals(new Object())); 178 } 179 180 /** 181 * Test #4 for <code>equals()</code> method.<br> 182 * 183 * Assertion: true if prime values match. 184 */ 185 public final void testEqualsObject04() { 186 assertTrue(new ECFieldFp(BigInteger.valueOf(23L)).equals( 187 new ECFieldFp(BigInteger.valueOf(23L)))); 188 } 189 190 } 191