check_asn1.py 2.84 KB
Newer Older
canterburym's avatar
canterburym committed
1
2
3
import logging

from asn1tools import parse_files, compile_dict, ParseError, CompileError
canterburym's avatar
canterburym committed
4
5
6
from glob import glob
from pathlib import Path

canterburym's avatar
canterburym committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
canterburym's avatar
canterburym committed
32

canterburym's avatar
canterburym committed
33
34
35
36

def compileASN1Files (fileList):
    logging.info("Compiling files...")
    errors = []
canterburym's avatar
canterburym committed
37
    try:
canterburym's avatar
canterburym committed
38
39
40
41
42
43
44
45
46
47
        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)
canterburym's avatar
canterburym committed
48
    except ParseError as ex:
canterburym's avatar
canterburym committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        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)
    return parseErrors, compileErrors


def validateAllASN1FilesInPath (path):
canterburym's avatar
canterburym committed
64
    globPattern = str(Path(path)) + '/*.asn1'
canterburym's avatar
canterburym committed
65
66
67
68
69
70
    logging.info("Searching: " + globPattern)
    schemaGlob = glob(globPattern, recursive=True)
    return validateASN1Files(schemaGlob)    


if __name__ == '__main__':
canterburym's avatar
canterburym committed
71
    parseErrors, compileErrors = validateAllASN1FilesInPath("./")
canterburym's avatar
canterburym committed
72
    parseErrorCount = 0
canterburym's avatar
canterburym committed
73
    print ("ASN.1 Parser checks:")
canterburym's avatar
canterburym committed
74
75
76
77
78
79
80
81
82
83
    print ("-----------------------------")
    for filename, errors in parseErrors.items():
        if len(errors) > 0:
            parseErrorCount += len(errors)
            print (f"{filename}: {len(errors)} errors")
            for error in errors:
                print ("  " + str(error))
        else:
            print (f"{filename}: OK")
    print ("-----------------------------")
canterburym's avatar
canterburym committed
84
    print ("ASN.1 Compilation:")
canterburym's avatar
canterburym committed
85
86
87
88
89
90
91
92
93
    print ("-----------------------------")
    if len(compileErrors) > 0:
        for error in compileErrors:            
            print ("  " + str(error))
    else:
        print ("Compilation OK")
    print ("-----------------------------")
    print (f"{parseErrorCount} parse errors, {len(compileErrors)} compile errors")
    exit (parseErrorCount + len(compileErrors))
canterburym's avatar
canterburym committed
94