Commit 1b6cae88 authored by Mark Canterbury's avatar Mark Canterbury
Browse files

Merge branch 'updating_asn_testing' into 'master'

Updating asn testing

See merge request li/trial!20
parents 8ed38ba8 f548e21e
Loading
Loading
Loading
Loading
+2 −69
Original line number Diff line number Diff line
import logging

from asn1tools import parse_files, compile_dict, ParseError, CompileError
from glob import glob
from pathlib import Path

from pprint import pprint


def parseASN1File (asnFile):
    try:
        parse_files(asnFile)
    except ParseError as ex:
        return [ex]
    return []


def parseASN1Files (fileList):
    if len(fileList) == 0:
        logging.warning ("No files specified")
        return {}
    errors = {}
    logging.info("Parsing files...")
    for f in fileList:
        ex = parseASN1File(f)
        if ex: 
            logging.info (f"  {f}: Failed - {ex!r}")
        else: 
            logging.info (f"  {f}: OK")            
        errors[f] = ex
    return errors


def compileASN1Files (fileList):
    logging.info("Compiling files...")
    errors = []
    try:
        d = parse_files(fileList)
        for modulename, module in d.items():
            # Weird fix because the compiler doesn't like RELATIVE-OID as a type
            # Not sure if the on-the-wire encoding would be affected or not
            # but for most checking purposes this doesn't matter
            module['types']["RELATIVE-OID"] = {'type' : 'OBJECT IDENTIFIER'}
        c = compile_dict(d)
    except CompileError as ex:
        logging.info (f"Compiler  error: {ex}")
        errors.append(ex)
    except ParseError as ex:
        logging.info (f"Parse error: {ex}")
        errors.append(ex)
    logging.info ("Compiled OK")
    return errors


def validateASN1Files (fileList):
    parseErrors = parseASN1Files(fileList)
#    if len(parseErrors > 0):
#        logging.info ("Abandonding compile due to parse errors")
#    compileErrors = compileASN1Files(fileList)
#   leave this for now - TBD
    compileErrors = []
    return parseErrors, compileErrors


def validateAllASN1FilesInPath (path):
    p = Path(path)
    fileGlob = [str(f) for f in  p.rglob('*.asn')]
    fileGlob += [str(f) for f in p.rglob('*.asn1')]
    return validateASN1Files(fileGlob)    
from compile_asn import *


if __name__ == '__main__':
    parseErrors, compileErrors = validateAllASN1FilesInPath("./")
    parseErrors, compileErrors, parser = validateAllASN1FilesInPath("./")
    parseErrorCount = 0
    print ("ASN.1 Parser checks:")
    print ("-----------------------------")

testing/compile_asn.py

0 → 100644
+136 −0
Original line number Diff line number Diff line
import logging
import copy
from asn1tools import parse_files, compile_dict, ParseError, CompileError
from glob import glob
from pathlib import Path
from pprint import pprint

def parseASN1File (asnFile):
    try:
        parse_files(asnFile)
    except ParseError as ex:
        return [ex]
    return []


def parseASN1Files (fileList):
    if len(fileList) == 0:
        logging.warning ("No files specified")
        return {}
    errors = {}
    logging.info("Parsing files...")
    for f in fileList:
        ex = parseASN1File(f)
        if ex: 
            logging.info (f"  {f}: Failed - {ex!r}")
        else: 
            logging.info (f"  {f}: OK")            
        errors[f] = ex
    return errors

def fixDottedReference (dict, importingModule, importingType, importingMember, importedModule, importedType):
    newName = importedModule + "_" + importedType
    dict[importedModule]['types'][newName] = copy.deepcopy(dict[importedModule]['types'][importedType])
    dict[importingModule]['imports'][importedModule].append(newName)
    member = [x for x in dict[importingModule]['types'][importingType]['members'] if x is not None and x['name'] == importingMember][0]
    member['type'] = newName

