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 23package tests.security.spec; 24 25import junit.framework.TestCase; 26 27import java.math.BigInteger; 28import java.security.spec.ECPoint; 29 30/** 31 * Tests for <code>ECPoint</code> class fields and methods. 32 * 33 */ 34public class ECPointTest extends TestCase { 35 36 // 37 // Tests 38 // 39 40 /** 41 * Test #1 for <code>ECPoint(BigInteger, BigInteger)</code> constructor<br> 42 * Assertion: creates <code>ECPoint</code> instance<br> 43 * Test preconditions: valid parameters passed<br> 44 * Expected: must pass without any exceptions 45 */ 46 public final void testECPoint01() { 47 new ECPoint(BigInteger.ZERO, BigInteger.ZERO); 48 new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(-23456L)); 49 new ECPoint(BigInteger.valueOf(123456L), BigInteger.valueOf(123456L)); 50 new ECPoint(BigInteger.valueOf(-56L), BigInteger.valueOf(234L)); 51 new ECPoint(BigInteger.valueOf(3456L), BigInteger.valueOf(-2344L)); 52 } 53 54 /** 55 * Test #2 for <code>ECPoint(BigInteger x, BigInteger y)</code> constructor<br> 56 * Assertion: throws <code>NullPointerException</code> if <code>x</code>or 57 * <code>y</code> is <code>null</code><br> 58 * Test preconditions: pass <code>null</code> as mentioned parameters<br> 59 * Expected: must throw <code>NullPointerException</code> 60 */ 61 public final void testECPoint02() { 62 // test case 1: x is null 63 try { 64 new ECPoint(null, BigInteger.ZERO); 65 fail("#1: Expected NPE not thrown"); 66 } catch (NullPointerException ok) { 67 } 68 69 70 // test case 2: y is null 71 try { 72 new ECPoint(BigInteger.ZERO, null); 73 fail("#2: Expected NPE not thrown"); 74 } catch (NullPointerException ok) { 75 } 76 77 78 // test case 3: both : x and y are null 79 try { 80 new ECPoint(null, null); 81 fail("#3: Expected NPE not thrown"); 82 } catch (NullPointerException ok) { 83 } 84 } 85 86 /** 87 * Test #1 for <code>getAffineX()</code> method<br> 88 * Assertion: returns affine <code>x</code> coordinate<br> 89 * Test preconditions: <code>ECPoint</code> instance 90 * created using valid parameters<br> 91 * Expected: must return affine <code>x</code> coordinate 92 * which is equal to the one passed to the constructor; 93 * (both must refer the same object) 94 */ 95 public final void testGetAffineX01() { 96 BigInteger x = BigInteger.valueOf(-23456L); 97 ECPoint p = new ECPoint(x, BigInteger.valueOf(23456L)); 98 BigInteger xRet = p.getAffineX(); 99 assertEquals(x, xRet); 100 assertSame(x, xRet); 101 } 102 103 /** 104 * Test #2 for <code>getAffineX()</code> method<br> 105 * Assertion: returns <code>null</code> for <code>ECPoint.POINT_INFINITY</code><br> 106 * Test preconditions: none<br> 107 * Expected: must return <code>null</code> for 108 * <code>ECPoint.POINT_INFINITY</code> 109 */ 110 public final void testGetAffineX02() { 111 assertNull(ECPoint.POINT_INFINITY.getAffineX()); 112 } 113 114 /** 115 * Test #1 for <code>getAffineY()</code> method<br> 116 * Assertion: returns affine <code>y</code> coordinate<br> 117 * Test preconditions: <code>ECPoint</code> instance 118 * created using valid parameters<br> 119 * Expected: must return affine <code>y</code> coordinate 120 * which is equal to the one passed to the constructor; 121 * (both must refer the same object) 122 */ 123 public final void testGetAffineY01() { 124 BigInteger y = BigInteger.valueOf(23456L); 125 ECPoint p = new ECPoint(BigInteger.valueOf(-23456L), y); 126 BigInteger yRet = p.getAffineY(); 127 assertEquals(y, yRet); 128 assertSame(y, yRet); 129 } 130 131 /** 132 * Test #2 for <code>getAffineX()</code> method<br> 133 * Assertion: returns <code>null</code> for <code>ECPoint.POINT_INFINITY</code><br> 134 * Test preconditions: none<br> 135 * Expected: must return <code>null</code> for 136 * <code>ECPoint.POINT_INFINITY</code> 137 */ 138 public final void testGetAffineY02() { 139 assertNull(ECPoint.POINT_INFINITY.getAffineY()); 140 } 141 142 /** 143 * Test #1 for <code>equals(Object other)</code> method<br> 144 * Assertion: return true if this and other objects are equal<br> 145 * Test preconditions: see test comments<br> 146 * Expected: all objects in this test must be equal 147 */ 148 public final void testEqualsObject01() { 149 // test case 1: must be equal to itself 150 ECPoint p2=null, p1 = 151 new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE); 152 assertTrue(p1.equals(p1)); 153 154 // test case 2: equal objects 155 p1 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE); 156 p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L)); 157 assertTrue(p1.equals(p2) && p2.equals(p1)); 158 159 // test case 3: equal POINT_INFINITY object(s) 160 p1 = ECPoint.POINT_INFINITY; 161 p2 = ECPoint.POINT_INFINITY; 162 assertTrue(p1.equals(p2) && p2.equals(p1)); 163 } 164 165 /** 166 * Test #2 for <code>equals(Object other)</code> method<br> 167 * Assertion: return false if this and other objects are not equal<br> 168 * Test preconditions: see test comments<br> 169 * Expected: all objects in this test must be not equal 170 */ 171 public final void testEqualsObject02() { 172 // test case 1: must be not equal to null 173 ECPoint p2=null, p1 = 174 new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE); 175 assertFalse(p1.equals(p2)); 176 177 // test case 2: not equal objects - x 178 p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE); 179 p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L)); 180 assertFalse(p1.equals(p2) || p2.equals(p1)); 181 182 // test case 3: not equal objects - y 183 p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE); 184 p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO); 185 assertFalse(p1.equals(p2) || p2.equals(p1)); 186 187 // test case 4: not equal - some point and POINT_INFINITY 188 p1 = ECPoint.POINT_INFINITY; 189 p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO); 190 assertFalse(p1.equals(p2) || p2.equals(p1)); 191 } 192 193 /** 194 * Test #1 for <code>hashCode()</code> method.<br> 195 * 196 * Assertion: must return the same value if invoked 197 * repeatedly on the same object. 198 */ 199 public final void testHashCode01() { 200 ECPoint f = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE); 201 int hc = f.hashCode(); 202 assertTrue(hc == f.hashCode() && 203 hc == f.hashCode() && 204 hc == f.hashCode() && 205 hc == f.hashCode() && 206 hc == f.hashCode() && 207 hc == f.hashCode() && 208 hc == f.hashCode() && 209 hc == f.hashCode()); 210 211 212 // the same for POINT_INFINITY 213 hc = ECPoint.POINT_INFINITY.hashCode(); 214 assertTrue(hc == ECPoint.POINT_INFINITY.hashCode() && 215 hc == ECPoint.POINT_INFINITY.hashCode() && 216 hc == ECPoint.POINT_INFINITY.hashCode() && 217 hc == ECPoint.POINT_INFINITY.hashCode() && 218 hc == ECPoint.POINT_INFINITY.hashCode() && 219 hc == ECPoint.POINT_INFINITY.hashCode() && 220 hc == ECPoint.POINT_INFINITY.hashCode() && 221 hc == ECPoint.POINT_INFINITY.hashCode()); 222 } 223 224 /** 225 * Test #2 for <code>hashCode()</code> method.<br> 226 * 227 * Assertion: must return the same value if invoked 228 * on equal (according to the <code>equals(Object)</code> method) objects. 229 */ 230 public final void testHashCode02() { 231 ECPoint p1 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE); 232 ECPoint p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L)); 233 assertEquals(p1.hashCode(), p2.hashCode()); 234 } 235 236} 237