Browse Source

Enhance Tests and TestUtils (Message pretty printing)

JNI-98
heck 5 years ago
parent
commit
53657d764a
  1. 3
      src/foundation/pEp/jniadapter/AbstractEngine.java
  2. 30
      test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java
  3. 259
      test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java

3
src/foundation/pEp/jniadapter/AbstractEngine.java

@ -233,9 +233,6 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AutoClosea
else if (encFormat == Message.EncFormat.PEPEncInlineEA) { else if (encFormat == Message.EncFormat.PEPEncInlineEA) {
// For EncFormat.PEPEncInlineEA // For EncFormat.PEPEncInlineEA
// The pgpText goes into the longMessage // 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.setShortmsg("");
msg.setLongmsg(pgpText); msg.setLongmsg(pgpText);
} }

30
test/java/foundation/pEp/jniadapter/test/jni98/TestMain.java

@ -115,7 +115,7 @@ class TestMain {
// Make msg1 by encrypting msgToBob // Make msg1 by encrypting msgToBob
logH2("Create target Message"); logH2("Create target Message");
Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEP); 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); log("EncPep:" + Message.EncFormat.PEP.value);
// Lets get the pgpText of the msg1, and the EncFormat // 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 // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1
logH2("incomingMessageFromPGPText()"); logH2("incomingMessageFromPGPText()");
Message msg2 = Engine.incomingMessageFromPGPText(pgpText, Message.EncFormat.PEP); Message msg2 = Engine.incomingMessageFromPGPText(pgpText, Message.EncFormat.PEP);
log(msgToString(msg2)); log("\n" + msgToString(msg2, false));
logH2("Verify msg2"); logH2("Verify msg2");
Engine.decrypt_message_Return result = null; Engine.decrypt_message_Return result = null;
result = env.engine.decrypt_message(msg2, env.vStr, 0); result = env.engine.decrypt_message(msg2, env.vStr, 0);
log(msgToString(result.dst)); log("\n" + msgToString(result.dst, false));
}).run(); }).run();
new TestUnit("JNI-98 - Message.EncFormat.PEP_enc_inline_EA", env -> { new TestUnit("JNI-98 - Message.EncFormat.PEP_enc_inline_EA", env -> {
// Make msg1 by encrypting msgToBob // Make msg1 by encrypting msgToBob
logH2("Create target Message"); logH2("Create target Message");
Message msg1 = env.engine.encrypt_message(env.msgToBob, null, Message.EncFormat.PEPEncInlineEA); 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 // Lets get the pgpText of the msg1, and the EncFormat
String pgpText = msg1.getLongmsg(); String pgpText = msg1.getLongmsg();
@ -148,12 +148,22 @@ class TestMain {
// Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1 // Create msg2 by using incomingMessageFromPGPText with the pgpText and EncFormat from msg1
logH2("incomingMessageFromPGPText()"); logH2("incomingMessageFromPGPText()");
Message msg2 = Engine.incomingMessageFromPGPText(pgpText, ef); Message msg2 = Engine.incomingMessageFromPGPText(pgpText, ef);
log(msgToString(msg2)); log("\n" + msgToString(msg2, false));
logH2("Verify msg2"); // Cant be just simply decrypted again
Engine.decrypt_message_Return result = null; // And thats correct according to fdik
result = env.engine.decrypt_message(msg2, env.vStr, 0); //[21:29] < heck> | Assertion failed: (value && size && mime_type && code && !code[0] && code_size), function decode_internal, file internal_format.c, line 113.
log(msgToString(result.dst)); //[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(); }).run();
} }
} }

259
test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java

@ -1,4 +1,5 @@
package foundation.pEp.jniadapter.test.utils; package foundation.pEp.jniadapter.test.utils;
import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.*;
import java.util.ArrayList; import java.util.ArrayList;
@ -17,7 +18,7 @@ public class TestUtils {
public static String identityToString(Identity i, Boolean full) { public static String identityToString(Identity i, Boolean full) {
String ret = ""; String ret = "";
if(full) { if (full) {
ret += "address: " + i.address + "\n"; ret += "address: " + i.address + "\n";
ret += "fpr: " + i.fpr + "\n"; ret += "fpr: " + i.fpr + "\n";
ret += "username: " + i.username + "\n"; ret += "username: " + i.username + "\n";
@ -33,141 +34,169 @@ public class TestUtils {
return ret; return ret;
} }
public static String msgToString(Message msg) { public static String msgToString(Message msg, boolean full) {
String ret = ""; String ret = "";
ret += "getAttachments: "; ArrayList<Pair<String, String>> kvs = new ArrayList<>();
String key = "";
String value = "";
key = "getAttachments";
try { try {
ret += blobListToString(msg.getAttachments()) + "\n"; value = blobListToString(msg.getAttachments(), full) + "\n";
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "Id: "; key = "Id";
try { try {
ret += msg.getId() + "\n"; value = msg.getId();
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getDir: "; key = "getDir";
try { try {
ret += msg.getDir().toString() + "\n"; value = msg.getDir().toString();
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getShortmsg: "; key = "getShortmsg";
try { try {
ret += msg.getShortmsg() + "\n"; value = msg.getShortmsg();
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getLongmsg: "; key = "getLongmsg";
try { try {
ret += msg.getLongmsg() + "\n"; value = msg.getLongmsg();
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getLongmsgFormatted: "; key = "getLongmsgFormatted";
try { try {
ret += msg.getLongmsgFormatted() + "\n"; value = msg.getLongmsgFormatted();
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getFrom: "; key = "getFrom";
try { try {
ret += identityToString(msg.getFrom(), false) + "\n"; value = identityToString(msg.getFrom(), full);
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getTo: "; key = "getTo";
try { try {
ret += identityListToString(msg.getTo(), false) + "\n"; value = identityListToString(msg.getTo(), full);
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getRecvBy: "; key = "getRecvBy";
try { try {
ret += identityToString(msg.getRecvBy(), false) + "\n"; value = identityToString(msg.getRecvBy(), full);
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getCc: "; key = "getCc";
try { try {
ret += identityListToString(msg.getCc(), false)+ "\n"; value = identityListToString(msg.getCc(), full) + "\n";
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getBcc: "; key = "getBcc";
try { try {
ret += identityListToString(msg.getBcc(), false) + "\n"; value = identityListToString(msg.getBcc(), full);
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getReplyTo: "; key = "getReplyTo";
try { try {
ret += identityListToString(msg.getReplyTo(), false) + "\n"; value = identityListToString(msg.getReplyTo(), full);
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getInReplyTo: "; key = "getInReplyTo";
try { try {
ret += stringVectorToString(msg.getInReplyTo()) + "\n"; value = stringVectorToString(msg.getInReplyTo());
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getReferences: "; key = "getReferences";
try { try {
ret += stringVectorToString(msg.getReferences()) + "\n"; value = stringVectorToString(msg.getReferences());
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getKeywords: "; key = "getKeywords";
try { try {
ret += stringVectorToString(msg.getKeywords()) + "\n"; value = stringVectorToString(msg.getKeywords());
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getComments: "; key = "getComments";
try { try {
ret += msg.getComments() + "\n"; value = msg.getComments();
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getOptFields: "; key = "getOptFields";
try { try {
ret += stringPairListToString(msg.getOptFields()) + "\n"; value = stringPairListToString(msg.getOptFields());
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
} }
kvs.add(new Pair<>(key, value));
ret += "getEncFormat: "; key = "getEncFormat";
try { try {
ret += msg.getEncFormat().toString() + "\n"; value = msg.getEncFormat().toString();
} catch(Throwable e) { } catch (Throwable e) {
ret += e.toString() + "\n"; value = e.toString();
}
kvs.add(new Pair<String,String>(key, value));
if (!full) {
kvs = clipStrings(kvs, 200);
} }
ret = stringPairListToString(kvs);
ret = ret.trim(); ret = ret.trim();
return ret; return ret;
} }
public static String stringVectorToString(Vector<String> vS) { public static String stringVectorToString(Vector<String> vS) {
String ret = ""; String ret = "";
for (String s : vS){ for (String s : vS) {
ret += s + "\n"; ret += s + "\n";
} }
ret = ret.trim(); ret = ret.trim();
@ -176,34 +205,88 @@ public class TestUtils {
public static String identityListToString(Vector<Identity> vI, Boolean full) { public static String identityListToString(Vector<Identity> vI, Boolean full) {
String ret = ""; String ret = "";
for(Identity i : vI) { for (Identity i : vI) {
ret += identityToString(i, full) + "\n"; ret += identityToString(i, full) + "\n";
} }
ret = ret.trim(); ret = ret.trim();
return ret; return ret;
} }
public static String stringPairListToString(ArrayList<Pair<String, String>> opts) { public static String stringPairListToString(ArrayList<Pair<String, String>> spl) {
String ret = ""; String ret = "Invalid List: null object\n";
for(Pair<String, String> el : opts) { if (spl != null) {
ret += el.first + "=" + el.second + "\n"; ret = "";
}; for (Pair<String, String> 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(); ret = ret.trim();
return ret; return ret;
} }
public static String blobListToString(Vector<Blob> attachments) { public static String blobListToString(Vector<Blob> attachments, boolean full) {
String ret = ""; String ret = "";
ret += "Attachments count: " + attachments.size() + "\n"; ret += "Attachments count: " + attachments.size() + "\n";
for( Blob a: attachments) { for (Blob a : attachments) {
ret += "-----BEGIN Attachment index: " + attachments.indexOf(a) + "-----\n"; 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 += "-----END Attachment index: " + attachments.indexOf(a) + "-----\n";
} }
ret = ret.trim(); 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<Pair<String,String>> clipStrings(ArrayList<Pair<String,String>> spv, int len) {
for (Pair<String,String> 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 ------------------------ // ------------------------ Logging ------------------------
private static boolean logEnabled = true; private static boolean logEnabled = true;
@ -217,7 +300,7 @@ public class TestUtils {
} }
public static void log(String msg) { public static void log(String msg) {
if(logEnabled) { if (logEnabled) {
String threadNameFmt = String.format("%-10s", Thread.currentThread().getName()); String threadNameFmt = String.format("%-10s", Thread.currentThread().getName());
String msgOut = threadNameFmt + ": " + msg; String msgOut = threadNameFmt + ": " + msg;
System.out.println(msgOut); System.out.println(msgOut);
@ -225,11 +308,11 @@ public class TestUtils {
} }
public static void logH1(String msg) { public static void logH1(String msg) {
log( getDecoratedString(msg, "=")); log(getDecoratedString(msg, "="));
} }
public static void logH2(String msg) { public static void logH2(String msg) {
log( getDecoratedString(msg, "-")); log(getDecoratedString(msg, "-"));
} }
private static String getDecoratedString(String msg, String s) { private static String getDecoratedString(String msg, String s) {

Loading…
Cancel
Save