
3 changed files with 127 additions and 1 deletions
@ -0,0 +1,33 @@ |
|||||
|
""" |
||||
|
test for simplest keysync scenario : two device setting up sam account |
||||
|
|
||||
|
Launch it with something like : |
||||
|
DYLD_LIBRARY_PATH=/Users/ed/lib/ PYTHONPATH=`pwd`/../build/lib.macosx-10.11-x86_64-3.4 python3.4 sync_test.py |
||||
|
|
||||
|
""" |
||||
|
|
||||
|
import multipEp as mp |
||||
|
|
||||
|
def create_account(address, name): |
||||
|
i = mp.pEp.Identity(address, name) |
||||
|
mp.pEp.myself(i) |
||||
|
mp.own_addresses.append(address) |
||||
|
|
||||
|
# unused |
||||
|
def send_message(from_address, to_address): |
||||
|
m = mp.pEp.outgoing_message(Identity(from_address, from_address)) |
||||
|
m.to = [mp.pEp.Identity(to_address, to_address)] |
||||
|
m.shortmsg = "Hello" |
||||
|
m.longmsg = "Something\\n" |
||||
|
m.encrypt() |
||||
|
mp.sent_messages.append(str(m)) |
||||
|
|
||||
|
scenario0 = [ |
||||
|
#("instance name", ["func name", [args], {kwargs}]), |
||||
|
("A", [create_account, ["some.one@some.where", "Some One"]]), |
||||
|
("B", [create_account, ["some.one@some.where", "Some One"]]) |
||||
|
] |
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
mp.run_scenario(scenario0) |
||||
|
|
@ -0,0 +1,93 @@ |
|||||
|
import os |
||||
|
import multiprocessing |
||||
|
import importlib |
||||
|
import tempfile |
||||
|
from collections import OrderedDict |
||||
|
|
||||
|
# per-instance globals |
||||
|
pEp = None |
||||
|
handler = None |
||||
|
own_addresses = [] |
||||
|
sent_messages = [] |
||||
|
|
||||
|
def pEp_instance_run(iname, conn): |
||||
|
global pEp, handler, sent_messages |
||||
|
|
||||
|
pEp = importlib.import_module("pEp") |
||||
|
|
||||
|
hdr = "-" * (39 - int(len(iname)/2)) + " " + iname + " " + "-" * (39 - len(iname) + int(len(iname)/2)) |
||||
|
|
||||
|
class Handler(pEp.SyncMixIn): |
||||
|
def messageToSend(self, msg): |
||||
|
msgstr = str(msg) |
||||
|
print(hdr) |
||||
|
print(msgstr.replace("\r", "")) |
||||
|
print("-" * 80) |
||||
|
sent_messages.append(msgstr) |
||||
|
|
||||
|
def showHandshake(self, me, partner): |
||||
|
print(hdr) |
||||
|
print("handshake needed between " + repr(me) + " and " + repr(partner)) |
||||
|
print("-" * 80) |
||||
|
# TODO: accept or reject |
||||
|
|
||||
|
handler = Handler() |
||||
|
|
||||
|
while True: |
||||
|
order = conn.recv() |
||||
|
if order is None: |
||||
|
break |
||||
|
|
||||
|
command, new_msgs = order |
||||
|
|
||||
|
if new_msgs is not None: |
||||
|
for msgstr in new_msgs: |
||||
|
msg = pEp.incoming_message(msgstr) |
||||
|
for to in msg.to: |
||||
|
# check if mail is for an account owned by that instance |
||||
|
# checking if to.user_id == pEp.PEP_OWN_USERID |
||||
|
# could lead to false positive depending on what pEpEngine |
||||
|
# consider to be its own identity |
||||
|
if to.address in own_addresses: |
||||
|
decrypt_result = msg.decrypt() |
||||
|
break |
||||
|
|
||||
|
res = None |
||||
|
if command is not None: |
||||
|
func = command[0] |
||||
|
args, kwargs = command[1:] + [[], {}][len(command) - 1:] |
||||
|
print(func, args, kwargs) |
||||
|
res = func(*args,**kwargs) |
||||
|
print(func, args, kwargs, " -> ", res) |
||||
|
|
||||
|
conn.send([res, sent_messages]) |
||||
|
sent_messages = [] |
||||
|
|
||||
|
def pEp_instance_main(iname, conn): |
||||
|
# run with a dispensable $HOME to get fresh DB and PGP keyrings |
||||
|
with tempfile.TemporaryDirectory() as tmpdirname: |
||||
|
print(iname + " instance runs into " + tmpdirname) |
||||
|
os.environ['HOME'] = tmpdirname |
||||
|
pEp_instance_run(iname, conn) |
||||
|
print(iname + " exiting") |
||||
|
|
||||
|
def run_scenario(scenario): |
||||
|
instances = OrderedDict() |
||||
|
for iname, order in scenario: |
||||
|
if iname not in instances: |
||||
|
to_inst_msg = [] |
||||
|
conn, child_conn = multiprocessing.Pipe() |
||||
|
proc = multiprocessing.Process(target=pEp_instance_main, args=(iname,child_conn)) |
||||
|
proc.start() |
||||
|
instances[iname] = (proc, conn, to_inst_msg) |
||||
|
else: |
||||
|
proc, conn, to_inst_msg = instances[iname] |
||||
|
|
||||
|
conn.send([order, to_inst_msg]) |
||||
|
res, from_inst_msgs = conn.recv() |
||||
|
|
||||
|
for iname, (proc, conn, to_inst_msg) in instances.items(): |
||||
|
# tell process to terminate |
||||
|
conn.send(None) |
||||
|
proc.join() |
||||
|
|
Loading…
Reference in new issue