Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ge and le function #154

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/epss_api/epss.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Score(object):
""" EPSS Score Object"""

def __init__(self, cve: str, epss: str, percentile: str):
"""Initialize EPSS Score Object
"""Initialize EPSS Score Object

Args:
cve (str): CVE-yyyy-n
Expand Down Expand Up @@ -136,6 +136,34 @@ def percentile_gt(self, min: float) -> list[Score]:

return list(self._sortedScoresByPercentile[i:])

def epss_ge(self, min: float) -> list[Score]:
"""Get CVEs with EPSS score greater than or equal to the parameter

Args:
min (float): limit of EPSS score

Returns:
list[Score] | None: EPSS score object list
"""
i = bisect_left(self._sortedScoresByEpss, min,
key=lambda x: x.epss)

return list(self._sortedScoresByEpss[i:])

def percentile_ge(self, min: float) -> list[Score]:
"""Get CVEs with percentile greater than or equal to the parameter

Args:
min (float): limit of percentile

Returns:
list[Score] | None: EPSS score object list
"""
i = bisect_left(self._sortedScoresByPercentile, min,
key=lambda x: x.percentile)

return list(self._sortedScoresByPercentile[i:])

def epss_lt(self, max: float) -> list[Score]:
"""Get CVEs with EPSS score lower than the parameter

Expand All @@ -151,7 +179,7 @@ def epss_lt(self, max: float) -> list[Score]:
return list(self._sortedScoresByEpss[:i])

def percentile_lt(self, max: float) -> list[Score]:
"""Get CVEs with percentile lower than the parameter
"""Get CVEs with percentile less than the parameter

Args:
max (float): limit of percentile
Expand All @@ -164,6 +192,34 @@ def percentile_lt(self, max: float) -> list[Score]:

return list(self._sortedScoresByPercentile[:i])

def epss_le(self, max: float) -> list[Score]:
"""Get CVEs with EPSS score less than or equal to the parameter

Args:
max (float): limit of EPSS score

Returns:
list[Score] | []: EPSS score object list
"""
i = bisect_right(self._sortedScoresByEpss, max,
key=lambda x: x.epss)

return list(self._sortedScoresByEpss[:i])

def percentile_le(self, max: float) -> list[Score]:
"""Get CVEs with percentile less than or equal to the parameter

Args:
max (float): limit of percentile

Returns:
list[Score] | []: EPSS score object list
"""
i = bisect_right(self._sortedScoresByPercentile, max,
key=lambda x: x.percentile)

return list(self._sortedScoresByPercentile[:i])

def csv(self) -> list[str]:
"""Get csv data containing all epss scores.

Expand Down
36 changes: 36 additions & 0 deletions tests/epss_api/test_epss.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ def test_percentile_gt(min):
assert s.percentile <= min


@pytest.mark.parametrize("min", [-1, 0, 0.5, 1, 2])
def test_epss_ge(min):
scores = epss.epss_ge(min)
for s in scores:
assert s.epss >= min
for s in list(set(scores) - set(epss.scores())):
assert s.epss < min


@pytest.mark.parametrize("min", [-1, 0, 0.5, 1, 2])
def test_percentile_ge(min):
scores = epss.percentile_ge(min)
for s in scores:
assert s.percentile >= min
for s in list(set(scores) - set(epss.scores())):
assert s.percentile < min


@pytest.mark.parametrize("max", [-1, 0, 0.5, 1, 2])
def test_epss_lt(max):
scores = epss.epss_lt(max)
Expand All @@ -60,6 +78,24 @@ def test_percentile_lt(max):
assert s.percentile >= max


@pytest.mark.parametrize("max", [-1, 0, 0.5, 1, 2])
def test_epss_le(max):
scores = epss.epss_le(max)
for s in scores:
assert s.epss <= max
for s in list(set(scores) - set(epss.scores())):
assert s.epss > max


@pytest.mark.parametrize("max", [-1, 0, 0.5, 1, 2])
def test_percentile_le(max):
scores = epss.percentile_le(max)
for s in scores:
assert s.percentile <= max
for s in list(set(scores) - set(epss.scores())):
assert s.percentile > max


def test_score():
score = epss.score(cve_id='CVE-2022-0669')
assert score.cve.startswith('CVE-')
Expand Down
Loading