def test_recv_plain(self): """Receive data from the socket""" self.cnx.sock = tests.DummySocket() # Receive a packet which is not 4 bytes long self.cnx.sock.add_packet('\01\01\01') self.assertRaises(errors.InterfaceError, self.cnx.recv_plain) # Receive the header of a packet, but nothing more self.cnx.sock.add_packet('\01\00\00\00') self.assertRaises(errors.InterfaceError, self.cnx.recv_plain) # Receive packets after a query, SELECT "Ham" exp = [ '\x01\x00\x00\x01\x01', '\x19\x00\x00\x02\x03\x64\x65\x66\x00\x00\x00\x03\x48\x61\x6d\x00'\ '\x0c\x21\x00\x09\x00\x00\x00\xfd\x01\x00\x1f\x00\x00', '\x05\x00\x00\x03\xfe\x00\x00\x02\x00', '\x04\x00\x00\x04\x03\x48\x61\x6d', '\x05\x00\x00\x05\xfe\x00\x00\x02\x00', ] self.cnx.sock.reset() self.cnx.sock.add_packets(exp) length_exp = len(exp) result = [] packet = self.cnx.recv_plain() while packet: result.append(packet) if length_exp == len(result): break packet = self.cnx.recv_plain() self.assertEqual(exp, result)
def test_recv_compressed(self): """Receive compressed data from the socket""" self.cnx.sock = tests.DummySocket() def get_address(): return 'dummy' self.cnx.get_address = get_address # Receive a packet which is not 7 bytes long self.cnx.sock.add_packet('\01\01\01\01\01\01') self.assertRaises(errors.InterfaceError, self.cnx.recv_compressed) # Receive the header of a packet, but nothing more self.cnx.sock.add_packet('\01\00\00\00\00\00\00') self.assertRaises(errors.InterfaceError, self.cnx.recv_compressed) # Socket fails to receive and produces an error self.cnx.sock.raise_socket_error() self.assertRaises(errors.OperationalError, self.cnx.recv_compressed) # Receive result of query SELECT REPEAT('a',1*1024*1024), 'XYZ' packets = ( "\x80\x00\x00\x01\x00\x40\x00\x78\x9c\xed\xcb\xbd\x0a\x81\x01" "\x14\xc7\xe1\xff\xeb\xa5\x28\x83\x4d\x26\x99\x7c\x44\x21\x37" "\x60\xb0\x4b\x06\x6c\x0a\xd7\xe3\x7a\x15\x79\xc9\xec\x0a\x9e" "\x67\x38\x9d\xd3\xaf\x53\x24\x45\x6d\x96\xd4\xca\xcb\xf5\x96" "\xa4\xbb\xdb\x6c\x37\xeb\xfd\x68\x78\x1e\x4e\x17\x93\xc5\x7c" "\xb9\xfa\x8e\x71\xda\x83\xaa\xde\xf3\x28\xd2\x4f\x7a\x49\xf9" "\x7b\x28\x0f\xc7\xd3\x27\xb6\xaa\xfd\xf9\x8d\x8d\xa4\xfe\xaa" "\xae\x34\xd3\x69\x3c\x93\xce\x19\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\xf8\xeb\x0d\xe7\xa5\x29\xb8", "\x05\x04\x00\x02\x68\xc0\x0f\x78\x9c\xed\xc1\x31\x01\x00\x00" "\x08\x03\xa0\xc3\x92\xea\xb7\xfe\x25\x8c\x60\x01\x20\x01" + "\x00" * 999 + "\xe0\x53\x3d\x7b\x0a\x29\x40\x7b" ) exp = [ "\x01\x00\x00\x01\x02", "\x2d\x00\x00\x02\x03\x64\x65\x66\x00\x00\x00\x17\x52\x45\x50" "\x45\x41\x54\x28\x27\x61\x27\x2c\x31\x2a\x31\x30\x32\x34\x2a" "\x31\x30\x32\x34\x29\x00\x0c\x21\x00\x00\x00\x90\x00\xfa\x01" "\x00\x1f\x00\x00", "\x19\x00\x00\x03\x03\x64\x65\x66\x00\x00\x00\x03\x58\x59\x5a" "\x00\x0c\x21\x00\x09\x00\x00\x00\xfd\x01\x00\x1f\x00\x00", "\x05\x00\x00\x04\xfe\x00\x00\x00\x00", "\x08\x00\x10\x05\xfd\x00\x00\x10" + "\x61" * 1 * 1024 * 1024 + "\x03\x58\x59\x5a" ] self.cnx.sock.reset() self.cnx.sock.add_packets(packets) length_exp = len(exp) packet = self.cnx.recv_compressed() counter = 0 while packet and counter < length_exp: self.assertEqual(exp[counter], packet) packet = self.cnx.recv_compressed() counter += 1
def test_send_compressed(self): """Send compressed data through the socket""" data = 'asddfasdfasdf' self.assertRaises(errors.OperationalError, self.cnx.send_compressed, data, 0) self.cnx.sock = tests.DummySocket() self.assertRaises(Exception, self.cnx.send_compressed, None, None) # Small packet data = ('\x03\x53\x45\x4c\x45\x43\x54\x20\x22\x61\x62\x63\x22', 1) exp = ['\x11\x00\x00\x00\x00\x00\x00\r\x00\x00\x01\x03SELECT "abc"'] try: self.cnx.send_compressed(*data) except Exception, e: self.fail("Failed sending pktnr %r: %s" % (d[1], e))
def test_send_compressed(self): """Send compressed data through the socket""" data = 'asddfasdfasdf' self.assertRaises(errors.OperationalError, self.cnx.send_compressed, data, 0) self.cnx.sock = tests.DummySocket() self.assertRaises(Exception, self.cnx.send_compressed, None, None) # Small packet data = ('\x03\x53\x45\x4c\x45\x43\x54\x20\x22\x61\x62\x63\x22', 1) exp = ['\x11\x00\x00\x00\x00\x00\x00\r\x00\x00\x01\x03SELECT "abc"'] try: self.cnx.send_compressed(*data) except errors.Error as err: self.fail("Failed sending pktnr {0}: {1}".format(data[1], err)) self.assertEqual(exp, self.cnx.sock._client_sends) self.cnx.sock.reset() # Slightly bigger packet (not getting compressed) data = ('\x03\x53\x45\x4c\x45\x43\x54\x20\x22\x61\x62\x63\x22', 1) exp = (24, '\x11\x00\x00\x00\x00\x00\x00\x0d\x00\x00\x01\x03' '\x53\x45\x4c\x45\x43\x54\x20\x22') try: self.cnx.send_compressed(*data) except errors.Error as err: self.fail("Failed sending pktnr {0}: {1}".format(data[1], err)) received = self.cnx.sock._client_sends[0] self.assertEqual(exp, (len(received), received[:20])) self.cnx.sock.reset() # Big packet data = ('\x03\x53\x45\x4c\x45\x43\x54\x20\x22' + '\x61' * (constants.MAX_PACKET_LENGTH + 1000) + '\x22', 2) exp = [ (63, '\x38\x00\x00\x00\x00\x40\x00\x78\x9c\xed\xc1\x31' '\x0d\x00\x20\x0c\x00\xb0\x04\x8c'), (16322, '\xbb\x3f\x00\x01\xf9\xc3\xff\x78\x9c\xec\xc1\x81' '\x00\x00\x00\x00\x80\x20\xd6\xfd')] try: self.cnx.send_compressed(*data) except Exception as err: self.fail("Failed sending pktnr {0}: {1}".format(data[1], err)) received = [(len(r), r[:20]) for r in self.cnx.sock._client_sends] self.assertEqual(exp, received) self.cnx.sock.reset()
def test_recv_compressed(self): """Receive compressed data from the socket""" self.cnx.sock = tests.DummySocket() def get_address(): return 'dummy' self.cnx.get_address = get_address # Receive a packet which is not 7 bytes long self.cnx.sock.add_packet(b'\01\01\01\01\01\01') self.assertRaises(errors.InterfaceError, self.cnx.recv_compressed) # Receive the header of a packet, but nothing more self.cnx.sock.add_packet(b'\01\00\00\00\00\00\00') self.assertRaises(errors.InterfaceError, self.cnx.recv_compressed) # Socket fails to receive and produces an error self.cnx.sock.raise_socket_error() self.assertRaises(errors.OperationalError, self.cnx.recv_compressed)
def test_recv_plain(self): """Receive data from the socket""" self.cnx.sock = tests.DummySocket() def get_address(): return 'dummy' self.cnx.get_address = get_address # Receive a packet which is not 4 bytes long self.cnx.sock.add_packet(b'\01\01\01') self.assertRaises(errors.InterfaceError, self.cnx.recv_plain) # Receive the header of a packet, but nothing more self.cnx.sock.add_packet(b'\01\00\00\00') self.assertRaises(errors.InterfaceError, self.cnx.recv_plain) # Socket fails to receive and produces an error self.cnx.sock.raise_socket_error() self.assertRaises(errors.OperationalError, self.cnx.recv_plain) # Receive packets after a query, SELECT "Ham" exp = [ b'\x01\x00\x00\x01\x01', b'\x19\x00\x00\x02\x03\x64\x65\x66\x00\x00\x00\x03\x48\x61\x6d\x00' b'\x0c\x21\x00\x09\x00\x00\x00\xfd\x01\x00\x1f\x00\x00', b'\x05\x00\x00\x03\xfe\x00\x00\x02\x00', b'\x04\x00\x00\x04\x03\x48\x61\x6d', b'\x05\x00\x00\x05\xfe\x00\x00\x02\x00', ] self.cnx.sock.reset() self.cnx.sock.add_packets(exp) length_exp = len(exp) result = [] packet = self.cnx.recv_plain() while packet: result.append(packet) if length_exp == len(result): break packet = self.cnx.recv_plain() self.assertEqual(exp, result)
def test_send_plain(self): """Send plain data through the socket""" data = 'asddfasdfasdf' self.assertRaises(errors.OperationalError, self.cnx.send_plain, data, 0) self.cnx.sock = tests.DummySocket() data = [('\x03\x53\x45\x4c\x45\x43\x54\x20\x22\x61\x62\x63\x22', 1), ('\x03\x53\x45\x4c\x45\x43\x54\x20\x22' + ('\x61' * (constants.MAX_PACKET_LENGTH + 1000)) + '\x22', 2)] self.assertRaises(Exception, self.cnx.send_plain, None, None) for buf in data: exp = network._prepare_packets(*buf) try: self.cnx.send_plain(*buf) except Exception, err: self.fail("Failed sending pktnr %r: %s" % (buf[1], err)) self.assertEqual(exp, self.cnx.sock._client_sends) self.cnx.sock.reset()
def test_send_plain(self): """Send plain data through the socket""" data = b'asddfasdfasdf' self.assertRaises(errors.OperationalError, self.cnx.send_plain, data, 0) self.cnx.sock = tests.DummySocket() data = [(b'\x03\x53\x45\x4c\x45\x43\x54\x20\x22\x61\x62\x63\x22', 1), (b'\x03\x53\x45\x4c\x45\x43\x54\x20\x22' + (b'\x61' * (constants.MAX_PACKET_LENGTH + 1000)) + b'\x22', 2) ] self.assertRaises(Exception, self.cnx.send_plain, None, None) for value in data: exp = network._prepare_packets(*value) try: self.cnx.send_plain(*value) except errors.Error as err: self.fail("Failed sending pktnr {}: {}".format( value[1], str(err))) self.assertEqual(exp, self.cnx.sock._client_sends) self.cnx.sock.reset()