From f2691e89f534af823e78ad93693dbbd57685e10f Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Fri, 4 Sep 2020 14:46:49 -0400 Subject: [PATCH 1/3] Fix make_constant() in the AST module, to always return a constant. It was failing to do so for timestamps, instead returning a STIXdatetime object. --- stix2/patterns.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stix2/patterns.py b/stix2/patterns.py index bbee7ac..f1472cd 100644 --- a/stix2/patterns.py +++ b/stix2/patterns.py @@ -227,7 +227,7 @@ def make_constant(value): return value try: - return parse_into_datetime(value) + return TimestampConstant(value) except (ValueError, TypeError): pass From ff35e8a01b8687b3a917f49917d60badacf81970 Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Sat, 12 Sep 2020 19:11:00 -0400 Subject: [PATCH 2/3] Add some unit tests for the AST make_constant() function. --- stix2/test/v20/test_pattern_expressions.py | 40 ++++++++++++++++++++++ stix2/test/v21/test_pattern_expressions.py | 40 ++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/stix2/test/v20/test_pattern_expressions.py b/stix2/test/v20/test_pattern_expressions.py index fa9000e..1a60b51 100644 --- a/stix2/test/v20/test_pattern_expressions.py +++ b/stix2/test/v20/test_pattern_expressions.py @@ -1,9 +1,11 @@ import datetime import pytest +import pytz import stix2 from stix2.pattern_visitor import create_pattern_object +import stix2.utils def test_create_comparison_expression(): @@ -482,6 +484,44 @@ def test_invalid_startstop_qualifier(): ) +@pytest.mark.parametrize( + "input_, expected_class, expected_value", [ + (1, stix2.patterns.IntegerConstant, 1), + (1.5, stix2.patterns.FloatConstant, 1.5), + ("abc", stix2.patterns.StringConstant, "abc"), + (True, stix2.patterns.BooleanConstant, True), + ( + "2001-02-10T21:36:15Z", stix2.patterns.TimestampConstant, + stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc), + ), + ( + datetime.datetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc), + stix2.patterns.TimestampConstant, + stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc), + ), + ], +) +def test_make_constant_simple(input_, expected_class, expected_value): + const = stix2.patterns.make_constant(input_) + + assert isinstance(const, expected_class) + assert const.value == expected_value + + +def test_make_constant_list(): + list_const = stix2.patterns.make_constant([1,2,3]) + + assert isinstance(list_const, stix2.patterns.ListConstant) + assert all( + isinstance(elt, stix2.patterns.IntegerConstant) + for elt in list_const.value + ) + assert all( + int_const.value == test_elt + for int_const, test_elt in zip(list_const.value, [1,2,3]) + ) + + def test_make_constant_already_a_constant(): str_const = stix2.StringConstant('Foo') result = stix2.patterns.make_constant(str_const) diff --git a/stix2/test/v21/test_pattern_expressions.py b/stix2/test/v21/test_pattern_expressions.py index ac6a439..8a169d9 100644 --- a/stix2/test/v21/test_pattern_expressions.py +++ b/stix2/test/v21/test_pattern_expressions.py @@ -1,10 +1,12 @@ import datetime import pytest +import pytz from stix2patterns.exceptions import ParseException import stix2 from stix2.pattern_visitor import create_pattern_object +import stix2.utils def test_create_comparison_expression(): @@ -603,6 +605,44 @@ def test_invalid_startstop_qualifier(): ) +@pytest.mark.parametrize( + "input_, expected_class, expected_value", [ + (1, stix2.patterns.IntegerConstant, 1), + (1.5, stix2.patterns.FloatConstant, 1.5), + ("abc", stix2.patterns.StringConstant, "abc"), + (True, stix2.patterns.BooleanConstant, True), + ( + "2001-02-10T21:36:15Z", stix2.patterns.TimestampConstant, + stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc), + ), + ( + datetime.datetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc), + stix2.patterns.TimestampConstant, + stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc), + ), + ], +) +def test_make_constant_simple(input_, expected_class, expected_value): + const = stix2.patterns.make_constant(input_) + + assert isinstance(const, expected_class) + assert const.value == expected_value + + +def test_make_constant_list(): + list_const = stix2.patterns.make_constant([1,2,3]) + + assert isinstance(list_const, stix2.patterns.ListConstant) + assert all( + isinstance(elt, stix2.patterns.IntegerConstant) + for elt in list_const.value + ) + assert all( + int_const.value == test_elt + for int_const, test_elt in zip(list_const.value, [1,2,3]) + ) + + def test_make_constant_already_a_constant(): str_const = stix2.StringConstant('Foo') result = stix2.patterns.make_constant(str_const) From bad42e5b7890d5027a8087bc22edd5ad27d97906 Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Sat, 12 Sep 2020 19:33:56 -0400 Subject: [PATCH 3/3] pre-commit stylistic junk --- stix2/test/v20/test_pattern_expressions.py | 4 ++-- stix2/test/v21/test_pattern_expressions.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stix2/test/v20/test_pattern_expressions.py b/stix2/test/v20/test_pattern_expressions.py index 1a60b51..526fe97 100644 --- a/stix2/test/v20/test_pattern_expressions.py +++ b/stix2/test/v20/test_pattern_expressions.py @@ -509,7 +509,7 @@ def test_make_constant_simple(input_, expected_class, expected_value): def test_make_constant_list(): - list_const = stix2.patterns.make_constant([1,2,3]) + list_const = stix2.patterns.make_constant([1, 2, 3]) assert isinstance(list_const, stix2.patterns.ListConstant) assert all( @@ -518,7 +518,7 @@ def test_make_constant_list(): ) assert all( int_const.value == test_elt - for int_const, test_elt in zip(list_const.value, [1,2,3]) + for int_const, test_elt in zip(list_const.value, [1, 2, 3]) ) diff --git a/stix2/test/v21/test_pattern_expressions.py b/stix2/test/v21/test_pattern_expressions.py index 8a169d9..58cef3e 100644 --- a/stix2/test/v21/test_pattern_expressions.py +++ b/stix2/test/v21/test_pattern_expressions.py @@ -630,7 +630,7 @@ def test_make_constant_simple(input_, expected_class, expected_value): def test_make_constant_list(): - list_const = stix2.patterns.make_constant([1,2,3]) + list_const = stix2.patterns.make_constant([1, 2, 3]) assert isinstance(list_const, stix2.patterns.ListConstant) assert all( @@ -639,7 +639,7 @@ def test_make_constant_list(): ) assert all( int_const.value == test_elt - for int_const, test_elt in zip(list_const.value, [1,2,3]) + for int_const, test_elt in zip(list_const.value, [1, 2, 3]) )