def testEndPointConnectDoesNothingWhileDisconnected(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) outgoingFrameBuffer[0] += [[0x42]] for i in range(0, 100): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(0 == len(userFrameHandler[0].payloads)); self.assertTrue(0 == len(userFrameHandler[1].payloads));
def testEndPointIsBornDisconnected(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) self.assertFalse(endPoint[0].isConnected())
def testEndPointConnectDoesWhatItSaysOnTheBox(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); for i in range(0, 100): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(endPoint[0].isConnected()); self.assertTrue(endPoint[1].isConnected());
def testFrameTransmitterTransmitsNothingByDefault(self): tmp = MockSink() sink = EscapingSink(tmp) transmitter = FrameTransmitter(sink) for i in range(0, 5): transmitter.schedule() sink.schedule() self.assertEqual([], tmp.items)
def testFrameTransmitterTransmitsAckFrameCorrectly(self): tmp = MockSink() sink = EscapingSink(tmp) transmitter = FrameTransmitter(sink) transmitter.transmit(FrameTransmitter.ACK() + 0x24) for i in range(0, 5): transmitter.schedule() sink.schedule() crc = crc_ccitt_update(0xFFFF, FrameTransmitter.ACK() + 0x24) self.assertEqual([EscapingSink.FLAG(), FrameTransmitter.ACK() + 0x24, crc >> 8, crc & 0xFF, EscapingSink.FLAG()], tmp.items)
def testFrameTransmitterBecomesReadyOnlyOnceTransmissionComplete(self): tmp = MockSink() sink = EscapingSink(tmp) transmitter = FrameTransmitter(sink) transmitter.transmit(FrameTransmitter.ACK() + 0x24) for i in range(0, 5): self.assertFalse(transmitter.isReady()) transmitter.schedule() sink.schedule() self.assertTrue(transmitter.isReady())
def testEndPointIgnoresFramesWithUnexpectedSequenceNumber(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] endPoint[0].connected.timeout = 0x0000000F endPoint[1].connected.timeout = 0x0000000F frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); self.assertTrue(0 == len(userFrameHandler[0].payloads)); self.assertTrue(0 == len(userFrameHandler[1].payloads)); mediumAB += [EscapingSource.FLAG()] crc = 0xFFFF mediumAB += [0x00 ^ 0x01] crc = crc_ccitt_update(crc, 0x00 ^ 0x01) mediumAB += [0x42] crc = crc_ccitt_update(crc, 0x42) mediumAB += [crc >> 8] mediumAB += [crc & 0xFF] mediumAB += [EscapingSource.FLAG()] while len(mediumAB) > 0: endPoint[1].schedule() self.assertTrue(len(userFrameHandler[1].payloads) == 0)
def testEscapingSinkTransmitsFlagOctetCorrectly(self): mockSink = MockSink() escapingSink = EscapingSink(mockSink) escapingSink.writeFlag() escapingSink.schedule() self.assertTrue(escapingSink.isReady()) self.assertEqual([EscapingSink.FLAG()], mockSink.items)
def testEndPointMovesOneHundredFramesInEachDirectionWithNoise(self): seed(1) mediumAB = deque() mediumBA = deque() writerA = MockSink(mediumAB) sinkA = NoisySink(writerA) writerB = MockSink(mediumBA) sinkB = NoisySink(writerB) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); frameCount = 100 for i in range(0, frameCount): outgoingFrameBuffer[0] += [[i]] outgoingFrameBuffer[1] += [[i]] while len(userFrameHandler[0].payloads) < frameCount or len(userFrameHandler[1].payloads) < frameCount: endPoint[0].schedule() endPoint[1].schedule() for i in range(0, frameCount): self.assertEqual([i], userFrameHandler[0].payloads[i]) self.assertEqual([i], userFrameHandler[1].payloads[i])
def __init__(self, tty, switchEventQueue): ''' Constructor ''' self.id = None self.log = logging.getLogger("EndPoint[{}]".format(self.id)) self.switchEventQueue = switchEventQueue escapingSink = EscapingSink(TtySink(tty)) escapingSource = EscapingSource(TtySource(tty)) self.outgoingFrameBuffer = deque() frameTransmitter = FrameTransmitter(escapingSink) frameReceiver = FrameReceiver(escapingSource) self.incomingFrameHandler = IncomingFrameHandler() self.endPoint = EndPoint(escapingSource, frameReceiver, self.incomingFrameHandler, self.outgoingFrameBuffer, frameTransmitter, escapingSink) frameReceiver.setFrameHandler(self.endPoint) self.incomingFrameHandler.frameNotifier.addObserver(self) self.pingPong = PingPong(self.outgoingFrameBuffer) self.incomingFrameHandler.frameNotifier.addObserver(self.pingPong) self.hardwareRuleManager = HardwareRuleManager( self.outgoingFrameBuffer) self.incomingFrameHandler.frameNotifier.addObserver( self.hardwareRuleManager)
def testEndPointMovesOneFrameInEachDirection(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); outgoingFrameBuffer[0] += [[0x42]] outgoingFrameBuffer[1] += [[0x43]] for i in range(0, 100): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(0 == len(outgoingFrameBuffer[0])) self.assertTrue(1 == len(userFrameHandler[1].payloads)) self.assertEqual([0x42], userFrameHandler[1].payloads[0]) self.assertTrue(0 == len(outgoingFrameBuffer[1])) self.assertTrue(1 == len(userFrameHandler[0].payloads)) self.assertEqual([0x43], userFrameHandler[0].payloads[0])
def testEscapingSinkTransmitsOctetCorrectly(self): mockSink = MockSink() escapingSink = EscapingSink(mockSink) escapingSink.write(0x42) escapingSink.schedule() self.assertTrue(escapingSink.isReady()) self.assertTrue([0x42] == mockSink.items)
def testEndPointMovesSeventyFrames(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); frameCount = 70; for i in range(0, frameCount) : outgoingFrameBuffer[0] += [[0x42 + i]]; for i in range(0, 1600): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(0 == len(outgoingFrameBuffer[0])) self.assertTrue(frameCount == len(userFrameHandler[1].payloads)) for i in range(0, frameCount): self.assertEqual([0x42 + i], userFrameHandler[1].payloads[i])
def testFrameTransmitterTransmitsUserFrameCorrectly(self): tmp = MockSink() sink = EscapingSink(tmp) transmitter = FrameTransmitter(sink) transmitter.transmit(0x00, [0x42]) for i in range(0, 6): transmitter.schedule() sink.schedule() crc = crc_ccitt_update(0xFFFF, 0x00) crc = crc_ccitt_update(crc, 0x42) self.assertEqual([EscapingSink.FLAG(), 0x00, 0x42, crc >> 8, crc & 0xFF, EscapingSink.FLAG()], tmp.items)
def testEscapingSinkIsBornReady(self): mockSink = MockSink() escapingSink = EscapingSink(mockSink) self.assertTrue(escapingSink.isReady())
def testEscapingSinkBecomesReadyAfterTransmitting(self): mockSink = MockSink() escapingSink = EscapingSink(mockSink) escapingSink.write(0x42) escapingSink.schedule() self.assertTrue(escapingSink.isReady())
def testEscapingSinkTransmitsEscapableOctetCorrectly(self): mockSink = MockSink() escapingSink = EscapingSink(mockSink) escapingSink.write(EscapingSink.FLAG()) escapingSink.schedule() escapingSink.write(EscapingSink.ESCAPE()) escapingSink.schedule() self.assertTrue(escapingSink.isReady()) self.assertEqual([ EscapingSink.ESCAPE(), EscapingSink.FLAG() ^ EscapingSink.MASK(), EscapingSink.ESCAPE(), EscapingSink.ESCAPE() ^ EscapingSink.MASK() ], mockSink.items)
def testFrameTransmitterIsBornReady(self): tmp = MockSink() sink = EscapingSink(tmp) transmitter = FrameTransmitter(sink) self.assertTrue(transmitter.isReady())
def testFrameTransmitterBecomesUnreadyWhenTransmitting(self): tmp = MockSink() sink = EscapingSink(tmp) transmitter = FrameTransmitter(sink) transmitter.transmit(FrameTransmitter.ACK() + 0x24) self.assertFalse(transmitter.isReady())
def testEscapingSinkBecomesUnreadyWhenTransmitting(self): mockSink = MockSink() escapingSink = EscapingSink(mockSink) escapingSink.write(0x42) self.assertFalse(escapingSink.isReady())