def compileASN1Files (fileList):
    logging.info("Compiling files...")
    errors = []
    imports = {}

    #p = re.compile(r"]\s+\S+\.\S+")
    #for f in fileList:
    #    with open(f) as fh:
    #        s = fh.read()
    #    for match in p.findall(s):
    #        print (f"In {f}: {match}")
    #exit()

    try:
        dr = parse_files(fileList)
        for modulename, module in dr.items():
            # Weird fix because the compiler doesn't like RELATIVE-OID as a type
            # Not sure if the on-the-wire encoding would be affected or not
            # but for most checking purposes this doesn't matter
            module['types']["RELATIVE-OID"] = {'type' : 'OBJECT IDENTIFIER'}
            for k,v in module['imports'].items():
                if not k in imports:
                    imports[k] = []
                imports[k].append({
                    "in" : modulename,
                    "types" : v
                })
        for k,v in imports.items():
            if not k in dr.keys():
                importers = [i['in'] for i in v]
                errors.append(f"Unsatisfied import [{k}] for {importers}")

        fixDottedReference(dr, 'LI-PS-PDU', 'Location', 'umtsHI2Location', 'UmtsHI2Operations', 'Location')        
        fixDottedReference(dr, 'LI-PS-PDU', 'Location', 'epsLocation', 'EpsHI2Operations', 'Location')        
        fixDottedReference(dr, 'LI-PS-PDU', 'Location', 'eTSI671HI2Location', 'HI2Operations', 'Location')        

        fixDottedReference(dr, 'LI-PS-PDU', 'UMTSIRI', 'iRI-Parameters', 'UmtsHI2Operations', 'IRI-Parameters')
        fixDottedReference(dr, 'LI-PS-PDU', 'UMTSIRI', 'iRI-CS-Parameters', 'UmtsCS-HI2Operations', 'IRI-Parameters')        
        fixDottedReference(dr, 'LI-PS-PDU', 'ETSI671IRI', 'iRI-Parameters', 'HI2Operations', 'IRI-Parameters')        
        fixDottedReference(dr, 'LI-PS-PDU', 'EPSIRI', 'iRI-EPS-Parameters', 'EpsHI2Operations', 'IRI-Parameters')        
        fixDottedReference(dr, 'LI-PS-PDU', 'ConfIRI', 'iRI-Conf-Parameters', 'CONFHI2Operations', 'IRI-Parameters')        
        fixDottedReference(dr, 'LI-PS-PDU', 'ProSeIRI', 'iRI-ProSe-Parameters', 'ProSeHI2Operations', 'IRI-Parameters')        
        fixDottedReference(dr, 'LI-PS-PDU', 'GcseIRI', 'iRI-Gcse-Parameters', 'GCSEHI2Operations', 'IRI-Parameters')        

        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'tTRAFFIC-1', 'TS101909201', 'TTRAFFIC')        
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'cTTRAFFIC-1', 'TS101909201', 'CTTRAFFIC')        
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'tTRAFFIC-2', 'TS101909202', 'TTRAFFIC')        
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'cTTRAFFIC-2', 'TS101909202', 'CTTRAFFIC')        
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'cCIPPacketHeader', 'CDMA2000CCModule', 'CCIPPacketHeader')        
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'uMTSCC-CC-PDU', 'Umts-HI3-PS', 'CC-PDU')   
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'ePSCC-CC-PDU', 'Eps-HI3-PS', 'CC-PDU')
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'confCC-CC-PDU', 'CONF-HI3-IMS', 'Conf-CC-PDU')
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'voipCC-CC-PDU', 'VoIP-HI3-IMS', 'Voip-CC-PDU')
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'gcseCC-CC-PDU', 'GCSE-HI3', 'Gcse-CC-PDU')
        fixDottedReference(dr, 'LI-PS-PDU', 'CCContents', 'cSvoice-CC-PDU', 'CSvoice-HI3-IP', 'CSvoice-CC-PDU')

        fixDottedReference(dr, 'LI-PS-PDU', 'IRIContents', 'tARGETACTIVITYMONITOR-1', 'TS101909201', 'TARGETACTIVITYMONITOR-1')        
        fixDottedReference(dr, 'LI-PS-PDU', 'IRIContents', 'tARGETACTIVITYMONITOR-2', 'TS101909202', 'TARGETACTIVITYMONITOR')        
        fixDottedReference(dr, 'LI-PS-PDU', 'IRIContents', 'lAESProtocol', 'Laesp-j-std-025-b', 'LAESProtocol')        
        fixDottedReference(dr, 'LI-PS-PDU', 'IRIContents', 'cDMA2000LAESMessage', 'CDMA2000CIIModule', 'CDMA2000LAESMessage')        

        fixDottedReference(dr, 'LI-PS-PDU', 'HI4Payload', 'threeGPP-LI-Notification', 'TS33128Payloads', 'LINotificationPayload')        

        fixDottedReference(dr, 'ILHIPDU', 'TimestampMapping', 'timeStampQualifier', 'LI-PS-PDU', 'TimeStampQualifier')        

        fixDottedReference(dr, 'ILHIPDU', 'ILHITimestamp', 'qualifiedDateTime', 'Common-Parameters', 'QualifiedDateTime')        
        fixDottedReference(dr, 'ILHIPDU', 'ILHITimestamp', 'qualifiedMicrosecondDateTime', 'Common-Parameters', 'QualifiedMicrosecondDateTime')        

        fixDottedReference(dr, 'ILHIPDU', 'OriginalTimestamp', 'microSecondTimeStamp', 'LI-PS-PDU', 'MicroSecondTimeStamp')        

        fixDottedReference(dr, 'ILHIPDU', 'LocationMapping', 'originalLocation', 'LI-PS-PDU', 'Location')        

        fixDottedReference(dr, 'ILHIPDU', 'GeocodedLocationData', 'wGS84CoordinateDecimal', 'Common-Parameters', 'WGS84CoordinateDecimal')        
        fixDottedReference(dr, 'ILHIPDU', 'GeocodedLocationData', 'wGS84CoordinateAngular', 'Common-Parameters', 'WGS84CoordinateAngular')        

        c = compile_dict(dr)
    except CompileError as ex:
        logging.info (f"Compiler  error: {ex}")
        errors.append(ex)
    except ParseError as ex:
        logging.info (f"Parse error: {ex}")
        errors.append(ex)
    logging.info ("Compiled OK")
    return errors, c


