From 53657d764a98a19106bb55548cd09ef1cef9728a Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 19 May 2020 23:31:01 +0200 Subject: [PATCH] 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) {