summaryrefslogtreecommitdiff
path: root/nexus/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'nexus/src/test')
-rw-r--r--nexus/src/test/kotlin/Ebics.kt2
-rw-r--r--nexus/src/test/kotlin/XmlCombinatorsTest.kt76
-rw-r--r--nexus/src/test/kotlin/XmlUtilTest.kt73
-rw-r--r--nexus/src/test/resources/signature1/doc.xml9
-rw-r--r--nexus/src/test/resources/signature1/public_key.txt1
5 files changed, 160 insertions, 1 deletions
diff --git a/nexus/src/test/kotlin/Ebics.kt b/nexus/src/test/kotlin/Ebics.kt
index 7bd05fa7..e2a2ab84 100644
--- a/nexus/src/test/kotlin/Ebics.kt
+++ b/nexus/src/test/kotlin/Ebics.kt
@@ -20,7 +20,7 @@
import io.ktor.client.engine.mock.*
import io.ktor.http.*
import org.junit.Test
-import tech.libeufin.ebics.XMLUtil
+import tech.libeufin.nexus.*
import tech.libeufin.nexus.ebics.*
import kotlin.io.path.Path
import kotlin.io.path.writeBytes
diff --git a/nexus/src/test/kotlin/XmlCombinatorsTest.kt b/nexus/src/test/kotlin/XmlCombinatorsTest.kt
new file mode 100644
index 00000000..b14920e3
--- /dev/null
+++ b/nexus/src/test/kotlin/XmlCombinatorsTest.kt
@@ -0,0 +1,76 @@
+/*
+ * This file is part of LibEuFin.
+ * Copyright (C) 2024 Taler Systems S.A.
+
+ * LibEuFin is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation; either version 3, or
+ * (at your option) any later version.
+
+ * LibEuFin is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+ * Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public
+ * License along with LibEuFin; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>
+ */
+
+import org.junit.Test
+import tech.libeufin.nexus.XmlBuilder
+import tech.libeufin.nexus.XMLUtil
+import kotlin.test.assertEquals
+
+class XmlCombinatorsTest {
+ fun testBuilder(expected: String, root: String, builder: XmlBuilder.() -> Unit) {
+ val toString = XmlBuilder.toString(root, builder)
+ val toDom = XmlBuilder.toDom(root, null, builder)
+ //assertEquals(expected, toString) TODO fix empty tag being closed only with toString
+ assertEquals(expected, XMLUtil.convertDomToBytes(toDom).toString(Charsets.UTF_8))
+ }
+
+ @Test
+ fun testWithModularity() {
+ fun module(base: XmlBuilder) {
+ base.el("module")
+ }
+ testBuilder(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><root><module/></root>",
+ "root"
+ ) {
+ module(this)
+ }
+ }
+
+ @Test
+ fun testWithIterable() {
+ testBuilder(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><iterable><endOfDocument><e1><e11>111</e11></e1><e2><e22>222</e22></e2><e3><e33>333</e33></e3><e4><e44>444</e44></e4><e5><e55>555</e55></e5><e6><e66>666</e66></e6><e7><e77>777</e77></e7><e8><e88>888</e88></e8><e9><e99>999</e99></e9><e10><e1010>101010</e1010></e10></endOfDocument></iterable>",
+ "iterable"
+ ) {
+ el("endOfDocument") {
+ for (i in 1..10)
+ el("e$i/e$i$i", "$i$i$i")
+ }
+ }
+ }
+
+ @Test
+ fun testBasicXmlBuilding() {
+ testBuilder(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ebicsRequest version=\"H004\"><a><b><c attribute-of=\"c\"><d><e><f nested=\"true\"><g><h/></g></f></e></d></c></b></a><one_more/></ebicsRequest>",
+ "ebicsRequest"
+ ) {
+ attr("version", "H004")
+ el("a/b/c") {
+ attr("attribute-of", "c")
+ el("d/e/f") {
+ attr("nested", "true")
+ el("g/h")
+ }
+ }
+ el("one_more")
+ }
+ }
+}
diff --git a/nexus/src/test/kotlin/XmlUtilTest.kt b/nexus/src/test/kotlin/XmlUtilTest.kt
new file mode 100644
index 00000000..f847c928
--- /dev/null
+++ b/nexus/src/test/kotlin/XmlUtilTest.kt
@@ -0,0 +1,73 @@
+/*
+ * This file is part of LibEuFin.
+ * Copyright (C) 2024 Taler Systems S.A.
+
+ * LibEuFin is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation; either version 3, or
+ * (at your option) any later version.
+
+ * LibEuFin is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+ * Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public
+ * License along with LibEuFin; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>
+ */
+
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import tech.libeufin.common.crypto.CryptoUtil
+import tech.libeufin.common.decodeBase64
+import tech.libeufin.nexus.XMLUtil
+import java.security.KeyPairGenerator
+import javax.xml.transform.stream.StreamSource
+
+class XmlUtilTest {
+
+ @Test
+ fun basicSigningTest() {
+ val doc = XMLUtil.parseIntoDom("""
+ <myMessage xmlns:ebics="urn:org:ebics:H004">
+ <ebics:AuthSignature />
+ <foo authenticate="true">Hello World</foo>
+ </myMessage>
+ """.trimIndent().toByteArray().inputStream())
+ val kpg = KeyPairGenerator.getInstance("RSA")
+ 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))
+ }
+
+ @Test
+ fun multiAuthSigningTest() {
+ val doc = XMLUtil.parseIntoDom("""
+ <myMessage xmlns:ebics="urn:org:ebics:H004">
+ <ebics:AuthSignature />
+ <foo authenticate="true">Hello World</foo>
+ <bar authenticate="true">Another one!</bar>
+ </myMessage>
+ """.trimIndent().toByteArray().inputStream())
+ 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))
+ }
+
+ @Test
+ fun testRefSignature() {
+ val classLoader = ClassLoader.getSystemClassLoader()
+ val docText = classLoader.getResourceAsStream("signature1/doc.xml")
+ val doc = XMLUtil.parseIntoDom(docText)
+ val keyStream = classLoader.getResourceAsStream("signature1/public_key.txt")
+ val keyBytes = keyStream.decodeBase64().readAllBytes()
+ val key = CryptoUtil.loadRsaPublicKey(keyBytes)
+ assertTrue(XMLUtil.verifyEbicsDocument(doc, key))
+ }
+} \ No newline at end of file
diff --git a/nexus/src/test/resources/signature1/doc.xml b/nexus/src/test/resources/signature1/doc.xml
new file mode 100644
index 00000000..271f8429
--- /dev/null
+++ b/nexus/src/test/resources/signature1/doc.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<myMessage xmlns:ebics="urn:org:ebics:H004">
+ <ebics:AuthSignature><ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><ds:Reference URI="#xpointer(//*[@authenticate='true'])"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>qiFUoCn9kE0zSidyraO2Br/wn3/XyvWObJZ0aLIBXyA=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#">LupLyRUJIuk0kCRwpFj4fpen2MI7Jw0BI944agwzXHfSDfq0Pp8h3sub6eSsKIAq7ekT3z+mlfMc&#13;
+ VFaKRi4B7kv4ja/URiYCKKbChQU2+kMGDvsncx9VcpcFrqAbWPmE9JXD2W2YW9OSkJ1tAZxZlZwS&#13;
+ A8KcvluV1wGEBuakHL2t3GqFPQEfKW4l8GYTjHh/w9jBve5d8tvMOjGtoyNemZGrVlzBxO9+hwbw&#13;
+ 8UFUCDA00dCjFDUHOnyAbBYsGzoaQyZprDn3iYDvlBz243zAN98PIKDclxlUEmkuF+JhrhCRjT9l&#13;
+ +JJxrELGHaDkFVadR4kaPdWPsbDaV0/2Fzc4Qg==</ds:SignatureValue></ebics:AuthSignature>
+ <foo authenticate="true">Hello World</foo>
+</myMessage> \ No newline at end of file
diff --git a/nexus/src/test/resources/signature1/public_key.txt b/nexus/src/test/resources/signature1/public_key.txt
new file mode 100644
index 00000000..6d52df58
--- /dev/null
+++ b/nexus/src/test/resources/signature1/public_key.txt
@@ -0,0 +1 @@
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqpUpetHZYdMjnaG544iSLZ5SnxlV4F/eQsIckG3mvMaXCQsY4rUTfJyle/fTZ0xGbjCUXCsbl1wkz8eB6chaX2LsHYDGiu/xNnU1nddAVB+5kkA5AIGncT9NVhdOgmpnZY/tae9qtZfCPAvbI0sGYQHea0pwyJ/hUnRJiMOjSRgIXALIvGVNqxe4U5ffLXFIUapTK2hOuhUH9BwDSK+mVR6gw0vDT05Z38sEpTeKUqJywL5cPSFIV+AN4ErSvsXNkTKUcbDxhGzOh/oTjTkz1kFFKe4ijPkSRkpK2sJMyAIretBKOK8SDICnsSrIh0YAcd6yTHQ3CeEjW4t0ZBULOQIDAQAB \ No newline at end of file