From 4bc21c82b2b8ed6feb4435e5f81049973900d873 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 7 May 2020 00:46:44 +0200 Subject: [PATCH 1/8] first draft of incomingMessageFromPGPText() --- .../pEp/jniadapter/AbstractEngine.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/foundation/pEp/jniadapter/AbstractEngine.java b/src/foundation/pEp/jniadapter/AbstractEngine.java index f882fce..c12d584 100644 --- a/src/foundation/pEp/jniadapter/AbstractEngine.java +++ b/src/foundation/pEp/jniadapter/AbstractEngine.java @@ -194,5 +194,39 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AutoClosea } return 0; } + + public static Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) { + Message msg = new Message(); + msg.setDir(Message.Direction.Incoming); + msg.setShortmsg("p≡p"); + msg.setLongmsg("this message was encrypted with p≡p https://pEp-project.org"); + msg.setEncFormat(encFormat); + + // Attachments + Blob att0 = new Blob(); + att0.mime_type = "application/pgp-encrypted"; + att0.filename = null; + att0.data = "Version: 1".getBytes(); + + Blob att1 = new Blob(); + att1.mime_type = "application/octet-stream"; + att1.filename = "file://msg.asc"; + att1.data = pgpText.getBytes(); + + Vector attachments = new Vector<>(); + attachments.add(att0); + attachments.add(att1); + msg.setAttachments(attachments); + + // Opts + ArrayList> opts = new ArrayList<>(); + Pair xpEp = new Pair<>(); + xpEp.first = "X-pEp-Version"; + xpEp.second = "2.1"; + opts.add(xpEp); + msg.setOptFields(opts); + + return msg; + } } From 124fac56a4da0240564ba83c2f1ae96d4413bb6e Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 7 May 2020 00:48:25 +0200 Subject: [PATCH 2/8] Test case for JNI-98 - Testing incomingMessageFromPGPText() --- .../pEp/jniadapter/test/jni98/Makefile | 32 ++++ .../pEp/jniadapter/test/jni98/TestMain.java | 139 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 test/java/foundation/pEp/jniadapter/test/jni98/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java diff --git a/test/java/foundation/pEp/jniadapter/test/jni98/Makefile b/test/java/foundation/pEp/jniadapter/test/jni98/Makefile new file mode 100644 index 0000000..c749ef7 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni98/Makefile @@ -0,0 +1,32 @@ +include ../../../../../../../Makefile.conf +include ../Makefile.conf + +TEST_UNIT_NAME=jni98 + +JAVA_CLASSES = \ + TestMain.class \ + ../utils/TestUtils.class + +.PHONY: compile run test clean + +all: compile + $(MAKE) run + +run: compile + cd $(JAVA_CWD);pwd;HOME=$(JAVA_PEP_HOME_DIR) $(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestMain + +compile: $(JAVA_CLASSES) + +%.class: %.java + cd $(JAVA_CWD);javac -cp $(CLASSPATH) $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$< + +clean: + rm -f $(JAVA_CLASSES) + rm -f *.class + rm -f *.log + rm -Rf .gnupg + rm -Rf .lldb + +clean-pep-home: + rm -rf $(PEP_HOME_DIR)/* + rm -rf $(PEP_HOME_DIR)/.pEp \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java new file mode 100644 index 0000000..6b52efc --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java @@ -0,0 +1,139 @@ +package foundation.pEp.jniadapter.test.jni98; + +import foundation.pEp.jniadapter.test.utils.TestUtils; +import foundation.pEp.jniadapter.*; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Vector; +import java.util.function.Consumer; + +/* +JNI-98 - "Factory function for generating incoming message from PGP text" + +Problem: +There must be a static function in class Engine, which is generating an encrypted +version of a Message, which is structured like messages coming out from encrypt_message() +when being used with Message.EncFormat.PEP. Additionally, it should work with inline format, too. +The signature is expected to be: + +public static Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) + +Solution: +First, we need to know how the Message Object coming out from encrypt_message() are looking. +Then, we try to get the same Message from "incomingMessageFromPGPText()" + */ + +/* +The one person we know is called bob and his pubkey is like this +bob.address = bob@peptest.org +bob.FPR = F804FBE1781F3E2F6158F9F709FB5BDA72BE51C1 +*/ +class TestEnv { + public Sync.DefaultCallback cb = new Sync.DefaultCallback(); + public Identity alice = new Identity(); + public Identity bob = new Identity(); + public Message msgToSelf; + public Message msgToBob; + public Vector vID = new Vector(); + public Vector vStr = new Vector(); + public byte[] keyBobPub; + private String fileNameKeyBobPub = "../resources/test_keys/bob-pub.asc"; + public Engine engine = new Engine(); + + public TestEnv() throws Exception { + // We are alice + alice.address = "alice@peptest.org"; + alice.user_id = "23"; + alice.me = true; + alice = engine.myself(alice); + + // We know Bob and his pubkey + try { + Path path = Paths.get(fileNameKeyBobPub); + keyBobPub = Files.readAllBytes(path); + } catch (Exception e) { + TestUtils.log("Could not open key file:" + fileNameKeyBobPub); + throw e; + } + engine.importKey(keyBobPub); + bob.address = "bob@peptest.org"; + + msgToSelf = makeNewMessage(alice, alice, Message.Direction.Outgoing); + msgToBob = makeNewMessage(alice, bob, Message.Direction.Outgoing); + + vID.add(bob); + vStr.add(""); + } + + public static Message makeNewMessage(Identity from, Identity to, Message.Direction dir) { + Message msg = new Message(); + Vector vID = new Vector(); + vID.add(to); + + msg.setFrom(from); + msg.setTo(vID); + msg.setDir(dir); + msg.setLongmsg("Hi i am the message longmsg"); + return msg; + } +} + +class TestUnit { + TestEnv env; + String testUnitName = "default test unit"; + Consumer lambda; + + public TestUnit(String name, Consumer consumer) throws Exception { + testUnitName = name; + lambda = consumer; + env = new TestEnv(); + + } + + public void run() { + TestUtils.logH1(testUnitName); + try { + lambda.accept(env); + } catch (Throwable e) { + TestUtils.logH1("TestUnit FAILED: " + e.toString()); + return; + } + TestUtils.logH2("SUCCESS!"); + } +} + + +class TestMain { + public static void main(String[] args) throws Exception { + testRunNew(); + } + + public static void testRunNew() throws Exception { + new TestUnit("JNI-98 - Factory function for generating incoming message from PGP text", env -> { + // Make msg1 by encrypting msgToBob + TestUtils.logH2("Create target Message"); + Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEP); + TestUtils.log(TestUtils.msgToString(msg1)); + TestUtils.log("msg returned from encrypt_message is null"); + + // Lets get the pgpText of the msg1, and the EncFormat + String pgpText = Engine.toUTF16(msg1.getAttachments().elementAt(1).data); + Message.EncFormat ef = msg1.getEncFormat(); + + // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 + TestUtils.logH2("incomingMessageFromPGPText()"); + Message msg2 = Engine.incomingMessageFromPGPText(pgpText, ef); + TestUtils.log(TestUtils.msgToString(msg2)); + TestUtils.log("msg returned from incomingMessageFromPGPText() is null"); + + TestUtils.logH2("Verify msg2"); + Engine.decrypt_message_Return result = null; + result = env.engine.decrypt_message(msg2, env.vStr, 0); + TestUtils.log(TestUtils.msgToString(result.dst)); + }).run(); + } +} + + From e8686e1ac42d14adbf6ab28260c1cf776a1c07bd Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 7 May 2020 17:43:27 +0200 Subject: [PATCH 3/8] Fix for pEpEngine default branch: mime_encode_messageO()/mime_decode_message() mime_encode_message() "has_pEp_msg_attachment" hard coded to false mime_decode_message() "has_pEp_msg_attachment" hard coded to NULL --- src/gen_cpp_Message.ysl2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gen_cpp_Message.ysl2 b/src/gen_cpp_Message.ysl2 index 2e587de..a5f4496 100644 --- a/src/gen_cpp_Message.ysl2 +++ b/src/gen_cpp_Message.ysl2 @@ -70,7 +70,7 @@ tstylesheet { size_t _size = (size_t) env->GetArrayLength(mime_text); ::«@name» *_msg = nullptr; - PEP_STATUS status = mime_decode_«@name»(_mime_text, _size, &_msg); + PEP_STATUS status = mime_decode_«@name»(_mime_text, _size, &_msg, NULL); if (status) throw_pEp_Exception(env, status); return (jlong) (int64_t) (intptr_t) _msg; @@ -81,7 +81,7 @@ tstylesheet { pEpLog("called"); «@name» *_obj = «@name»_ptr(env, msg); char *mime_text = nullptr; - PEP_STATUS status = ::mime_encode_«@name»(_obj, false, &mime_text); + PEP_STATUS status = ::mime_encode_«@name»(_obj, false, &mime_text, false); if (status) throw_pEp_Exception(env, status); jbyteArray result = from_string(env, mime_text); From 9affc54d8f003ff3f72c56f604501e240f85d778 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 7 May 2020 17:43:57 +0200 Subject: [PATCH 4/8] Add test jni98 to tests main Makefile --- test/java/foundation/pEp/jniadapter/test/Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/java/foundation/pEp/jniadapter/test/Makefile b/test/java/foundation/pEp/jniadapter/test/Makefile index c5ac816..d154fbf 100644 --- a/test/java/foundation/pEp/jniadapter/test/Makefile +++ b/test/java/foundation/pEp/jniadapter/test/Makefile @@ -10,6 +10,7 @@ run: compile $(MAKE) -C jni91 run $(MAKE) -C jni92 run $(MAKE) -C jni94 run + $(MAKE) -C jni98 run compile: $(MAKE) -C basic compile @@ -19,6 +20,7 @@ compile: $(MAKE) -C jni91 compile $(MAKE) -C jni92 compile $(MAKE) -C jni94 compile + $(MAKE) -C jni98 compile clean: $(MAKE) -C basic clean @@ -28,6 +30,7 @@ clean: $(MAKE) -C jni91 clean $(MAKE) -C jni92 clean $(MAKE) -C jni94 clean + $(MAKE) -C jni98 clean clean-pep-home: $(MAKE) -C basic clean-pep-home From a1555e720f94147623d8ec57ce4013f5d1e36b55 Mon Sep 17 00:00:00 2001 From: heck Date: Sat, 16 May 2020 01:38:44 +0200 Subject: [PATCH 5/8] Add EncFormat PEPEncInlineEA. BUT a lot of question marks. see TODO: --- .../pEp/jniadapter/AbstractEngine.java | 56 ++++++++++++------- src/pEp.yml2 | 11 ++-- .../pEp/jniadapter/test/jni98/TestMain.java | 50 ++++++++++++----- 3 files changed, 78 insertions(+), 39 deletions(-) diff --git a/src/foundation/pEp/jniadapter/AbstractEngine.java b/src/foundation/pEp/jniadapter/AbstractEngine.java index c12d584..62b8f40 100644 --- a/src/foundation/pEp/jniadapter/AbstractEngine.java +++ b/src/foundation/pEp/jniadapter/AbstractEngine.java @@ -198,26 +198,8 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AutoClosea public static Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) { Message msg = new Message(); msg.setDir(Message.Direction.Incoming); - msg.setShortmsg("p≡p"); - msg.setLongmsg("this message was encrypted with p≡p https://pEp-project.org"); msg.setEncFormat(encFormat); - // Attachments - Blob att0 = new Blob(); - att0.mime_type = "application/pgp-encrypted"; - att0.filename = null; - att0.data = "Version: 1".getBytes(); - - Blob att1 = new Blob(); - att1.mime_type = "application/octet-stream"; - att1.filename = "file://msg.asc"; - att1.data = pgpText.getBytes(); - - Vector attachments = new Vector<>(); - attachments.add(att0); - attachments.add(att1); - msg.setAttachments(attachments); - // Opts ArrayList> opts = new ArrayList<>(); Pair xpEp = new Pair<>(); @@ -226,7 +208,41 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AutoClosea opts.add(xpEp); msg.setOptFields(opts); + if(encFormat == Message.EncFormat.PEP) { + // For EncFormat.PEP + // The pgpText goes into the attachment index 1 + msg.setShortmsg("p≡p"); + msg.setLongmsg("this message was encrypted with p≡p https://pEp-project.org"); + + // Attachments + Blob att0 = new Blob(); + att0.mime_type = "application/pgp-encrypted"; + att0.filename = null; + att0.data = "Version: 1".getBytes(); + + Blob att1 = new Blob(); + att1.mime_type = "application/octet-stream"; + att1.filename = "file://msg.asc"; + att1.data = pgpText.getBytes(); + + Vector attachments = new Vector<>(); + attachments.add(att0); + attachments.add(att1); + msg.setAttachments(attachments); + } + else if (encFormat == Message.EncFormat.PEPEncInlineEA) { + // For EncFormat.PEPEncInlineEA + // The pgpText goes into the longMessage + // TODO: Attachment index 0 should contain, i guess, the pubkey of the sender + // TODO: BUT why is there an attachemnt ElevatedAttachments are for non attachment transports + + msg.setShortmsg(""); + msg.setLongmsg(pgpText); + } + else { + throw new pEpCannotEncode("Message.Encformat not supported: " + encFormat.toString()); + } + return msg; } -} - +} \ No newline at end of file diff --git a/src/pEp.yml2 b/src/pEp.yml2 index 0a96c3f..dd1b14d 100644 --- a/src/pEp.yml2 +++ b/src/pEp.yml2 @@ -285,11 +285,12 @@ namespace pEp { } enum EncFormat { - none > 0 - inline > 1 - SMIME > 2 - PGPMIME > 3 - PEP > 4 + none > 0 + inline > 1 + SMIME > 2 + PGPMIME > 3 + PEP > 4 + PEP_enc_inline_EA > 6 } direction dir; diff --git a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java index 6b52efc..c0537be 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java @@ -1,6 +1,6 @@ package foundation.pEp.jniadapter.test.jni98; -import foundation.pEp.jniadapter.test.utils.TestUtils; +import static foundation.pEp.jniadapter.test.utils.TestUtils.*; import foundation.pEp.jniadapter.*; import java.nio.file.Files; @@ -54,7 +54,7 @@ class TestEnv { Path path = Paths.get(fileNameKeyBobPub); keyBobPub = Files.readAllBytes(path); } catch (Exception e) { - TestUtils.log("Could not open key file:" + fileNameKeyBobPub); + log("Could not open key file:" + fileNameKeyBobPub); throw e; } engine.importKey(keyBobPub); @@ -93,14 +93,14 @@ class TestUnit { } public void run() { - TestUtils.logH1(testUnitName); + logH1(testUnitName); try { lambda.accept(env); } catch (Throwable e) { - TestUtils.logH1("TestUnit FAILED: " + e.toString()); + logH1("TestUnit FAILED: " + e.toString()); return; } - TestUtils.logH2("SUCCESS!"); + logH2("SUCCESS!"); } } @@ -111,27 +111,49 @@ class TestMain { } public static void testRunNew() throws Exception { - new TestUnit("JNI-98 - Factory function for generating incoming message from PGP text", env -> { + new TestUnit("JNI-98 - Message.EncFormat.PEP", env -> { // Make msg1 by encrypting msgToBob - TestUtils.logH2("Create target Message"); + logH2("Create target Message"); Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEP); - TestUtils.log(TestUtils.msgToString(msg1)); - TestUtils.log("msg returned from encrypt_message is null"); + log(msgToString(msg1)); + log("EncPep:" + Message.EncFormat.PEP.value); // Lets get the pgpText of the msg1, and the EncFormat String pgpText = Engine.toUTF16(msg1.getAttachments().elementAt(1).data); Message.EncFormat ef = msg1.getEncFormat(); + log("EncPepReturn:" + ef.value); + //TODO: setting encformat to 4 (PEP) getting back 3 (PGPMIME) // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 - TestUtils.logH2("incomingMessageFromPGPText()"); + logH2("incomingMessageFromPGPText()"); + Message msg2 = Engine.incomingMessageFromPGPText(pgpText, Message.EncFormat.PEP); + log(msgToString(msg2)); + + logH2("Verify msg2"); + Engine.decrypt_message_Return result = null; + result = env.engine.decrypt_message(msg2, env.vStr, 0); + log(msgToString(result.dst)); + }).run(); + + new TestUnit("JNI-98 - Message.EncFormat.PEP_enc_inline_EA", env -> { + // Make msg1 by encrypting msgToBob + logH2("Create target Message"); + Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEPEncInlineEA); + log(msgToString(msg1)); + + // Lets get the pgpText of the msg1, and the EncFormat + String pgpText = msg1.getLongmsg(); + Message.EncFormat ef = msg1.getEncFormat(); + + // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 + logH2("incomingMessageFromPGPText()"); Message msg2 = Engine.incomingMessageFromPGPText(pgpText, ef); - TestUtils.log(TestUtils.msgToString(msg2)); - TestUtils.log("msg returned from incomingMessageFromPGPText() is null"); + log(msgToString(msg2)); - TestUtils.logH2("Verify msg2"); + logH2("Verify msg2"); Engine.decrypt_message_Return result = null; result = env.engine.decrypt_message(msg2, env.vStr, 0); - TestUtils.log(TestUtils.msgToString(result.dst)); + log(msgToString(result.dst)); }).run(); } } From 53657d764a98a19106bb55548cd09ef1cef9728a Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 19 May 2020 23:31:01 +0200 Subject: [PATCH 6/8] Enhance Tests and TestUtils (Message pretty printing) --- .../pEp/jniadapter/AbstractEngine.java | 3 - .../pEp/jniadapter/test/jni98/TestMain.java | 30 +- .../pEp/jniadapter/test/utils/TestUtils.java | 259 ++++++++++++------ 3 files changed, 191 insertions(+), 101 deletions(-) diff --git a/src/foundation/pEp/jniadapter/AbstractEngine.java b/src/foundation/pEp/jniadapter/AbstractEngine.java index 62b8f40..0617e69 100644 --- a/src/foundation/pEp/jniadapter/AbstractEngine.java +++ b/src/foundation/pEp/jniadapter/AbstractEngine.java @@ -233,9 +233,6 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AutoClosea else if (encFormat == Message.EncFormat.PEPEncInlineEA) { // For EncFormat.PEPEncInlineEA // The pgpText goes into the longMessage - // TODO: Attachment index 0 should contain, i guess, the pubkey of the sender - // TODO: BUT why is there an attachemnt ElevatedAttachments are for non attachment transports - msg.setShortmsg(""); msg.setLongmsg(pgpText); } diff --git a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java index c0537be..7fac43a 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java @@ -115,7 +115,7 @@ class TestMain { // Make msg1 by encrypting msgToBob logH2("Create target Message"); Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEP); - log(msgToString(msg1)); + log("\n" + msgToString(msg1, false)); log("EncPep:" + Message.EncFormat.PEP.value); // Lets get the pgpText of the msg1, and the EncFormat @@ -127,19 +127,19 @@ class TestMain { // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 logH2("incomingMessageFromPGPText()"); Message msg2 = Engine.incomingMessageFromPGPText(pgpText, Message.EncFormat.PEP); - log(msgToString(msg2)); + log("\n" + msgToString(msg2, false)); logH2("Verify msg2"); Engine.decrypt_message_Return result = null; result = env.engine.decrypt_message(msg2, env.vStr, 0); - log(msgToString(result.dst)); + log("\n" + msgToString(result.dst, false)); }).run(); new TestUnit("JNI-98 - Message.EncFormat.PEP_enc_inline_EA", env -> { // Make msg1 by encrypting msgToBob logH2("Create target Message"); Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEPEncInlineEA); - log(msgToString(msg1)); + log("\n" + msgToString(msg1, false)); // Lets get the pgpText of the msg1, and the EncFormat String pgpText = msg1.getLongmsg(); @@ -148,12 +148,22 @@ class TestMain { // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 logH2("incomingMessageFromPGPText()"); Message msg2 = Engine.incomingMessageFromPGPText(pgpText, ef); - log(msgToString(msg2)); - - logH2("Verify msg2"); - Engine.decrypt_message_Return result = null; - result = env.engine.decrypt_message(msg2, env.vStr, 0); - log(msgToString(result.dst)); + log("\n" + msgToString(msg2, false)); + + // Cant be just simply decrypted again + // And thats correct according to fdik + //[21:29] < heck> | Assertion failed: (value && size && mime_type && code && !code[0] && code_size), function decode_internal, file internal_format.c, line 113. + //[21:31] < fdik> | ja + //[21:31] < fdik> | auch das ist korrekt + //[21:31] < fdik> | wenn Du EA verwendest, dann geht es nicht, dass man die Nachricht so wie sie ist wieder decrypted + //[21:31] < fdik> | sondern das geht nur, wenn man sie zerlegt + //[21:32] < fdik> | dafür ist das Verfahren da + //[21:34] < fdik> | ich hab einen Test dafür geschrieben + //[21:34] < fdik> | pEpEngine/test/src/ElevatedAttachmentsTest.cc + //[21:34] < fdik> | in default + //[21:35] < fdik> | Doku hier https://dev.pep.foundation/Engine/ElevatedAttachments + //[21:35] < fdik> | siehe hier: + //[21:35] < fdik> | https://dev.pep.foundation/Engine/ElevatedAttachments#support-in-message-api }).run(); } } diff --git a/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java b/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java index 223c351..b0f2b63 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java @@ -1,4 +1,5 @@ package foundation.pEp.jniadapter.test.utils; + import foundation.pEp.jniadapter.*; import java.util.ArrayList; @@ -17,7 +18,7 @@ public class TestUtils { public static String identityToString(Identity i, Boolean full) { String ret = ""; - if(full) { + if (full) { ret += "address: " + i.address + "\n"; ret += "fpr: " + i.fpr + "\n"; ret += "username: " + i.username + "\n"; @@ -33,141 +34,169 @@ public class TestUtils { return ret; } - public static String msgToString(Message msg) { + public static String msgToString(Message msg, boolean full) { String ret = ""; - ret += "getAttachments: "; + ArrayList> kvs = new ArrayList<>(); + String key = ""; + String value = ""; + + key = "getAttachments"; try { - ret += blobListToString(msg.getAttachments()) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = blobListToString(msg.getAttachments(), full) + "\n"; + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "Id: "; + key = "Id"; try { - ret += msg.getId() + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = msg.getId(); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getDir: "; + key = "getDir"; try { - ret += msg.getDir().toString() + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = msg.getDir().toString(); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getShortmsg: "; + key = "getShortmsg"; try { - ret += msg.getShortmsg() + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = msg.getShortmsg(); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getLongmsg: "; + key = "getLongmsg"; try { - ret += msg.getLongmsg() + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = msg.getLongmsg(); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getLongmsgFormatted: "; + key = "getLongmsgFormatted"; try { - ret += msg.getLongmsgFormatted() + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = msg.getLongmsgFormatted(); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getFrom: "; + key = "getFrom"; try { - ret += identityToString(msg.getFrom(), false) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = identityToString(msg.getFrom(), full); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getTo: "; + key = "getTo"; try { - ret += identityListToString(msg.getTo(), false) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = identityListToString(msg.getTo(), full); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getRecvBy: "; + key = "getRecvBy"; try { - ret += identityToString(msg.getRecvBy(), false) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = identityToString(msg.getRecvBy(), full); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getCc: "; + key = "getCc"; try { - ret += identityListToString(msg.getCc(), false)+ "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = identityListToString(msg.getCc(), full) + "\n"; + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getBcc: "; + key = "getBcc"; try { - ret += identityListToString(msg.getBcc(), false) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = identityListToString(msg.getBcc(), full); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getReplyTo: "; + key = "getReplyTo"; try { - ret += identityListToString(msg.getReplyTo(), false) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = identityListToString(msg.getReplyTo(), full); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getInReplyTo: "; + key = "getInReplyTo"; try { - ret += stringVectorToString(msg.getInReplyTo()) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = stringVectorToString(msg.getInReplyTo()); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getReferences: "; + key = "getReferences"; try { - ret += stringVectorToString(msg.getReferences()) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = stringVectorToString(msg.getReferences()); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getKeywords: "; + key = "getKeywords"; try { - ret += stringVectorToString(msg.getKeywords()) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = stringVectorToString(msg.getKeywords()); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getComments: "; + key = "getComments"; try { - ret += msg.getComments() + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = msg.getComments(); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getOptFields: "; + key = "getOptFields"; try { - ret += stringPairListToString(msg.getOptFields()) + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = stringPairListToString(msg.getOptFields()); + } catch (Throwable e) { + value = e.toString(); } + kvs.add(new Pair<>(key, value)); - ret += "getEncFormat: "; + key = "getEncFormat"; try { - ret += msg.getEncFormat().toString() + "\n"; - } catch(Throwable e) { - ret += e.toString() + "\n"; + value = msg.getEncFormat().toString(); + } catch (Throwable e) { + value = e.toString(); + } + kvs.add(new Pair(key, value)); + + if (!full) { + kvs = clipStrings(kvs, 200); } + ret = stringPairListToString(kvs); + ret = ret.trim(); return ret; } public static String stringVectorToString(Vector vS) { String ret = ""; - for (String s : vS){ + for (String s : vS) { ret += s + "\n"; } ret = ret.trim(); @@ -176,34 +205,88 @@ public class TestUtils { public static String identityListToString(Vector vI, Boolean full) { String ret = ""; - for(Identity i : vI) { + for (Identity i : vI) { ret += identityToString(i, full) + "\n"; } ret = ret.trim(); return ret; } - public static String stringPairListToString(ArrayList> opts) { - String ret = ""; - for(Pair el : opts) { - ret += el.first + "=" + el.second + "\n"; - }; + public static String stringPairListToString(ArrayList> spl) { + String ret = "Invalid List: null object\n"; + if (spl != null) { + ret = ""; + for (Pair el : spl) { + String item = "Invalid StringPair: null object\n"; + if (el != null) { + item = ""; + String k = "Invalid String: null object"; + String v = "Invalid String: null object"; + if (el.first != null) { + k = el.first; + } + if (el.second != null) { + v = el.second; + } + + String delimBegin = "'"; + String delimEnd = "'"; + String indent = ""; + if (v.contains("\n") || (v.length() > 200)) { + delimBegin = " {\n"; + delimEnd = "\n}"; + delimEnd = String.format("%-100s", delimEnd); + indent = String.format("%4s", " "); + v = v.replace("\n", "\n" + indent); + } + + item = k + " = " + delimBegin + indent + v + delimEnd + "\n"; + } + ret += item; + } + } ret = ret.trim(); return ret; } - public static String blobListToString(Vector attachments) { + public static String blobListToString(Vector attachments, boolean full) { String ret = ""; ret += "Attachments count: " + attachments.size() + "\n"; - for( Blob a: attachments) { + for (Blob a : attachments) { ret += "-----BEGIN Attachment index: " + attachments.indexOf(a) + "-----\n"; - ret += a.toString(); + String tmp = a.toString(); + if (!full) { + tmp = clipString(tmp, 250); + } + ret += tmp + "\n"; ret += "-----END Attachment index: " + attachments.indexOf(a) + "-----\n"; } ret = ret.trim(); - return ret; + return ret; } + public static String clipString(String str, int len) { + String ret = str; + if (str.length() > len) { + ret = str.substring(0, len); + ret += " clipped... "; + } + return ret; + } + + public static ArrayList> clipStrings(ArrayList> spv, int len) { + for (Pair p : spv) { + if(p != null) { + if(p.first != null) { + p.first = clipString(p.first, len); + } + if(p.second != null) { + p.second = clipString(p.second, len); + } + } + } + return spv; + } // ------------------------ Logging ------------------------ private static boolean logEnabled = true; @@ -217,7 +300,7 @@ public class TestUtils { } public static void log(String msg) { - if(logEnabled) { + if (logEnabled) { String threadNameFmt = String.format("%-10s", Thread.currentThread().getName()); String msgOut = threadNameFmt + ": " + msg; System.out.println(msgOut); @@ -225,11 +308,11 @@ public class TestUtils { } public static void logH1(String msg) { - log( getDecoratedString(msg, "=")); + log(getDecoratedString(msg, "=")); } public static void logH2(String msg) { - log( getDecoratedString(msg, "-")); + log(getDecoratedString(msg, "-")); } private static String getDecoratedString(String msg, String s) { From 2e6b1ebccd8f7a196d849c96d3111abd110a9019 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 20 May 2020 00:27:13 +0200 Subject: [PATCH 7/8] X-pEp-Version = '2.1' <- using getProtocolVersion() --- src/foundation/pEp/jniadapter/AbstractEngine.java | 4 ++-- test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/foundation/pEp/jniadapter/AbstractEngine.java b/src/foundation/pEp/jniadapter/AbstractEngine.java index 0617e69..f492ccb 100644 --- a/src/foundation/pEp/jniadapter/AbstractEngine.java +++ b/src/foundation/pEp/jniadapter/AbstractEngine.java @@ -195,7 +195,7 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AutoClosea return 0; } - public static Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) { + public Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) { Message msg = new Message(); msg.setDir(Message.Direction.Incoming); msg.setEncFormat(encFormat); @@ -204,7 +204,7 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AutoClosea ArrayList> opts = new ArrayList<>(); Pair xpEp = new Pair<>(); xpEp.first = "X-pEp-Version"; - xpEp.second = "2.1"; + xpEp.second = this.getProtocolVersion();; opts.add(xpEp); msg.setOptFields(opts); diff --git a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java index 7fac43a..129b6a0 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java @@ -126,9 +126,8 @@ class TestMain { // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 logH2("incomingMessageFromPGPText()"); - Message msg2 = Engine.incomingMessageFromPGPText(pgpText, Message.EncFormat.PEP); + Message msg2 = env.engine.incomingMessageFromPGPText(pgpText, Message.EncFormat.PEP); log("\n" + msgToString(msg2, false)); - logH2("Verify msg2"); Engine.decrypt_message_Return result = null; result = env.engine.decrypt_message(msg2, env.vStr, 0); @@ -147,7 +146,7 @@ class TestMain { // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 logH2("incomingMessageFromPGPText()"); - Message msg2 = Engine.incomingMessageFromPGPText(pgpText, ef); + Message msg2 = env.engine.incomingMessageFromPGPText(pgpText, ef); log("\n" + msgToString(msg2, false)); // Cant be just simply decrypted again From 554a2c9c6860b4c597b50e75d46b8c999591358b Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 20 May 2020 14:37:01 +0200 Subject: [PATCH 8/8] Update test to use the test framework --- .../pEp/jniadapter/test/jni98/TestMain.java | 111 ++---------------- 1 file changed, 12 insertions(+), 99 deletions(-) diff --git a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java index 283b101..8883a33 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java @@ -1,14 +1,13 @@ package foundation.pEp.jniadapter.test.jni98; -import static foundation.pEp.jniadapter.test.framework.TestLogger.*; -import static foundation.pEp.jniadapter.test.utils.AdapterTestUtils.*; -import foundation.pEp.jniadapter.*; +import foundation.pEp.jniadapter.Engine; +import foundation.pEp.jniadapter.Message; +import foundation.pEp.jniadapter.test.framework.TestUnit; +import foundation.pEp.jniadapter.test.utils.AdapterBaseTestContext; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Vector; -import java.util.function.Consumer; +import static foundation.pEp.jniadapter.test.framework.TestLogger.log; +import static foundation.pEp.jniadapter.test.framework.TestLogger.logH2; +import static foundation.pEp.jniadapter.test.utils.AdapterTestUtils.msgToString; /* JNI-98 - "Factory function for generating incoming message from PGP text" @@ -21,121 +20,35 @@ The signature is expected to be: public static Message incomingMessageFromPGPText(String pgpText, Message.EncFormat encFormat) -Solution: -First, we need to know how the Message Object coming out from encrypt_message() are looking. -Then, we try to get the same Message from "incomingMessageFromPGPText()" - */ - -/* -The one person we know is called bob and his pubkey is like this -bob.address = bob@peptest.org -bob.FPR = F804FBE1781F3E2F6158F9F709FB5BDA72BE51C1 +Please see https://pep.foundation/jira/browse/JNI-98 for further discussion */ -class TestEnv { - public Sync.DefaultCallback cb = new Sync.DefaultCallback(); - public Identity alice = new Identity(); - public Identity bob = new Identity(); - public Message msgToSelf; - public Message msgToBob; - public Vector vID = new Vector(); - public Vector vStr = new Vector(); - public byte[] keyBobPub; - private String fileNameKeyBobPub = "../resources/test_keys/bob-pub.asc"; - public Engine engine = new Engine(); - - public TestEnv() throws Exception { - // We are alice - alice.address = "alice@peptest.org"; - alice.user_id = "23"; - alice.me = true; - alice = engine.myself(alice); - - // We know Bob and his pubkey - try { - Path path = Paths.get(fileNameKeyBobPub); - keyBobPub = Files.readAllBytes(path); - } catch (Exception e) { - log("Could not open key file:" + fileNameKeyBobPub); - throw e; - } - engine.importKey(keyBobPub); - bob.address = "bob@peptest.org"; - - msgToSelf = makeNewMessage(alice, alice, Message.Direction.Outgoing); - msgToBob = makeNewMessage(alice, bob, Message.Direction.Outgoing); - - vID.add(bob); - vStr.add(""); - } - - public static Message makeNewMessage(Identity from, Identity to, Message.Direction dir) { - Message msg = new Message(); - Vector vID = new Vector(); - vID.add(to); - - msg.setFrom(from); - msg.setTo(vID); - msg.setDir(dir); - msg.setLongmsg("Hi i am the message longmsg"); - return msg; - } -} - -class TestUnit { - TestEnv env; - String testUnitName = "default test unit"; - Consumer lambda; - - public TestUnit(String name, Consumer consumer) throws Exception { - testUnitName = name; - lambda = consumer; - env = new TestEnv(); - - } - - public void run() { - logH1(testUnitName); - try { - lambda.accept(env); - } catch (Throwable e) { - logH1("TestUnit FAILED: " + e.toString()); - return; - } - logH2("SUCCESS!"); - } -} class TestMain { public static void main(String[] args) throws Exception { - testRunNew(); - } - - public static void testRunNew() throws Exception { - new TestUnit("JNI-98 - Message.EncFormat.PEP", env -> { + new TestUnit("JNI-98 - Message.EncFormat.PEP", new AdapterBaseTestContext(), env -> { // Make msg1 by encrypting msgToBob logH2("Create target Message"); Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEP); log("\n" + msgToString(msg1, false)); - log("EncPep:" + Message.EncFormat.PEP.value); // Lets get the pgpText of the msg1, and the EncFormat String pgpText = Engine.toUTF16(msg1.getAttachments().elementAt(1).data); Message.EncFormat ef = msg1.getEncFormat(); - log("EncPepReturn:" + ef.value); - //TODO: setting encformat to 4 (PEP) getting back 3 (PGPMIME) + //TODO: setting encformat to 4 (PEP) but getting back 3 (PGPMIME) // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 logH2("incomingMessageFromPGPText()"); Message msg2 = env.engine.incomingMessageFromPGPText(pgpText, Message.EncFormat.PEP); log("\n" + msgToString(msg2, false)); + logH2("Verify msg2"); Engine.decrypt_message_Return result = null; result = env.engine.decrypt_message(msg2, env.vStr, 0); log("\n" + msgToString(result.dst, false)); }).run(); - new TestUnit("JNI-98 - Message.EncFormat.PEP_enc_inline_EA", env -> { + new TestUnit("JNI-98 - Message.EncFormat.PEP_enc_inline_EA", new AdapterBaseTestContext(), env -> { // Make msg1 by encrypting msgToBob logH2("Create target Message"); Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEPEncInlineEA);