|
2 | 2 | # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
|
3 | 3 | # See the file 'docs/LICENSE' for copying permission.
|
4 | 4 |
|
| 5 | +import pathlib |
| 6 | +import random |
| 7 | +import sys |
5 | 8 | import tempfile
|
| 9 | +from unittest import mock |
6 | 10 |
|
7 | 11 | import httpretty
|
| 12 | +import mongomock |
| 13 | +import pymongo |
8 | 14 | import pytest
|
9 | 15 |
|
| 16 | +from lib.cuckoo.common.config import ConfigMeta |
10 | 17 | from lib.cuckoo.common.path_utils import path_delete, path_write_file
|
11 | 18 | from lib.cuckoo.common.web_utils import _download_file, force_int, get_file_content, parse_request_arguments
|
| 19 | +from modules.reporting.mongodb_constants import ANALYSIS_COLL |
| 20 | + |
| 21 | +TEST_DB_NAME = "cuckoo_test_db" |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mongodb_enabled(custom_conf_path: pathlib.Path): |
| 26 | + with open(custom_conf_path / "reporting.conf", "wt") as fil: |
| 27 | + print(f"[mongodb]\nenabled = yes\ndb = {TEST_DB_NAME}", file=fil) |
| 28 | + ConfigMeta.refresh() |
| 29 | + yield |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def mongodb_mock_client(): |
| 34 | + with mongomock.patch(servers=(("127.0.0.1", 27017),)): |
| 35 | + client = pymongo.MongoClient(host=f"mongodb://127.0.0.1/{TEST_DB_NAME}") |
| 36 | + with mock.patch("dev_utils.mongodb.conn", new=client): |
| 37 | + yield client |
12 | 38 |
|
13 | 39 |
|
14 | 40 | @pytest.fixture
|
@@ -90,3 +116,56 @@ def test_parse_request_arguments(mock_request):
|
90 | 116 | def test_force_int():
|
91 | 117 | assert force_int(value="1") == 1
|
92 | 118 | assert force_int(value="$") == 0
|
| 119 | + |
| 120 | + |
| 121 | +def test_perform_search_invalid_ttp(): |
| 122 | + sys.modules.pop("lib.cuckoo.common.web_utils", None) |
| 123 | + from lib.cuckoo.common.web_utils import perform_search |
| 124 | + |
| 125 | + with pytest.raises(ValueError) as exc: |
| 126 | + _ = perform_search(term="ttp", value="SPOONS") |
| 127 | + assert "Invalid TTP" in str(exc) |
| 128 | + |
| 129 | + |
| 130 | +def test_perform_search_not_in_search_term_map(): |
| 131 | + sys.modules.pop("lib.cuckoo.common.web_utils", None) |
| 132 | + from lib.cuckoo.common.web_utils import perform_search, search_term_map |
| 133 | + |
| 134 | + term = "Unexpected" |
| 135 | + assert term not in search_term_map |
| 136 | + actual_result = perform_search(term=term, value="not in search term map") |
| 137 | + assert actual_result is None |
| 138 | + |
| 139 | + |
| 140 | +def test_perform_search_invalid_int_value(): |
| 141 | + sys.modules.pop("lib.cuckoo.common.web_utils", None) |
| 142 | + from lib.cuckoo.common.web_utils import normalized_int_terms, perform_search |
| 143 | + |
| 144 | + term = random.choice(normalized_int_terms) |
| 145 | + non_integer_value = "not an integer" |
| 146 | + with pytest.raises(ValueError) as exc: |
| 147 | + _ = perform_search(term=term, value=non_integer_value) |
| 148 | + assert non_integer_value in str(exc) |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.usefixtures("mongodb_enabled") |
| 152 | +def test_perform_search_mongo(mongodb_mock_client): |
| 153 | + sys.modules.pop("lib.cuckoo.common.web_utils", None) |
| 154 | + from lib.cuckoo.common.web_utils import perform_search, search_term_map |
| 155 | + |
| 156 | + term = "tlp" |
| 157 | + value = "red" |
| 158 | + assert term in search_term_map |
| 159 | + assert search_term_map[term] == "info.tlp" |
| 160 | + id = random.randint(1, 1000) |
| 161 | + analysis = { |
| 162 | + "info": { |
| 163 | + "id": id, |
| 164 | + term: value, |
| 165 | + } |
| 166 | + } |
| 167 | + mongodb_mock_client[TEST_DB_NAME][ANALYSIS_COLL].insert_one(analysis) |
| 168 | + result = perform_search(term=term, value=value) |
| 169 | + assert len(result) == 1 |
| 170 | + assert result[0]["info"][term] == value |
| 171 | + assert result[0]["info"]["id"] == id |
0 commit comments