def test_sample(): # Samples from X.690-0207 Name=asn1.TYPE(asn1.IMPLICIT(1,cls=asn1.APPLICATION_FLAG), asn1.SEQUENCE([('givenName', None, asn1.VisibleString, 0), ('initial', None, asn1.VisibleString, 0), ('familyName', None, asn1.VisibleString, 0)], seq_name = 'Name')) EmployeeNumber=asn1.TYPE(asn1.IMPLICIT(2,cls=asn1.APPLICATION_FLAG), asn1.INTEGER_class([],0,2147483647)) Date=asn1.TYPE(asn1.IMPLICIT(3,cls=asn1.APPLICATION_FLAG), asn1.VisibleString) ChildInformation=asn1.SEQUENCE([('name',None,Name,0),('dateOfBirth',asn1.EXPLICIT(0,cls=asn1.CONTEXT_FLAG),Date,0)], seq_name = 'ChildInformation') PersonnelRecord=asn1.TYPE(asn1.IMPLICIT(0,cls=asn1.APPLICATION_FLAG), asn1.SEQUENCE([ ('name',None,Name,0), ('title',None,asn1.TYPE(asn1.EXPLICIT(0,cls=asn1.CONTEXT_FLAG),asn1.VisibleString),0), ('number',None,EmployeeNumber,1), ('dateOfHire',None,asn1.TYPE(asn1.EXPLICIT(1,cls=asn1.CONTEXT_FLAG),Date),0), ('nameOfSpouse',None,asn1.TYPE(asn1.EXPLICIT(2,cls=asn1.CONTEXT_FLAG),Name),0), ('children',None,asn1.TYPE(asn1.EXPLICIT(3,cls=asn1.CONTEXT_FLAG),asn1.SEQUENCE_OF(ChildInformation)),0), ], seq_name = 'PersonnelRecord')) decoded = { 'name': { 'givenName': 'John', 'initial': 'P', 'familyName': 'Smith' }, 'title': 'Director', 'number': 51, 'dateOfHire': '19710917', 'nameOfSpouse': { 'givenName': 'Mary', 'initial': 'T', 'familyName': 'Smith' }, 'children': [ { 'name': { 'givenName': 'Ralph', 'initial': 'T', 'familyName': 'Smith' }, 'dateOfBirth': '19571111' }, { 'name': { 'givenName': 'Susan', 'initial': 'B', 'familyName': 'Jones' }, 'dateOfBirth': '19590717' }, ]} pdu = PersonnelRecord print "Encoded:" encoded = asn1.encode(pdu, decoded) print oo(encoded) print "Decoded:" redecoded = asn1.decode(pdu, encoded) print repr(redecoded) assert(redecoded == decoded) print "Re-encoded:" reencoded = asn1.encode(pdu, redecoded) print oo(reencoded) print "Original:" print oo(encoded) assert(encoded == reencoded)
ObjectSyntax = asn1.CHOICE([('simple', None, SimpleSyntax), ('application-wide', None, ApplicationSyntax)]) VarBind = asn1.SEQUENCE( [('name', None, ObjectName, 0, None), ('choice', None, asn1.CHOICE([ ('value', None, ObjectSyntax), ('unSpecified', None, asn1.NULL), ('noSuchObject', None, asn1.TYPE(asn1.IMPLICIT(0, cls=asn1.CONTEXT_FLAG), asn1.NULL)), ('noSuchInstance', None, asn1.TYPE(asn1.IMPLICIT(1, cls=asn1.CONTEXT_FLAG), asn1.NULL)), ('endOfMibView', None, asn1.TYPE(asn1.IMPLICIT(2, cls=asn1.CONTEXT_FLAG), asn1.NULL)) ]), 0, None)], seq_name='VarBind') VarBindList = asn1.SEQUENCE_OF(VarBind) PDU = asn1.SEQUENCE( [('request-id', None, Integer32, 0, None), ('error-status', None, asn1.INTEGER_class([('noError', 0), ('tooBig', 1), ('noSuchName', 2), ('badValue', 3), ('readOnly', 4), ('genErr', 5), ('noAccess', 6), ('wrongType', 7), ('wrongLength', 8), ('wrongEncoding', 9), ('wrongValue', 10), ('noCreation', 11), ('inconsistentValue', 12), ('resourceUnavailable', 13), ('commitFailed', 14), ('undoFailed', 15), ('authorizationError', 16), ('notWritable', 17), ('inconsistentName', 18)], None, None), 0, None), ('error-index', None, asn1.INTEGER_class([], 0, 2147483647), 0, None), ('variable-bindings', None, VarBindList, 0, None)],
('returnResultLast', None, asn1.TYPE(asn1.IMPLICIT(2, cls=asn1.CONTEXT_FLAG), ReturnResult)), ('returnError', None, asn1.TYPE(asn1.IMPLICIT(3, cls=asn1.CONTEXT_FLAG), ReturnError)), ('reject', None, asn1.TYPE(asn1.IMPLICIT(4, cls=asn1.CONTEXT_FLAG), Reject)), ('returnResultNotLast', None, asn1.TYPE(asn1.IMPLICIT(7, cls=asn1.CONTEXT_FLAG), ReturnResult)) ]) Reason = asn1.CHOICE([('p-abortCause', None, P_AbortCause), ('u-abortCause', None, DialoguePortion)]) Abort = asn1.SEQUENCE([('dtid', None, DestTransactionID, 0, None), ('reason', None, Reason, 1, None)], seq_name='Abort') ComponentPortion = asn1.TYPE(asn1.IMPLICIT(12, cls=asn1.APPLICATION_FLAG), asn1.SEQUENCE_OF(Component)) End = asn1.SEQUENCE([('dtid', None, DestTransactionID, 0, None), ('dialoguePortion', None, DialoguePortion, 1, None), ('components', None, ComponentPortion, 1, None)], seq_name='End') Begin = asn1.SEQUENCE([('otid', None, OrigTransactionID, 0, None), ('dialoguePortion', None, DialoguePortion, 1, None), ('components', None, ComponentPortion, 1, None)], seq_name='Begin') Continue = asn1.SEQUENCE([('otid', None, OrigTransactionID, 0, None), ('dtid', None, DestTransactionID, 0, None), ('dialoguePortion', None, DialoguePortion, 1, None), ('components', None, ComponentPortion, 1, None)], seq_name='Continue') Unidirectional = asn1.SEQUENCE( [('dialoguePortion', None, DialoguePortion, 1, None),
('not-finished', 1), ('user-defined', 30)], None, None) AARQ_apdu = asn1.TYPE( asn1.IMPLICIT(0, cls=asn1.APPLICATION_FLAG), asn1.SEQUENCE( [('protocol-version', None, asn1.TYPE(asn1.IMPLICIT(0, cls=asn1.CONTEXT_FLAG), asn1.BITSTRING_class([('version1', 0)], None, None)), 1, 'version1'), ('application-context-name', None, asn1.TYPE(asn1.EXPLICIT(1, cls=asn1.CONTEXT_FLAG), asn1.OBJECT_IDENTIFIER), 0, None), ('user-information', None, asn1.TYPE(asn1.IMPLICIT(30, cls=asn1.CONTEXT_FLAG), asn1.SEQUENCE_OF(asn1.EXTERNAL)), 1, None)], seq_name='AARQ-apdu')) ABRT_source = asn1.INTEGER_class([('dialogue-service-user', 0), ('dialogue-service-provider', 1)], None, None) RLRQ_apdu = asn1.TYPE( asn1.IMPLICIT(2, cls=asn1.APPLICATION_FLAG), asn1.SEQUENCE([('reason', None, asn1.TYPE(asn1.IMPLICIT(0, cls=asn1.CONTEXT_FLAG), Release_request_reason), 1, None), ('user-information', None, asn1.TYPE(asn1.IMPLICIT(30, cls=asn1.CONTEXT_FLAG), asn1.SEQUENCE_OF(asn1.EXTERNAL)), 1, None)], seq_name='RLRQ-apdu')) AARE_apdu = asn1.TYPE( asn1.IMPLICIT(1, cls=asn1.APPLICATION_FLAG),