Merge branch 'master' of https://github.com/oasis-open/cti-python-stix2 into fix_issue_351
commit
8810983ca0
|
@ -1,16 +1,12 @@
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
from antlr4 import CommonTokenStream, InputStream
|
|
||||||
from antlr4.tree.Trees import Trees
|
|
||||||
import six
|
|
||||||
from stix2patterns.exceptions import ParseException
|
from stix2patterns.exceptions import ParseException
|
||||||
from stix2patterns.grammars.STIXPatternLexer import STIXPatternLexer
|
|
||||||
from stix2patterns.grammars.STIXPatternParser import (
|
from stix2patterns.grammars.STIXPatternParser import (
|
||||||
STIXPatternParser, TerminalNode,
|
STIXPatternParser, TerminalNode,
|
||||||
)
|
)
|
||||||
from stix2patterns.grammars.STIXPatternVisitor import STIXPatternVisitor
|
from stix2patterns.grammars.STIXPatternVisitor import STIXPatternVisitor
|
||||||
from stix2patterns.validator import STIXPatternErrorListener
|
from stix2patterns.v20.pattern import Pattern
|
||||||
|
|
||||||
from .patterns import *
|
from .patterns import *
|
||||||
from .patterns import _BooleanExpression
|
from .patterns import _BooleanExpression
|
||||||
|
@ -328,41 +324,9 @@ class STIXPatternVisitorForSTIX2(STIXPatternVisitor):
|
||||||
|
|
||||||
def create_pattern_object(pattern, module_suffix="", module_name=""):
|
def create_pattern_object(pattern, module_suffix="", module_name=""):
|
||||||
"""
|
"""
|
||||||
Validates a pattern against the STIX Pattern grammar. Error messages are
|
Create a STIX pattern AST from a pattern string.
|
||||||
returned in a list. The test passed if the returned list is empty.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
start = ''
|
pattern_obj = Pattern(pattern)
|
||||||
if isinstance(pattern, six.string_types):
|
|
||||||
start = pattern[:2]
|
|
||||||
pattern = InputStream(pattern)
|
|
||||||
|
|
||||||
if not start:
|
|
||||||
start = pattern.readline()[:2]
|
|
||||||
pattern.seek(0)
|
|
||||||
|
|
||||||
parseErrListener = STIXPatternErrorListener()
|
|
||||||
|
|
||||||
lexer = STIXPatternLexer(pattern)
|
|
||||||
# it always adds a console listener by default... remove it.
|
|
||||||
lexer.removeErrorListeners()
|
|
||||||
|
|
||||||
stream = CommonTokenStream(lexer)
|
|
||||||
|
|
||||||
parser = STIXPatternParser(stream)
|
|
||||||
|
|
||||||
parser.buildParseTrees = True
|
|
||||||
# it always adds a console listener by default... remove it.
|
|
||||||
parser.removeErrorListeners()
|
|
||||||
parser.addErrorListener(parseErrListener)
|
|
||||||
|
|
||||||
# To improve error messages, replace "<INVALID>" in the literal
|
|
||||||
# names with symbolic names. This is a hack, but seemed like
|
|
||||||
# the simplest workaround.
|
|
||||||
for i, lit_name in enumerate(parser.literalNames):
|
|
||||||
if lit_name == u"<INVALID>":
|
|
||||||
parser.literalNames[i] = parser.symbolicNames[i]
|
|
||||||
|
|
||||||
tree = parser.pattern()
|
|
||||||
builder = STIXPatternVisitorForSTIX2(module_suffix, module_name)
|
builder = STIXPatternVisitorForSTIX2(module_suffix, module_name)
|
||||||
return builder.visit(tree)
|
return pattern_obj.visit(builder)
|
||||||
|
|
|
@ -271,7 +271,7 @@ def test_indicator_stix20_invalid_pattern():
|
||||||
)
|
)
|
||||||
|
|
||||||
assert excinfo.value.cls == stix2.v21.Indicator
|
assert excinfo.value.cls == stix2.v21.Indicator
|
||||||
assert "FAIL: The same qualifier is used more than once" in str(excinfo.value)
|
assert "FAIL: Duplicate qualifier type encountered" in str(excinfo.value)
|
||||||
|
|
||||||
ind = stix2.v21.Indicator(
|
ind = stix2.v21.Indicator(
|
||||||
type="indicator",
|
type="indicator",
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from stix2patterns.exceptions import ParseException
|
||||||
|
|
||||||
import stix2
|
import stix2
|
||||||
from stix2.pattern_visitor import create_pattern_object
|
from stix2.pattern_visitor import create_pattern_object
|
||||||
|
@ -515,3 +516,8 @@ def test_list_constant():
|
||||||
def test_parsing_multiple_slashes_quotes():
|
def test_parsing_multiple_slashes_quotes():
|
||||||
patt_obj = create_pattern_object("[ file:name = 'weird_name\\'' ]")
|
patt_obj = create_pattern_object("[ file:name = 'weird_name\\'' ]")
|
||||||
assert str(patt_obj) == "[file:name = 'weird_name\\'']"
|
assert str(patt_obj) == "[file:name = 'weird_name\\'']"
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_error():
|
||||||
|
with pytest.raises(ParseException):
|
||||||
|
create_pattern_object("[ file: name = 'weirdname]")
|
||||||
|
|
Loading…
Reference in New Issue