def testRestricted2ZStackCommunication(tdir, looper): """ Create 2 ZStack and make them send and receive messages. Both stacks allow communication only when keys are shared :return: """ names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) betaP = Printer(names[1]) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaP.print, restricted=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) alpha.send({'greetings': 'hi'}, beta.name) beta.send({'greetings': 'hello'}, alpha.name) looper.run(eventually(chkPrinted, alphaP, {'greetings': 'hello'})) looper.run(eventually(chkPrinted, betaP, {'greetings': 'hi'}))
def testMessageQuota(tdir, looper): names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) receivedMessages = [] betaMshHandler = makeHandler(receivedMessages) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaMshHandler, restricted=True, onlyListener=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) messages = [] numMessages = 100 * beta.listenerQuota for i in range(numMessages): msg = json.dumps({'random': randomSeed().decode()}).encode() messages.append(json.loads(msg.decode())) alpha.send(msg, beta.name) looper.runFor(2) assert messages == receivedMessages
def test_stashing_unknown_remote_msgs(looper, tdir, tconf): names = ['Alpha', 'Beta'] genKeys(tdir, names) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=None, restricted=False) beta_msg_handler = CounterMsgsHandler() beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=beta_msg_handler.handler, restricted=False) prepStacks(looper, alpha, beta, connect=False) assert not alpha.hasRemote(beta.name) assert not alpha.isConnectedTo(beta.name) assert not beta.hasRemote(alpha.name) assert not beta.isConnectedTo(alpha.name) alpha.connect(name=beta.name, ha=beta.ha, verKeyRaw=beta.verKeyRaw, publicKeyRaw=beta.publicKeyRaw) alpha.getRemote(beta.name, beta.ha).setConnected() assert alpha.hasRemote(beta.name) assert alpha.isConnectedTo(beta.name) assert not beta.hasRemote(alpha.name) assert not beta.isConnectedTo(alpha.name) def check_unknown_remote_msg(): assert len(alpha._stashed_to_disconnected) == 0 assert len(beta._stashed_unknown_remote_msgs) == len(sent_msgs) for index, item in enumerate(sent_msgs): assert item == beta._stashed_unknown_remote_msgs[index][0] assert alpha.remotes[ 'Beta'].socket.IDENTITY == beta._stashed_unknown_remote_msgs[ index][1] sent_msgs = deque(maxlen=tconf.ZMQ_STASH_UNKNOWN_REMOTE_MSGS_QUEUE_SIZE) msg = 'message num: {}' for i in range(5): _msg = msg.format(i) alpha.send(_msg) sent_msgs.append(_msg) looper.run( eventually(check_unknown_remote_msg, retryWait=1, timeout=60))
def testZStackSendRecvHugeDataUnderLimit(set_info_log_level, tdir, looper, tconf): names = ['Alpha', 'Beta'] genKeys(tdir, names) # we use json serializer now, so final form will be {'k':'vvv...vvv'} # so here we try to prepare exactly tconf.MSG_LEN_LIMIT bytes after serialization msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}"))} betaHandler = [False] def recvHandlerAlpha(wrpMsg): pass def recvHandlerBeta(wrpMsg): rmsg, frm = wrpMsg betaHandler[0] = True assert frm == 'Alpha' assert rmsg == msg alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerAlpha, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=None) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerBeta, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=None) assert len(alpha.serializeMsg(msg)) == tconf.MSG_LEN_LIMIT prepStacks(looper, *(alpha, beta), connect=True, useKeys=True) stat = alpha.send(msg, beta.name) assert stat[0] is True looper.runFor(5) assert betaHandler[0] is True
def testMessageQuota(tdir, looper): names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) betaMsgHandler = CollectingMsgsHandler() alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaMsgHandler.handler, restricted=True, onlyListener=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) messages = [] numMessages = 150 * beta.listenerQuota for i in range(numMessages): msg = json.dumps({'random': randomSeed().decode()}).encode() if alpha.send(msg, beta.name): messages.append(json.loads(msg.decode())) def checkAllReceived(): assert len(messages) == len(betaMsgHandler.receivedMessages) assert messages == betaMsgHandler.receivedMessages looper.run(eventually(checkAllReceived, retryWait=0.5, timeout=5))
def testMessageQuota(set_info_log_level, tdir, looper): names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) betaMsgHandler = CollectingMsgsHandler() alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaMsgHandler.handler, restricted=True, onlyListener=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) messages = [] numMessages = 150 * beta.listenerQuota for i in range(numMessages): msg = json.dumps({'random': randomSeed().decode()}).encode() if alpha.send(msg, beta.name): messages.append(json.loads(msg.decode())) def checkAllReceived(): assert len(messages) == len(betaMsgHandler.receivedMessages) assert messages == betaMsgHandler.receivedMessages looper.run(eventually(checkAllReceived, retryWait=0.5, timeout=5))
def testZStackSendRecvHugeDataUnderLimit(set_info_log_level, tdir, looper, tconf): names = ['Alpha', 'Beta'] genKeys(tdir, names) # we use json serializer now, so final form will be {'k':'vvv...vvv'} # so here we try to prepare exactly tconf.MSG_LEN_LIMIT bytes after serialization msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}"))} betaHandler = [False] def recvHandlerAlpha(wrpMsg): pass def recvHandlerBeta(wrpMsg): rmsg, frm = wrpMsg betaHandler[0] = True assert frm == 'Alpha' assert rmsg == msg alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerAlpha, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=None) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerBeta, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=None) assert len(alpha.serializeMsg(msg)) == tconf.MSG_LEN_LIMIT prepStacks(looper, *(alpha, beta), connect=True, useKeys=True) stat = alpha.send(msg, beta.name) assert stat[0] is True looper.runFor(5) assert betaHandler[0] is True
def test_no_size_limit_for_outbound_msgs(looper, tdir, msg): names = ['Alpha', 'Beta'] genKeys(tdir, names) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=None, restricted=False) alpha.msgLenVal.max_allowed = MSG_LEN_LIMIT beta_msg_handler = CounterMsgsHandler() beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=beta_msg_handler.handler, restricted=False) prepStacks(looper, alpha, beta) def check_received(value): assert beta_msg_handler.receivedMsgCount == value alpha.send(msg) looper.run(eventually(check_received, 1, retryWait=1, timeout=10))
def testUnrestricted2ZStackCommunication(tdir, looper, tconf): """ Create 2 ZStack and make them send and receive messages. Both stacks allow communication even when keys are not shared :return: """ names = ['Alpha', 'Beta'] alphaP = Printer(names[0]) betaP = Printer(names[1]) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=False, seed=randomSeed(), config=tconf) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaP.print, restricted=False, seed=randomSeed(), config=tconf) prepStacks(looper, alpha, beta, connect=True, useKeys=True) alpha.send({'greetings': 'hi'}, beta.name) beta.send({'greetings': 'hello'}, alpha.name) looper.run(eventually(chkPrinted, alphaP, {'greetings': 'hello'})) looper.run(eventually(chkPrinted, betaP, {'greetings': 'hi'}))
def testZStackSendHugeDataOverLimit(set_info_log_level, tdir, looper, tconf): names = ['Alpha', 'Beta'] genKeys(tdir, names) # we use json serializer now, so final form will be {'k':'vvv...vvv'} # so here we try to prepare exactly tconf.MSG_LEN_LIMIT + 1 bytes after serialization msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}") + 1)} betaHandlers = [False, False] def recvHandlerAlpha(wrpMsg): pass def recvHandlerBeta(wrpMsg): rmsg, frm = wrpMsg betaHandlers[0] = True assert frm is not None assert rmsg is not None def rejectHandlerBeta(reason, frm): betaHandlers[1] = True assert reason is not None assert frm is not None alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerAlpha, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=None) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerBeta, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=rejectHandlerBeta) assert len(alpha.serializeMsg(msg)) == (tconf.MSG_LEN_LIMIT + 1) prepStacks(looper, *(alpha, beta), connect=True, useKeys=True) stat = alpha.send(msg, beta.name) assert stat[0] is False assert 'exceeded allowed limit of {}'.format( tconf.MSG_LEN_LIMIT) in stat[1] looper.runFor(5) assert betaHandlers[0] is False assert betaHandlers[1] is False
def testZStackSendHugeDataOverLimit(set_info_log_level, tdir, looper, tconf): names = ['Alpha', 'Beta'] genKeys(tdir, names) # we use json serializer now, so final form will be {'k':'vvv...vvv'} # so here we try to prepare exactly tconf.MSG_LEN_LIMIT + 1 bytes after serialization msg = {'k': 'v' * (tconf.MSG_LEN_LIMIT - len("{'k':''}") + 1)} betaHandlers = [False, False] def recvHandlerAlpha(wrpMsg): pass def recvHandlerBeta(wrpMsg): rmsg, frm = wrpMsg betaHandlers[0] = True assert frm is not None assert rmsg is not None def rejectHandlerBeta(reason, frm): betaHandlers[1] = True assert reason is not None assert frm is not None alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerAlpha, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=None) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=recvHandlerBeta, restricted=True, config=adict(**tconf.__dict__), msgRejectHandler=rejectHandlerBeta) assert len(alpha.serializeMsg(msg)) == (tconf.MSG_LEN_LIMIT + 1) prepStacks(looper, *(alpha, beta), connect=True, useKeys=True) stat = alpha.send(msg, beta.name) assert stat[0] is False assert 'exceeded allowed limit of {}'.format( tconf.MSG_LEN_LIMIT) in stat[1] looper.runFor(5) assert betaHandlers[0] is False assert betaHandlers[1] is False