def testEncodeFunction(self): self.assertEqual( tools.EncodeAttr('string', six.u('string')), six.b('string')) self.assertEqual( tools.EncodeAttr('octets', six.b('string')), six.b('string')) self.assertEqual( tools.EncodeAttr('ipaddr', '192.168.0.255'), six.b('\xc0\xa8\x00\xff')) self.assertEqual( tools.EncodeAttr('integer', 0x01020304), six.b('\x01\x02\x03\x04')) self.assertEqual( tools.EncodeAttr('date', 0x01020304), six.b('\x01\x02\x03\x04'))
def _EncodeValue(self, attr, value): result = '' if attr.values.HasForward(value): result = attr.values.GetForward(value) else: result = tools.EncodeAttr(attr.type, value) if attr.encrypt == 2: # salt encrypt attribute result = self.SaltCrypt(result) return result
def testDecodeFunction(self): self.assertEqual(tools.DecodeAttr('string', six.b('string')), six.u('string')) self.assertEqual(tools.EncodeAttr('octets', six.b('string')), six.b('string')) self.assertEqual(tools.DecodeAttr('ipaddr', six.b('\xc0\xa8\x00\xff')), '192.168.0.255') self.assertEqual( tools.DecodeAttr('integer', six.b('\x01\x02\x03\x04')), 0x01020304) self.assertEqual(tools.DecodeAttr('date', six.b('\x01\x02\x03\x04')), 0x01020304) self.assertEqual( tools.DecodeAttr('integer64', six.b('\x01\x02\x03\x04\x05\x06\x07\x08')), 0x0102030405060708)
def _EncodeValue(self, attr, value): if attr.has_tag: if isinstance(value, tuple): tag, value = value else: tag = 0x00 if attr.values.HasForward(value): encoded_value = attr.values.GetForward(value) elif (attr.encrypt == 1 or attr.encrypt == 2) and self.auto_crypt: encoded_value = self._EncodeEncryptedValue(attr, value) else: encoded_value = tools.EncodeAttr(attr.type, value) if attr.has_tag: # Always prefix with tag for tagged attribute (0 if not present) return tools.EncodeTaggedAttr(attr.type, tag, encoded_value) else: return encoded_value
def __ParseValue(self, state, tokens, defer): if len(tokens) != 4: raise ParseError('Incorrect number of tokens for value definition', file=state['file'], line=state['line']) (attr, key, value) = tokens[1:] try: adef = self.attributes[attr] except KeyError: if defer: self.defer_parse.append((copy(state), copy(tokens))) return raise ParseError('Value defined for unknown attribute ' + attr, file=state['file'], line=state['line']) if adef.type in ['integer', 'signed', 'short', 'byte']: value = int(value, 0) value = tools.EncodeAttr(adef.type, value) self.attributes[attr].values.Add(key, value)
def _EncodeValue(self, attr, value): if attr.values.HasForward(value): return attr.values.GetForward(value) else: return tools.EncodeAttr(attr.type, value)
def testEncodeFunctionIP(self): self.assertEqual( tools.EncodeAttr( 'ipv6prefix', ipaddress.IPv6Network(six.u('2001:db8:1234::/48'))), six.b('\x00\x30\x20\x01\x0d\xb8\x12\x34'))