From e6ccd2808e62b698761c43fcaed87e8b8bc9fb89 Mon Sep 17 00:00:00 2001 From: mark Date: Mon, 15 Jun 2020 11:47:12 +0100 Subject: [PATCH 1/3] Adds test fixtures --- .gitignore | 125 ++++++++++++++++++++++++++++++++++++++++++ .gitlab-ci.yml | 13 +++++ testing/check_asn1.py | 96 ++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 testing/check_asn1.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e37603 --- /dev/null +++ b/.gitignore @@ -0,0 +1,125 @@ +# Editors +.vscode/ +.idea/ + +# Vagrant +.vagrant/ + +# Mac/OSX +.DS_Store + +# Windows +Thumbs.db + +# Source for the following rules: https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..1b43a20 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,13 @@ +image: "python:3.7" + +before_script: + - python3 --version + - pip3 install -q asn1tools lxml xmlschema + +stages: + - Check Schemas + +checkASN1: + stage: Check Schemas + script: + - python3 testing/check_asn1.py \ No newline at end of file diff --git a/testing/check_asn1.py b/testing/check_asn1.py new file mode 100644 index 0000000..494737c --- /dev/null +++ b/testing/check_asn1.py @@ -0,0 +1,96 @@ +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) + + +if __name__ == '__main__': + parseErrors, compileErrors = validateAllASN1FilesInPath("./") + parseErrorCount = 0 + print ("ASN.1 Parser checks:") + 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 ("-----------------------------") + print ("ASN.1 Compilation:") + 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)) + -- GitLab From dc20dab361b3739f729b7c9bfcd4522d1f978a9c Mon Sep 17 00:00:00 2001 From: mark Date: Mon, 15 Jun 2020 11:48:17 +0100 Subject: [PATCH 2/3] Quick test of asn1 checks --- test.asn1 | 0 test2.asn | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.asn1 create mode 100644 test2.asn diff --git a/test.asn1 b/test.asn1 new file mode 100644 index 0000000..e69de29 diff --git a/test2.asn b/test2.asn new file mode 100644 index 0000000..e69de29 -- GitLab From fc391c9b06eba0b221a595db836e410ed35a095a Mon Sep 17 00:00:00 2001 From: mark Date: Mon, 15 Jun 2020 11:49:21 +0100 Subject: [PATCH 3/3] Removing tests --- test.asn1 | 0 test2.asn | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test.asn1 delete mode 100644 test2.asn diff --git a/test.asn1 b/test.asn1 deleted file mode 100644 index e69de29..0000000 diff --git a/test2.asn b/test2.asn deleted file mode 100644 index e69de29..0000000 -- GitLab