Skip to content

Commit 8650935

Browse files
sethmlarsonhugovk
andauthored
Add test suite and GitHub Actions CI
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent f39e155 commit 8650935

5 files changed

Lines changed: 92 additions & 0 deletions

File tree

.github/workflows/test.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
on: [push, pull_request, workflow_dispatch]
2+
3+
env:
4+
FORCE_COLOR: 1
5+
6+
jobs:
7+
tests:
8+
name: "Tests"
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
12+
- uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v5.0.0
13+
with:
14+
python-version: 3.x
15+
- run: |
16+
python -m pip install -r dev-requirements.txt
17+
pytest tests/

dev-requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest

dev-requirements.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.10
3+
# by the following command:
4+
#
5+
# pip-compile --generate-hashes --output-file=dev-requirements.txt dev-requirements.in
6+
#
7+
exceptiongroup==1.2.0 \
8+
--hash=sha256:4bfd3996ac73b41e9b9628b04e079f193850720ea5945fc96a08633c66912f14 \
9+
--hash=sha256:91f5c769735f051a4290d52edd0858999b57e5876e9f85937691bd4c9fa3ed68
10+
# via pytest
11+
iniconfig==2.0.0 \
12+
--hash=sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3 \
13+
--hash=sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374
14+
# via pytest
15+
packaging==23.2 \
16+
--hash=sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5 \
17+
--hash=sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7
18+
# via pytest
19+
pluggy==1.4.0 \
20+
--hash=sha256:7db9f7b503d67d1c5b95f59773ebb58a8c1c288129a88665838012cfb07b8981 \
21+
--hash=sha256:8c85c2876142a764e5b7548e7d9a0e0ddb46f5185161049a79b7e974454223be
22+
# via pytest
23+
pytest==8.0.0 \
24+
--hash=sha256:249b1b0864530ba251b7438274c4d251c58d868edaaec8762893ad4a0d71c36c \
25+
--hash=sha256:50fb9cbe836c3f20f0dfa99c565201fb75dc54c8d76373cd1bde06b06657bdb6
26+
# via -r dev-requirements.in
27+
tomli==2.0.1 \
28+
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
29+
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
30+
# via pytest

tests/__init__.py

Whitespace-only changes.

tests/test_sbom.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
import random
3+
import hashlib
4+
import sbom
5+
6+
7+
@pytest.mark.parametrize(
8+
["package_sha1s", "package_verification_code"],
9+
[
10+
# No files -> empty SHA1
11+
([], hashlib.sha1().hexdigest()),
12+
# One file -> SHA1(SHA1(file))
13+
(["F" * 40], hashlib.sha1(b"f" * 40).hexdigest()),
14+
# Tests ordering and lowercasing of SHA1s
15+
(["0" * 40, "e" * 40, "F" * 40], hashlib.sha1((b"0" * 40) + (b"e" * 40) + (b"f" * 40)).hexdigest())
16+
]
17+
)
18+
def test_calculate_package_verification_code(package_sha1s, package_verification_code):
19+
# Randomize because PackageVerificationCode is deterministic.
20+
random.shuffle(package_sha1s)
21+
22+
input_sbom = {
23+
"files": [
24+
{
25+
"SPDXID": f"SPDXRef-FILE-{package_sha1}",
26+
"checksums": [{"algorithm": "SHA1", "checksumValue": package_sha1}]
27+
} for package_sha1 in package_sha1s
28+
],
29+
"packages": [{"SPDXID": "SPDXRef-PACKAGE", "filesAnalyzed": True}],
30+
"relationships": [
31+
{
32+
"spdxElementId": "SPDXRef-PACKAGE",
33+
"relatedSpdxElement": f"SPDXRef-FILE-{package_sha1}",
34+
"relationshipType": "CONTAINS"
35+
}
36+
for package_sha1 in package_sha1s
37+
]
38+
}
39+
40+
sbom.calculate_package_verification_codes(input_sbom)
41+
42+
assert input_sbom["packages"][0]["packageVerificationCode"] == {
43+
"packageVerificationCodeValue": package_verification_code
44+
}

0 commit comments

Comments
 (0)