def validateASN1Files (fileList):
    parseErrors = parseASN1Files(fileList)
    if len(parseErrors) > 0:
        logging.info ("Abandonding compile due to parse errors")
    compileErrors, parser = compileASN1Files(fileList)
    return parseErrors, compileErrors, parser


def validateAllASN1FilesInPath (path):
    p = Path(path)
    fileGlob = [str(f) for f in  p.rglob('*.asn')]
    fileGlob += [str(f) for f in p.rglob('*.asn1')]
    return validateASN1Files(fileGlob)    
 No newline at end of file
+106 −0
Original line number Diff line number Diff line
HI1NotificationOperations
{itu-t(0) identified-organization(4) etsi(0) securityDomain(2) lawfulIntercept(2) hi1(0) notificationOperations(1) version7(7)}

DEFINITIONS IMPLICIT TAGS ::=
BEGIN

IMPORTS

	-- from clause�D.5
	CommunicationIdentifier,
	TimeStamp,
	LawfulInterceptionIdentifier
		FROM HI2Operations
		{itu-t(0) identified-organization(4) etsi(0) securityDomain(2) lawfulIntercept(2) hi2(1) version10(10)};

-- =============================
-- Object Identifier Definitions
-- =============================

-- LawfulIntercept DomainId
lawfulInterceptDomainId OBJECT IDENTIFIER ::= {itu-t(0) identified-organization(4) etsi(0) securityDomain(2) lawfulIntercept(2)}

