
8 changed files with 353 additions and 196 deletions
@ -0,0 +1,93 @@ |
|||
package foundation.pEp.jniadapter.test.templateAliceBob; |
|||
|
|||
import foundation.pEp.jniadapter.Blob; |
|||
import foundation.pEp.jniadapter.Identity; |
|||
import foundation.pEp.jniadapter.Message; |
|||
import foundation.pEp.jniadapter.test.utils.AdapterTestUtils; |
|||
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.FsMQMessage; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Vector; |
|||
|
|||
import static foundation.pEp.pitytest.TestLogger.log; |
|||
|
|||
public class Utils { |
|||
public static List<TransportMessage> encryptInlineEA(MultiPeerCTX ctx, Identity from, Identity to, String payloadPlain) throws RuntimeException { |
|||
List<TransportMessage> ret = new ArrayList<>(); |
|||
|
|||
// 1. put payload into .longmsg
|
|||
Message msgPlain = AdapterTestUtils.makeNewTestMessage(from, to, Message.Direction.Outgoing); |
|||
msgPlain.setLongmsg(payloadPlain); |
|||
msgPlain.setEncFormat(Message.EncFormat.PEPEncInlineEA); |
|||
log("MSG PLAIN: " + AdapterTestUtils.msgToString(msgPlain, false)); |
|||
|
|||
// 2. call encrypt_message() with enc_format = PEP_enc_inline_EA
|
|||
Message msgEnc = ctx.engine.encrypt_message(msgPlain, null, Message.EncFormat.PEPEncInlineEA); |
|||
Message msgTx = null; |
|||
if (msgEnc == null) { |
|||
log("UNENCRYPTED"); |
|||
msgTx = msgPlain; |
|||
} else { |
|||
log("ENCRYPTED"); |
|||
msgTx = msgEnc; |
|||
} |
|||
log("MSG AFTER ENCRYPT: \n" + AdapterTestUtils.msgToString(msgTx, false)); |
|||
|
|||
// 3. you’re getting back crypto text in .longmsg and possibly ASCII encoded crypto text in .value of attachments in .attachments; .enc_format is PEP_enc_inline_EA
|
|||
// 4. send one message with the crypto text of .longmsg
|
|||
TransportMessage tmp = new TransportMessage(msgTx); |
|||
tmp.setAttachments(new Vector<>()); |
|||
ret.add(new TransportMessage(tmp)); |
|||
|
|||
// 5. send messages for each attachment in .attachments with the crypto text of .value, respectively
|
|||
for (Blob b : msgTx.getAttachments()) { |
|||
if(b != null) { |
|||
tmp.setLongMessage(new String(b.data)); |
|||
ret.add(new TransportMessage(tmp)); |
|||
} else { |
|||
throw new RuntimeException("NULL ATTACHMENT"); |
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
public static String serializepEpMessage(Message msgTx) { |
|||
TransportMessage msgTransport = new TransportMessage(msgTx); |
|||
String msgTxSerialized = null; |
|||
try { |
|||
msgTxSerialized = msgTransport.serialize(); |
|||
} catch (IOException e) { |
|||
log("Exception while serializing: " + e.toString()); |
|||
} |
|||
return msgTxSerialized; |
|||
} |
|||
|
|||
public static Message deserializepEpMessageEA(MultiPeerCTX ctx, FsMQMessage msgRxSerialized) { |
|||
Message ret = null; |
|||
try { |
|||
TransportMessage msgTransportRx = TransportMessage.deserialize(msgRxSerialized.getMsg()); |
|||
ret = ctx.engine.incomingMessageFromPGPText(msgTransportRx.getLongMessage(), Message.EncFormat.PEPEncInlineEA); |
|||
// From
|
|||
Identity from = new Identity(); |
|||
from.address = msgTransportRx.getFromAddress(); |
|||
from = ctx.engine.updateIdentity(from); |
|||
ret.setFrom(from); |
|||
|
|||
// To
|
|||
Vector<Identity> toList = new Vector<>(); |
|||
for (String addr: msgTransportRx.getToAddresses()) { |
|||
Identity to = new Identity(); |
|||
to.address = addr; |
|||
to = ctx.engine.myself(to); |
|||
toList.add(to); |
|||
} |
|||
ret.setTo(toList); |
|||
|
|||
} catch (Exception e) { |
|||
log("Exception while deserializing: " + e.toString()); |
|||
} |
|||
return ret; |
|||
} |
|||
} |
@ -1,81 +0,0 @@ |
|||
package foundation.pEp.jniadapter.test.templateAliceBob; |
|||
|
|||
import foundation.pEp.jniadapter.Blob; |
|||
import foundation.pEp.jniadapter.Identity; |
|||
import foundation.pEp.jniadapter.Message; |
|||
|
|||
import java.io.*; |
|||
import java.util.ArrayList; |
|||
import java.util.Base64; |
|||
import java.util.List; |
|||
import java.util.Vector; |
|||
|
|||
public class pEpMessage implements Serializable { |
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
String fromAddress = null; |
|||
List<String> toAddresses = new ArrayList<String>(); |
|||
List<Blob> attachments = new ArrayList<Blob>(); |
|||
String shortMessage = null; |
|||
String longMessage = null; |
|||
|
|||
pEpMessage(Message msg) { |
|||
fromAddress = msg.getFrom().address; |
|||
for(Identity i : msg.getTo() ) { |
|||
toAddresses.add(i.address); |
|||
} |
|||
for(Blob b : msg.getAttachments()) { |
|||
attachments.add(b); |
|||
} |
|||
shortMessage = msg.getShortmsg(); |
|||
longMessage = msg.getLongmsg(); |
|||
} |
|||
|
|||
public Message toMessage() { |
|||
Message ret = new Message(); |
|||
Identity from = new Identity(); |
|||
from.address = fromAddress; |
|||
ret.setFrom(from); |
|||
Vector<Identity> toIdents = new Vector<>(); |
|||
for( String addr : toAddresses) { |
|||
Identity i = new Identity(); |
|||
i.address = addr; |
|||
toIdents.add(i); |
|||
} |
|||
ret.setTo(toIdents); |
|||
Vector<Blob> atts = new Vector<>(); |
|||
for(Blob b : attachments) { |
|||
atts.add(b); |
|||
} |
|||
ret.setAttachments(atts); |
|||
ret.setShortmsg(shortMessage); |
|||
ret.setLongmsg(longMessage); |
|||
return ret; |
|||
} |
|||
|
|||
public static pEpMessage deserialize(String serializedMsg) throws IOException, ClassNotFoundException { |
|||
pEpMessage ret = null; |
|||
byte[] data = Base64.getDecoder().decode(serializedMsg); |
|||
// byte[] data = serializedMsg.getBytes();
|
|||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data)); |
|||
Object obj = ois.readObject(); |
|||
ois.close(); |
|||
if (!(obj instanceof pEpMessage)) { |
|||
throw new ClassNotFoundException("Invalid serialized string"); |
|||
} else { |
|||
ret = (pEpMessage) obj; |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
public String serialize() throws IOException { |
|||
String ret = null; |
|||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|||
ObjectOutputStream oos = new ObjectOutputStream(baos); |
|||
oos.writeObject(this); |
|||
oos.close(); |
|||
ret = Base64.getEncoder().encodeToString(baos.toByteArray()); |
|||
// ret = baos.toString();
|
|||
return ret; |
|||
} |
|||
} |
@ -0,0 +1,183 @@ |
|||
package foundation.pEp.jniadapter.test.templateAliceBob; |
|||
|
|||
import foundation.pEp.jniadapter.Blob; |
|||
import foundation.pEp.jniadapter.Identity; |
|||
import foundation.pEp.jniadapter.Message; |
|||
import foundation.pEp.jniadapter.Pair; |
|||
import foundation.pEp.jniadapter.test.utils.AdapterTestUtils; |
|||
|
|||
import java.io.*; |
|||
import java.util.ArrayList; |
|||
import java.util.Base64; |
|||
import java.util.List; |
|||
import java.util.Vector; |
|||
|
|||
public class TransportMessage implements Serializable { |
|||
private static final long serialVersionUID = 1L; |
|||
private String fromAddress = null; |
|||
private List<String> toAddresses = new ArrayList<String>(); |
|||
private Vector<Blob> attachments = new Vector<>(); |
|||
private String shortMessage = null; |
|||
private String longMessage = null; |
|||
|
|||
TransportMessage() { |
|||
} |
|||
|
|||
TransportMessage(Message msg) { |
|||
fromAddress = msg.getFrom().address; |
|||
for (Identity i : msg.getTo()) { |
|||
toAddresses.add(i.address); |
|||
} |
|||
for (Blob b : msg.getAttachments()) { |
|||
attachments.add(b); |
|||
} |
|||
shortMessage = msg.getShortmsg(); |
|||
longMessage = msg.getLongmsg(); |
|||
} |
|||
|
|||
// Deep Copy
|
|||
TransportMessage(TransportMessage msg) { |
|||
fromAddress = msg.fromAddress; |
|||
toAddresses = new ArrayList<>(msg.toAddresses); |
|||
attachments = new Vector<>(msg.attachments); |
|||
shortMessage = msg.shortMessage; |
|||
longMessage = msg.longMessage; |
|||
} |
|||
|
|||
|
|||
public String getFromAddress() { |
|||
return fromAddress; |
|||
} |
|||
|
|||
public void setFromAddress(String fromAddress) { |
|||
this.fromAddress = fromAddress; |
|||
} |
|||
|
|||
public List<String> getToAddresses() { |
|||
return toAddresses; |
|||
} |
|||
|
|||
public void setToAddresses(List<String> toAddresses) { |
|||
this.toAddresses = toAddresses; |
|||
} |
|||
|
|||
public Vector<Blob> getAttachments() { |
|||
return attachments; |
|||
} |
|||
|
|||
public void setAttachments(Vector<Blob> attachments) { |
|||
this.attachments = attachments; |
|||
} |
|||
|
|||
public String getShortMessage() { |
|||
return shortMessage; |
|||
} |
|||
|
|||
public void setShortMessage(String shortMessage) { |
|||
this.shortMessage = shortMessage; |
|||
} |
|||
|
|||
public String getLongMessage() { |
|||
return longMessage; |
|||
} |
|||
|
|||
public void setLongMessage(String longMessage) { |
|||
this.longMessage = longMessage; |
|||
} |
|||
|
|||
public Message toMessage() { |
|||
Message ret = new Message(); |
|||
|
|||
// from
|
|||
Identity from = new Identity(); |
|||
from.address = fromAddress; |
|||
ret.setFrom(from); |
|||
|
|||
// to
|
|||
Vector<Identity> toIdents = new Vector<>(); |
|||
for (String addr : toAddresses) { |
|||
Identity i = new Identity(); |
|||
i.address = addr; |
|||
toIdents.add(i); |
|||
} |
|||
ret.setTo(toIdents); |
|||
|
|||
// attachments
|
|||
ret.setAttachments(new Vector<>(attachments)); |
|||
|
|||
// shortMessage
|
|||
if (shortMessage != null) { |
|||
ret.setShortmsg(shortMessage); |
|||
} |
|||
|
|||
// longMessage
|
|||
if (longMessage != null) { |
|||
ret.setLongmsg(longMessage); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
public String toString() { |
|||
String ret = ""; |
|||
ArrayList<Pair<String, String>> kvs = new ArrayList<>(); |
|||
String key = ""; |
|||
String value = ""; |
|||
boolean full = false; |
|||
|
|||
key = "from"; |
|||
value = fromAddress; |
|||
kvs.add(new Pair<>(key, value)); |
|||
|
|||
key = "to"; |
|||
value = toAddresses.toString(); |
|||
kvs.add(new Pair<>(key, value)); |
|||
|
|||
key = "shortMessage"; |
|||
value = shortMessage; |
|||
kvs.add(new Pair<>(key, value)); |
|||
|
|||
key = "longMessage"; |
|||
value = longMessage; |
|||
kvs.add(new Pair<>(key, value)); |
|||
|
|||
key = "getAttachments"; |
|||
value = AdapterTestUtils.blobListToString(attachments, full) + "\n"; |
|||
kvs.add(new Pair<>(key, value)); |
|||
|
|||
if (!full) { |
|||
kvs = AdapterTestUtils.clipStrings(kvs, 200, "clipped..."); |
|||
} |
|||
|
|||
ret = AdapterTestUtils.stringPairListToString(kvs); |
|||
ret = ret.trim(); |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
|
|||
public static TransportMessage deserialize(String serializedMsg) throws IOException, ClassNotFoundException { |
|||
TransportMessage ret = null; |
|||
byte[] data = Base64.getDecoder().decode(serializedMsg); |
|||
// byte[] data = serializedMsg.getBytes();
|
|||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data)); |
|||
Object obj = ois.readObject(); |
|||
ois.close(); |
|||
if (!(obj instanceof TransportMessage)) { |
|||
throw new ClassNotFoundException("Invalid serialized string"); |
|||
} else { |
|||
ret = (TransportMessage) obj; |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
public String serialize() throws IOException { |
|||
String ret = null; |
|||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|||
ObjectOutputStream oos = new ObjectOutputStream(baos); |
|||
oos.writeObject(this); |
|||
oos.close(); |
|||
ret = Base64.getEncoder().encodeToString(baos.toByteArray()); |
|||
// ret = baos.toString();
|
|||
return ret; |
|||
} |
|||
} |
Loading…
Reference in new issue