def test_read_short(self): for fixture in self.data: for l in xrange(len(fixture[0])-1): try: RTMPHeader.read(BufferedByteStream(fixture[0][0:l])) self.fail() except NeedBytes, (bytes,): self.failUnlessEqual(len(fixture[0])-l if l != 0 else 1, bytes)
def test_diff(self): self.failUnlessEqual(0, self.h1.diff(self.h1)) self.failUnlessEqual(0, self.h1.diff(self.h3)) self.failUnlessRaises(AssertionError, self.h1.diff, self.h2) self.failUnlessEqual(1, self.h1.diff(RTMPHeader(3, 444, 261, 0x14, 0))) self.failUnlessEqual(2, self.h1.diff(RTMPHeader(3, 1, 262, 0x14, 0))) self.failUnlessEqual(2, self.h1.diff(RTMPHeader(3, 1, 261, 0x15, 0))) self.failUnlessEqual(3, self.h1.diff(RTMPHeader(3, 1, 261, 0x14, 11)))
def disassemble(self): """ Disassemble L{buffer} into packets. Returns first decoded packet or None, if no packet could be decoded at the moment. @return: decoded packet @rtype: L{Packet} """ self.buffer.seek(0) while self.buffer.remaining() > 0: try: # try to parse header from stream header = RTMPHeader.read(self.buffer) except NeedBytes, (bytes, ): # not enough bytes, return what we've already parsed return None # fill header with extra data from previous headers received # with same object_id header.fill(self.lastHeaders.get(header.object_id, RTMPHeader())) # get buffer for data of this packet buf = self.pool.get(header.object_id, BufferedByteStream()) # this chunk size is minimum of regular chunk size in this # disassembler and what we have left here thisChunk = min(header.length - len(buf), self.chunkSize) if self.buffer.remaining() < thisChunk: # we have not enough bytes to read this chunk of data return None # we got complete chunk buf.write(self.buffer.read(thisChunk)) # store packet header for this object_id self.lastHeaders[header.object_id] = header # skip data left in input buffer self.buffer.consume() # this chunk completes full packet? if len(buf) < header.length: # no, store buffer for further chunks self.pool[header.object_id] = buf else: # parse packet from header and data buf.seek(0, 0) # delete stored data for this packet if header.object_id in self.pool: del self.pool[header.object_id] return self._decode_packet(header, buf)
class RTMPHeaderTestCase(unittest.TestCase): """ Test case for L{fmspy.rtmp.header.RTMPHeader}. """ data = [ ( "\x03\x00\x00\x01\x00\x01\x05\x14\x00\x00\x00\x00", RTMPHeader(3, 1, 261, 0x14, 0), None ), ( "\x02\x00\x00\x01\x00\x01\x05\x14\x00\x00\x00\x00", RTMPHeader(2, 1, 261, 0x14, 0), None ), ( "\xc3", RTMPHeader(3, None, None, None, None), RTMPHeader(3, 1, 261, 0x14, 0), ), ( "\x83\x00\x00\x01", RTMPHeader(3, 1, None, None, None), RTMPHeader(3, 2, 261, 0x14, 0), ), ( "\x43\x00\x00\x01\x00\x01\x05\x14", RTMPHeader(3, 1, 261, 0x14, None), RTMPHeader(3, 1, 261, 0x15, 0), ) ] def setUp(self): self.h1 = RTMPHeader(3, 1, 261, 0x14, 0) self.h2 = RTMPHeader(2, 1, 261, 0x14, 0) self.h3 = RTMPHeader(3, 1, 261, 0x14, 0) def test_eq(self): self.failUnlessEqual(self.h1, self.h3) self.failIfEqual(self.h1, self.h2) def test_repr(self): self.failUnlessEqual("<RTMPHeader(object_id=3, timestamp=1, length=261, type=0x14, stream_id=0)>", repr(self.h1)) def test_read(self): for fixture in self.data: self.failUnlessEqual(fixture[1], RTMPHeader.read(BufferedByteStream(fixture[0]))) def test_read_short(self): for fixture in self.data: for l in xrange(len(fixture[0])-1): try: RTMPHeader.read(BufferedByteStream(fixture[0][0:l])) self.fail() except NeedBytes, (bytes,): self.failUnlessEqual(len(fixture[0])-l if l != 0 else 1, bytes)
class BytesReadTestCase(unittest.TestCase): """ Test case for L{fmspy.rtmp.packets.BytesRead}. """ data = [ ( { 'header': RTMPHeader(object_id=2, timestamp=0, length=4, type=0x03, stream_id=0L), 'buf': BufferedByteStream('\x00\x00\x00\x89'), }, BytesRead(bytes=137, header=RTMPHeader(object_id=2, timestamp=0, length=4, type=0x03, stream_id=0L)), ), ] def test_eq(self): self.failUnlessEqual( BytesRead(bytes=5, header=RTMPHeader(object_id=3)), BytesRead(bytes=5, header=RTMPHeader(object_id=3))) self.failIfEqual(BytesRead(bytes=5, header=RTMPHeader(object_id=4)), BytesRead(bytes=5, header=RTMPHeader(object_id=3))) self.failIfEqual(BytesRead(bytes=6, header=RTMPHeader(object_id=3)), BytesRead(bytes=5, header=RTMPHeader(object_id=3))) def test_read(self): for fixture in self.data: fixture[0]['buf'].seek(0) self.failUnlessEqual(fixture[1], BytesRead.read(**fixture[0])) def test_write(self): for fixture in self.data: fixture[0]['buf'].seek(0) self.failUnlessEqual(fixture[0]['buf'].read(), fixture[1].write())
def disassemble(self): """ Disassemble L{buffer} into packets. Returns first decoded packet or None, if no packet could be decoded at the moment. @return: decoded packet @rtype: L{Packet} """ self.buffer.seek(0) while self.buffer.remaining() > 0: try: # try to parse header from stream header = RTMPHeader.read(self.buffer) except NeedBytes, (bytes,): # not enough bytes, return what we've already parsed return None # fill header with extra data from previous headers received # with same object_id header.fill(self.lastHeaders.get(header.object_id, RTMPHeader())) # get buffer for data of this packet buf = self.pool.get(header.object_id, BufferedByteStream()) # this chunk size is minimum of regular chunk size in this # disassembler and what we have left here thisChunk = min(header.length - len(buf), self.chunkSize) if self.buffer.remaining() < thisChunk: # we have not enough bytes to read this chunk of data return None # we got complete chunk buf.write(self.buffer.read(thisChunk)) # store packet header for this object_id self.lastHeaders[header.object_id] = header # skip data left in input buffer self.buffer.consume() # this chunk completes full packet? if len(buf) < header.length: # no, store buffer for further chunks self.pool[header.object_id] = buf else: # parse packet from header and data buf.seek(0, 0) # delete stored data for this packet if header.object_id in self.pool: del self.pool[header.object_id] return self._decode_packet(header, buf)
def test_eq(self): self.failUnlessEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='b', argv=(), id=35.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=('a'), id=35.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=(), id=36.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=4)))
def __init__(self, bytes, header=None): """ Construct BytesRead packet. @param bytes: number of bytes received so far @type bytes: C{int} @param header: packet header @type header: L{RTMPHeader} """ if header is None: header = RTMPHeader(constants.DEFAULT_BYTES_READ_OBJECT_ID, 0, 0, constants.BYTES_READ, 0) else: if header.type is None: header.type = constants.BYTES_READ if header.object_id is None: header.object_id = constants.DEFAULT_BYTES_READ_OBJECT_ID super(BytesRead, self).__init__(header) self.bytes = bytes
def test_eq(self): self.failUnlessEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=5, data=[3], header=RTMPHeader(object_id=3))) self.failIfEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=5, data=[3], header=RTMPHeader(object_id=4))) self.failIfEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=5, data=[3, 4], header=RTMPHeader(object_id=3))) self.failIfEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=6, data=[3], header=RTMPHeader(object_id=3)))
def test_eq(self): self.failUnlessEqual( BytesRead(bytes=5, header=RTMPHeader(object_id=3)), BytesRead(bytes=5, header=RTMPHeader(object_id=3))) self.failIfEqual(BytesRead(bytes=5, header=RTMPHeader(object_id=4)), BytesRead(bytes=5, header=RTMPHeader(object_id=3))) self.failIfEqual(BytesRead(bytes=6, header=RTMPHeader(object_id=3)), BytesRead(bytes=5, header=RTMPHeader(object_id=3)))
def __init__(self, event, data, header=None): """ Construct Ping packet. @param event: ping event (1 byte) @type event: C{int} @param data: ping data, 1..3 longs (4 bytes * 1..3) @type data: C{list(int)} @param header: packet header @type header: L{RTMPHeader} """ if header is None: header = RTMPHeader(constants.DEFAULT_PING_OBJECT_ID, 0, 0, constants.PING, 0) else: if header.type is None: header.type = constants.PING if header.object_id is None: header.object_id = constants.DEFAULT_PING_OBJECT_ID super(Ping, self).__init__(header) self.event = event self.data = data
def test_repr(self): self.failUnlessEqual( "<Invoke(name=u'destroy', argv=({'videoCodecs': 252},), id=33, header=<RTMPHeader(object_id=3, timestamp=0, length=0, type=0x14, stream_id=0L)>)>", repr( Invoke(name=u'destroy', argv=({ 'videoCodecs': 252 }, ), id=33, header=RTMPHeader(object_id=3, timestamp=0, length=0, type=0x14, stream_id=0L))))
def invoke(self, name, *args): """ Perform invoke on other side of connection. @param name: method being invoked @type name: C{str} @param args: arguments for the call @type args: C{list} """ packet = Invoke(name, (None, ) + args, self.nextInvokeId, RTMPHeader(timestamp=0, stream_id=0)) d = self.invokeReplies[self.nextInvokeId] = defer.Deferred() self.nextInvokeId += 1 self.pushPacket(packet) return d
def test_fill(self): f = RTMPHeader(6, 7, 8, 9, 10) h = RTMPHeader(1, 2, 3, 4, 5) h.fill(f) self.assertEqual(RTMPHeader(1, 2, 3, 4, 5), h) h = RTMPHeader(1, 2, 3, 4, None) h.fill(f) self.assertEqual(RTMPHeader(1, 2, 3, 4, 10), h) h = RTMPHeader(1, 2, 3, None, None) h.fill(f) self.assertEqual(RTMPHeader(1, 2, 3, 9, 10), h) h = RTMPHeader(1, 2, None, None, None) h.fill(f) self.assertEqual(RTMPHeader(1, 2, 8, 9, 10), h) h = RTMPHeader(1, None, None, None, None) h.fill(f) self.assertEqual(RTMPHeader(1, 7, 8, 9, 10), h) h = RTMPHeader(None, None, None, None, None) self.failUnlessRaises(AssertionError, h.fill, f) h = RTMPHeader(1, 2, 3, 4, None) f = RTMPHeader(6, 7, 8, 9, None) self.failUnlessRaises(AssertionError, h.fill, f)
def test_read(self): for fixture in self.data: self.failUnlessEqual(fixture[1], RTMPHeader.read(BufferedByteStream(fixture[0])))
def setUp(self): self.h1 = RTMPHeader(3, 1, 261, 0x14, 0) self.h2 = RTMPHeader(2, 1, 261, 0x14, 0) self.h3 = RTMPHeader(3, 1, 261, 0x14, 0)
class PingTestCase(unittest.TestCase): """ Test case for L{fmspy.rtmp.packets.Ping}. """ data = [ ( { 'header': RTMPHeader(object_id=2, timestamp=0, length=6, type=0x04, stream_id=0L), 'buf': BufferedByteStream('\x00\x06\x00\x00\x00\x89'), }, Ping(event=6, data=[137], header=RTMPHeader(object_id=2, timestamp=0, length=6, type=0x04, stream_id=0L)), ), ( { 'header': RTMPHeader(object_id=2, timestamp=0, length=10, type=0x04, stream_id=0L), 'buf': BufferedByteStream('\x00\x06\x00\x00\x00\x89\x00\x00\x00\x0e'), }, Ping(event=6, data=[137, 14], header=RTMPHeader(object_id=2, timestamp=0, length=10, type=0x04, stream_id=0L)), ), ( { 'header': RTMPHeader(object_id=2, timestamp=0, length=14, type=0x04, stream_id=0L), 'buf': BufferedByteStream( '\x00\x06\x00\x00\x00\x89\x00\x00\x00\x0e\x00\x00\x03y'), }, Ping(event=6, data=[137, 14, 889], header=RTMPHeader(object_id=2, timestamp=0, length=14, type=0x04, stream_id=0L)), ), ] def test_eq(self): self.failUnlessEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=5, data=[3], header=RTMPHeader(object_id=3))) self.failIfEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=5, data=[3], header=RTMPHeader(object_id=4))) self.failIfEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=5, data=[3, 4], header=RTMPHeader(object_id=3))) self.failIfEqual( Ping(event=5, data=[3], header=RTMPHeader(object_id=3)), Ping(event=6, data=[3], header=RTMPHeader(object_id=3))) def test_read(self): for fixture in self.data: fixture[0]['buf'].seek(0) self.failUnlessEqual(fixture[1], Ping.read(**fixture[0])) def test_write(self): for fixture in self.data: fixture[0]['buf'].seek(0) self.failUnlessEqual(fixture[0]['buf'].read(), fixture[1].write())
class InvokeTestCase(unittest.TestCase): """ Test case for L{fmspy.rtmp.packets.Invoke}. """ data = [ ({ 'header': RTMPHeader(object_id=3, timestamp=0, length=235, type=0x14, stream_id=0L), 'buf': BufferedByteStream( '\x02\x00\x07connect\x00?\xf0\x00\x00\x00\x00\x00\x00\x03\x00\x03app\x02\x00\x04echo\x00\x08flashVer\x02\x00\rLNX 10,0,20,7\x00\x06swfUrl\x06\x00\x05tcUrl\x02\x00\x15rtmp://localhost/echo\x00\x04fpad\x01\x00\x00\x0ccapabilities\x00@.\x00\x00\x00\x00\x00\x00\x00\x0baudioCodecs\x00@\xa8\xee\x00\x00\x00\x00\x00\x00\x0bvideoCodecs\x00@o\x80\x00\x00\x00\x00\x00\x00\rvideoFunction\x00?\xf0\x00\x00\x00\x00\x00\x00\x00\x07pageUrl\x06\x00\x0eobjectEncoding\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t' ), }, Invoke(name=u'connect', argv=({ 'videoCodecs': 252, 'audioCodecs': 3191, 'flashVer': u'LNX 10,0,20,7', 'app': u'echo', 'tcUrl': u'rtmp://localhost/echo', 'videoFunction': 1, 'capabilities': 15, 'pageUrl': pyamf.Undefined, 'fpad': False, 'swfUrl': pyamf.Undefined, 'objectEncoding': 0 }, ), id=1, header=RTMPHeader(object_id=3, timestamp=0, length=235, type=0x14, stream_id=0L)), False), ({ 'header': RTMPHeader(object_id=3, timestamp=0, length=0, type=0x14, stream_id=0L), 'buf': BufferedByteStream( '\x02\x00\x07destroy\x00@@\x80\x00\x00\x00\x00\x00\x03\x00\x0bvideoCodecs\x00@o\x80\x00\x00\x00\x00\x00\x00\x00\t' ), }, Invoke(name=u'destroy', argv=({ 'videoCodecs': 252 }, ), id=33, header=RTMPHeader(object_id=3, timestamp=0, length=0, type=0x14, stream_id=0L)), True), ] def test_eq(self): self.failUnlessEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='b', argv=(), id=35.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=('a'), id=35.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=(), id=36.0, header=RTMPHeader(object_id=3))) self.failIfEqual( Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=3)), Invoke(name='a', argv=(), id=35.0, header=RTMPHeader(object_id=4))) def test_repr(self): self.failUnlessEqual( "<Invoke(name=u'destroy', argv=({'videoCodecs': 252},), id=33, header=<RTMPHeader(object_id=3, timestamp=0, length=0, type=0x14, stream_id=0L)>)>", repr( Invoke(name=u'destroy', argv=({ 'videoCodecs': 252 }, ), id=33, header=RTMPHeader(object_id=3, timestamp=0, length=0, type=0x14, stream_id=0L)))) def test_read(self): for fixture in self.data: fixture[0]['buf'].seek(0) self.failUnlessEqual(fixture[1], Invoke.read(**fixture[0])) def test_write(self): for fixture in self.data: if not fixture[2]: continue fixture[0]['buf'].seek(0) self.failUnlessEqual(fixture[0]['buf'].read(), fixture[1].write())
def setUp(self): self.p1 = DataPacket(RTMPHeader(3, 1, 0, 0x14, 0), "aaaa") self.p2 = DataPacket(RTMPHeader(2, 1, 0, 0x14, 0), "dddddd") self.p3 = DataPacket(RTMPHeader(3, 1, 0, 0x14, 0), "aaaa")