-- hi1 Domain
hi1NotificationOperationsId OBJECT IDENTIFIER ::= {lawfulInterceptDomainId hi1(0) notificationOperations(1)}
hi1OperationId OBJECT IDENTIFIER ::= {hi1NotificationOperationsId version6(6)}

-- Class 2 operation. The timer shall be set to a value between 3s and 240s.
-- The timer default value is 60s.
-- NOTE: The value for this timer is to be set on the equipment waiting for the returned message;
-- its value shall be agreed between the NWO/AP/SvP and the LEA, depending on their equipment
-- properties.


HI1-Operation 	::= CHOICE
{
	liActivated						[1] Notification,
	liDeactivated					[2] Notification,
	liModified						[3] Notification,
	alarms-indicator				[4] Alarm-Indicator,
	...,
	national-HI1-ASN1parameters		[5] National-HI1-ASN1parameters
}

-- ==================
-- PARAMETERS FORMATS
-- ==================

Notification 	::= SEQUENCE
{
	domainID						[0] OBJECT IDENTIFIER (hi1OperationId) OPTIONAL,
		-- Once using FTP delivery mechanism
	lawfulInterceptionIdentifier 	[1] LawfulInterceptionIdentifier,
		-- This identifier is the LIID identity provided with the lawful authorization
		-- for each target.
	communicationIdentifier 		[2] CommunicationIdentifier OPTIONAL,
		-- Only the NWO/PA/SvPIdentifier is provided (the one provided with the Lawful
		-- authorization).
		-- Called "callIdentifier" in V1.1.1 of ES 201 671
	timeStamp						[3] TimeStamp,
		-- date and time of the report.
	...,
	national-HI1-ASN1parameters		[5] National-HI1-ASN1parameters OPTIONAL,
	target-Information				[6] OCTET STRING (SIZE (1..256)) OPTIONAL
		-- provides information about the number or the characteristic of the target 
		-- (e.g. E-mail address, E.164 number), ASCII format
}

Alarm-Indicator 	::= SEQUENCE
{
	domainID						[0] OBJECT IDENTIFIER (hi1OperationId) OPTIONAL,
		-- Once using FTP delivery mechanism
	communicationIdentifier					[1] CommunicationIdentifier OPTIONAL,
		-- Only the NWO/PA/SvPIdentifier is provided (the one provided with the
		-- Lawful authorization)
	timeStamp						[2] TimeStamp,
		-- date and time of the report.
	alarm-information					[3] OCTET STRING (SIZE (1..256)),
		-- Provides information about alarms (free format).
		-- Until ASN.1 version 6 (document version v3.12.1) the octet string
		-- was limited to a size of 25
	...,
	lawfulInterceptionIdentifier				[4] LawfulInterceptionIdentifier OPTIONAL,
		-- This identifier is the LIID identity provided with the lawful authorization
		-- for each target in according to national law.
	national-HI1-ASN1parameters				[5] National-HI1-ASN1parameters OPTIONAL
}

National-HI1-ASN1parameters		::= SEQUENCE
{
	domainID		[0] OBJECT IDENTIFIER (hi1OperationId) OPTIONAL,
		-- Once using FTP delivery mechanism.
	countryCode		[1] PrintableString (SIZE (2)),
		-- Country Code according to ISO 3166-1 [67],
		-- the country to which the parameters inserted after the extension marker apply.
	...
		-- In case a given country wants to use additional national parameters according to its law,
		-- these national parameters should be defined using the ASN.1 syntax and added after the
		-- extension marker (...).
		-- It is recommended that "version parameter" and "vendor identification parameter" are
		-- included in the national parameters definition. Vendor identifications can be
		-- retrieved from IANA web site (see�annex�K). Besides, it is recommended to avoid
		-- using tags from 240 to 255 in a formal type definition.
}

END -- end of HI1NotificationOperations
+1014 −0

File added.

Preview size limit exceeded, changes collapsed.

+428 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading