def test_option_normal(self): self.rs.startProtocol() self.rs.datagramReceived( OACKDatagram({ b'blksize': b'9' }).to_wire(), ('127.0.0.1', 65465)) self.clock.advance(0.1) self.assertEqual(self.rs.session.block_size, 9) self.clock.pump((1, ) * 3) self.assertEqual(self.transport.value(), DATADatagram(1, self.test_data[:9]).to_wire()) self.rs.datagramReceived( OACKDatagram({ b'blksize': b'12' }).to_wire(), ('127.0.0.1', 65465)) self.clock.advance(0.1) self.assertEqual(self.rs.session.block_size, 9) self.transport.clear() self.rs.datagramReceived( ACKDatagram(1).to_wire(), ('127.0.0.1', 65465)) self.clock.pump((1, ) * 3) self.assertEqual(self.transport.value(), DATADatagram(2, self.test_data[9:18]).to_wire()) self.addCleanup(self.rs.cancel)
def test_option_normal(self): self.ws.startProtocol() self.ws.datagramReceived(OACKDatagram({'blksize':'12'}).to_wire(), ('127.0.0.1', 65465)) self.clock.advance(0.1) self.assertEqual(self.ws.session.block_size, WriteSession.block_size) self.assertEqual(self.transport.value(), ACKDatagram(0).to_wire()) self.transport.clear() self.ws.datagramReceived(OACKDatagram({'blksize':'9'}).to_wire(), ('127.0.0.1', 65465)) self.clock.advance(0.1) self.assertEqual(self.ws.session.block_size, WriteSession.block_size) self.assertEqual(self.transport.value(), ACKDatagram(0).to_wire()) self.transport.clear() self.ws.datagramReceived(DATADatagram(1, 'foobarbaz').to_wire(), ('127.0.0.1', 65465)) self.clock.advance(3) self.failUnless(self.ws.session.started) self.clock.advance(0.1) self.assertEqual(self.ws.session.block_size, 9) self.assertEqual(self.transport.value(), ACKDatagram(1).to_wire()) self.transport.clear() self.ws.datagramReceived(DATADatagram(2, 'asdfghjkl').to_wire(), ('127.0.0.1', 65465)) self.clock.advance(3) self.assertEqual(self.transport.value(), ACKDatagram(2).to_wire()) self.writer.finish() self.assertEqual(self.writer.file_path.open('r').read(), 'foobarbazasdfghjkl') self.transport.clear() self.ws.datagramReceived(OACKDatagram({'blksize':'12'}).to_wire(), ('127.0.0.1', 65465)) self.clock.advance(0.1) self.assertEqual(self.ws.session.block_size, 9) self.assertEqual(self.transport.value(), ACKDatagram(0).to_wire())
def test_option_normal(self): self.ws.startProtocol() self.clock.advance(0.1) oack_datagram = OACKDatagram(self.options).to_wire() self.assertEqual(self.transport.value(), oack_datagram) self.clock.advance(3) self.assertEqual(self.transport.value(), oack_datagram * 2) self.transport.clear() self.ws.datagramReceived( DATADatagram(1, b'foobarbaz').to_wire(), ('127.0.0.1', 65465)) self.clock.pump((1, ) * 3) self.assertEqual(self.transport.value(), ACKDatagram(1).to_wire()) self.assertEqual(self.ws.session.block_size, 9) self.transport.clear() self.ws.datagramReceived( DATADatagram(2, b'smthng').to_wire(), ('127.0.0.1', 65465)) self.clock.pump((1, ) * 3) self.assertEqual(self.transport.value(), ACKDatagram(2).to_wire()) self.clock.pump((1, ) * 10) self.writer.finish() self.assertEqual( self.writer.file_path.open('r').read(), b'foobarbazsmthng') self.assertTrue(self.transport.disconnecting)
def test_oack(self): # Zero options (I don't know if it is ok, the standard doesn't say anything) dgram = OACKDatagram.from_wire('') self.assertEqual(dgram.to_wire(), '\x00\x06') # One option, terminated dgram = OACKDatagram.from_wire('foo\x00bar\x00') self.assertEqual(dgram.options, {'foo':'bar'}) self.assertEqual(dgram.to_wire(), '\x00\x06foo\x00bar\x00') # Not terminated dgram = OACKDatagram.from_wire('foo\x00bar\x00baz\x00spam') self.assertEqual(dgram.options, {'foo':'bar', 'baz':'spam'}) self.assertEqual(dgram.to_wire(), '\x00\x06foo\x00bar\x00baz\x00spam\x00') # Option with no value self.assertRaises(OptionsDecodeError, OACKDatagram.from_wire, 'foo\x00bar\x00baz') # Duplicate option self.assertRaises(OptionsDecodeError, OACKDatagram.from_wire, 'baz\x00spam\x00one\x00two\x00baz\x00val\x00')
def test_oack(self): # Zero options (I don't know if it is ok, the standard doesn't say anything) dgram = OACKDatagram.from_wire(b'') self.assertEqual(dgram.to_wire(), b'\x00\x06') # One option, terminated dgram = OACKDatagram.from_wire(b'foo\x00bar\x00') self.assertEqual(dgram.options, {b'foo': b'bar'}) self.assertEqual(dgram.to_wire(), b'\x00\x06foo\x00bar\x00') # Not terminated dgram = OACKDatagram.from_wire(b'foo\x00bar\x00baz\x00spam') self.assertEqual(dgram.options, {b'foo': b'bar', b'baz': b'spam'}) self.assertEqual(dgram.to_wire(), b'\x00\x06foo\x00bar\x00baz\x00spam\x00') # Option with no value self.assertRaises(OptionsDecodeError, OACKDatagram.from_wire, b'foo\x00bar\x00baz') # Duplicate option self.assertRaises(OptionsDecodeError, OACKDatagram.from_wire, b'baz\x00spam\x00one\x00two\x00baz\x00val\x00')
def test_option_tsize(self): # A tsize option of 0 sent as part of a read session prompts a tsize # response with the actual size of the file. self.options['tsize'] = '0' self.rs.startProtocol() self.clock.advance(0.1) self.transport.clear() self.clock.advance(3) # The response contains the size of the test data. self.options['tsize'] = str(len(self.test_data)) oack_datagram = OACKDatagram(self.options).to_wire() self.assertEqual(self.transport.value(), oack_datagram)
def test_option_normal(self): self.rs.startProtocol() self.clock.advance(0.1) oack_datagram = OACKDatagram(self.options).to_wire() self.assertEqual(self.transport.value(), oack_datagram) self.clock.advance(3) self.assertEqual(self.transport.value(), oack_datagram * 2) self.transport.clear() self.rs.datagramReceived(ACKDatagram(0).to_wire(), ('127.0.0.1', 65465)) self.clock.pump((1,)*3) self.assertEqual(self.transport.value(), DATADatagram(1, self.test_data[:9]).to_wire()) self.addCleanup(self.rs.cancel)
def test_option_tsize(self): # A tsize option sent as part of a write session is recorded. self.ws.startProtocol() self.clock.advance(0.1) oack_datagram = OACKDatagram(self.options).to_wire() self.assertEqual(self.transport.value(), oack_datagram) self.failIf(self.transport.disconnecting) self.assertIsInstance(self.ws.session, WriteSession) # Options are not applied to the WriteSession until the first DATA # datagram is received, self.assertTrue(self.ws.session.tsize is None) self.ws.datagramReceived( DATADatagram(1, 'foobarbaz').to_wire(), ('127.0.0.1', 65465)) # The tsize option has been applied to the WriteSession. self.assertEqual(45, self.ws.session.tsize)
def startProtocol(self): """Connect the transport, respond with an initial ACK or OACK (depending on if we were initialized with options or not). """ self.transport.connect(*self.remote) if self.options: self.resultant_options = self.processOptions(self.options) bytes = OACKDatagram(self.resultant_options).to_wire() else: bytes = ACKDatagram(0).to_wire() self.timeout_watchdog = timedCaller(chain((0, ), self.timeout), partial(self.transport.write, bytes), self.timedOut, clock=self._clock)
def test_option_timeout(self): self.rs.startProtocol() self.clock.advance(0.1) oack_datagram = OACKDatagram(self.options).to_wire() self.assertEqual(self.transport.value(), oack_datagram) self.failIf(self.transport.disconnecting) self.clock.advance(3) self.assertEqual(self.transport.value(), oack_datagram * 2) self.failIf(self.transport.disconnecting) self.clock.advance(2) self.assertEqual(self.transport.value(), oack_datagram * 3) self.failIf(self.transport.disconnecting) self.clock.advance(2) self.assertEqual(self.transport.value(), oack_datagram * 3) self.failUnless(self.transport.disconnecting)
def startProtocol(self): """Connect the transport, respond with an initial ACK or OACK (depending on if we were initialized with options or not). """ self.transport.connect(*self.remote) if self.options: self.resultant_options = self.processOptions(self.options) bytes = OACKDatagram(self.resultant_options).to_wire() else: bytes = ACKDatagram(0).to_wire() self.timeout_watchdog = SequentialCall.run( self.timeout[:-1], callable=self.transport.write, callable_args=[bytes, ], on_timeout=lambda: self._clock.callLater(self.timeout[-1], self.timedOut), run_now=True, _clock=self._clock )
def startProtocol(self): """Start sending an OACK datagram if we were initialized with options or start the L{ReadSession} immediately. """ self.transport.connect(*self.remote) if self.options: self.resultant_options = self.processOptions(self.options) bytes = OACKDatagram(self.resultant_options).to_wire() self.timeout_watchdog = timedCaller(chain((0, ), self.timeout), partial( self.transport.write, bytes), self.timedOut, clock=self._clock) else: self.session.transport = self.transport self.session.startProtocol() return self.session.nextBlock()
def startProtocol(self): """Start sending an OACK datagram if we were initialized with options or start the L{ReadSession} immediately. """ self.transport.connect(*self.remote) if self.options: self.resultant_options = self.processOptions(self.options) bytes = OACKDatagram(self.resultant_options).to_wire() self.timeout_watchdog = SequentialCall.run( self.timeout[:-1], callable=self.transport.write, callable_args=[bytes, ], on_timeout=lambda: self._clock.callLater(self.timeout[-1], self.timedOut), run_now=True, _clock=self._clock ) else: self.session.transport = self.transport self.session.startProtocol() return self.session.nextBlock()