Skip to content

Instantly share code, notes, and snippets.

@akkornel
Created August 17, 2017 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkornel/3f3c302ef7271cf2d3958d65a4bd6f15 to your computer and use it in GitHub Desktop.
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
#!/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