kimgr / asn1ate

A Python library for translating ASN.1 into other forms.
Other
69 stars 41 forks source link

output missing size constaint #42

Closed aholtzma closed 8 years ago

aholtzma commented 8 years ago

The following input ASN1 definition:

Class-definitions DEFINITIONS AUTOMATIC TAGS ::=

BEGIN

MultiplePLMN-List-r6 ::=      SEQUENCE {
  mibPLMN-Identity          BOOLEAN,
  multiplePLMNs           SEQUENCE (SIZE (1..5)) OF
                      PLMN-IdentityWithOptionalMCC-r6
}

END

produces the following output, which is missing the size constraint on multiplePLMNs.

# Auto-generated by asn1ate v.0.5.1.dev from missing_size.asn1
# (last modified on 2016-09-07 12:30:06)

from pyasn1.type import univ, char, namedtype, namedval, tag, constraint, useful

class MultiplePLMN_List_r6(univ.Sequence):
    pass

MultiplePLMN_List_r6.componentType = namedtype.NamedTypes(
    namedtype.NamedType('mibPLMN-Identity', univ.Boolean().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
    namedtype.NamedType('multiplePLMNs', univ.SequenceOf(componentType=PLMN_IdentityWithOptionalMCC_r6()).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
)

Since size contstraints work elsewhere on sequence-ofs, I suspect this problem is due to the sequence-of being nested inside a sequence.

aholtzma commented 8 years ago

FWIW the size constaint makes it into the parse tree:

  ModuleDefinition:
    ModuleReference:
      Class-definitions
    DefinitiveIdentifier:
    AUTOMATIC TAGS
    None
    ModuleBody:
      Exports:
      Imports:
      AssignmentList:
        TypeAssignment:
          MultiplePLMN-List-r6
          ::=
          Type:
            SequenceType:
              SEQUENCE
               ComponentType:
                 NamedType:
                   Identifier:
                     mibPLMN-Identity
                   Type:
                     SimpleType:
                       BOOLEAN
               ComponentType:
                 NamedType:
                   Identifier:
                     multiplePLMNs
                   Type:
                     SequenceOfType:
                       SizeConstraint:
                         ValueRangeConstraint:
                           1
                           5
                       Type:
                         DefinedType:
                           None
                           PLMN-IdentityWithOptionalMCC-r6
                           None

However dumping the semantic tree seems be broken:

aaron:asn1ate aholtzma$ python asn1ate/test.py --sema ./missing_size.asn1
Traceback (most recent call last):
  File "asn1ate/test.py", line 73, in <module>
    sys.exit(main())
  File "asn1ate/test.py", line 61, in main
    print(module)
  File "build/bdist.macosx-10.11-intel/egg/asn1ate/sema.py", line 350, in __str__
    + 'END\n'
  File "build/bdist.macosx-10.11-intel/egg/asn1ate/sema.py", line 379, in __str__
    return '%s ::= %s' % (self.type_name, self.type_decl)
  File "build/bdist.macosx-10.11-intel/egg/asn1ate/sema.py", line 420, in __str__
    component_type_list = ', '.join(map(str, self.components))
  File "build/bdist.macosx-10.11-intel/egg/asn1ate/sema.py", line 669, in __str__
    result = '%s %s' % (self.identifier, self.type_decl)
  File "build/bdist.macosx-10.11-intel/egg/asn1ate/sema.py", line 505, in __str__
    result = '[%s] ' % ' '.join(class_spec)
TypeError: sequence item 0: expected string, int found
kimgr commented 8 years ago

Thanks! I didn't know SIZE constraints were allowed on SEQUENCE OF, so this is probably just an oversight. I'll try to take a look, but my asn1ate bandwidth is limited these days, so if you feel comfortable with contributing a patch that might go faster.

aholtzma commented 8 years ago

Okay I'll take a look. First thing is to fix the sema dump. Any ideas what is up there? It also fails with non-problematic ASN input.

kimgr commented 8 years ago

I took a quick look and it appears the class_number is an integer when using automatic tagging. This bug crept in here: https://github.com/kimgr/asn1ate/pull/29/files#diff-6d613dac67af772711af50dfafa21774R396

I think the right thing to do is change sema.py, line 416 like so:

-                tagged_type = TaggedType((None, tag_number, None, element))
+                tagged_type = TaggedType((None, str(tag_number), None, element))

I'll try to make that change later today and push.

kimgr commented 8 years ago

Done, please resync to master.

aholtzma commented 8 years ago

Great, thanks. Now we can see that the semantic tree is fine:

Class-definitions DEFINITIONS ::=
BEGIN
MultiplePLMN-List-r6 ::= SEQUENCE { mibPLMN-Identity [0] BOOLEAN, multiplePLMNs [1] SEQUENCE SIZE(1..5) OF PLMN-IdentityWithOptionalMCC-r6 }
END

Will poke around in the pyasn1 back end now to see where the constraints get spit out.