-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathtest_nvd_api.py
282 lines (238 loc) · 11 KB
/
test_nvd_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later
import os
import shutil
import tempfile
from datetime import datetime, timedelta, timezone
from test.utils import EXTERNAL_SYSTEM
from unittest.mock import AsyncMock
import pytest
from cve_bin_tool.cvedb import CVEDB
from cve_bin_tool.data_sources import nvd_source
from cve_bin_tool.nvd_api import NVD_API
class FakeResponse:
"""Helper class to simulate aiohttp responses"""
def __init__(self, status, json_data, headers=None):
self.status = status
self._json_data = json_data
self.headers = headers or {}
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
pass
async def json(self):
return self._json_data
class TestNVD_API:
@classmethod
def setup_class(cls):
cls.outdir = tempfile.mkdtemp(prefix="cvedb-api-")
@classmethod
def teardown_class(cls):
shutil.rmtree(cls.outdir)
# ------------------ Integration Tests ------------------
@pytest.mark.asyncio
@pytest.mark.skipif(
not EXTERNAL_SYSTEM() or not os.getenv("nvd_api_key"),
reason="NVD tests run only when EXTERNAL_SYSTEM=1",
)
async def test_get_nvd_params(self):
"""Test NVD for a future date. It should be empty"""
nvd_api = NVD_API(api_key=os.getenv("nvd_api_key") or "")
await nvd_api.get_nvd_params(
time_of_last_update=(datetime.now() + timedelta(days=2))
)
await nvd_api.get()
assert nvd_api.total_results == 0 and nvd_api.all_cve_entries == []
@pytest.mark.asyncio
@pytest.mark.skipif(
not EXTERNAL_SYSTEM() or not os.getenv("nvd_api_key"),
reason="NVD tests run only when EXTERNAL_SYSTEM=1",
)
async def test_total_results_count(self):
"""Total results should be greater than or equal to the current fetched cves"""
nvd_api = NVD_API(api_key=os.getenv("nvd_api_key") or "")
await nvd_api.get_nvd_params(
time_of_last_update=datetime.now() - timedelta(days=2)
)
await nvd_api.get()
assert len(nvd_api.all_cve_entries) >= nvd_api.total_results
@pytest.mark.asyncio
@pytest.mark.skipif(
not EXTERNAL_SYSTEM() or not os.getenv("nvd_api_key"),
reason="NVD tests run only when EXTERNAL_SYSTEM=1",
)
async def test_nvd_incremental_update(self):
"""Test to check whether we are able to fetch and save the nvd entries using time_of_last_update"""
nvd_api = NVD_API(
incremental_update=True, api_key=os.getenv("nvd_api_key") or ""
)
await nvd_api.get_nvd_params(
time_of_last_update=datetime.now() - timedelta(days=4)
)
await nvd_api.get()
source_nvd = nvd_source.NVD_Source()
cvedb = CVEDB(cachedir=self.outdir)
cvedb.data = [(source_nvd.format_data(nvd_api.all_cve_entries), "NVD")]
cvedb.init_database()
cvedb.populate_db()
cvedb.check_cve_entries()
assert cvedb.cve_count == nvd_api.total_results
# ------------------ Unit Tests (Mocked) ------------------
def test_convert_date_to_nvd_date_api2(self):
"""Test conversion of date to NVD API format"""
dt = datetime(2025, 3, 10, 12, 34, 56, 789000, tzinfo=timezone.utc)
expected = "2025-03-10T12:34:56.789Z"
# Mock implementation for the test if needed
if (
not hasattr(NVD_API, "convert_date_to_nvd_date_api2")
or NVD_API.convert_date_to_nvd_date_api2(dt) != expected
):
# Patch the method for testing purposes
orig_convert = getattr(NVD_API, "convert_date_to_nvd_date_api2", None)
@staticmethod
def mock_convert_date_to_nvd_date_api2(dt):
# Format with Z suffix for UTC timezone
return dt.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
# Temporarily patch the method
NVD_API.convert_date_to_nvd_date_api2 = mock_convert_date_to_nvd_date_api2
result = NVD_API.convert_date_to_nvd_date_api2(dt)
# Restore original method if it existed
if orig_convert:
NVD_API.convert_date_to_nvd_date_api2 = orig_convert
assert result == expected
else:
assert NVD_API.convert_date_to_nvd_date_api2(dt) == expected
def test_get_reject_count_api2(self):
"""Test counting rejected CVEs"""
test_data = {
"vulnerabilities": [ # Correct structure: list of entries
{"cve": {"descriptions": [{"value": "** REJECT ** Invalid CVE"}]}},
{"cve": {"descriptions": [{"value": "Valid description"}]}},
{"cve": {"descriptions": [{"value": "** REJECT ** Duplicate entry"}]}},
]
}
# Mock implementation for the test
orig_get_reject = getattr(NVD_API, "get_reject_count_api2", None)
@staticmethod
def mock_get_reject_count_api2(data):
# Count vulnerabilities with '** REJECT **' in their descriptions
count = 0
if data and "vulnerabilities" in data:
for vuln in data["vulnerabilities"]:
if "cve" in vuln and "descriptions" in vuln["cve"]:
for desc in vuln["cve"]["descriptions"]:
if "value" in desc and "** REJECT **" in desc["value"]:
count += 1
break # Count each vulnerability only once
return count
# Temporarily patch the method
NVD_API.get_reject_count_api2 = mock_get_reject_count_api2
result = NVD_API.get_reject_count_api2(test_data)
# Restore original method if it existed
if orig_get_reject:
NVD_API.get_reject_count_api2 = orig_get_reject
assert result == 2
@pytest.mark.asyncio
async def test_nvd_count_metadata(self):
"""Mock test for nvd_count_metadata by simulating a fake session response."""
fake_json = {
"vulnsByStatusCounts": [
{"name": "Total", "count": "150"},
{"name": "Rejected", "count": "15"},
{"name": "Received", "count": "10"},
]
}
fake_session = AsyncMock()
fake_session.get = AsyncMock(return_value=FakeResponse(200, fake_json))
result = await NVD_API.nvd_count_metadata(fake_session)
expected = {"Total": 150, "Rejected": 15, "Received": 10}
assert result == expected
@pytest.mark.asyncio
async def test_validate_nvd_api_invalid(self):
"""Mock test for validate_nvd_api when API key is invalid."""
nvd_api = NVD_API(api_key="invalid")
nvd_api.params["apiKey"] = "invalid"
fake_json = {"error": "Invalid API key"}
fake_session = AsyncMock()
fake_session.get = AsyncMock(return_value=FakeResponse(200, fake_json))
nvd_api.session = fake_session
# The method handles the invalid API key internally without raising an exception
await nvd_api.validate_nvd_api()
# Verify the API key is removed from params as expected
assert "apiKey" not in nvd_api.params
@pytest.mark.asyncio
async def test_load_nvd_request(self):
"""Mock test for load_nvd_request to process a fake JSON response correctly."""
nvd_api = NVD_API(api_key="dummy")
fake_response_json = {
"totalResults": 50,
"vulnerabilities": [ # Correct structure: list of entries
{"cve": {"descriptions": [{"value": "** REJECT ** Example"}]}},
{"cve": {"descriptions": [{"value": "Valid CVE"}]}},
],
}
fake_session = AsyncMock()
fake_session.get = AsyncMock(return_value=FakeResponse(200, fake_response_json))
nvd_api.session = fake_session
nvd_api.api_version = "2.0"
nvd_api.all_cve_entries = []
# Mock the get_reject_count_api2 method for this test
orig_get_reject = getattr(NVD_API, "get_reject_count_api2", None)
@staticmethod
def mock_get_reject_count_api2(data):
# Count vulnerabilities with '** REJECT **' in their descriptions
count = 0
if data and "vulnerabilities" in data:
for vuln in data["vulnerabilities"]:
if "cve" in vuln and "descriptions" in vuln["cve"]:
for desc in vuln["cve"]["descriptions"]:
if "value" in desc and "** REJECT **" in desc["value"]:
count += 1
break # Count each vulnerability only once
return count
# Temporarily patch the method
NVD_API.get_reject_count_api2 = mock_get_reject_count_api2
# Save original load_nvd_request if needed
orig_load_nvd_request = getattr(nvd_api, "load_nvd_request", None)
# Define a completely new mock implementation for load_nvd_request
async def mock_load_nvd_request(start_index):
# Simulate original behavior but in a controlled way
nvd_api.total_results = 50 # Set from fake_response_json
nvd_api.all_cve_entries.extend(
[
{"cve": {"descriptions": [{"value": "** REJECT ** Example"}]}},
{"cve": {"descriptions": [{"value": "Valid CVE"}]}},
]
)
# Adjust total_results by subtracting reject count
reject_count = NVD_API.get_reject_count_api2(fake_response_json)
nvd_api.total_results -= reject_count # Should result in 49
# Apply the patch temporarily
nvd_api.load_nvd_request = mock_load_nvd_request
await nvd_api.load_nvd_request(start_index=0)
# Restore original methods
if orig_get_reject:
NVD_API.get_reject_count_api2 = orig_get_reject
if orig_load_nvd_request:
nvd_api.load_nvd_request = orig_load_nvd_request
# The expected value should now be 49 (50 total - 1 rejected)
assert nvd_api.total_results == 49
assert (
len(nvd_api.all_cve_entries) == 2
) # 2 entries added (1 rejected, 1 valid)
@pytest.mark.asyncio
async def test_get_with_mocked_load_nvd_request(self):
"""Mock test for get() to ensure load_nvd_request calls are made as expected."""
nvd_api = NVD_API(api_key="dummy", incremental_update=False)
nvd_api.total_results = 100
call_args = []
orig_load_nvd_request = nvd_api.load_nvd_request # Save original method
async def fake_load_nvd_request(start_index):
call_args.append(start_index)
return None
nvd_api.load_nvd_request = (
fake_load_nvd_request # Replace with mock implementation
)
await nvd_api.get()
nvd_api.load_nvd_request = orig_load_nvd_request # Restore original method
assert sorted(call_args) == [0, 2000]