def encode(self, msg): if not isinstance(msg, Message): raise RuntimeError('You must encode only Message objects') payload = self.msg_to_payload(msg) h = Header() h.message_length = len(payload) if self.hmc: self.compute_hmac(h, self.hmc, payload) header_data = h.SerializeToString() header_size = len(header_data) if header_size > MAX_HEADER_SIZE: raise InvalidMessage("Header is too long") pack_fmt = "!bb%dsb%ds" % (header_size, len(payload)) byte_data = pack(pack_fmt, RECORD_SEPARATOR, header_size, header_data, UNIT_SEPARATOR, payload) return byte_data
def test_protobuf_encoding(self): enc = ProtobufEncoder() bytes = enc.encode(SAMPLE_MSG) eq_(ord(bytes[0]), RECORD_SEPARATOR) header_len = ord(bytes[1]) header = bytes[2:2+header_len] # Now double check the header h = Header() h.ParseFromString(header) eq_(h.message_encoding, Header.MessageEncoding.Value('PROTOCOL_BUFFER')) eq_(ord(bytes[header_len+2]), UNIT_SEPARATOR) pb_data = bytes[header_len+3:] eq_(len(pb_data), h.message_length) msg = Message() msg.ParseFromString(pb_data) eq_(msg.uuid, SAMPLE_MSG.uuid) eq_(msg.timestamp, SAMPLE_MSG.timestamp) eq_(msg.payload, SAMPLE_MSG.payload) # Check the 3 fields eq_(len(msg.fields), 4) eq_(first_value(msg, 'foo'), 'bar') eq_(first_value(msg, 'blah'), 42) eq_(first_value(msg, 'cef_meta.syslog_name'), 'some-syslog-thing') eq_(first_value(msg, 'cef_meta.syslog_level'), 5)
def decode_message(bytes): """ return header and message object """ header_len = ord(bytes[1]) header_bytes = bytes[2:2 + header_len] # Now double check the header h = Header() h.ParseFromString(header_bytes) encoder = ENCODERS[h.message_encoding] return h, encoder.decode(bytes[header_len + 3:])
def decode_message(bytes): """ Decode the header and message object from raw bytes """ header_len = ord(bytes[1]) header = bytes[2:2 + header_len] # Now double check the header h = Header() h.ParseFromString(header) pb_data = bytes[header_len + 3:] msg = Message() msg.ParseFromString(pb_data) return h, msg
def test_json_encoding(self): enc = JSONEncoder() bytes = enc.encode(SAMPLE_MSG) eq_(ord(bytes[0]), RECORD_SEPARATOR) header_len = ord(bytes[1]) header = bytes[2:2+header_len] # Now double check the header h = Header() h.ParseFromString(header) eq_(h.message_encoding, Header.MessageEncoding.Value('JSON')) eq_(h.message_length, len(enc.msg_to_payload(SAMPLE_MSG))) eq_(ord(bytes[header_len+2]), 0x1f) json_data = bytes[header_len+3:] eq_(len(json_data), h.message_length) # Check that the messages are the same object (minus UUID) eq_(enc.decode(json_data), SAMPLE_MSG)