def test_read_value_max_padding(self): encoding = (b'\x48\x00\x00\x00\x00\x00\x00\x00') self.stream = BytearrayStream(encoding) ts = TextString() ts.length = 0x01 ts.read_value(self.stream) expected = 'H' self.assertEqual(expected, ts.value, self.bad_read.format('value', expected, ts.value))
def test_read(self): encoding = (b'\x42\x00\x00\x07\x00\x00\x00\x0B\x48\x65\x6C\x6C\x6F\x20' b'\x57\x6F\x72\x6C\x64\x00\x00\x00\x00\x00') self.stream = BytearrayStream(encoding) ts = TextString() ts.read(self.stream) expected = 'Hello World' self.assertEqual(expected, ts.value, self.bad_read.format('value', expected, ts.value))
def test_read_value_no_padding(self): encoding = (b'\x48\x65\x6C\x6C\x6F\x20\x57\x6F') self.stream = BytearrayStream(encoding) ts = TextString() ts.length = 0x08 ts.read_value(self.stream) expected = 'Hello Wo' self.assertEqual(expected, ts.value, self.bad_read.format('value', expected, ts.value))
def test_write_value_max_padding(self): encoding = (b'\x48\x00\x00\x00\x00\x00\x00\x00') self.stream = BytearrayStream() value = 'H' ts = TextString(value) ts.write_value(self.stream) result = self.stream.read() len_exp = len(encoding) len_rcv = len(result) self.assertEqual(len_exp, len_rcv, self.bad_length.format(len_exp, len_rcv)) self.assertEqual(encoding, result, self.bad_encoding)
def test_write(self): encoding = (b'\x42\x00\x00\x07\x00\x00\x00\x0B\x48\x65\x6C\x6C\x6F\x20' b'\x57\x6F\x72\x6C\x64\x00\x00\x00\x00\x00') self.stream = BytearrayStream() value = 'Hello World' ts = TextString(value) ts.write(self.stream) result = self.stream.read() len_exp = len(encoding) len_rcv = len(result) self.assertEqual(len_exp, len_rcv, self.bad_length.format(len_exp, len_rcv)) self.assertEqual(encoding, result, self.bad_encoding)
def read(self, istream): """ Read the data encoding the RevocationReason object and decode it into its constituent parts. Args: istream (Stream): A data stream containing encoded object data, supporting a read method; usually a BytearrayStream object. """ super(RevocationReason, self).read(istream) tstream = BytearrayStream(istream.read(self.length)) self.revocation_code = RevocationReasonCode() self.revocation_code.read(tstream) if self.is_tag_next(Tags.REVOCATION_MESSAGE, tstream): self.revocation_message = TextString() self.revocation_message.read(tstream) self.is_oversized(tstream) self.validate()
def __init__(self, code=None, message=None): """ Construct a RevocationReason object. Parameters: code(RevocationReasonCode): revocation reason code message(string): An optional revocation message """ super(RevocationReason, self).__init__(tag=Tags.REVOCATION_REASON) if code is not None: self.revocation_code = RevocationReasonCode(value=code) else: self.revocation_code = RevocationReasonCode() if message is not None: self.revocation_message = TextString(value=message, tag=Tags.REVOCATION_MESSAGE) else: self.revocation_message = None self.validate()
def __init__(self, code=None, message=None): """ Construct a RevocationReason object. Parameters: code(RevocationReasonCode): revocation reason code message(string): An optional revocation message """ super(RevocationReason, self).__init__(tag=Tags.REVOCATION_REASON) if code is not None: self.revocation_code = RevocationReasonCode(value=code) else: self.revocation_code = RevocationReasonCode() if message is not None: self.revocation_message = TextString( value=message, tag=Tags.REVOCATION_MESSAGE) else: self.revocation_message = None self.validate()
class RevocationReason(Struct): """ A structure describing the reason for a revocation operation. See Sections 2.1.9 and 4.25 of the KMIP 1.1 specification for more information. Attributes: code: The revocation reason code enumeration message: An optional revocation message """ def __init__(self, code=None, message=None): """ Construct a RevocationReason object. Parameters: code(RevocationReasonCode): revocation reason code message(string): An optional revocation message """ super(RevocationReason, self).__init__(tag=Tags.REVOCATION_REASON) if code is not None: self.revocation_code = RevocationReasonCode(value=code) else: self.revocation_code = RevocationReasonCode() if message is not None: self.revocation_message = TextString( value=message, tag=Tags.REVOCATION_MESSAGE) else: self.revocation_message = None self.validate() def read(self, istream): """ Read the data encoding the RevocationReason object and decode it into its constituent parts. Args: istream (Stream): A data stream containing encoded object data, supporting a read method; usually a BytearrayStream object. """ super(RevocationReason, self).read(istream) tstream = BytearrayStream(istream.read(self.length)) self.revocation_code = RevocationReasonCode() self.revocation_code.read(tstream) if self.is_tag_next(Tags.REVOCATION_MESSAGE, tstream): self.revocation_message = TextString() self.revocation_message.read(tstream) self.is_oversized(tstream) self.validate() def write(self, ostream): """ Write the data encoding the RevocationReason object to a stream. Args: ostream (Stream): A data stream in which to encode object data, supporting a write method; usually a BytearrayStream object. """ tstream = BytearrayStream() self.revocation_code.write(tstream) if self.revocation_message is not None: self.revocation_message.write(tstream) # Write the length and value self.length = tstream.length() super(RevocationReason, self).write(ostream) ostream.write(tstream.buffer) def validate(self): """ validate the RevocationReason object """ if not isinstance(self.revocation_code, RevocationReasonCode): msg = "RevocationReaonCode expected" raise TypeError(msg) if self.revocation_message is not None: if not isinstance(self.revocation_message, TextString): msg = "TextString expect" raise TypeError(msg)
def test_validate_on_invalid_type(self): ts = TextString() ts.value = 0 self.assertRaises(TypeError, ts.validate)
def test_validate_on_valid_unset(self): ts = TextString() # Check no exception thrown. ts.validate()
def test_validate_on_valid(self): ts = TextString() ts.value = 'Hello World' # Check no exception thrown. ts.validate()