def testEscapingSourceCanBecomeReadyAfterCallToNext(self):
     mock = MockSource(deque([0x42, 0x43]))
     escapingSource = EscapingSource(mock)
     escapingSource.schedule()
     escapingSource.next()
     self.assertTrue(escapingSource.isReady())
     self.assertTrue(escapingSource.read() == 0x43)
    def testFrameReceiverIgnoresFrameWithBadCrc(self):
        medium = deque()
        medium += [EscapingSource.FLAG()]
        crc = 0xFFFF
        medium += [0x00]
        crc = crc_ccitt_update(crc, 0x00)
        medium += [0x42]
        crc = crc_ccitt_update(crc, 0x42)
        crc ^= 0x01
        medium += [crc >> 8]
        medium += [crc & 0xFF]
        medium += [EscapingSource.FLAG()]

        source = EscapingSource(MockSource(medium))

        frameHandler = FrameHandler()
        frameReceiver = FrameReceiver(source)
        frameReceiver.setFrameHandler(frameHandler)

        source.schedule()
        while (source.isReady()):
            frameReceiver.schedule()
            source.schedule()

        self.assertFalse(frameHandler.hasFrame())
    def testFrameReceiverReceivesFrame(self):
        medium = deque()
        medium += [EscapingSource.FLAG()]
        crc = 0xFFFF
        medium += [0x00]
        crc = crc_ccitt_update(crc, 0x00)
        medium += [0x42]
        crc = crc_ccitt_update(crc, 0x42)
        medium += [crc >> 8]
        medium += [crc & 0xFF]
        medium += [EscapingSource.FLAG()]

        source = EscapingSource(MockSource(medium))

        frameHandler = FrameHandler()
        frameReceiver = FrameReceiver(source)
        frameReceiver.setFrameHandler(frameHandler)

        source.schedule()
        while (source.isReady()):
            frameReceiver.schedule()
            source.schedule()

        self.assertEqual(0x00, frameHandler.header)
        self.assertEqual([0x42], frameHandler.payload)
    def testFrameReceiverReceivesMultipleFrames(self):
        medium = deque()

        count = 7
        for i in range(0, count):
            medium += [EscapingSource.FLAG()]
            crc = 0xFFFF
            medium += [0x00 + i]
            crc = crc_ccitt_update(crc, 0x00 + i)
            medium += [i]
            crc = crc_ccitt_update(crc, i)
            medium += [crc >> 8]
            medium += [crc & 0xFF]
            medium += [EscapingSource.FLAG()]

        source = EscapingSource(MockSource(medium))

        frameHandler = FrameHandler()
        frameReceiver = FrameReceiver(source)
        frameReceiver.setFrameHandler(frameHandler)

        source.schedule()
        while (source.isReady()):
            frameReceiver.schedule()
            source.schedule()

        self.assertEqual(count, frameHandler.count)
 def testEscapingSourceReceivesRegularOctetCorrectlyAfterReceivingFlag(
         self):
     mock = MockSource(deque([EscapingSource.FLAG(), 0x42]))
     escapingSource = EscapingSource(mock)
     escapingSource.schedule()
     self.assertTrue(escapingSource.isFlag())
     escapingSource.next()
     escapingSource.schedule()
     self.assertFalse(escapingSource.isFlag())
     self.assertTrue(0x42 == escapingSource.read())
 def testEscapingSourceCanBecomeReadyAfterCallToNextWithEscapeSequence(
         self):
     mock = MockSource(
         deque(
             [0x42,
              EscapingSource.ESCAPE(), 0x43 ^ EscapingSource.MASK()]))
     escapingSource = EscapingSource(mock)
     escapingSource.schedule()
     escapingSource.next()
     self.assertTrue(escapingSource.isReady())
     self.assertTrue(escapingSource.read() == 0x43)
    def testFrameReceiverIgnoresEmptyFrames(self):
        medium = deque()
        medium += [EscapingSource.FLAG()]
        medium += [EscapingSource.FLAG()]

        source = EscapingSource(MockSource(medium))

        frameHandler = FrameHandler()
        frameReceiver = FrameReceiver(source)
        frameReceiver.setFrameHandler(frameHandler)

        source.schedule()
        while (source.isReady()):
            frameReceiver.schedule()
            source.schedule()

        self.assertFalse(frameHandler.hasFrame())
 def testEscapingSourceReceivesEscapedOctetCorrectly(self):
     mock = MockSource(
         deque([
             EscapingSource.ESCAPE(),
             EscapingSource.FLAG() ^ EscapingSource.MASK(),
             EscapingSource.ESCAPE(),
             EscapingSource.ESCAPE() ^ EscapingSource.MASK()
         ]))
     escapingSource = EscapingSource(mock)
     escapingSource.schedule()
     escapingSource.schedule()
     self.assertFalse(escapingSource.isFlag())
     self.assertTrue(EscapingSource.FLAG() == escapingSource.read())
     escapingSource.next()
     escapingSource.schedule()
     escapingSource.schedule()
     self.assertFalse(escapingSource.isFlag())
     self.assertTrue(EscapingSource.ESCAPE() == escapingSource.read())
    def testFrameReceiverRecognizesAckFrames(self):
        medium = deque()
        medium += [EscapingSource.FLAG()]
        crc = 0xFFFF
        medium += [FrameReceiver.ACK() + 0x24]
        crc = crc_ccitt_update(crc, FrameReceiver.ACK() + 0x24)
        medium += [crc >> 8]
        medium += [crc & 0xFF]
        medium += [EscapingSource.FLAG()]

        source = EscapingSource(MockSource(medium))

        frameHandler = FrameHandler()
        frameReceiver = FrameReceiver(source)
        frameReceiver.setFrameHandler(frameHandler)

        source.schedule()
        while (source.isReady()):
            frameReceiver.schedule()
            source.schedule()

        self.assertEqual(FrameReceiver.ACK() + 0x24, frameHandler.header)
        self.assertEqual([], frameHandler.payload)
 def testEscapingSourceReceivesFlagOctetCorrectly(self):
     mock = MockSource(deque([EscapingSource.FLAG()]))
     escapingSource = EscapingSource(mock)
     escapingSource.schedule()
     self.assertTrue(escapingSource.isReady())
     self.assertTrue(escapingSource.isFlag())
 def testEscapingSourceReceivesOctetCorrectly(self):
     mock = MockSource(deque([0x42]))
     escapingSource = EscapingSource(mock)
     escapingSource.schedule()
     self.assertFalse(escapingSource.isFlag())
     self.assertTrue(0x42 == escapingSource.read())
 def testEscapingSourceBecomesUnreadyAfterReceiving(self):
     mock = MockSource(deque([0x42]))
     escapingSource = EscapingSource(mock)
     escapingSource.schedule()
     escapingSource.next()
     self.assertFalse(escapingSource.isReady())
 def testEscapingSourceIsBornUnready(self):
     mock = MockSource(deque([0x42]))
     escapingSource = EscapingSource(mock)
     self.assertFalse(escapingSource.isReady())