Skip to content

Commit

Permalink
Add parser tests and fix a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroSteiner committed Jun 14, 2024
1 parent 218fd74 commit c6fff08
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rule_engine/parser/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def parse_bytes(string):
:rtype: bytes
"""
if not string:
return bytes
return b''
elif re.match(r'^(\\x[A-Fa-f0-9]{2})+$', string):
string = re.sub(r'\\x', '', string)
return binascii.a2b_hex(string)
elif re.match(r'^([A-Fa-f0-9]{2}:)*[A-Fa-f0-9]{2}$', string):
elif re.match(r'^([A-Fa-f0-9]{2}:?)*[A-Fa-f0-9]{2}$', string):
string = string.replace(':', '')
return binascii.a2b_hex(string)
raise errors.BytesSyntaxError('invalid bytes literal', string)
Expand Down
7 changes: 7 additions & 0 deletions tests/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ def test_parse_boolean(self):
self.assertLiteralStatementEqual('true', ast.BooleanExpression, True)
self.assertLiteralStatementEqual('false', ast.BooleanExpression, False)

def test_parse_bytes(self):
self.assertLiteralStatementEqual('b""', ast.BytesExpression, b'')
self.assertLiteralStatementEqual('b"de:ad"', ast.BytesExpression, b'\xde\xad')
self.assertLiteralStatementEqual('b"DE:AD"', ast.BytesExpression, b'\xde\xad')
self.assertLiteralStatementEqual('b"\\xde\\xad"', ast.BytesExpression, b'\xde\xad')
self.assertLiteralStatementEqual('b"\\xDE\\xAD"', ast.BytesExpression, b'\xde\xad')

def test_parse_datetime(self):
self.assertLiteralStatementEqual('d"2016-10-15"', ast.DatetimeExpression, datetime.datetime(2016, 10, 15, tzinfo=dateutil.tz.tzlocal()))
self.assertLiteralStatementEqual('d"2016-10-15 12:30"', ast.DatetimeExpression, datetime.datetime(2016, 10, 15, 12, 30, tzinfo=dateutil.tz.tzlocal()))
Expand Down

0 comments on commit c6fff08

Please sign in to comment.