From 0cc7f2a61b17f2870d1c8f9c45d0eb754a60a27b Mon Sep 17 00:00:00 2001 From: Antoine A <> Date: Tue, 12 Mar 2024 10:57:23 +0100 Subject: Clean code and fix tests --- .../tech/libeufin/nexus/ebics/EbicsKeyMng.kt | 28 ++++++++++------------ nexus/src/test/kotlin/EbicsTest.kt | 14 +++++------ nexus/src/test/kotlin/XmlUtilTest.kt | 12 +++++----- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsKeyMng.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsKeyMng.kt index 1847beec..a6d965e5 100644 --- a/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsKeyMng.kt +++ b/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsKeyMng.kt @@ -45,11 +45,7 @@ class Ebics3KeyMng( el("ns2:SignatureVersion", "A006") } } - val doc = XmlBuilder.toDom("ebicsUnsecuredRequest", "urn:org:ebics:H004") { - attr("http://www.w3.org/2000/xmlns/", "xmlns", "urn:org:ebics:H004") - attr("http://www.w3.org/2000/xmlns/", "xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") - attr("Version", "H004") - attr("Revision", "1") + val doc = request("ebicsUnsecuredRequest") { el("header") { attr("authenticate", "true") el("static") { @@ -80,11 +76,7 @@ class Ebics3KeyMng( el("ns2:EncryptionVersion", "E002") } } - val doc = XmlBuilder.toDom("ebicsUnsecuredRequest", "urn:org:ebics:H004") { - attr("http://www.w3.org/2000/xmlns/", "xmlns", "urn:org:ebics:H004") - attr("http://www.w3.org/2000/xmlns/", "xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") - attr("Version", "H004") - attr("Revision", "1") + val doc = request("ebicsUnsecuredRequest") { el("header") { attr("authenticate", "true") el("static") { @@ -106,11 +98,7 @@ class Ebics3KeyMng( fun HPB(): ByteArray { val nonce = getNonce(128) - val doc = XmlBuilder.toDom("ebicsNoPubKeyDigestsRequest", "urn:org:ebics:H004") { - attr("http://www.w3.org/2000/xmlns/", "xmlns", "urn:org:ebics:H004") - attr("http://www.w3.org/2000/xmlns/", "xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") - attr("Version", "H004") - attr("Revision", "1") + val doc = request("ebicsNoPubKeyDigestsRequest") { el("header") { attr("authenticate", "true") el("static") { @@ -136,6 +124,16 @@ class Ebics3KeyMng( /* ----- Helpers ----- */ + private fun request(name: String, build: XmlBuilder.() -> Unit): Document { + return XmlBuilder.toDom(name, "urn:org:ebics:H004") { + attr("http://www.w3.org/2000/xmlns/", "xmlns", "urn:org:ebics:H004") + attr("http://www.w3.org/2000/xmlns/", "xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") + attr("Version", "H004") + attr("Revision", "1") + build() + } + } + private fun XmlBuilder.RSAKeyXml(key: RSAPrivateCrtKey) { el("ns2:PubKeyValue") { el("ds:RSAKeyValue") { diff --git a/nexus/src/test/kotlin/EbicsTest.kt b/nexus/src/test/kotlin/EbicsTest.kt index c22a469c..a67a48d6 100644 --- a/nexus/src/test/kotlin/EbicsTest.kt +++ b/nexus/src/test/kotlin/EbicsTest.kt @@ -35,29 +35,29 @@ class EbicsTest { assertFailsWith { getMockedClient { respondError(HttpStatusCode.NotFound) - }.postToBank("http://ignored.example.com/", ByteArray(0)) + }.postToBank("http://ignored.example.com/", ByteArray(0), "Test") }.run { - assertEquals("bank HTTP error: 404 Not Found", message) + assertEquals("Test: bank HTTP error: 404 Not Found", message) } assertFailsWith { getMockedClient { throw Exception("Simulate failure") - }.postToBank("http://ignored.example.com/", ByteArray(0)) + }.postToBank("http://ignored.example.com/", ByteArray(0), "Test") }.run { - assertEquals("failed to contact bank", message) + assertEquals("Test: failed to contact bank", message) assertEquals("Simulate failure", cause!!.message) } assertFailsWith { getMockedClient { respondOk("") - }.postToBank("http://ignored.example.com/", ByteArray(0)) + }.postToBank("http://ignored.example.com/", ByteArray(0), "Test") }.run { - assertEquals("invalid XML bank reponse", message) + assertEquals("Test: invalid XML bank reponse", message) assertEquals("Attribute name \"broken\" associated with an element type \"ebics\" must be followed by the ' = ' character.", cause!!.message) } getMockedClient { respondOk("") - }.postToBank("http://ignored.example.com/", ByteArray(0)) + }.postToBank("http://ignored.example.com/", ByteArray(0), "Test") } // Tests that internal repr. of keys lead to valid PDF. diff --git a/nexus/src/test/kotlin/XmlUtilTest.kt b/nexus/src/test/kotlin/XmlUtilTest.kt index f847c928..03941f3f 100644 --- a/nexus/src/test/kotlin/XmlUtilTest.kt +++ b/nexus/src/test/kotlin/XmlUtilTest.kt @@ -39,9 +39,9 @@ class XmlUtilTest { kpg.initialize(2048) val pair = kpg.genKeyPair() val otherPair = kpg.genKeyPair() - XMLUtil.signEbicsDocument(doc, pair.private) - kotlin.test.assertTrue(XMLUtil.verifyEbicsDocument(doc, pair.public)) - kotlin.test.assertFalse(XMLUtil.verifyEbicsDocument(doc, otherPair.public)) + XMLUtil.signEbicsDocument(doc, pair.private, "H004") + kotlin.test.assertTrue(XMLUtil.verifyEbicsDocument(doc, pair.public, "H004")) + kotlin.test.assertFalse(XMLUtil.verifyEbicsDocument(doc, otherPair.public, "H004")) } @Test @@ -56,8 +56,8 @@ class XmlUtilTest { val kpg = KeyPairGenerator.getInstance("RSA") kpg.initialize(2048) val pair = kpg.genKeyPair() - XMLUtil.signEbicsDocument(doc, pair.private) - kotlin.test.assertTrue(XMLUtil.verifyEbicsDocument(doc, pair.public)) + XMLUtil.signEbicsDocument(doc, pair.private, "H004") + kotlin.test.assertTrue(XMLUtil.verifyEbicsDocument(doc, pair.public, "H004")) } @Test @@ -68,6 +68,6 @@ class XmlUtilTest { val keyStream = classLoader.getResourceAsStream("signature1/public_key.txt") val keyBytes = keyStream.decodeBase64().readAllBytes() val key = CryptoUtil.loadRsaPublicKey(keyBytes) - assertTrue(XMLUtil.verifyEbicsDocument(doc, key)) + assertTrue(XMLUtil.verifyEbicsDocument(doc, key, "H004")) } } \ No newline at end of file -- cgit v1.2.3