diff options
| author | J08nY | 2017-12-27 01:10:06 +0100 |
|---|---|---|
| committer | J08nY | 2017-12-27 01:10:06 +0100 |
| commit | 71006eb01c60b3556b620b7d4579d65ed6f86926 (patch) | |
| tree | 0667e793fa09b780702208d8158b37c06640a699 | |
| parent | 4dbc748a207bcee2c8fbe22566646b27a9f61dc0 (diff) | |
| download | ECTester-71006eb01c60b3556b620b7d4579d65ed6f86926.tar.gz ECTester-71006eb01c60b3556b620b7d4579d65ed6f86926.tar.zst ECTester-71006eb01c60b3556b620b7d4579d65ed6f86926.zip | |
16 files changed, 533 insertions, 443 deletions
diff --git a/nbproject/reader/project.properties b/nbproject/reader/project.properties index 5eb8ae3..69db523 100644 --- a/nbproject/reader/project.properties +++ b/nbproject/reader/project.properties @@ -30,7 +30,7 @@ dist.javadoc.dir=${dist.dir}/javadoc libs.CopyLibs.classpath=nbproject/copylibstask.jar endorsed.classpath= excludes= -includes=**/applet/**,**/common/**,**/data/**,**/reader/**,**/standalone/** +includes=**/applet/**,**/common/**,**/data/**,**/reader/** jar.compress=false javac.classpath=\ lib/bcprov-jdk15on-1.58.jar:\ diff --git a/nbproject/standalone/project.properties b/nbproject/standalone/project.properties index 2d3a9eb..9fed4c2 100644 --- a/nbproject/standalone/project.properties +++ b/nbproject/standalone/project.properties @@ -30,7 +30,7 @@ dist.javadoc.dir=${dist.dir}/javadoc libs.CopyLibs.classpath=nbproject/copylibstask.jar endorsed.classpath= excludes= -includes=**/common/**,**/standalone/**,**/data/**,**/applet/*,**/reader/** +includes=**/common/**,**/standalone/**,**/data/**,**/applet/* jar.compress=false javac.classpath=\ lib/bcprov-jdk15on-1.58.jar:\ diff --git a/src/cz/crcs/ectester/common/output/TextTestWriter.java b/src/cz/crcs/ectester/common/output/BaseTextTestWriter.java index 2691ccb..9635d0c 100644 --- a/src/cz/crcs/ectester/common/output/TextTestWriter.java +++ b/src/cz/crcs/ectester/common/output/BaseTextTestWriter.java @@ -1,24 +1,19 @@ package cz.crcs.ectester.common.output; -import cz.crcs.ectester.common.test.CompoundTest; -import cz.crcs.ectester.common.test.SimpleTest; -import cz.crcs.ectester.common.test.Test; -import cz.crcs.ectester.common.test.TestSuite; +import cz.crcs.ectester.common.test.*; import java.io.PrintStream; /** * @author Jan Jancar johny@neuromancer.sk */ -public class TextTestWriter implements TestWriter { +public abstract class BaseTextTestWriter implements TestWriter { private PrintStream output; - private TestableWriter testableWriter; public static int BASE_WIDTH = 76; - public TextTestWriter(PrintStream output) { + public BaseTextTestWriter(PrintStream output) { this.output = output; - this.testableWriter = new TestableWriter(output); } @Override @@ -27,6 +22,8 @@ public class TextTestWriter implements TestWriter { output.println("=== " + suite.getDescription()); } + protected abstract String testableString(Testable t); + private String testString(Test t, int offset) { if (!t.hasRun()) { return null; @@ -60,7 +57,7 @@ public class TextTestWriter implements TestWriter { } } else { SimpleTest test = (SimpleTest) t; - out.append(testableWriter.outputTestableSuffix(test.getTestable())); + out.append(testableString(test.getTestable())); } return out.toString(); } diff --git a/src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java b/src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java new file mode 100644 index 0000000..9d3c8f3 --- /dev/null +++ b/src/cz/crcs/ectester/common/output/BaseXMLTestWriter.java @@ -0,0 +1,100 @@ +package cz.crcs.ectester.common.output; + +import cz.crcs.ectester.common.test.*; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.OutputStream; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public abstract class BaseXMLTestWriter implements TestWriter { + private OutputStream output; + private DocumentBuilder db; + protected Document doc; + private Node root; + + public BaseXMLTestWriter(OutputStream output) throws ParserConfigurationException { + this.output = output; + this.db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + } + + @Override + public void begin(TestSuite suite) { + doc = db.newDocument(); + Element rootElem = doc.createElement("testSuite"); + rootElem.setAttribute("name", suite.getName()); + rootElem.setAttribute("desc", suite.getDescription()); + + root = rootElem; + doc.appendChild(root); + } + + protected abstract Element testableElement(Testable t); + + 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()) { + testElem.appendChild(testElement(innerTest)); + } + } else { + SimpleTest test = (SimpleTest) t; + testElem = testableElement(test.getTestable()); + } + + Element description = doc.createElement("desc"); + 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); + testElem.appendChild(result); + + return testElem; + } + + @Override + public void outputTest(Test t) { + if (!t.hasRun()) + return; + root.appendChild(testElement(t)); + } + + @Override + public void end() { + try { + DOMSource domSource = new DOMSource(doc); + StreamResult result = new StreamResult(output); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); + transformer.transform(domSource, result); + } catch (TransformerException e) { + e.printStackTrace(); + } + } +} diff --git a/src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java b/src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java new file mode 100644 index 0000000..af76927 --- /dev/null +++ b/src/cz/crcs/ectester/common/output/BaseYAMLTestWriter.java @@ -0,0 +1,88 @@ +package cz.crcs.ectester.common.output; + +import cz.crcs.ectester.common.test.*; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +import java.io.PrintStream; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public abstract class BaseYAMLTestWriter implements TestWriter { + private PrintStream output; + private Map<String, Object> testRun; + private Map<String, String> testSuite; + protected List<Object> tests; + + public BaseYAMLTestWriter(PrintStream output) { + this.output = output; + } + + @Override + public void begin(TestSuite suite) { + output.println("---"); + testRun = new HashMap<>(); + testSuite = new HashMap<>(); + tests = new LinkedList<>(); + testSuite.put("name", suite.getName()); + testSuite.put("desc", suite.getDescription()); + + testRun.put("suite", testSuite); + testRun.put("tests", tests); + } + + abstract protected Map<String, Object> testableObject(Testable t); + + private Map<String, Object> testObject(Test t) { + Map<String, Object> testObj; + if (t instanceof CompoundTest) { + CompoundTest test = (CompoundTest) t; + testObj = new HashMap<>(); + testObj.put("type", "compound"); + List<Map<String, Object>> innerTests = new LinkedList<>(); + for (Test innerTest : test.getTests()) { + innerTests.add(testObject(innerTest)); + } + testObj.put("tests", innerTests); + } else { + SimpleTest test = (SimpleTest) t; + testObj = testableObject(test.getTestable()); + } + + 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); + + return testObj; + } + + @Override + public void outputTest(Test t) { + if (!t.hasRun()) + return; + tests.add(testObject(t)); + } + + @Override + public void end() { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setPrettyFlow(true); + Yaml yaml = new Yaml(options); + + Map<String, Object> result = new HashMap<>(); + result.put("testRun", testRun); + String out = yaml.dump(result); + + output.println(out); + output.println("---"); + } +} diff --git a/src/cz/crcs/ectester/common/output/TestableWriter.java b/src/cz/crcs/ectester/common/output/TestableWriter.java deleted file mode 100644 index 9876064..0000000 --- a/src/cz/crcs/ectester/common/output/TestableWriter.java +++ /dev/null @@ -1,65 +0,0 @@ -package cz.crcs.ectester.common.output; - -import cz.crcs.ectester.common.test.BaseTestable; -import cz.crcs.ectester.reader.output.ResponseWriter; -import cz.crcs.ectester.reader.response.Response; -import cz.crcs.ectester.reader.test.CommandTestable; -import cz.crcs.ectester.standalone.test.KeyAgreementTestable; -import cz.crcs.ectester.standalone.test.KeyGeneratorTestable; -import cz.crcs.ectester.standalone.test.SignatureTestable; - -import java.io.OutputStream; -import java.io.PrintStream; - -/** - * @author Jan Jancar johny@neuromancer.sk - */ -public class TestableWriter { - private PrintStream output; - private ResponseWriter respWriter; - - public TestableWriter(PrintStream output) { - this.output = output; - this.respWriter = new ResponseWriter(output); - } - - public TestableWriter(OutputStream output) { - this(new PrintStream(output)); - } - - public String outputTestableSuffix(BaseTestable t) { - if (t instanceof CommandTestable) { - Response r = ((CommandTestable) t).getResponse(); - return respWriter.responseSuffix(r); - } else if (t instanceof KeyAgreementTestable) { - - } else if (t instanceof KeyGeneratorTestable) { - - } else if (t instanceof SignatureTestable) { - - } - return null; - } - - public void writeTestableSuffix(BaseTestable t) { - output.println(outputTestableSuffix(t)); - } - - public String outputTestable(BaseTestable t) { - if (t instanceof CommandTestable) { - CommandTestable testable = (CommandTestable) t; - return respWriter.responseString(testable.getResponse()); - } else if (t instanceof KeyAgreementTestable) { - - } else if (t instanceof KeyGeneratorTestable) { - - } else if (t instanceof SignatureTestable) { - - } - return null; - } - - public void writeTestable(BaseTestable t) { - output.println(outputTestable(t)); - } -} diff --git a/src/cz/crcs/ectester/common/output/XMLTestWriter.java b/src/cz/crcs/ectester/common/output/XMLTestWriter.java deleted file mode 100644 index 4139330..0000000 --- a/src/cz/crcs/ectester/common/output/XMLTestWriter.java +++ /dev/null @@ -1,204 +0,0 @@ -package cz.crcs.ectester.common.output; - -import cz.crcs.ectester.common.test.CompoundTest; -import cz.crcs.ectester.common.test.Test; -import cz.crcs.ectester.common.test.TestSuite; -import cz.crcs.ectester.common.util.ByteUtil; -import cz.crcs.ectester.reader.command.Command; -import cz.crcs.ectester.reader.response.Response; -import cz.crcs.ectester.reader.test.CommandTest; -import cz.crcs.ectester.standalone.test.*; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import java.io.OutputStream; -import java.security.PrivateKey; -import java.security.PublicKey; - -/** - * @author Jan Jancar johny@neuromancer.sk - */ -public class XMLTestWriter implements TestWriter { - private OutputStream output; - private DocumentBuilder db; - private Document doc; - private Node root; - - public XMLTestWriter(OutputStream output) throws ParserConfigurationException { - this.output = output; - this.db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - } - - @Override - public void begin(TestSuite suite) { - doc = db.newDocument(); - Element rootElem = doc.createElement("testSuite"); - rootElem.setAttribute("name", suite.getName()); - rootElem.setAttribute("desc", suite.getDescription()); - - root = rootElem; - doc.appendChild(root); - } - - private Element commandElement(Command c) { - Element commandElem = doc.createElement("command"); - - Element apdu = doc.createElement("apdu"); - apdu.setTextContent(ByteUtil.bytesToHex(c.getAPDU().getBytes())); - commandElem.appendChild(apdu); - - return commandElem; - } - - private Element responseElement(Response r) { - Element responseElem = doc.createElement("response"); - responseElem.setAttribute("successful", r.successful() ? "true" : "false"); - - Element apdu = doc.createElement("apdu"); - apdu.setTextContent(ByteUtil.bytesToHex(r.getAPDU().getBytes())); - responseElem.appendChild(apdu); - - Element naturalSW = doc.createElement("natural-sw"); - naturalSW.setTextContent(String.valueOf(Short.toUnsignedInt(r.getNaturalSW()))); - responseElem.appendChild(naturalSW); - - Element sws = doc.createElement("sws"); - for (int i = 0; i < r.getNumSW(); ++i) { - Element sw = doc.createElement("sw"); - sw.setTextContent(String.valueOf(Short.toUnsignedInt(r.getSW(i)))); - sws.appendChild(sw); - } - responseElem.appendChild(sws); - - Element duration = doc.createElement("duration"); - duration.setTextContent(String.valueOf(r.getDuration())); - responseElem.appendChild(duration); - - Element description = doc.createElement("desc"); - description.setTextContent(r.getDescription()); - responseElem.appendChild(description); - - return responseElem; - } - - private Element kaElement(KeyAgreementTestable kat) { - Element katElem = doc.createElement("key-agreement"); - - Element secret = doc.createElement("secret"); - secret.setTextContent(ByteUtil.bytesToHex(kat.getSecret())); - katElem.appendChild(secret); - - return katElem; - } - - private Element kgtElement(KeyGeneratorTestable kgt) { - Element kgtElem = doc.createElement("key-pair-generator"); - - Element keyPair = doc.createElement("key-pair"); - Element pubkey = doc.createElement("pubkey"); - PublicKey pkey = kgt.getKeyPair().getPublic(); - pubkey.setAttribute("algorithm", pkey.getAlgorithm()); - pubkey.setAttribute("format", pkey.getFormat()); - pubkey.setTextContent(ByteUtil.bytesToHex(pkey.getEncoded())); - keyPair.appendChild(pubkey); - - Element privkey = doc.createElement("privkey"); - PrivateKey skey = kgt.getKeyPair().getPrivate(); - privkey.setAttribute("algorithm", skey.getAlgorithm()); - privkey.setAttribute("format", skey.getFormat()); - privkey.setTextContent(ByteUtil.bytesToHex(skey.getEncoded())); - keyPair.appendChild(privkey); - - return kgtElem; - } - - private Element sigElement(SignatureTestable sig) { - Element sigElem = doc.createElement("signature"); - sigElem.setAttribute("verified", sig.getVerified() ? "true" : "false"); - - Element raw = doc.createElement("raw"); - raw.setTextContent(ByteUtil.bytesToHex(sig.getSignature())); - sigElem.appendChild(raw); - - return sigElem; - } - - private Element testElement(Test t) { - Element testElem = doc.createElement("test"); - - if (t instanceof CommandTest) { - CommandTest test = (CommandTest) t; - testElem.setAttribute("type", "command"); - testElem.appendChild(commandElement(test.getCommand())); - testElem.appendChild(responseElement(test.getResponse())); - } else if (t instanceof KeyAgreementTest) { - KeyAgreementTest test = (KeyAgreementTest) t; - testElem.setAttribute("type", "key-agreement"); - testElem.appendChild(kaElement(test.getTestable())); - } else if (t instanceof KeyGeneratorTest) { - KeyGeneratorTest test = (KeyGeneratorTest) t; - testElem.setAttribute("type", "key-pair-generator"); - testElem.appendChild(kgtElement(test.getTestable())); - } else if (t instanceof SignatureTest) { - SignatureTest test = (SignatureTest) t; - testElem.setAttribute("type", "signature"); - testElem.appendChild(sigElement(test.getTestable())); - } else if (t instanceof CompoundTest) { - CompoundTest test = (CompoundTest) t; - testElem.setAttribute("type", "compound"); - for (Test innerTest : test.getTests()) { - testElem.appendChild(testElement(innerTest)); - } - } - - Element description = doc.createElement("desc"); - 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); - testElem.appendChild(result); - - return testElem; - } - - @Override - public void outputTest(Test t) { - if (!t.hasRun()) - return; - root.appendChild(testElement(t)); - } - - @Override - public void end() { - try { - DOMSource domSource = new DOMSource(doc); - StreamResult result = new StreamResult(output); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); - transformer.transform(domSource, result); - } catch (TransformerException e) { - e.printStackTrace(); - } - } -}
\ No newline at end of file diff --git a/src/cz/crcs/ectester/common/output/YAMLTestWriter.java b/src/cz/crcs/ectester/common/output/YAMLTestWriter.java deleted file mode 100644 index ba9fa43..0000000 --- a/src/cz/crcs/ectester/common/output/YAMLTestWriter.java +++ /dev/null @@ -1,161 +0,0 @@ -package cz.crcs.ectester.common.output; - -import cz.crcs.ectester.common.test.CompoundTest; -import cz.crcs.ectester.common.test.Test; -import cz.crcs.ectester.common.test.TestSuite; -import cz.crcs.ectester.common.util.ByteUtil; -import cz.crcs.ectester.reader.command.Command; -import cz.crcs.ectester.reader.response.Response; -import cz.crcs.ectester.reader.test.CommandTest; -import cz.crcs.ectester.standalone.test.*; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; - -import java.io.PrintStream; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -/** - * @author Jan Jancar johny@neuromancer.sk - */ -public class YAMLTestWriter implements TestWriter { - private PrintStream output; - private Map<String, Object> testRun; - private Map<String, String> testSuite; - private List<Object> tests; - - public YAMLTestWriter(PrintStream output) { - this.output = output; - } - - @Override - public void begin(TestSuite suite) { - output.println("---"); - testRun = new HashMap<>(); - testSuite = new HashMap<>(); - tests = new LinkedList<>(); - testSuite.put("name", suite.getName()); - testSuite.put("desc", suite.getDescription()); - - testRun.put("suite", testSuite); - testRun.put("tests", tests); - } - - private Map<String, Object> commandObject(Command c) { - Map<String, Object> commandObj = new HashMap<>(); - commandObj.put("apdu", ByteUtil.bytesToHex(c.getAPDU().getBytes())); - return commandObj; - } - - private Map<String, Object> responseObject(Response r) { - Map<String, Object> responseObj = new HashMap<>(); - responseObj.put("successful", r.successful()); - responseObj.put("apdu", ByteUtil.bytesToHex(r.getAPDU().getBytes())); - responseObj.put("natural_sw", Short.toUnsignedInt(r.getNaturalSW())); - List<Integer> sws = new LinkedList<>(); - for (int i = 0; i < r.getNumSW(); ++i) { - sws.add(Short.toUnsignedInt(r.getSW(i))); - } - responseObj.put("sws", sws); - responseObj.put("duration", r.getDuration()); - responseObj.put("desc", r.getDescription()); - return responseObj; - } - - private Map<String, Object> kaObject(KeyAgreementTestable kat) { - Map<String, Object> katObject = new HashMap<>(); - katObject.put("secret", ByteUtil.bytesToHex(kat.getSecret())); - return katObject; - } - - private Map<String, Object> kgtObject(KeyGeneratorTestable kgt) { - Map<String, Object> kgtObject = new HashMap<>(); - Map<String, Object> pubObject = new HashMap<>(); - PublicKey pkey = kgt.getKeyPair().getPublic(); - pubObject.put("algorithm", pkey.getAlgorithm()); - pubObject.put("format", pkey.getFormat()); - pubObject.put("raw", ByteUtil.bytesToHex(pkey.getEncoded())); - kgtObject.put("pubkey", pubObject); - - Map<String, Object> privObject = new HashMap<>(); - PrivateKey skey = kgt.getKeyPair().getPrivate(); - privObject.put("algorithm", skey.getAlgorithm()); - privObject.put("format", skey.getFormat()); - privObject.put("raw", ByteUtil.bytesToHex(skey.getEncoded())); - kgtObject.put("privkey", privObject); - return kgtObject; - } - - private Map<String, Object> sigObject(SignatureTestable sig) { - Map<String, Object> sigObject = new HashMap<>(); - sigObject.put("verified", sig.getVerified()); - sigObject.put("raw", ByteUtil.bytesToHex(sig.getSignature())); - return sigObject; - } - - private Map<String, Object> testObject(Test t) { - Map<String, Object> testObj = new HashMap<>(); - - if (t instanceof CommandTest) { - CommandTest test = (CommandTest) t; - testObj.put("type", "command"); - testObj.put("command", commandObject(test.getCommand())); - testObj.put("response", responseObject(test.getResponse())); - } else if (t instanceof KeyAgreementTest) { - KeyAgreementTest test = (KeyAgreementTest) t; - testObj.put("type", "key-agreement"); - testObj.put("key-agreement", kaObject(test.getTestable())); - } else if (t instanceof KeyGeneratorTest) { - KeyGeneratorTest test = (KeyGeneratorTest) t; - testObj.put("type", "key-pair-generator"); - testObj.put("key-pair-generator", kgtObject(test.getTestable())); - } else if (t instanceof SignatureTest) { - SignatureTest test = (SignatureTest) t; - testObj.put("type", "signature"); - testObj.put("signature", sigObject(test.getTestable())); - } else if (t instanceof CompoundTest) { - CompoundTest test = (CompoundTest) t; - testObj.put("type", "compound"); - List<Map<String, Object>> tests = new LinkedList<>(); - for (Test innerTest : test.getTests()) { - tests.add(testObject(innerTest)); - } - testObj.put("tests", tests); - } - - 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); - - return testObj; - } - - @Override - public void outputTest(Test t) { - if (!t.hasRun()) - return; - tests.add(testObject(t)); - } - - @Override - public void end() { - DumperOptions options = new DumperOptions(); - options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - options.setPrettyFlow(true); - Yaml yaml = new Yaml(options); - - Map<String, Object> result = new HashMap<>(); - result.put("testRun", testRun); - String out = yaml.dump(result); - - output.println(out); - output.println("---"); - } -} diff --git a/src/cz/crcs/ectester/reader/ECTesterReader.java b/src/cz/crcs/ectester/reader/ECTesterReader.java index 6fa7068..f566c9c 100644 --- a/src/cz/crcs/ectester/reader/ECTesterReader.java +++ b/src/cz/crcs/ectester/reader/ECTesterReader.java @@ -31,7 +31,10 @@ import cz.crcs.ectester.common.test.TestRunner; import cz.crcs.ectester.common.util.ByteUtil; import cz.crcs.ectester.data.EC_Store; import cz.crcs.ectester.reader.command.Command; -import cz.crcs.ectester.reader.output.*; +import cz.crcs.ectester.reader.output.ResponseWriter; +import cz.crcs.ectester.reader.output.TextTestWriter; +import cz.crcs.ectester.reader.output.XMLTestWriter; +import cz.crcs.ectester.reader.output.YAMLTestWriter; import cz.crcs.ectester.reader.response.Response; import cz.crcs.ectester.reader.test.*; import javacard.security.KeyPair; diff --git a/src/cz/crcs/ectester/reader/output/TextTestWriter.java b/src/cz/crcs/ectester/reader/output/TextTestWriter.java new file mode 100644 index 0000000..dedf561 --- /dev/null +++ b/src/cz/crcs/ectester/reader/output/TextTestWriter.java @@ -0,0 +1,28 @@ +package cz.crcs.ectester.reader.output; + +import cz.crcs.ectester.common.output.BaseTextTestWriter; +import cz.crcs.ectester.common.test.Testable; +import cz.crcs.ectester.reader.test.CommandTestable; + +import java.io.PrintStream; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class TextTestWriter extends BaseTextTestWriter { + private ResponseWriter writer; + + public TextTestWriter(PrintStream output) { + super(output); + this.writer = new ResponseWriter(output); + } + + @Override + protected String testableString(Testable t) { + if (t instanceof CommandTestable) { + CommandTestable cmd = (CommandTestable) t; + return writer.responseSuffix(cmd.getResponse()); + } + return ""; + } +} diff --git a/src/cz/crcs/ectester/reader/output/XMLTestWriter.java b/src/cz/crcs/ectester/reader/output/XMLTestWriter.java new file mode 100644 index 0000000..d88a64e --- /dev/null +++ b/src/cz/crcs/ectester/reader/output/XMLTestWriter.java @@ -0,0 +1,75 @@ +package cz.crcs.ectester.reader.output; + +import cz.crcs.ectester.common.output.BaseXMLTestWriter; +import cz.crcs.ectester.common.test.Testable; +import cz.crcs.ectester.common.util.ByteUtil; +import cz.crcs.ectester.reader.command.Command; +import cz.crcs.ectester.reader.response.Response; +import cz.crcs.ectester.reader.test.CommandTestable; +import org.w3c.dom.Element; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.OutputStream; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class XMLTestWriter extends BaseXMLTestWriter { + public XMLTestWriter(OutputStream output) throws ParserConfigurationException { + super(output); + } + + private Element commandElement(Command c) { + Element commandElem = doc.createElement("command"); + + Element apdu = doc.createElement("apdu"); + apdu.setTextContent(ByteUtil.bytesToHex(c.getAPDU().getBytes())); + commandElem.appendChild(apdu); + + return commandElem; + } + + private Element responseElement(Response r) { + Element responseElem = doc.createElement("response"); + responseElem.setAttribute("successful", r.successful() ? "true" : "false"); + + Element apdu = doc.createElement("apdu"); + apdu.setTextContent(ByteUtil.bytesToHex(r.getAPDU().getBytes())); + responseElem.appendChild(apdu); + + Element naturalSW = doc.createElement("natural-sw"); + naturalSW.setTextContent(String.valueOf(Short.toUnsignedInt(r.getNaturalSW()))); + responseElem.appendChild(naturalSW); + + Element sws = doc.createElement("sws"); + for (int i = 0; i < r.getNumSW(); ++i) { + Element sw = doc.createElement("sw"); + sw.setTextContent(String.valueOf(Short.toUnsignedInt(r.getSW(i)))); + sws.appendChild(sw); + } + responseElem.appendChild(sws); + + Element duration = doc.createElement("duration"); + duration.setTextContent(String.valueOf(r.getDuration())); + responseElem.appendChild(duration); + + Element description = doc.createElement("desc"); + description.setTextContent(r.getDescription()); + responseElem.appendChild(description); + + return responseElem; + } + + @Override + protected Element testableElement(Testable t) { + if (t instanceof CommandTestable) { + CommandTestable cmd = (CommandTestable) t; + Element result = doc.createElement("test"); + result.setAttribute("type", "command"); + result.appendChild(commandElement(cmd.getCommand())); + result.appendChild(responseElement(cmd.getResponse())); + return result; + } + return null; + } +}
\ No newline at end of file diff --git a/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java b/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java new file mode 100644 index 0000000..49a20f1 --- /dev/null +++ b/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java @@ -0,0 +1,57 @@ +package cz.crcs.ectester.reader.output; + +import cz.crcs.ectester.common.output.BaseYAMLTestWriter; +import cz.crcs.ectester.common.test.Testable; +import cz.crcs.ectester.common.util.ByteUtil; +import cz.crcs.ectester.reader.command.Command; +import cz.crcs.ectester.reader.response.Response; +import cz.crcs.ectester.reader.test.CommandTestable; + +import java.io.PrintStream; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class YAMLTestWriter extends BaseYAMLTestWriter { + public YAMLTestWriter(PrintStream output) { + super(output); + } + + private Map<String, Object> commandObject(Command c) { + Map<String, Object> commandObj = new HashMap<>(); + commandObj.put("apdu", ByteUtil.bytesToHex(c.getAPDU().getBytes())); + return commandObj; + } + + private Map<String, Object> responseObject(Response r) { + Map<String, Object> responseObj = new HashMap<>(); + responseObj.put("successful", r.successful()); + responseObj.put("apdu", ByteUtil.bytesToHex(r.getAPDU().getBytes())); + responseObj.put("natural_sw", Short.toUnsignedInt(r.getNaturalSW())); + List<Integer> sws = new LinkedList<>(); + for (int i = 0; i < r.getNumSW(); ++i) { + sws.add(Short.toUnsignedInt(r.getSW(i))); + } + responseObj.put("sws", sws); + responseObj.put("duration", r.getDuration()); + responseObj.put("desc", r.getDescription()); + return responseObj; + } + + @Override + protected Map<String, Object> testableObject(Testable t) { + if (t instanceof CommandTestable) { + CommandTestable cmd = (CommandTestable) t; + Map<String, Object> result = new HashMap<>(); + result.put("type", "command"); + result.put("command", commandObject(cmd.getCommand())); + result.put("response", responseObject(cmd.getResponse())); + return result; + } + return null; + } +} diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java index c3dfbbe..237897c 100644 --- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -2,7 +2,6 @@ package cz.crcs.ectester.standalone; import cz.crcs.ectester.common.cli.*; import cz.crcs.ectester.common.ec.EC_Curve; -import cz.crcs.ectester.common.output.TextTestWriter; import cz.crcs.ectester.common.test.TestException; import cz.crcs.ectester.common.test.TestRunner; import cz.crcs.ectester.common.util.ByteUtil; @@ -12,6 +11,7 @@ import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; import cz.crcs.ectester.standalone.consts.SignatureIdent; import cz.crcs.ectester.standalone.libs.*; +import cz.crcs.ectester.standalone.output.TextTestWriter; import cz.crcs.ectester.standalone.test.StandaloneDefaultSuite; import cz.crcs.ectester.standalone.test.StandaloneTestSuite; import org.apache.commons.cli.DefaultParser; diff --git a/src/cz/crcs/ectester/standalone/output/TextTestWriter.java b/src/cz/crcs/ectester/standalone/output/TextTestWriter.java new file mode 100644 index 0000000..ddb1029 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/output/TextTestWriter.java @@ -0,0 +1,20 @@ +package cz.crcs.ectester.standalone.output; + +import cz.crcs.ectester.common.output.BaseTextTestWriter; +import cz.crcs.ectester.common.test.Testable; + +import java.io.PrintStream; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class TextTestWriter extends BaseTextTestWriter { + public TextTestWriter(PrintStream output) { + super(output); + } + + @Override + protected String testableString(Testable t) { + return ""; + } +}
\ No newline at end of file diff --git a/src/cz/crcs/ectester/standalone/output/XMLTestWriter.java b/src/cz/crcs/ectester/standalone/output/XMLTestWriter.java new file mode 100644 index 0000000..cc1a19e --- /dev/null +++ b/src/cz/crcs/ectester/standalone/output/XMLTestWriter.java @@ -0,0 +1,82 @@ +package cz.crcs.ectester.standalone.output; + +import cz.crcs.ectester.common.output.BaseXMLTestWriter; +import cz.crcs.ectester.common.test.Testable; +import cz.crcs.ectester.common.util.ByteUtil; +import cz.crcs.ectester.standalone.test.KeyAgreementTestable; +import cz.crcs.ectester.standalone.test.KeyGeneratorTestable; +import cz.crcs.ectester.standalone.test.SignatureTestable; +import org.w3c.dom.Element; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.OutputStream; +import java.security.PrivateKey; +import java.security.PublicKey; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class XMLTestWriter extends BaseXMLTestWriter { + + public XMLTestWriter(OutputStream output) throws ParserConfigurationException { + super(output); + } + + private Element kaElement(KeyAgreementTestable kat) { + Element katElem = doc.createElement("key-agreement"); + + Element secret = doc.createElement("secret"); + secret.setTextContent(ByteUtil.bytesToHex(kat.getSecret())); + katElem.appendChild(secret); + + return katElem; + } + + private Element kgtElement(KeyGeneratorTestable kgt) { + Element kgtElem = doc.createElement("key-pair-generator"); + + Element keyPair = doc.createElement("key-pair"); + Element pubkey = doc.createElement("pubkey"); + PublicKey pkey = kgt.getKeyPair().getPublic(); + pubkey.setAttribute("algorithm", pkey.getAlgorithm()); + pubkey.setAttribute("format", pkey.getFormat()); + pubkey.setTextContent(ByteUtil.bytesToHex(pkey.getEncoded())); + keyPair.appendChild(pubkey); + + Element privkey = doc.createElement("privkey"); + PrivateKey skey = kgt.getKeyPair().getPrivate(); + privkey.setAttribute("algorithm", skey.getAlgorithm()); + privkey.setAttribute("format", skey.getFormat()); + privkey.setTextContent(ByteUtil.bytesToHex(skey.getEncoded())); + keyPair.appendChild(privkey); + + return kgtElem; + } + + private Element sigElement(SignatureTestable sig) { + Element sigElem = doc.createElement("signature"); + sigElem.setAttribute("verified", sig.getVerified() ? "true" : "false"); + + Element raw = doc.createElement("raw"); + raw.setTextContent(ByteUtil.bytesToHex(sig.getSignature())); + sigElem.appendChild(raw); + + return sigElem; + } + + @Override + protected Element testableElement(Testable t) { + Element result = doc.createElement("test"); + if (t instanceof KeyGeneratorTestable) { + result.setAttribute("type", "key-pair-generator"); + result.appendChild(kgtElement((KeyGeneratorTestable) t)); + } else if (t instanceof KeyAgreementTestable) { + result.setAttribute("type", "key-agreement"); + result.appendChild(kaElement((KeyAgreementTestable) t)); + } else if (t instanceof SignatureTestable) { + result.setAttribute("type", "signature"); + result.appendChild(sigElement((SignatureTestable) t)); + } + return result; + } +} diff --git a/src/cz/crcs/ectester/standalone/output/YAMLTestWriter.java b/src/cz/crcs/ectester/standalone/output/YAMLTestWriter.java new file mode 100644 index 0000000..7ede623 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/output/YAMLTestWriter.java @@ -0,0 +1,70 @@ +package cz.crcs.ectester.standalone.output; + +import cz.crcs.ectester.common.output.BaseYAMLTestWriter; +import cz.crcs.ectester.common.test.Testable; +import cz.crcs.ectester.common.util.ByteUtil; +import cz.crcs.ectester.standalone.test.KeyAgreementTestable; +import cz.crcs.ectester.standalone.test.KeyGeneratorTestable; +import cz.crcs.ectester.standalone.test.SignatureTestable; + +import java.io.PrintStream; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class YAMLTestWriter extends BaseYAMLTestWriter { + public YAMLTestWriter(PrintStream output) { + super(output); + } + + private Map<String, Object> kaObject(KeyAgreementTestable kat) { + Map<String, Object> katObject = new HashMap<>(); + katObject.put("secret", ByteUtil.bytesToHex(kat.getSecret())); + return katObject; + } + + private Map<String, Object> kgtObject(KeyGeneratorTestable kgt) { + Map<String, Object> kgtObject = new HashMap<>(); + Map<String, Object> pubObject = new HashMap<>(); + PublicKey pkey = kgt.getKeyPair().getPublic(); + pubObject.put("algorithm", pkey.getAlgorithm()); + pubObject.put("format", pkey.getFormat()); + pubObject.put("raw", ByteUtil.bytesToHex(pkey.getEncoded())); + kgtObject.put("pubkey", pubObject); + + Map<String, Object> privObject = new HashMap<>(); + PrivateKey skey = kgt.getKeyPair().getPrivate(); + privObject.put("algorithm", skey.getAlgorithm()); + privObject.put("format", skey.getFormat()); + privObject.put("raw", ByteUtil.bytesToHex(skey.getEncoded())); + kgtObject.put("privkey", privObject); + return kgtObject; + } + + private Map<String, Object> sigObject(SignatureTestable sig) { + Map<String, Object> sigObject = new HashMap<>(); + sigObject.put("verified", sig.getVerified()); + sigObject.put("raw", ByteUtil.bytesToHex(sig.getSignature())); + return sigObject; + } + + @Override + protected Map<String, Object> testableObject(Testable t) { + Map<String, Object> result = new HashMap<>(); + if (t instanceof KeyGeneratorTestable) { + result.put("type", "key-pair-generator"); + result.put("key-pair-generator", kgtObject((KeyGeneratorTestable) t)); + } else if (t instanceof KeyAgreementTestable) { + result.put("type", "key-agreement"); + result.put("key-agreement", kaObject((KeyAgreementTestable) t)); + } else if (t instanceof SignatureTestable) { + result.put("type", "signature"); + result.put("signature", sigObject((SignatureTestable) t)); + } + return result; + } +} |
