aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/cz/crcs/ectester/common')
-rw-r--r--src/cz/crcs/ectester/common/output/BaseTextTestWriter.java27
-rw-r--r--src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java57
-rw-r--r--src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java21
-rw-r--r--src/cz/crcs/ectester/common/output/TestWriter.java17
-rw-r--r--src/cz/crcs/ectester/common/test/CompoundTest.java102
-rw-r--r--src/cz/crcs/ectester/common/test/Result.java6
-rw-r--r--src/cz/crcs/ectester/common/test/SimpleTest.java6
-rw-r--r--src/cz/crcs/ectester/common/test/Test.java44
-rw-r--r--src/cz/crcs/ectester/common/test/TestException.java7
-rw-r--r--src/cz/crcs/ectester/common/test/TestSuite.java22
-rw-r--r--src/cz/crcs/ectester/common/test/TestSuiteException.java13
-rw-r--r--src/cz/crcs/ectester/common/test/Testable.java2
-rw-r--r--src/cz/crcs/ectester/common/util/CardUtil.java14
13 files changed, 262 insertions, 76 deletions
diff --git a/src/cz/crcs/ectester/common/output/BaseTextTestWriter.java b/src/cz/crcs/ectester/common/output/BaseTextTestWriter.java
index 29eb671..f29d28e 100644
--- a/src/cz/crcs/ectester/common/output/BaseTextTestWriter.java
+++ b/src/cz/crcs/ectester/common/output/BaseTextTestWriter.java
@@ -28,11 +28,10 @@ public abstract class BaseTextTestWriter implements TestWriter {
protected abstract String deviceString(TestSuite suite);
private String testString(Test t, String prefix) {
- if (!t.hasRun()) {
- return null;
- }
boolean compound = t instanceof CompoundTest;
+ Result result = t.getResult();
+
StringBuilder out = new StringBuilder();
out.append(t.ok() ? " OK " : "NOK ");
out.append(compound ? "┳ " : "━ ");
@@ -40,14 +39,14 @@ public abstract class BaseTextTestWriter implements TestWriter {
String widthSpec = "%-" + String.valueOf(width) + "s";
out.append(String.format(widthSpec, t.getDescription()));
out.append(" ┃ ");
- out.append(String.format("%-9s", t.getResultValue().name()));
+ out.append(String.format("%-9s", result.getValue().name()));
out.append(" ┃ ");
if (compound) {
CompoundTest test = (CompoundTest) t;
- out.append(test.getResultCause());
+ out.append(result.getCause().toString());
out.append(System.lineSeparator());
- Test[] tests = test.getTests();
+ Test[] tests = test.getStartedTests();
for (int i = 0; i < tests.length; ++i) {
if (i == tests.length - 1) {
out.append(prefix).append(" ┗ ");
@@ -76,6 +75,22 @@ public abstract class BaseTextTestWriter implements TestWriter {
output.flush();
}
+ private String errorString(Throwable error) {
+ StringBuilder sb = new StringBuilder();
+ for (Throwable t = error; t != null; t = t.getCause()) {
+ sb.append("═══ ").append(t.toString()).append(" ═══");
+ sb.append(System.lineSeparator());
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public void outputError(Test t, Throwable cause) {
+ output.println(testString(t, ""));
+ output.print(errorString(cause));
+ output.flush();
+ }
+
@Override
public void end() {
}
diff --git a/src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java b/src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java
index f3e9411..5d6d53d 100644
--- a/src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java
+++ b/src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java
@@ -24,6 +24,7 @@ public abstract class BaseXMLTestWriter implements TestWriter {
private DocumentBuilder db;
protected Document doc;
private Node root;
+ private Node tests;
public BaseXMLTestWriter(OutputStream output) throws ParserConfigurationException {
this.output = output;
@@ -40,19 +41,55 @@ public abstract class BaseXMLTestWriter implements TestWriter {
root = rootElem;
doc.appendChild(root);
root.appendChild(deviceElement(suite));
+ tests = doc.createElement("tests");
+ root.appendChild(tests);
}
protected abstract Element testableElement(Testable t);
protected abstract Element deviceElement(TestSuite suite);
+ private String causeString(Object cause) {
+ if (cause == null) {
+ return "null";
+ } else if (cause instanceof String) {
+ return (String) cause;
+ } else if (cause instanceof Throwable) {
+ StringBuilder sb = new StringBuilder();
+ for (Throwable t = (Throwable) cause; t != null; t = t.getCause()) {
+ sb.append(t.toString());
+ sb.append(System.lineSeparator());
+ }
+ return sb.toString();
+ } else {
+ return cause.toString();
+ }
+ }
+
+ private Element resultElement(Result result) {
+ Element resultElem = doc.createElement("result");
+
+ Element ok = doc.createElement("ok");
+ ok.setTextContent(String.valueOf(result.ok()));
+ Element value = doc.createElement("value");
+ value.setTextContent(result.getValue().name());
+ Element cause = doc.createElement("cause");
+ cause.setTextContent(causeString(cause));
+
+ resultElem.appendChild(ok);
+ resultElem.appendChild(value);
+ resultElem.appendChild(cause);
+
+ return resultElem;
+ }
+
private Element testElement(Test t) {
Element testElem;
if (t instanceof CompoundTest) {
CompoundTest test = (CompoundTest) t;
testElem = doc.createElement("test");
testElem.setAttribute("type", "compound");
- for (Test innerTest : test.getTests()) {
+ for (Test innerTest : test.getStartedTests()) {
testElem.appendChild(testElement(innerTest));
}
} else {
@@ -64,16 +101,7 @@ public abstract class BaseXMLTestWriter implements TestWriter {
description.setTextContent(t.getDescription());
testElem.appendChild(description);
- Element result = doc.createElement("result");
- Element ok = doc.createElement("ok");
- ok.setTextContent(String.valueOf(t.ok()));
- Element value = doc.createElement("value");
- value.setTextContent(t.getResultValue().name());
- Element cause = doc.createElement("cause");
- cause.setTextContent(t.getResultCause());
- result.appendChild(ok);
- result.appendChild(value);
- result.appendChild(cause);
+ Element result = resultElement(t.getResult());
testElem.appendChild(result);
return testElem;
@@ -83,7 +111,12 @@ public abstract class BaseXMLTestWriter implements TestWriter {
public void outputTest(Test t) {
if (!t.hasRun())
return;
- root.appendChild(testElement(t));
+ tests.appendChild(testElement(t));
+ }
+
+ @Override
+ public void outputError(Test t, Throwable cause) {
+ tests.appendChild(testElement(t));
}
@Override
diff --git a/src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java b/src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java
index 0769e83..1e13082 100644
--- a/src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java
+++ b/src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java
@@ -41,6 +41,14 @@ public abstract class BaseYAMLTestWriter implements TestWriter {
abstract protected Map<String, Object> deviceObject(TestSuite suite);
+ private Map<String, Object> resultObject(Result result) {
+ Map<String, Object> resultObject = new HashMap<>();
+ resultObject.put("ok", result.ok());
+ resultObject.put("value", result.getValue().name());
+ resultObject.put("cause", result.getCause());
+ return resultObject;
+ }
+
private Map<String, Object> testObject(Test t) {
Map<String, Object> testObj;
if (t instanceof CompoundTest) {
@@ -48,7 +56,7 @@ public abstract class BaseYAMLTestWriter implements TestWriter {
testObj = new HashMap<>();
testObj.put("type", "compound");
List<Map<String, Object>> innerTests = new LinkedList<>();
- for (Test innerTest : test.getTests()) {
+ for (Test innerTest : test.getStartedTests()) {
innerTests.add(testObject(innerTest));
}
testObj.put("tests", innerTests);
@@ -58,11 +66,7 @@ public abstract class BaseYAMLTestWriter implements TestWriter {
}
testObj.put("desc", t.getDescription());
- Map<String, Object> result = new HashMap<>();
- result.put("ok", t.ok());
- result.put("value", t.getResultValue().name());
- result.put("cause", t.getResultCause());
- testObj.put("result", result);
+ testObj.put("result", resultObject(t.getResult()));
return testObj;
}
@@ -75,6 +79,11 @@ public abstract class BaseYAMLTestWriter implements TestWriter {
}
@Override
+ public void outputError(Test t, Throwable cause) {
+ tests.add(testObject(t));
+ }
+
+ @Override
public void end() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
diff --git a/src/cz/crcs/ectester/common/output/TestWriter.java b/src/cz/crcs/ectester/common/output/TestWriter.java
index 0ecfd5a..5fa7f67 100644
--- a/src/cz/crcs/ectester/common/output/TestWriter.java
+++ b/src/cz/crcs/ectester/common/output/TestWriter.java
@@ -7,9 +7,26 @@ import cz.crcs.ectester.common.test.TestSuite;
* @author Jan Jancar johny@neuromancer.sk
*/
public interface TestWriter {
+ /**
+ * @param suite
+ */
void begin(TestSuite suite);
+ /**
+ *
+ * @param t
+ */
void outputTest(Test t);
+ /**
+ *
+ * @param t
+ * @param cause
+ */
+ void outputError(Test t, Throwable cause);
+
+ /**
+ *
+ */
void end();
}
diff --git a/src/cz/crcs/ectester/common/test/CompoundTest.java b/src/cz/crcs/ectester/common/test/CompoundTest.java
index 69122b0..4b1df16 100644
--- a/src/cz/crcs/ectester/common/test/CompoundTest.java
+++ b/src/cz/crcs/ectester/common/test/CompoundTest.java
@@ -2,6 +2,7 @@ package cz.crcs.ectester.common.test;
import java.util.Arrays;
import java.util.Objects;
+import java.util.function.Consumer;
import java.util.function.Function;
/**
@@ -10,26 +11,34 @@ import java.util.function.Function;
* @author Jan Jancar johny@neuromancer.sk
*/
public class CompoundTest extends Test {
- private Function<Test[], Result> callback;
+ private Function<Test[], Result> resultCallback;
+ private Consumer<Test[]> runCallback;
private Test[] tests;
private String description = "";
- private CompoundTest(Function<Test[], Result> callback, Test... tests) {
- this.callback = callback;
+ private final static Consumer<Test[]> RUN_ALL = tests -> {
+ for (Test t : tests) {
+ t.run();
+ }
+ };
+
+ private CompoundTest(Function<Test[], Result> resultCallback, Consumer<Test[]> runCallback, Test... tests) {
+ this.resultCallback = resultCallback;
+ this.runCallback = runCallback;
this.tests = Arrays.stream(tests).filter(Objects::nonNull).toArray(Test[]::new);
}
- private CompoundTest(Function<Test[], Result> callback, String descripiton, Test... tests) {
- this(callback, tests);
+ private CompoundTest(Function<Test[], Result> callback, Consumer<Test[]> runCallback, String descripiton, Test... tests) {
+ this(callback, runCallback, tests);
this.description = descripiton;
}
public static CompoundTest function(Function<Test[], Result> callback, Test... tests) {
- return new CompoundTest(callback, tests);
+ return new CompoundTest(callback, RUN_ALL, tests);
}
public static CompoundTest function(Function<Test[], Result> callback, String description, Test... tests) {
- return new CompoundTest(callback, description, tests);
+ return new CompoundTest(callback, RUN_ALL, description, tests);
}
public static CompoundTest all(Result.ExpectedValue what, Test... all) {
@@ -40,7 +49,7 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
- }, all);
+ }, RUN_ALL, all);
}
public static CompoundTest all(Result.ExpectedValue what, String description, Test... all) {
@@ -49,7 +58,31 @@ public class CompoundTest extends Test {
return result;
}
- public static CompoundTest any(Result.ExpectedValue what, Test... any) {
+ public static CompoundTest greedyAll(Result.ExpectedValue what, Test... all) {
+ return new CompoundTest((tests) -> {
+ for (Test test : tests) {
+ if (!Result.Value.fromExpected(what, test.ok()).ok()) {
+ return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
+ }
+ }
+ return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
+ }, (tests) -> {
+ for (Test t : tests) {
+ t.run();
+ if (!t.ok()) {
+ break;
+ }
+ }
+ }, all);
+ }
+
+ public static CompoundTest greedyAll(Result.ExpectedValue what, String description, Test... all) {
+ CompoundTest result = CompoundTest.greedyAll(what, all);
+ result.setDescription(description);
+ return result;
+ }
+
+ public static CompoundTest greedyAny(Result.ExpectedValue what, Test... any) {
return new CompoundTest((tests) -> {
for (Test test : tests) {
if (Result.Value.fromExpected(what, test.ok()).ok()) {
@@ -57,9 +90,33 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result.");
+ }, (tests) -> {
+ for (Test t : tests) {
+ t.run();
+ if (t.ok()) {
+ break;
+ }
+ }
}, any);
}
+ public static CompoundTest greedyAny(Result.ExpectedValue what, String description, Test... any) {
+ CompoundTest result = CompoundTest.greedyAny(what, any);
+ result.setDescription(description);
+ return result;
+ }
+
+ public static CompoundTest any(Result.ExpectedValue what, Test... any) {
+ return new CompoundTest((tests) -> {
+ for (Test test : tests) {
+ if (Result.Value.fromExpected(what, test.ok()).ok()) {
+ return new Result(Result.Value.SUCCESS, "Some sub-tests did have the expected result.");
+ }
+ }
+ return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result.");
+ }, RUN_ALL, any);
+ }
+
public static CompoundTest any(Result.ExpectedValue what, String description, Test... any) {
CompoundTest result = CompoundTest.any(what, any);
result.setDescription(description);
@@ -74,7 +131,7 @@ public class CompoundTest extends Test {
}
}
return new Result(Result.Value.SUCCESS, "All sub-tests matched the expected mask.");
- }, masked);
+ }, RUN_ALL, masked);
}
public static CompoundTest mask(Result.ExpectedValue[] results, String description, Test... masked) {
@@ -84,20 +141,25 @@ public class CompoundTest extends Test {
}
public Test[] getTests() {
- return tests;
+ return tests.clone();
}
- @Override
- public void run() throws TestException {
- if (hasRun)
- return;
+ public Test[] getRunTests() {
+ return Arrays.stream(tests).filter(Test::hasRun).toArray(Test[]::new);
+ }
- for (Test test : tests) {
- test.run();
- }
+ public Test[] getStartedTests() {
+ return Arrays.stream(tests).filter(Test::hasStarted).toArray(Test[]::new);
+ }
- result = callback.apply(tests);
- this.hasRun = true;
+ public Test[] getSkippedTests() {
+ return Arrays.stream(tests).filter((test) -> !test.hasRun()).toArray(Test[]::new);
+ }
+
+ @Override
+ protected void runSelf() {
+ runCallback.accept(tests);
+ result = resultCallback.apply(tests);
}
public void setDescription(String description) {
diff --git a/src/cz/crcs/ectester/common/test/Result.java b/src/cz/crcs/ectester/common/test/Result.java
index 11fcb4d..5d15a60 100644
--- a/src/cz/crcs/ectester/common/test/Result.java
+++ b/src/cz/crcs/ectester/common/test/Result.java
@@ -8,13 +8,13 @@ package cz.crcs.ectester.common.test;
public class Result {
private Value value;
- private String cause;
+ private Object cause;
public Result(Value value) {
this.value = value;
}
- public Result(Value value, String cause) {
+ public Result(Value value, Object cause) {
this(value);
this.cause = cause;
}
@@ -23,7 +23,7 @@ public class Result {
return value;
}
- public String getCause() {
+ public Object getCause() {
return cause;
}
diff --git a/src/cz/crcs/ectester/common/test/SimpleTest.java b/src/cz/crcs/ectester/common/test/SimpleTest.java
index f68320a..85f1072 100644
--- a/src/cz/crcs/ectester/common/test/SimpleTest.java
+++ b/src/cz/crcs/ectester/common/test/SimpleTest.java
@@ -16,4 +16,10 @@ public abstract class SimpleTest<T extends BaseTestable> extends Test {
public T getTestable() {
return testable;
}
+
+ @Override
+ protected void runSelf() {
+ testable.run();
+ result = callback.apply(testable);
+ }
}
diff --git a/src/cz/crcs/ectester/common/test/Test.java b/src/cz/crcs/ectester/common/test/Test.java
index 3d0baf6..868fd22 100644
--- a/src/cz/crcs/ectester/common/test/Test.java
+++ b/src/cz/crcs/ectester/common/test/Test.java
@@ -9,31 +9,15 @@ import static cz.crcs.ectester.common.test.Result.Value;
*/
public abstract class Test implements Testable {
protected boolean hasRun;
+ protected boolean hasStarted;
protected Result result;
public Result getResult() {
- if (!hasRun) {
- return null;
- }
return result;
}
- public Value getResultValue() {
- if (!hasRun) {
- return null;
- }
- return result.getValue();
- }
-
- public String getResultCause() {
- if (!hasRun) {
- return null;
- }
- return result.getCause();
- }
-
public boolean ok() {
- if (!hasRun) {
+ if (result == null) {
return true;
}
return result.ok();
@@ -41,7 +25,7 @@ public abstract class Test implements Testable {
@Override
public boolean error() {
- if (!hasRun) {
+ if (result == null) {
return false;
}
return result.compareTo(Value.ERROR);
@@ -52,15 +36,35 @@ public abstract class Test implements Testable {
return hasRun;
}
+ public boolean hasStarted() {
+ return hasStarted;
+ }
+
@Override
public void reset() {
hasRun = false;
+ hasStarted = false;
result = null;
}
public abstract String getDescription();
@Override
- public abstract void run() throws TestException;
+ public void run() {
+ if (hasRun)
+ return;
+ try {
+ hasStarted = true;
+ runSelf();
+ hasRun = true;
+ } catch (TestException e) {
+ result = new Result(Value.ERROR, e);
+ throw e;
+ } catch (Exception e) {
+ result = new Result(Value.ERROR, e);
+ throw new TestException(e);
+ }
+ }
+ protected abstract void runSelf();
}
diff --git a/src/cz/crcs/ectester/common/test/TestException.java b/src/cz/crcs/ectester/common/test/TestException.java
index 008e9f6..291a073 100644
--- a/src/cz/crcs/ectester/common/test/TestException.java
+++ b/src/cz/crcs/ectester/common/test/TestException.java
@@ -2,11 +2,12 @@ package cz.crcs.ectester.common.test;
/**
* A TestException is an Exception that can be thrown during the running of a Testable,
- * or a TestSuite. It means that the Testable/TestSuite encountered an unexpected error
- * during it's run which points to an error in ECTester or it's runtime environment.cd
+ * or a Test. It means that the Testable/TestSuite encountered an unexpected error
+ * and has to terminate.
+ *
* @author Jan Jancar johny@neuromancer.sk
*/
-public class TestException extends Exception {
+public class TestException extends RuntimeException {
public TestException(Throwable e) {
super(e);
}
diff --git a/src/cz/crcs/ectester/common/test/TestSuite.java b/src/cz/crcs/ectester/common/test/TestSuite.java
index 2de0b9c..ca1b199 100644
--- a/src/cz/crcs/ectester/common/test/TestSuite.java
+++ b/src/cz/crcs/ectester/common/test/TestSuite.java
@@ -9,6 +9,7 @@ public abstract class TestSuite {
protected String name;
protected String description;
protected TestWriter writer;
+ private Test running;
public TestSuite(TestWriter writer, String name, String description) {
this.writer = writer;
@@ -16,12 +17,18 @@ public abstract class TestSuite {
this.description = description;
}
- public void run() throws TestException {
+ /**
+ *
+ */
+ public void run() {
writer.begin(this);
try {
runTests();
+ } catch (TestException e) {
+ writer.outputError(running, e);
} catch (Exception e) {
- throw new TestException(e);
+ writer.end();
+ throw new TestSuiteException(e);
}
writer.end();
}
@@ -33,8 +40,10 @@ public abstract class TestSuite {
* @return The test that was run.
* @throws TestException
*/
- protected Test runTest(Test t) throws TestException {
+ protected Test runTest(Test t) {
+ running = t;
t.run();
+ running = null;
return t;
}
@@ -45,12 +54,15 @@ public abstract class TestSuite {
* @return The test that was run.
* @throws TestException
*/
- protected Test doTest(Test t) throws TestException {
- t.run();
+ protected Test doTest(Test t) {
+ runTest(t);
writer.outputTest(t);
return t;
}
+ /**
+ *
+ */
protected abstract void runTests() throws Exception;
public String getName() {
diff --git a/src/cz/crcs/ectester/common/test/TestSuiteException.java b/src/cz/crcs/ectester/common/test/TestSuiteException.java
new file mode 100644
index 0000000..cc3cfda
--- /dev/null
+++ b/src/cz/crcs/ectester/common/test/TestSuiteException.java
@@ -0,0 +1,13 @@
+package cz.crcs.ectester.common.test;
+
+/**
+ * An unexpected exception was thrown while running a TestSuite, outside Test
+ * or a Testable.
+ *
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class TestSuiteException extends RuntimeException {
+ public TestSuiteException(Throwable e) {
+ super(e);
+ }
+}
diff --git a/src/cz/crcs/ectester/common/test/Testable.java b/src/cz/crcs/ectester/common/test/Testable.java
index 33c9485..ea1380a 100644
--- a/src/cz/crcs/ectester/common/test/Testable.java
+++ b/src/cz/crcs/ectester/common/test/Testable.java
@@ -29,5 +29,5 @@ public interface Testable {
*
* @throws TestException If an unexpected exception/error is encountered.
*/
- void run() throws TestException;
+ void run();
}
diff --git a/src/cz/crcs/ectester/common/util/CardUtil.java b/src/cz/crcs/ectester/common/util/CardUtil.java
index dbe53be..7d7bb78 100644
--- a/src/cz/crcs/ectester/common/util/CardUtil.java
+++ b/src/cz/crcs/ectester/common/util/CardUtil.java
@@ -281,4 +281,18 @@ public class CardUtil {
return "";
}
}
+
+ public static String getParameterString(short params) {
+ String what = "";
+ if (params == EC_Consts.PARAMETERS_DOMAIN_F2M || params == EC_Consts.PARAMETERS_DOMAIN_FP) {
+ what = "curve";
+ } else if (params == EC_Consts.PARAMETER_W) {
+ what = "pubkey";
+ } else if (params == EC_Consts.PARAMETER_S) {
+ what = "privkey";
+ } else if (params == EC_Consts.PARAMETERS_KEYPAIR) {
+ what = "keypair";
+ }
+ return what;
+ }
}