Created
August 17, 2017 04:03
-
-
Save akkornel/3f3c302ef7271cf2d3958d65a4bd6f15 to your computer and use it in GitHub Desktop.
This is a script which tests for the bug in https://github.com/akkornel/syncrepl/issues/18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import pyasn1 | |
from pyasn1.type import constraint, namedtype, namedval, univ | |
from pyasn1.codec.ber.decoder import decode | |
from uuid import UUID | |
# These come from Python-LDAP's syncrepl module. | |
# They're definitions for pyasn1 to understand what it's parsing. | |
class syncUUID(univ.OctetString): | |
subtypeSpec = constraint.ValueSizeConstraint(16,16) | |
class syncCookie(univ.OctetString): | |
pass | |
class syncStateOp(univ.Enumerated): | |
namedValues = namedval.NamedValues( | |
('present', 0), | |
('add', 1), | |
('modify', 2), | |
('delete', 3) | |
) | |
subtypeSpec = univ.Enumerated.subtypeSpec + constraint.SingleValueConstraint(0,1,2,3) | |
class syncStateValue(univ.Sequence): | |
componentType = namedtype.NamedTypes( | |
namedtype.NamedType('state', syncStateOp()), | |
namedtype.NamedType('entryUUID', syncUUID()), | |
namedtype.OptionalNamedType('cookie', syncCookie()) | |
) | |
# Now, let's take our ASN.1 and decode it. | |
asn1_binary = b'0\x15\n\x01\x01\x04\x10\xe1\xd5\x7fN#O\x105\x97\xa2MV\x06\x18\x0co' | |
asn1_decoded = decode(asn1_binary, | |
asn1Spec = syncStateValue() | |
) | |
# Break out each item from the decoded ASN.1 | |
decoded_state = asn1_decoded[0].getComponentByName('state') | |
decoded_uuid = asn1_decoded[0].getComponentByName('entryUUID') | |
decoded_cookie = asn1_decoded[0].getComponentByName('cookie') | |
# Now, try to print it. | |
# Note that PyASN.1 is lazy, so it doesn't evaluate everything until now. | |
print('pyasn1 version is', pyasn1.__version__) | |
print('state is', decoded_state) | |
print('uuid is', UUID(bytes=bytes(decoded_uuid))) | |
print('cookie is', decoded_cookie) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment