/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Vladimir N. Molotkov
*/
package org.apache.harmony.security.tests.java.security.spec;
import java.math.BigInteger;
import java.security.spec.ECFieldF2m;
import java.util.Arrays;
import junit.framework.TestCase;
/**
* Tests for ECFieldF2m
class fields and methods.
*/
public class ECFieldF2mTest extends TestCase {
/**
* Support class for this test.
* Encapsulates ECFieldF2m
testing
* domain parameters.
*/
private static final class ECFieldF2mDomainParams {
/**
* NPE
reference object of class NullPointerException.
* NullPointerException must be thrown by ECFieldF2m
* ctors in some circumstances
*/
static final NullPointerException NPE = new NullPointerException();
/**
* IArgE
reference object of class IllegalArgumentException.
* IllegalArgumentException must be thrown by ECFieldF2m
* ctors in some circumstances
*/
static final IllegalArgumentException IArgE = new IllegalArgumentException();
/**
* The m
parameter for ECFieldF2m
* ctor for the current test.
*/
final int m;
/**
* The rp
parameter for ECFieldF2m
* ctor for the current test.
*/
final BigInteger rp;
/**
* The ks
parameter for ECFieldF2m
* ctor for the current test.
*/
final int[] ks;
/**
* Exception expected with this parameters set or null
* if no exception expected.
*/
final Exception x;
/**
* Constructs ECFieldF2mDomainParams
*
* @param m
* @param rp
* @param ks
* @param expectedException
*/
ECFieldF2mDomainParams(final int m,
final BigInteger rp,
final int[] ks,
final Exception expectedException) {
this.m = m;
this.rp = rp;
this.ks = ks;
this.x = expectedException;
}
}
/**
* Constructor for ECFieldF2mTest.
*
* @param name
*/
public ECFieldF2mTest(String name) {
super(name);
}
//
// Tests
//
/**
* Set of parameters used for ECFieldF2m(int)
* constructor tests.
*/
private final ECFieldF2mDomainParams[] intCtorTestParameters =
new ECFieldF2mDomainParams[] {
// set 0: valid m
new ECFieldF2mDomainParams(1, null, null, null),
// set 1: valid m
new ECFieldF2mDomainParams(Integer.MAX_VALUE, null, null, null),
// set 2: invalid m
new ECFieldF2mDomainParams(0, null, null, ECFieldF2mDomainParams.IArgE),
// set 3: invalid m
new ECFieldF2mDomainParams(-1, null, null, ECFieldF2mDomainParams.IArgE)
};
/**
* Tests for constructor ECFieldF2m(int)
*
ECFieldF2m
object
* using valid parameter m.
*
* Assertion: IllegalArgumentException if m is not positive.
*/
public final void testECFieldF2mint() {
for (int i = 0; i < intCtorTestParameters.length; i++) {
ECFieldF2mDomainParams tp = intCtorTestParameters[i];
try {
// perform test
new ECFieldF2m(tp.m);
if (tp.x != null) {
// exception has been expected
fail(getName() + ", set " + i +
" FAILED: expected exception has not been thrown");
}
} catch (Exception e) {
if (tp.x == null || !e.getClass().isInstance(tp.x)) {
// exception: failure
// if it has not been expected
// or wrong one has been thrown
fail(getName() + ", set " + i +
" FAILED: unexpected " + e);
}
}
}
}
/**
* Set of parameters used for ECFieldF2m(int, BigInteger)
* constructor tests.
*/
private final ECFieldF2mDomainParams[] intIntArrayCtorTestParameters =
new ECFieldF2mDomainParams[] {
// set 0: valid m and ks - trinomial basis params
new ECFieldF2mDomainParams(
1999,
null,
new int[] { 367 },
null),
// set 1: valid m and ks - pentanomial basis params
new ECFieldF2mDomainParams(
2000,
null,
new int[] { 981, 2, 1 },
null),
// set 2: valid m, invalid (null) ks
new ECFieldF2mDomainParams(
1963,
null,
null,
ECFieldF2mDomainParams.NPE),
// set 3: valid m, invalid ks - wrong length
new ECFieldF2mDomainParams(
1963,
null,
new int[] { 981, 2 },
ECFieldF2mDomainParams.IArgE),
// set 4: valid m, invalid ks - wrong length
new ECFieldF2mDomainParams(
1963,
null,
new int[] { 981, 124, 2, 1 },
ECFieldF2mDomainParams.IArgE),
// set 5: valid m, invalid ks - wrong value
new ECFieldF2mDomainParams(
1999,
null,
new int[] { 1999 },
ECFieldF2mDomainParams.IArgE),
// set 6: valid m, invalid ks - wrong value
new ECFieldF2mDomainParams(
1999,
null,
new int[] { 0 },
ECFieldF2mDomainParams.IArgE),
// set 7: valid m, invalid ks - wrong values
new ECFieldF2mDomainParams(
2000,
null,
new int[] { 2000, 2, 1 },
ECFieldF2mDomainParams.IArgE),
// set 8: valid m, invalid ks - wrong values
new ECFieldF2mDomainParams(
2000,
null,
new int[] { 981, 2, 0 },
ECFieldF2mDomainParams.IArgE),
// set 9: invalid m
new ECFieldF2mDomainParams(
-5,
null,
new int[] { 981, 2, 1 },
ECFieldF2mDomainParams.IArgE),
// set 10: valid m, invalid ks - wrong order
new ECFieldF2mDomainParams(
2000,
null,
new int[] { 981, 1, 2 },
ECFieldF2mDomainParams.IArgE),
// set 11: valid m, invalid ks - no content
new ECFieldF2mDomainParams(
2000,
null,
new int[3],
ECFieldF2mDomainParams.IArgE),
// set 12: valid m, invalid ks - length is 0
new ECFieldF2mDomainParams(
2000,
null,
new int[0],
ECFieldF2mDomainParams.IArgE),
};
/**
* Tests for constructor ECFieldF2m(int m, int[] ks)
ECFieldF2m
object
* using valid parameters m and rp. ks represents trinomial basis.
*
* Assertion: constructs new ECFieldF2m
object
* using valid parameters m and ks. ks represents pentanomial basis.
*
* Assertion: IllegalArgumentException if m is not positive.
*
* Assertion: NullPointerException if ks is null.
*
* Assertion: IllegalArgumentException if ks is invalid.
*/
public final void testECFieldF2mintintArray() {
for (int i = 0; i < intIntArrayCtorTestParameters.length; i++) {
ECFieldF2mDomainParams tp = intIntArrayCtorTestParameters[i];
try {
// perform test
new ECFieldF2m(tp.m, tp.ks);
if (tp.x != null) {
// exception has been expected
fail(getName() + ", set " + i +
" FAILED: expected exception has not been thrown");
}
} catch (Exception e) {
if (tp.x == null || !e.getClass().isInstance(tp.x)) {
// exception: failure
// if it has not been expected
// or wrong one has been thrown
fail(getName() + ", set " + i +
" FAILED: unexpected " + e);
}
}
}
}
/**
* Test #1 for hashCode()
method.hashCode()
method.hashCode()
method.equals(Object)
method) objects.
*/
public final void testHashCode03() {
assertTrue(new ECFieldF2m(111).hashCode() ==
new ECFieldF2m(111).hashCode());
}
/**
* Test #4 for hashCode()
method.equals(Object)
method) objects.
*/
public final void testHashCode04() {
assertTrue(new ECFieldF2m(2000, new int[] { 981, 2, 1 }).hashCode() ==
new ECFieldF2m(2000, new int[] { 981, 2, 1 }).hashCode());
}
/**
* Test #5 for hashCode()
method.equals(Object)
method) objects.
*/
public final void testHashCode05() {
assertTrue(new ECFieldF2m(2000, new int[] { 981, 2, 1 }).hashCode() ==
new ECFieldF2m(2000, BigInteger.valueOf(0L).
setBit(0).setBit(1).setBit(2).
setBit(981).setBit(2000)).hashCode());
}
/**
* Test #1 for equals()
method.equals()
method.equals()
method.equals()
method.equals()
method.equals(Object obj)
method.null
*/
public final void testEqualsObject06() {
assertFalse(new ECFieldF2m(2000).equals(null));
}
/**
* Test #7 for equals(Object obj)
method.ECFieldF2m
*/
public final void testEqualsObject07() {
assertFalse(new ECFieldF2m(2000).equals(new Object()));
}
/**
* Test for getFieldSize()
method.ECFieldF2m
*/
public final void testGetFieldSize() {
assertEquals(2000, new ECFieldF2m(2000).getFieldSize());
}
/**
* Test for getM()
method.ECFieldF2m
*/
public final void testGetM() {
assertEquals(2000, new ECFieldF2m(2000).getM());
}
/**
* Test #1 for getMidTermsOfReductionPolynomial()
method.getMidTermsOfReductionPolynomial()
method.getMidTermsOfReductionPolynomial()
method.getReductionPolynomial()
method.getReductionPolynomial()
method.getMidTermsOfReductionPolynomial()
method.
*/
public final void testIsStatePreserved02() {
// reference array
int[] a = new int[] { 981, 2, 1 };
// reference array copy
int[] aCopy = a.clone();
// create obj using copy
ECFieldF2m f = new ECFieldF2m(2000, aCopy);
// get array reference and modify returned array
f.getMidTermsOfReductionPolynomial()[0] = 1532;
// compare reference with returned for the second time array
assertTrue(Arrays.equals(a, f.getMidTermsOfReductionPolynomial()));
}
}