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*/ 21 22package org.apache.harmony.security.tests.java.security.spec; 23 24import java.math.BigInteger; 25import java.security.spec.ECFieldF2m; 26 27import junit.framework.TestCase; 28 29/** 30 * Tests for <code>ECFieldF2m</code> class fields and methods. 31 * 32 */ 33public class ECFieldF2m_ImplTest extends TestCase { 34 35 /** 36 * Support class for this test. 37 * Encapsulates <code>ECFieldF2m</code> testing 38 * domain parameters. 39 * 40 */ 41 private static final class ECFieldF2mDomainParams { 42 43 /** 44 * <code>NPE</code> reference object of class NullPointerException. 45 * NullPointerException must be thrown by <code>ECFieldF2m</code> 46 * ctors in some circumstances 47 */ 48 static final NullPointerException NPE = new NullPointerException(); 49 /** 50 * <code>IArgE</code> reference object of class IllegalArgumentException. 51 * IllegalArgumentException must be thrown by <code>ECFieldF2m</code> 52 * ctors in some circumstances 53 */ 54 static final IllegalArgumentException IArgE = new IllegalArgumentException(); 55 56 /** 57 * The <code>m</code> parameter for <code>ECFieldF2m</code> 58 * ctor for the current test. 59 */ 60 final int m; 61 /** 62 * The <code>rp</code> parameter for <code>ECFieldF2m</code> 63 * ctor for the current test. 64 */ 65 final BigInteger rp; 66 /** 67 * The <code>ks</code> parameter for <code>ECFieldF2m</code> 68 * ctor for the current test. 69 */ 70 final int[] ks; 71 72 73 /** 74 * Exception expected with this parameters set or <code>null</code> 75 * if no exception expected. 76 */ 77 final Exception x; 78 79 /** 80 * Constructs ECFieldF2mDomainParams 81 * 82 * @param m 83 * @param rp 84 * @param ks 85 * @param expectedException 86 */ 87 ECFieldF2mDomainParams(final int m, 88 final BigInteger rp, 89 final int[] ks, 90 final Exception expectedException) { 91 this.m = m; 92 this.rp = rp; 93 this.ks = ks; 94 this.x = expectedException; 95 } 96 } 97 98 99 /** 100 * Set of parameters used for <code>ECFieldF2m(int, BigInteger)</code> 101 * constructor tests. 102 */ 103 private final ECFieldF2mDomainParams[] intBigIntegerCtorTestParameters = 104 new ECFieldF2mDomainParams[] { 105 // set 0: valid m and rp - trinomial basis params 106 new ECFieldF2mDomainParams( 107 1999, 108 BigInteger.valueOf(0L).setBit(0).setBit(367).setBit(1999), 109 null, 110 null), 111 // set 1: valid m and rp - pentanomial basis params 112 new ECFieldF2mDomainParams( 113 2000, 114 BigInteger.valueOf(0L).setBit(0).setBit(1).setBit(2).setBit(981).setBit(2000), 115 null, 116 null), 117 // set 2: valid m, invalid (null) rp 118 new ECFieldF2mDomainParams( 119 1963, 120 (BigInteger)null, 121 null, 122 ECFieldF2mDomainParams.NPE), 123 // set 3: valid m, invalid rp - bit 0 not set 124 new ECFieldF2mDomainParams( 125 1999, 126 BigInteger.valueOf(0L).setBit(1).setBit(367).setBit(1999), 127 null, 128 ECFieldF2mDomainParams.IArgE), 129 // set 4: valid m, invalid rp - bit m not set 130 new ECFieldF2mDomainParams( 131 1999, 132 BigInteger.valueOf(0L).setBit(0).setBit(367).setBit(1998), 133 null, 134 ECFieldF2mDomainParams.IArgE), 135 // set 5: valid m, invalid rp - bit k improperly set 136 new ECFieldF2mDomainParams( 137 1999, 138 BigInteger.valueOf(0L).setBit(0).setBit(2367).setBit(1999), 139 null, 140 ECFieldF2mDomainParams.IArgE), 141 // set 6: valid m, invalid rp - k1 k2 k3 142 new ECFieldF2mDomainParams( 143 2000, 144 BigInteger.valueOf(0L).setBit(0).setBit(2001).setBit(2002).setBit(2003).setBit(2000), 145 null, 146 ECFieldF2mDomainParams.IArgE), 147 // set 7: valid m, invalid rp - number of bits set 148 new ECFieldF2mDomainParams( 149 2000, 150 BigInteger.valueOf(0L).setBit(0).setBit(2000), 151 null, 152 ECFieldF2mDomainParams.IArgE), 153 // set 8: valid m, invalid rp - number of bits set 154 new ECFieldF2mDomainParams( 155 2000, 156 BigInteger.valueOf(0L).setBit(0).setBit(1).setBit(2).setBit(2000), 157 null, 158 ECFieldF2mDomainParams.IArgE), 159 // set 9: valid m, invalid rp - number of bits set 160 new ECFieldF2mDomainParams( 161 2000, 162 BigInteger.valueOf(0L).setBit(0).setBit(1).setBit(2). 163 setBit(981).setBit(985).setBit(2000), 164 null, 165 ECFieldF2mDomainParams.IArgE), 166 // set 10: valid m, invalid rp 167 new ECFieldF2mDomainParams( 168 2000, 169 BigInteger.valueOf(0L), 170 null, 171 ECFieldF2mDomainParams.IArgE), 172 // set 11: invalid m 173 new ECFieldF2mDomainParams( 174 -2000, 175 BigInteger.valueOf(0L).setBit(0).setBit(1).setBit(2). 176 setBit(981).setBit(2000), 177 null, 178 ECFieldF2mDomainParams.IArgE), 179 }; 180 181 /** 182 * Tests for constructor <code>ECFieldF2m(int, BigInteger)</code><br> 183 * 184 * Assertion: constructs new <code>ECFieldF2m</code> object 185 * using valid parameters m and rp. rp represents trinomial basis. 186 * 187 * Assertion: constructs new <code>ECFieldF2m</code> object 188 * using valid parameters m and rp. rp represents pentanomial basis. 189 * 190 * Assertion: IllegalArgumentException if m is not positive. 191 * 192 * Assertion: NullPointerException if rp is null. 193 * 194 * Assertion: IllegalArgumentException if rp is invalid. 195 */ 196 public final void testECFieldF2mintBigInteger() { 197 for(int i=0; i<intBigIntegerCtorTestParameters.length; i++) { 198 ECFieldF2mDomainParams tp = intBigIntegerCtorTestParameters[i]; 199 try { 200 // perform test 201 new ECFieldF2m(tp.m, tp.rp); 202 203 if (tp.x != null) { 204 // exception has been expected 205 fail(getName() + ", set " + i + 206 " FAILED: expected exception has not been thrown"); 207 } 208 } catch (Exception e){ 209 if (tp.x == null || !e.getClass().isInstance(tp.x)) { 210 // exception: failure 211 // if it has not been expected 212 // or wrong one has been thrown 213 fail(getName() + ", set " + i + 214 " FAILED: unexpected " + e); 215 } 216 } 217 } 218 } 219} 220