Skip to content

Commit

Permalink
Handle malformed json / xml file input (fixes #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-ifak committed Apr 18, 2024
1 parent 551f7e4 commit 206e22f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions aas_test_engines/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def check_json_data(data: any, version: str = _DEFAULT_VERSION) -> AasTestResult


def check_json_file(file: TextIO, version: str = _DEFAULT_VERSION) -> AasTestResult:
data = json.load(file)
try:
data = json.load(file)
except json.decoder.JSONDecodeError as e:
return AasTestResult(f"Invalid JSON: {e}", '', Level.ERROR)
return check_json_data(data, version)


Expand Down Expand Up @@ -138,7 +141,10 @@ def preprocess(data: ElementTree.Element, validator: SchemaValidator) -> JSON:


def check_xml_file(file: TextIO, version: str = _DEFAULT_VERSION) -> AasTestResult:
data = ElementTree.fromstring(file.read())
try:
data = ElementTree.fromstring(file.read())
except ElementTree.ParseError as e:
return AasTestResult(f"Invalid xml: {e}", '', Level.ERROR)
return check_xml_data(data, version)


Expand Down
8 changes: 8 additions & 0 deletions test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def test_empty(self):
result = file.check_json_data({})
self.assertTrue(result.ok())

def test_no_json(self):
result = file.check_json_file(io.StringIO("no json"))
self.assertFalse(result.ok())


class CheckXmlTest(TestCase):

Expand All @@ -44,6 +48,10 @@ def test_invalid_namespace(self):
self.assertFalse(result.ok())
result.dump()

def test_no_xml(self):
result = file.check_xml_file(io.StringIO("no xml"))
self.assertFalse(result.ok())

class CheckAasxTest(TestCase):

def test_not_a_zip(self):
Expand Down

0 comments on commit 206e22f

Please sign in to comment.