Skip to content

Commit c0541e5

Browse files
committed
Add tests for exploits enhancement pipeline
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent c66d400 commit c0541e5

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import os
11+
from datetime import datetime
12+
from unittest import mock
13+
from unittest.mock import Mock
14+
15+
import pytest
16+
17+
from vulnerabilities.models import AdvisoryAlias
18+
from vulnerabilities.models import AdvisoryExploit
19+
from vulnerabilities.models import AdvisoryV2
20+
from vulnerabilities.pipelines.v2_improvers.enhance_with_exploitdb import ExploitDBImproverPipeline
21+
22+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
23+
TEST_DATA = os.path.join(BASE_DIR, "../test_data", "exploitdb_improver/files_exploits.csv")
24+
25+
26+
@pytest.mark.django_db
27+
@mock.patch("requests.get")
28+
def test_exploit_db_improver(mock_get):
29+
mock_response = Mock(status_code=200)
30+
with open(TEST_DATA, "r") as f:
31+
mock_response.text = f.read()
32+
mock_get.return_value = mock_response
33+
34+
improver = ExploitDBImproverPipeline()
35+
36+
# Run the improver when there is no matching aliases
37+
improver.execute()
38+
39+
assert AdvisoryExploit.objects.count() == 0
40+
41+
adv1 = AdvisoryV2.objects.create(
42+
advisory_id="VCIO-123-2002",
43+
datasource_id="ds",
44+
avid="ds/VCIO-123-2002",
45+
unique_content_id="i3giu",
46+
url="https://test.com",
47+
date_collected=datetime.now(),
48+
)
49+
50+
alias = AdvisoryAlias.objects.create(alias="CVE-2009-3699")
51+
52+
adv1.aliases.add(alias)
53+
54+
# Run Exploit-DB Improver again when there are matching aliases.
55+
improver.execute()
56+
assert AdvisoryExploit.objects.count() == 1
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import os
11+
from datetime import datetime
12+
from unittest import mock
13+
from unittest.mock import Mock
14+
15+
import pytest
16+
17+
from vulnerabilities.models import AdvisoryAlias
18+
from vulnerabilities.models import AdvisoryExploit
19+
from vulnerabilities.models import AdvisoryV2
20+
from vulnerabilities.pipelines.v2_improvers.enhance_with_kev import VulnerabilityKevPipeline
21+
from vulnerabilities.utils import load_json
22+
23+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
24+
TEST_DATA = os.path.join(BASE_DIR, "../test_data", "kev_data.json")
25+
26+
27+
@pytest.mark.django_db
28+
@mock.patch("requests.get")
29+
def test_kev_improver(mock_get):
30+
mock_response = Mock(status_code=200)
31+
mock_response.json.return_value = load_json(TEST_DATA)
32+
mock_get.return_value = mock_response
33+
34+
improver = VulnerabilityKevPipeline()
35+
36+
# Run the improver when there is no matching aliases
37+
improver.execute()
38+
39+
assert AdvisoryExploit.objects.count() == 0
40+
41+
adv1 = AdvisoryV2.objects.create(
42+
advisory_id="VCIO-123-2002",
43+
datasource_id="ds",
44+
avid="ds/VCIO-123-2002",
45+
unique_content_id="i3giu",
46+
url="https://test.com",
47+
date_collected=datetime.now(),
48+
)
49+
adv1.save()
50+
51+
alias = AdvisoryAlias.objects.create(alias="CVE-2021-38647")
52+
53+
adv1.aliases.add(alias)
54+
55+
# Run Kev Improver again when there are matching aliases.
56+
improver.execute()
57+
assert AdvisoryExploit.objects.count() == 1
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import os
11+
from datetime import datetime
12+
from unittest import mock
13+
from unittest.mock import Mock
14+
15+
import pytest
16+
17+
from vulnerabilities.models import AdvisoryAlias
18+
from vulnerabilities.models import AdvisoryExploit
19+
from vulnerabilities.models import AdvisoryV2
20+
from vulnerabilities.pipelines.v2_improvers.enhance_with_metasploit import (
21+
MetasploitImproverPipeline,
22+
)
23+
from vulnerabilities.utils import load_json
24+
25+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
26+
TEST_DATA = os.path.join(BASE_DIR, "../test_data", "metasploit_improver/modules_metadata_base.json")
27+
28+
29+
@pytest.mark.django_db
30+
@mock.patch("requests.get")
31+
def test_metasploit_improver(mock_get):
32+
mock_response = Mock(status_code=200)
33+
mock_response.json.return_value = load_json(TEST_DATA)
34+
mock_get.return_value = mock_response
35+
36+
improver = MetasploitImproverPipeline()
37+
38+
# Run the improver when there is no matching aliases
39+
improver.execute()
40+
assert AdvisoryExploit.objects.count() == 0
41+
42+
adv1 = AdvisoryV2.objects.create(
43+
advisory_id="VCIO-123-2002",
44+
datasource_id="ds",
45+
avid="ds/VCIO-123-2002",
46+
unique_content_id="i3giu",
47+
url="https://test.com",
48+
date_collected=datetime.now(),
49+
)
50+
alias = AdvisoryAlias.objects.create(alias="CVE-2007-4387")
51+
52+
adv1.aliases.add(alias)
53+
54+
# Run metasploit Improver again when there are matching aliases.
55+
improver.execute()
56+
assert AdvisoryExploit.objects.count() == 1

0 commit comments

Comments
 (0)