def test_unframed_messages( self ): parser = SyslogRequestParser( None, 32 ) handler = Handler() parser.process( "hello\nworld\n", handler.handle ) self.assertEqual( 2, len( handler.values ) ) self.assertEqual( "hello", handler.values[0] ) self.assertEqual( "world", handler.values[1] )
def test_unframed_messages( self ): parser = SyslogRequestParser( None, 32 ) handler = Handler() parser.process( "hello\nworld\n", handler.handle ) self.assertEqual( 2, len( handler.values ) ) self.assertEqual( "hello", handler.values[0] ) self.assertEqual( "world", handler.values[1] )
def test_framed_message_exceeds_max_size( self ): parser = SyslogRequestParser( None, 11 ) handler = Handler() parser.process( "23 hello world h", handler.handle ) parser.process( "10 lo world .", handler.handle ) self.assertEqual( 2, len( handler.values ) ) self.assertEqual( "23 hello world h", handler.values[0] ) self.assertEqual( " 10 lo world .", handler.values[1] )
def test_unframed_message_exceeds_max_size( self ): parser = SyslogRequestParser( None, 13 ) handler = Handler() parser.process( "in my hand i have ", handler.handle ) self.assertEqual( 1, len( handler.values ) ) self.assertEqual( "in my hand i have ", handler.values[0] ) parser.process( "100 dollars\n", handler.handle ) self.assertEqual( 2, len( handler.values ) ) self.assertEqual( "100 dollars", handler.values[1] )
def test_unframed_messages_incomplete( self ): parser = SyslogRequestParser( None, 32 ) handler = Handler() parser.process( "hello", handler.handle ) self.assertEqual( 0, len( handler.values ) ) parser.process( " world\n", handler.handle ) self.assertEqual( 1, len( handler.values ) ) self.assertEqual( "hello world", handler.values[0] )
def test_framed_message_multiple_incomplete(self): parser = SyslogRequestParser(None, 32) handler = Handler() parser.process("11 hello", handler.handle) self.assertEqual(0, len(handler.values)) parser.process(" w", handler.handle) self.assertEqual(0, len(handler.values)) parser.process("or", handler.handle) self.assertEqual(0, len(handler.values)) parser.process("ld", handler.handle) self.assertEqual(1, len(handler.values)) self.assertEqual("hello world", handler.values[0])
def test_internal_buffer_and_offset_is_reset_on_handler_method_call_single_complete_message( self, ): mock_socket = mock.Mock() mock_handle_frame = mock.Mock() max_buffer_size = 1024 mock_msg_1 = b"<14>Dec 24 16:12:48 hosttest.example.com tag-0-0-17[2593]: Hey diddle diddle, The Cat and the Fiddle, The Cow jump'd over the Spoon\n" parser = SyslogRequestParser(socket=mock_socket, max_buffer_size=max_buffer_size) self.assertEqual(parser._remaining, None) parser.process(mock_msg_1, mock_handle_frame) self.assertEqual(mock_handle_frame.call_count, 1) self.assertEqual( six.ensure_binary(mock_handle_frame.call_args_list[0][0][0]), mock_msg_1[:-1], ) # Verify internal state is correctly reset self.assertEqual(parser._remaining, bytearray()) self.assertEqual(parser._offset, 0)
def test_framed_message_exceeds_max_size(self): parser = SyslogRequestParser(None, 11) handler = Handler() parser.process("23 hello world h", handler.handle) parser.process("10 lo world .", handler.handle) self.assertEqual(2, len(handler.values)) self.assertEqual("23 hello world h", handler.values[0]) self.assertEqual(" 10 lo world .", handler.values[1])
def test_unframed_message_exceeds_max_size(self): parser = SyslogRequestParser(None, 13) handler = Handler() parser.process("in my hand i have ", handler.handle) self.assertEqual(1, len(handler.values)) self.assertEqual("in my hand i have ", handler.values[0]) parser.process("100 dollars\n", handler.handle) self.assertEqual(2, len(handler.values)) self.assertEqual("100 dollars", handler.values[1])
def test_unframed_messages_incomplete(self): parser = SyslogRequestParser(None, 32) handler = Handler() parser.process(b"hello", handler.handle) self.assertEqual(0, len(handler.values)) parser.process(b" world\n", handler.handle) self.assertEqual(1, len(handler.values)) self.assertEqual(b"hello world", handler.values[0])
def test_internal_buffer_and_offset_is_reset_on_handler_method_call_no_data( self, mock_global_log): # Verify internal buffer and offset is reset after handling the frame mock_socket = mock.Mock() mock_handle_frame = mock.Mock() max_buffer_size = 1024 parser = SyslogRequestParser(socket=mock_socket, max_buffer_size=max_buffer_size) self.assertEqual(parser._remaining, None) self.assertEqual(mock_global_log.warning.call_count, 0) parser.process(None, mock_handle_frame) self.assertEqual(mock_handle_frame.call_count, 0) self.assertEqual(mock_global_log.warning.call_count, 1) self.assertEqual(parser._remaining, None) parser.process(b"", mock_handle_frame) self.assertEqual(mock_handle_frame.call_count, 0) self.assertEqual(parser._remaining, None)
def test_framed_message_invalid_frame_size(self): parser = SyslogRequestParser(None, 32) handler = Handler() self.assertRaises(ValueError, lambda: parser.process("1a1 hello", handler.handle))
def test_framed_message_invalid_frame_size( self ): parser = SyslogRequestParser( None, 32 ) handler = Handler() self.assertRaises( ValueError, lambda: parser.process( "1a1 hello", handler.handle ) )