def test_rq(self): # Only one field - not ok self.assertRaises(WireProtocolError, RQDatagram.from_wire, 'foobar') # Two fields - ok (unterminated, slight deviation from the spec) dgram = RQDatagram.from_wire('foo\x00bar') dgram.opcode = OP_RRQ self.assertEqual(dgram.to_wire(), '\x00\x01foo\x00bar\x00') # Two fields terminated is ok too RQDatagram.from_wire('foo\x00bar\x00') dgram.opcode = OP_RRQ self.assertEqual(dgram.to_wire(), '\x00\x01foo\x00bar\x00') # More than two fields is also ok (unterminated, slight deviation from the spec) dgram = RQDatagram.from_wire('foo\x00bar\x00baz\x00spam') self.assertEqual(dgram.options, {'baz':'spam'}) dgram.opcode = OP_WRQ self.assertEqual(dgram.to_wire(), '\x00\x02foo\x00bar\x00baz\x00spam\x00') # More than two fields is also ok (terminated) dgram = RQDatagram.from_wire('foo\x00bar\x00baz\x00spam\x00one\x00two\x00') self.assertEqual(dgram.options, {'baz':'spam', 'one':'two'}) dgram.opcode = OP_RRQ self.assertEqual(dgram.to_wire(), '\x00\x01foo\x00bar\x00baz\x00spam\x00one\x00two\x00') # Option with no value - not ok self.assertRaises(OptionsDecodeError, RQDatagram.from_wire, 'foo\x00bar\x00baz\x00spam\x00one\x00') # Duplicate option - not ok self.assertRaises(OptionsDecodeError, RQDatagram.from_wire, 'foo\x00bar\x00baz\x00spam\x00one\x00two\x00baz\x00val\x00')
def test_rq(self): # Only one field - not ok self.assertRaises(WireProtocolError, RQDatagram.from_wire, b'foobar') # Two fields - ok (unterminated, slight deviation from the spec) dgram = RQDatagram.from_wire(b'foo\x00bar') dgram.opcode = OP_RRQ self.assertEqual(dgram.to_wire(), b'\x00\x01foo\x00bar\x00') # Two fields terminated is ok too RQDatagram.from_wire(b'foo\x00bar\x00') dgram.opcode = OP_RRQ self.assertEqual(dgram.to_wire(), b'\x00\x01foo\x00bar\x00') # More than two fields is also ok (unterminated, slight deviation from the spec) dgram = RQDatagram.from_wire(b'foo\x00bar\x00baz\x00spam') self.assertEqual(dgram.options, {b'baz': b'spam'}) dgram.opcode = OP_WRQ self.assertEqual(dgram.to_wire(), b'\x00\x02foo\x00bar\x00baz\x00spam\x00') # More than two fields is also ok (terminated) dgram = RQDatagram.from_wire( b'foo\x00bar\x00baz\x00spam\x00one\x00two\x00') self.assertEqual(dgram.options, {b'baz': b'spam', b'one': b'two'}) dgram.opcode = OP_RRQ self.assertEqual( dgram.to_wire(), b'\x00\x01foo\x00bar\x00baz\x00spam\x00one\x00two\x00') # Option with no value - not ok self.assertRaises(OptionsDecodeError, RQDatagram.from_wire, b'foo\x00bar\x00baz\x00spam\x00one\x00') # Duplicate option - not ok self.assertRaises( OptionsDecodeError, RQDatagram.from_wire, b'foo\x00bar\x00baz\x00spam\x00one\x00two\x00baz\x00val\x00')
def test_wb_start_session(self): prometheus_metrics = create_metrics( METRICS_DEFINITIONS, registry=prometheus_client.CollectorRegistry()) stream_session = FakeStreamSession() session = FakeSession(stream_session) tftp_mock = self.patch(tftp.protocol.TFTP, '_startSession') tftp_mock.return_value = succeed(session) tracking_tftp = TransferTimeTrackingTFTP(sentinel.backend) datagram = RQDatagram(b'file.txt', b'octet', {}) result = yield tracking_tftp._startSession( datagram, '192.168.1.1', 'read', prometheus_metrics=prometheus_metrics) result.session.cancel() metrics = prometheus_metrics.generate_latest().decode('ascii') self.assertIs(result, session) self.assertTrue(stream_session.cancelled) self.assertIn( 'maas_tftp_file_transfer_latency_count{filename="file.txt"} 1.0', metrics)
def clean_filename(self, path): datagram = RQDatagram(path, b"octet", {}) tftp = TransferTimeTrackingTFTP(sentinel.backend) return tftp._clean_filename(datagram)