def test_truncated_utf8_message(self): expected = "abc... (4 more bytes)" self.entry["command"] = compat.struct_pack_unicode( "3sB18s", b"abc", 0xCE, b"... (4 more bytes)").rstrip(b"\0") self.host.log_entry(self.logger, self.entry) self.assertEquals(expected, self.logger.command)
def _send_to_client(self, stream_id, content): if type(content) is six.text_type: encoded_content = six.text_type(content).encode("utf-8") else: encoded_content = content code = len(encoded_content) * 2 + stream_id # 2->TODO struct.pack|unpack in python < 2.7.7 does not allow unicode format string. self._client_channel.simulate_server_write( compat.struct_pack_unicode("i", code) + encoded_content)
def close(self): """Closes the channel to the client.""" try: # 2->TODO struct.pack|unpack in python < 2.7.7 does not allow unicode format string. win32file.WriteFile(self.__pipe_handle, compat.struct_pack_unicode("I", 0)) win32file.FlushFileBuffers(self.__pipe_handle) finally: win32pipe.DisconnectNamedPipe(self.__pipe_handle) self.__pipe_handle = None
def test_invalild_utf8_message(self): expected = "abc\ufffddef" self.entry["command"] = compat.struct_pack_unicode( "3sB13s", b"abc", 0xCE, b"def").rstrip(b"\0") self.host.utf8_warning_interval = 1 self.host.log_entry(self.logger, self.entry) self.assertEquals(expected, self.logger.command) self.assertTrue( self.logger.warning.startswith( "Redis command contains invalid utf8"))
def run_test_case(self, input_string, length_to_send, truncate_size=None): input_buffer = io.BytesIO() # 2->TODO struct.pack|unpack in python < 2.7.7 does not allow unicode format string. input_buffer.write(compat.struct_pack_unicode("!I", length_to_send)) input_buffer.write(input_string) if truncate_size is not None: input_buffer.truncate(truncate_size) input_buffer.seek(truncate_size) num_bytes = input_buffer.tell() input_buffer.seek(0) result = Int32RequestParser(10).parse_request(input_buffer, num_bytes) if result is None: self.assertEquals(0, input_buffer.tell()) else: self.assertEquals( len(result) + struct.calcsize("!I"), input_buffer.tell()) return result
def serialize_as_length_prefixed_string(value, output_buffer): """Serializes the str or unicode value using the length-prefixed format special to Scalyr. This is a bit more efficient since the value does not need to be blackslash or quote escaped. @param value: The string value to serialize. @param output_buffer: The buffer to serialize the string to. @type value: str or unicode @type output_buffer: BytesIO """ output_buffer.write(b"`s") if type(value) is six.text_type: to_serialize = value.encode("utf-8") else: to_serialize = value # 2->TODO struct.pack|unpack in python < 2.7.7 does not allow unicode format string. output_buffer.write(compat.struct_pack_unicode(">i", len(to_serialize))) output_buffer.write(to_serialize)