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

fix: add missing Vulnerability comparator for sorting #246

Merged
merged 1 commit into from
Jun 10, 2022
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
8 changes: 8 additions & 0 deletions cyclonedx/model/vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,14 @@ def __eq__(self, other: object) -> bool:
return hash(other) == hash(self)
return False

def __lt__(self, other: Any) -> bool:
if isinstance(other, Vulnerability):
return ComparableTuple(
(self.id, self.description, self.detail, self.source, self.created, self.published)) < \
ComparableTuple(
(other.id, other.description, other.detail, other.source, other.created, other.published))
return NotImplemented

def __hash__(self) -> int:
return hash((
self.id, self.source, tuple(self.references), tuple(self.ratings), tuple(self.cwes), self.description,
Expand Down
36 changes: 36 additions & 0 deletions tests/test_model_vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Copyright (c) OWASP Foundation. All Rights Reserved.

import unittest
from datetime import datetime, timedelta
from decimal import Decimal
from unittest import TestCase
from unittest.mock import Mock, patch
Expand Down Expand Up @@ -206,6 +207,41 @@ def test_empty_vulnerability(self, mock_uuid: Mock) -> None:
self.assertIsNone(v.analysis)
self.assertFalse(v.affects)

def test_sort(self) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

source1 = VulnerabilitySource(name='a')
source2 = VulnerabilitySource(name='b')
datetime1 = datetime.utcnow()
datetime2 = datetime1 + timedelta(seconds=5)

# expected sort order: (id, description, detail, source, created, published)
expected_order = [0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11]
vulnerabilities = [
Vulnerability(bom_ref='0', id='a', description='a', detail='a',
source=source1, created=datetime1, published=datetime1),
Vulnerability(bom_ref='1', id='a', description='a', detail='a',
source=source1, created=datetime1),
Vulnerability(bom_ref='2', id='a', description='a', detail='a',
source=source1),
Vulnerability(bom_ref='3', id='a', description='a', detail='a'),
Vulnerability(bom_ref='4', id='a', description='a'),
Vulnerability(bom_ref='5', id='a'),
Vulnerability(bom_ref='6', id='a', description='a', detail='a',
source=source1, created=datetime1, published=datetime2),
Vulnerability(bom_ref='7', id='a', description='a', detail='a',
source=source1, created=datetime2, published=datetime1),
Vulnerability(bom_ref='8', id='a', description='a', detail='a',
source=source2, created=datetime1, published=datetime1),
Vulnerability(bom_ref='9', id='a', description='a', detail='b',
source=source1, created=datetime1, published=datetime1),
Vulnerability(bom_ref='10', id='a', description='b', detail='b',
source=source1, created=datetime1, published=datetime1),
Vulnerability(bom_ref='11', id='b', description='a', detail='a',
source=source1, created=datetime1, published=datetime1),
]
sorted_vulnerabilities = sorted(vulnerabilities)
expected_vulnerabilities = reorder(vulnerabilities, expected_order)
self.assertListEqual(sorted_vulnerabilities, expected_vulnerabilities)


class TestModelVulnerabilityAdvisory(TestCase):

Expand Down