Skip to content

test(language_scanner): use scan_file() & add tests for python packages #1758

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

Merged
merged 2 commits into from
Jul 11, 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
3 changes: 3 additions & 0 deletions test/language_data/FAIL-PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Metadata-Version: 2.1
Name: cve-bin-tool
Version: 3.1.1
23 changes: 23 additions & 0 deletions test/language_data/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Metadata-Version: 2.1
Name: zstandard
Version: 0.18.0
Summary: Zstandard bindings for Python
Home-page: https://github.com/indygreg/python-zstandard
Author: Gregory Szorc
Author-email: [email protected]
License: BSD
Keywords: zstandard,zstd,compression
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Classifier: Development Status :: 5 - Production/Stable
Requires-Python: >=3.6
License-File: LICENSE
Requires-Dist: cffi (>=1.11) ; platform_python_implementation == "PyPy"
Provides-Extra: cffi
Requires-Dist: cffi (>=1.11) ; platform_python_implementation == "PyPy"
File renamed without changes.
39 changes: 30 additions & 9 deletions test/test_language_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from cve_bin_tool.util import ProductInfo
from cve_bin_tool.version_scanner import VersionScanner


Expand All @@ -28,43 +29,63 @@ def test_java_package(self, filename: str, product_name: str) -> None:
scanner = VersionScanner()
scanner.file_stack.append(filename)
# Only expecting to get one product with a vendor in the database
for product in scanner.run_java_checker(filename):
for product in scanner.scan_file(filename):
if product:
product_info, file_path = product
assert product_info.product == product_name
assert file_path == filename

@pytest.mark.parametrize("filename", ((str(TEST_FILE_PATH / "pom_fail.xml")),))
@pytest.mark.parametrize("filename", ((str(TEST_FILE_PATH / "fail_pom.xml")),))
def test_java_package_none_found(self, filename: str) -> None:
scanner = VersionScanner()
scanner.file_stack.append(filename)
product = None
# Not expecting any product to match with a vendor in the database
for product in scanner.run_java_checker(filename):
for product in scanner.scan_file(filename):
pass
assert product is None

@pytest.mark.parametrize(
"filename", ((str(TEST_FILE_PATH / "package-lock1.json")),)
)
@pytest.mark.parametrize("filename", ((str(TEST_FILE_PATH / "package-lock.json")),))
def test_javascript_package(self, filename: str) -> None:
scanner = VersionScanner()
scanner.file_stack.append(filename)
found_product = []
for product in scanner.run_js_checker(filename):
for product in scanner.scan_file(filename):
if product:
product_info, file_path = product
if product_info.product not in found_product:
found_product.append(product_info.product)
assert found_product == self.JAVASCRIPT_PRODUCTS
assert file_path == filename

@pytest.mark.parametrize("filename", ((str(TEST_FILE_PATH / "package.json")),))
@pytest.mark.parametrize(
"filename", ((str(TEST_FILE_PATH / "fail-package-lock.json")),)
)
def test_javascript_package_none_found(self, filename: str) -> None:
scanner = VersionScanner()
scanner.file_stack.append(filename)
product = None
# Not expecting any product to match with a vendor in the database
for product in scanner.run_js_checker(filename):
for product in scanner.scan_file(filename):
pass
assert product is not None

@pytest.mark.parametrize("filename", ((str(TEST_FILE_PATH / "PKG-INFO")),))
def test_python_package(self, filename: str) -> None:
scanner = VersionScanner()
scanner.file_stack.append(filename)
for product in scanner.scan_file(filename):
if product:
product_info, file_path = product
assert product_info == ProductInfo("facebook", "zstandard", "0.18.0")
assert file_path == filename

@pytest.mark.parametrize("filename", ((str(TEST_FILE_PATH / "FAIL-PKG-INFO")),))
def test_python_package_none_found(self, filename: str) -> None:
scanner = VersionScanner()
scanner.file_stack.append(filename)
product = None
# Not expecting any product to match with a vendor in the database
for product in scanner.scan_file(filename):
pass
assert product is None