summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine A <>2024-03-12 10:57:23 +0100
committerAntoine A <>2024-03-12 10:57:23 +0100
commit0cc7f2a61b17f2870d1c8f9c45d0eb754a60a27b (patch)
tree2fb49d578202c0956c07f0d0d676cc0de8bf75cb
parent0d62875dd2287857c5da172dcb3301062880810a (diff)
downloadlibeufin-0cc7f2a61b17f2870d1c8f9c45d0eb754a60a27b.tar.gz
libeufin-0cc7f2a61b17f2870d1c8f9c45d0eb754a60a27b.tar.bz2
libeufin-0cc7f2a61b17f2870d1c8f9c45d0eb754a60a27b.zip
Clean code and fix tests
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsKeyMng.kt28
-rw-r--r--nexus/src/test/kotlin/EbicsTest.kt14
-rw-r--r--nexus/src/test/kotlin/XmlUtilTest.kt12
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<EbicsError.Transport> {
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<EbicsError.Transport> {
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<EbicsError.Protocol> {
getMockedClient {
respondOk("<ebics broken></ebics>")
- }.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("<ebics></ebics>")
- }.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