Skip to content

Commit dc605b0

Browse files
committed
add a test
Signed-off-by: ziad <ziadhany2016@gmail.com>
1 parent 637c3d5 commit dc605b0

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

vulnerabilities/importers/gitlab.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ def advisory_data(self) -> Iterable[AdvisoryData]:
8686
glob = "**/*.yml"
8787
files = (p for p in path.glob(glob) if p.is_file())
8888
for file in files:
89-
# split a path according to gitlab conventions where package type and name are a part of path
90-
# For example with this path:
91-
# /tmp/tmpi1klhpmd/pypi/gradio/CVE-2021-43831.yml
92-
# the package type is pypi and the package name is gradio
93-
# to ('/', 'tmp', 'tmpi1klhpmd', 'pypi', 'gradio', 'CVE-2021-43831.yml')
9489
purl_type = get_gitlab_package_type(path=file)
9590
if not purl_type:
9691
logger.error(f"Unknow gitlab directory structure {file!r}")
@@ -111,13 +106,12 @@ def get_gitlab_package_type(path: Path):
111106
"""
112107
Return a package type extracted from a gitlab advisory path or None
113108
"""
114-
parts = path.parts[-3:]
109+
parts = path.parts
115110

116111
if len(parts) < 3:
117112
return
118113

119-
type, _name, _vid = parts
120-
return type
114+
return parts[3]
121115

122116

123117
def get_purl(package_slug):

vulnerabilities/tests/test_gitlab.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99

1010
import json
1111
import os
12+
from pathlib import Path
1213
from unittest import mock
1314

1415
import pytest
16+
from packageurl import PackageURL
1517

1618
from vulnerabilities.importer import AdvisoryData
1719
from vulnerabilities.importers.gitlab import GitLabBasicImprover
20+
from vulnerabilities.importers.gitlab import get_gitlab_package_type
21+
from vulnerabilities.importers.gitlab import get_purl
1822
from vulnerabilities.importers.gitlab import parse_gitlab_advisory
1923
from vulnerabilities.improvers.default import DefaultImprover
2024
from vulnerabilities.tests import util_tests
@@ -84,3 +88,34 @@ def test_gitlab_improver(mock_response, pkg_type):
8488
inference = [data.to_dict() for data in improver.get_inferences(advisory)]
8589
result.extend(inference)
8690
util_tests.check_results_against_json(result, expected_file)
91+
92+
93+
def test_get_purl():
94+
assert get_purl("nuget/MessagePack") == PackageURL(type="nuget", name="MessagePack")
95+
assert get_purl("nuget/Microsoft.NETCore.App") == PackageURL(
96+
type="nuget", name="Microsoft.NETCore.App"
97+
)
98+
assert get_purl("npm/fresh") == PackageURL(type="npm", name="fresh")
99+
100+
101+
def test_get_gitlab_package_type():
102+
assert (
103+
get_gitlab_package_type(Path("/tmp/tmp9317bd5i/maven/com.google.gwt/gwt/CVE-2013-4204.yml"))
104+
== "maven"
105+
)
106+
assert (
107+
get_gitlab_package_type(
108+
Path(
109+
"/tmp/tmp9317bd5i/maven/io.projectreactor.netty/reactor-netty-http/CVE-2020-5404.yml"
110+
)
111+
)
112+
== "maven"
113+
)
114+
assert (
115+
get_gitlab_package_type(
116+
Path("/tmp/tmp9317bd5i/go/github.com/cloudflare/cfrpki/CVE-2021-3909.yml")
117+
)
118+
== "go"
119+
)
120+
assert get_gitlab_package_type(Path("/tmp/tmp9317bd5i/gem/rexml/CVE-2021-28965.yml")) == "gem"
121+
assert get_gitlab_package_type(Path()) is None

0 commit comments

Comments
 (0)