summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2018-01-20 01:12:33 +0100
committerJ08nY2018-01-20 01:14:02 +0100
commitc6345358ab582d96462209d50d37f731690ef311 (patch)
tree13b2990454b92b7b6882e82bc193041d94b523f8
parent87ee30fdf73db49cc538985a3937b8a792c2b30f (diff)
downloadECTester-c6345358ab582d96462209d50d37f731690ef311.tar.gz
ECTester-c6345358ab582d96462209d50d37f731690ef311.tar.zst
ECTester-c6345358ab582d96462209d50d37f731690ef311.zip
-rw-r--r--src/cz/crcs/ectester/common/test/BaseTestable.java7
-rw-r--r--src/cz/crcs/ectester/common/test/CompoundTest.java4
-rw-r--r--src/cz/crcs/ectester/common/test/Test.java6
-rw-r--r--src/cz/crcs/ectester/common/test/Testable.java6
-rw-r--r--src/cz/crcs/ectester/reader/ECTesterReader.java1
-rw-r--r--src/cz/crcs/ectester/reader/test/CardDefaultSuite.java18
-rw-r--r--src/cz/crcs/ectester/reader/test/PerformanceTest.java103
7 files changed, 140 insertions, 5 deletions
diff --git a/src/cz/crcs/ectester/common/test/BaseTestable.java b/src/cz/crcs/ectester/common/test/BaseTestable.java
index f376299..a4b9a00 100644
--- a/src/cz/crcs/ectester/common/test/BaseTestable.java
+++ b/src/cz/crcs/ectester/common/test/BaseTestable.java
@@ -22,4 +22,11 @@ public abstract class BaseTestable implements Testable {
public boolean error() {
return error;
}
+
+ @Override
+ public void reset() {
+ hasRun = false;
+ ok = false;
+ error = false;
+ }
}
diff --git a/src/cz/crcs/ectester/common/test/CompoundTest.java b/src/cz/crcs/ectester/common/test/CompoundTest.java
index 3b0b542..10ecf9c 100644
--- a/src/cz/crcs/ectester/common/test/CompoundTest.java
+++ b/src/cz/crcs/ectester/common/test/CompoundTest.java
@@ -1,5 +1,7 @@
package cz.crcs.ectester.common.test;
+import java.util.Arrays;
+import java.util.Objects;
import java.util.function.Function;
/**
@@ -14,7 +16,7 @@ public class CompoundTest extends Test {
private CompoundTest(Function<Test[], Result> callback, Test... tests) {
this.callback = callback;
- this.tests = tests;
+ this.tests = Arrays.stream(tests).filter(Objects::nonNull).toArray(Test[]::new);
}
private CompoundTest(Function<Test[], Result> callback, String descripiton, Test... tests) {
diff --git a/src/cz/crcs/ectester/common/test/Test.java b/src/cz/crcs/ectester/common/test/Test.java
index 4f5bb6e..3d0baf6 100644
--- a/src/cz/crcs/ectester/common/test/Test.java
+++ b/src/cz/crcs/ectester/common/test/Test.java
@@ -52,6 +52,12 @@ public abstract class Test implements Testable {
return hasRun;
}
+ @Override
+ public void reset() {
+ hasRun = false;
+ result = null;
+ }
+
public abstract String getDescription();
@Override
diff --git a/src/cz/crcs/ectester/common/test/Testable.java b/src/cz/crcs/ectester/common/test/Testable.java
index 5b84a45..33c9485 100644
--- a/src/cz/crcs/ectester/common/test/Testable.java
+++ b/src/cz/crcs/ectester/common/test/Testable.java
@@ -13,12 +13,18 @@ public interface Testable {
* @return Whether an error happened.
*/
boolean error();
+
/**
* @return Whether this runnable was run.
*/
boolean hasRun();
/**
+ *
+ */
+ void reset();
+
+ /**
* Run this Runnable.
*
* @throws TestException If an unexpected exception/error is encountered.
diff --git a/src/cz/crcs/ectester/reader/ECTesterReader.java b/src/cz/crcs/ectester/reader/ECTesterReader.java
index 4097d81..5e3a3fe 100644
--- a/src/cz/crcs/ectester/reader/ECTesterReader.java
+++ b/src/cz/crcs/ectester/reader/ECTesterReader.java
@@ -181,6 +181,7 @@ public class ECTesterReader {
} catch (CardException | TestException ex) {
if (logger != null)
logger.println(ex.getMessage());
+ ex.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} finally {
diff --git a/src/cz/crcs/ectester/reader/test/CardDefaultSuite.java b/src/cz/crcs/ectester/reader/test/CardDefaultSuite.java
index b05d4fe..0987160 100644
--- a/src/cz/crcs/ectester/reader/test/CardDefaultSuite.java
+++ b/src/cz/crcs/ectester/reader/test/CardDefaultSuite.java
@@ -57,9 +57,14 @@ public class CardDefaultSuite extends CardTestSuite {
for (byte kaType : EC_Consts.KA_TYPES) {
Test allocate = runTest(CommandTest.expect(new Command.AllocateKeyAgreement(this.card, kaType), ExpectedValue.SUCCESS));
if (allocate.ok()) {
- Test ka = runTest(CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.CORRUPTION_NONE, kaType), ExpectedValue.SUCCESS));
+ Command ecdh = new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.CORRUPTION_NONE, kaType);
+ Test ka = runTest(CommandTest.expect(ecdh, ExpectedValue.SUCCESS));
Test kaCompressed = runTest(CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.CORRUPTION_COMPRESS, kaType), ExpectedValue.SUCCESS));
- Test compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, "Test of the " + CardUtil.getKATypeString(kaType) + " KeyAgreement.", allocate, ka, kaCompressed));
+ Test perfTest = null;
+ if (ka.ok()) {
+ perfTest = runTest(PerformanceTest.repeat(ecdh, 100));
+ }
+ Test compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, "Test of the " + CardUtil.getKATypeString(kaType) + " KeyAgreement.", allocate, ka, kaCompressed, perfTest));
supportTests.add(compound);
} else {
runTest(allocate);
@@ -69,8 +74,13 @@ public class CardDefaultSuite extends CardTestSuite {
for (byte sigType : EC_Consts.SIG_TYPES) {
Test allocate = runTest(CommandTest.expect(new Command.AllocateSignature(this.card, sigType), ExpectedValue.SUCCESS));
if (allocate.ok()) {
- Test expect = runTest(CommandTest.expect(new Command.ECDSA(this.card, ECTesterApplet.KEYPAIR_LOCAL, sigType, ECTesterApplet.EXPORT_FALSE, null), ExpectedValue.SUCCESS));
- Test compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, "Test of the " + CardUtil.getSigTypeString(sigType) + " signature.", allocate, expect));
+ Command ecdsa = new Command.ECDSA(this.card, ECTesterApplet.KEYPAIR_LOCAL, sigType, ECTesterApplet.EXPORT_FALSE, null);
+ Test expect = runTest(CommandTest.expect(ecdsa, ExpectedValue.SUCCESS));
+ Test perfTest = null;
+ if (expect.ok()) {
+ perfTest = runTest(PerformanceTest.repeat(ecdsa, 100));
+ }
+ Test compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, "Test of the " + CardUtil.getSigTypeString(sigType) + " signature.", allocate, expect, perfTest));
supportTests.add(compound);
} else {
supportTests.add(allocate);
diff --git a/src/cz/crcs/ectester/reader/test/PerformanceTest.java b/src/cz/crcs/ectester/reader/test/PerformanceTest.java
new file mode 100644
index 0000000..4a27bad
--- /dev/null
+++ b/src/cz/crcs/ectester/reader/test/PerformanceTest.java
@@ -0,0 +1,103 @@
+package cz.crcs.ectester.reader.test;
+
+import cz.crcs.ectester.common.test.Result;
+import cz.crcs.ectester.common.test.SimpleTest;
+import cz.crcs.ectester.common.test.TestCallback;
+import cz.crcs.ectester.common.test.TestException;
+import cz.crcs.ectester.reader.command.Command;
+
+import java.util.Arrays;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class PerformanceTest extends SimpleTest<CommandTestable> {
+ private long[] times;
+ private long mean;
+ private long median;
+ private long mode;
+ private int count;
+
+ private PerformanceTest(CommandTestable testable, int count) {
+ super(testable, new TestCallback<CommandTestable>() {
+ @Override
+ public Result apply(CommandTestable testable) {
+ return new Result(Result.Value.SUCCESS);
+ }
+ });
+ this.count = count;
+ }
+
+ public static PerformanceTest repeat(Command cmd, int count) {
+ return new PerformanceTest(new CommandTestable(cmd), count);
+ }
+
+ @Override
+ public String getDescription() {
+ return String.format("Mean = %d ns, Median = %d ns, Mode = %d ns", mean, median, mode);
+ }
+
+ @Override
+ public void run() throws TestException {
+ if (hasRun)
+ return;
+
+ times = new long[count];
+ for (int i = 0; i < count; ++i) {
+ testable.run();
+ times[i] = testable.getResponse().getDuration();
+ testable.reset();
+ }
+
+ mean = Arrays.stream(times).sum() / count;
+
+ long[] sorted = times.clone();
+ Arrays.sort(sorted);
+ if (count % 2 == 0) {
+ median = (sorted[(count / 2) - 1] + sorted[count / 2]) / 2;
+ } else {
+ median = sorted[count / 2];
+ }
+
+ long max_occurences = 0;
+ int i = 0;
+ while (i < count) {
+ long current_value = sorted[i];
+ long current_occurences = 0;
+ while (i < count && sorted[i] == current_value) {
+ i++;
+ current_occurences++;
+ }
+ if (current_occurences > max_occurences) {
+ max_occurences = current_occurences;
+ mode = current_value;
+ }
+ }
+ hasRun = true;
+ result = callback.apply(testable);
+ }
+
+ public long getCount() {
+ return count;
+ }
+
+ public Command getCommand() {
+ return testable.getCommand();
+ }
+
+ public long[] getTimes() {
+ return times;
+ }
+
+ public long getMean() {
+ return mean;
+ }
+
+ public long getMedian() {
+ return median;
+ }
+
+ public long getMode() {
+ return mode;
+ }
+}