
5 changed files with 310 additions and 15 deletions
@ -0,0 +1,91 @@ |
|||
package foundation.pEp.jniadapter.test.templateAliceBob; |
|||
|
|||
|
|||
import foundation.pEp.jniadapter.Engine; |
|||
import foundation.pEp.jniadapter.Identity; |
|||
import foundation.pEp.jniadapter.Message; |
|||
import foundation.pEp.jniadapter.test.utils.AdapterTestUtils; |
|||
import foundation.pEp.jniadapter.test.utils.TestCallbacks; |
|||
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.ctx.FsMQManagerTestContext; |
|||
|
|||
import java.nio.file.Files; |
|||
import java.nio.file.Path; |
|||
import java.nio.file.Paths; |
|||
import java.util.Vector; |
|||
|
|||
|
|||
class MultiPeerCTX extends FsMQManagerTestContext { |
|||
// Basic
|
|||
public Engine engine; |
|||
public TestCallbacks callbacks; |
|||
|
|||
// Identities
|
|||
public Identity alice; |
|||
public Identity bob; |
|||
public Identity carol; |
|||
|
|||
// Keys
|
|||
public byte[] keyBobSec; |
|||
private String filenameBobSec = "../resources/test_keys/bob-sec.asc"; |
|||
|
|||
public byte[] keyBobPub; |
|||
private String filenameBobPub = "../resources/test_keys/bob-pub.asc"; |
|||
|
|||
public byte[] keyAlicePub; |
|||
private String filenameAlicePub = "../resources/test_keys/alice-pub.asc"; |
|||
|
|||
public byte[] keyAliceSec; |
|||
private String filenameAliceSec = "../resources/test_keys/alice-sec.asc"; |
|||
|
|||
// Messages
|
|||
public Message msgToSelf; |
|||
public Message msgToBob; |
|||
|
|||
// Misc
|
|||
public Vector<Identity> vID; |
|||
public Vector<String> vStr; |
|||
|
|||
public MultiPeerCTX(String selfAddress) { |
|||
super(selfAddress); |
|||
} |
|||
|
|||
public void init() throws Throwable { |
|||
super.init(); |
|||
vID = new Vector<Identity>(); |
|||
vStr = new Vector<String>(); |
|||
|
|||
callbacks = new TestCallbacks(); |
|||
engine = new Engine(); |
|||
engine.setMessageToSendCallback(callbacks); |
|||
engine.setNotifyHandshakeCallback(callbacks); |
|||
|
|||
alice = new Identity(); |
|||
alice.address = "alice@peptest.org"; |
|||
alice.username = "alice"; |
|||
alice.user_id = "23"; |
|||
|
|||
bob = new Identity(); |
|||
bob.address = "bob@peptest.org"; |
|||
bob.username = "bob"; |
|||
bob.user_id = "42"; |
|||
|
|||
msgToSelf = AdapterTestUtils.makeNewTestMessage(alice, alice, Message.Direction.Outgoing); |
|||
msgToBob = AdapterTestUtils.makeNewTestMessage(alice, bob, Message.Direction.Outgoing); |
|||
|
|||
vID.add(bob); |
|||
vStr.add("StringItem"); |
|||
|
|||
Path path; |
|||
path = Paths.get(filenameBobPub); |
|||
keyBobPub = Files.readAllBytes(path); |
|||
|
|||
path = Paths.get(filenameBobSec); |
|||
keyBobSec = Files.readAllBytes(path); |
|||
|
|||
path = Paths.get(filenameAlicePub); |
|||
keyAlicePub = Files.readAllBytes(path); |
|||
|
|||
path = Paths.get(filenameAliceSec); |
|||
keyAliceSec = Files.readAllBytes(path); |
|||
} |
|||
} |
@ -0,0 +1,81 @@ |
|||
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; |
|||
} |
|||
} |
Loading…
Reference in new issue