def test_normal_generic_message_with_extra(self): m = FrontendMessage() done, extra = m.consume('Q\x00\x00\x00\x05Nfoo') self.assertTrue(done) self.assertEqual(extra, 'foo') self.assertEqual(m.length, 6) self.assertEqual(m.type, 'Q') self.assertEqual(m.data, 'N')
def test_separate_packets(self): m = FrontendMessage() done, _ = m.consume('Q\x00') self.assertFalse(done) done, extra = m.consume('\x00\x00\x05Nfoo') self.assertEqual(extra, 'foo') self.assertEqual(m.length, 6) self.assertEqual(m.type, 'Q') self.assertEqual(m.data, 'N')
def test_CancelRequest(self): msg = ('\x00\x00\x00\x10' '\x80\x87\x71\x02' '\xde\xad\xbe\xef' '\xfe\xed\xbe\xef' 'extra') m = FrontendMessage() self.assertEqual((True, 'extra'), m.consume(msg)) self.assertEqual(m.type, 'Cancel') self.assertEqual(m.length, 16) self.assertEqual(m.pid, 0xdeadbeef) self.assertEqual(m.key, 0xfeedbeef)
def test_serialize(self): # test a special message msg = ('\x00\x00\x00\x26' '\x00\x03\x00\x00' 'user\x00postgres\x00' 'database\x00master\x00' 'foobar baz \x00 goo') m = FrontendMessage() m.consume(msg) self.assertEqual( m.serialize(), '\x00\x00\x00\x26' '\x00\x03\x00\x00' 'user\x00postgres\x00' 'database\x00master\x00') # test a normal message m = FrontendMessage() m.consume('S\x00\x00\x00\x05NX') self.assertEqual(m.serialize(), 'S\x00\x00\x00\x05N')
def test_Startup(self): msg = ('\x00\x00\x00\x26' # length '\x00\x03\x00\x00' # protocol version # fields 'user\x00postgres\x00' 'database\x00master\x00' # extra data 'foobar baz \x00 goo') m = FrontendMessage() done, extra = m.consume(msg) self.assertTrue(done) self.assertEqual(extra, 'foobar baz \x00 goo') self.assertEqual(m.length, 0x26) self.assertEqual(m.type, 'Startup') self.assertEqual( m.parameters, {'user': '******', 'database': 'master'})
def test_Startup(self): msg = ( '\x00\x00\x00\x26' # length '\x00\x03\x00\x00' # protocol version # fields 'user\x00postgres\x00' 'database\x00master\x00' # extra data 'foobar baz \x00 goo') m = FrontendMessage() done, extra = m.consume(msg) self.assertTrue(done) self.assertEqual(extra, 'foobar baz \x00 goo') self.assertEqual(m.length, 0x26) self.assertEqual(m.type, 'Startup') self.assertEqual(m.parameters, { 'user': '******', 'database': 'master' })
def test_not_enough_data(self): m = FrontendMessage() done, extra = m.consume('R\x00\x00\x00\x07') self.assertEqual(m.length, 8) self.assertFalse(done) self.assertFalse(extra)
def assertUnknown(self, msg): m = FrontendMessage() try: m.consume(msg) except ValueError, e: self.assertTrue(e.message.startswith('Unknown '))
def test_SSLRequest(self): m = FrontendMessage() done, _ = m.consume('\x00\x00\x00\x08\x80\x87\x71\x03') self.assertTrue(done) self.assertEqual(m.type, 'SSLRequest')
def test_sub_header_length(self): m = FrontendMessage() done, extra = m.consume('R') self.assertFalse(done) self.assertFalse(extra)