Skip to content

Commit e063568

Browse files
committed
Added wrapper for generating dummy repositories with vulnerabilities
1 parent 8c3b346 commit e063568

File tree

5 files changed

+173
-1
lines changed

5 files changed

+173
-1
lines changed

osv/impact_git_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from .test_tools.test_repository import TestRepository
2+
import unittest
3+
from . import impact
4+
import logging
5+
6+
7+
class TestImpactTest(unittest.TestCase):
8+
9+
def test_range_simple(self):
10+
"""TODO see doc """
11+
12+
13+
repo_analyzer = impact.RepoAnalyzer()
14+
## create a mock repo
15+
repo = TestRepository('test')
16+
first = repo.add_empty_commit(
17+
vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
18+
second = repo.add_empty_commit(parents=[first])
19+
third = repo.add_empty_commit(
20+
parents=[second], vulnerability=TestRepository.VulnerabilityType.FIXED)
21+
22+
(all_introduced, all_fixed, all_last_affected,
23+
all_limit) = repo.get_ranges()
24+
25+
result = repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
26+
all_limit, all_last_affected)
27+
for commit in result.commits:
28+
print(commit)
29+

osv/impact_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import codecs
1717
import unittest
18+
import pygit2
19+
1820

1921
from . import impact
2022
from . import tests
@@ -144,3 +146,12 @@ def test_update_no_commits(self):
144146
impact.update_affected_commits('BUG-1', set(), True)
145147
affected_commits = list(models.AffectedCommits.query())
146148
self.assertEqual(0, len(affected_commits))
149+
150+
151+
def test_range_simple():
152+
repo_analyzer = impact.RepoAnalyzer()
153+
linear3_repo = pygit2.Repository
154+
linear3_repo.ad
155+
156+
157+
repo_analyzer.get_affected()

osv/test_tools/test_repository.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import pygit2
2+
import json
3+
from datetime import datetime
4+
from enum import Enum
5+
import os
6+
import shutil
7+
8+
9+
class TestRepository:
10+
11+
class VulnerabilityType(Enum):
12+
INTRODUCED = 1
13+
FIXED = 2
14+
LAST_AFFECTED = 3
15+
LIMIT = 4
16+
NONE = 5
17+
18+
__AUTHOR = pygit2.Signature('John Smith', '[email protected]')
19+
__COMMITER = pygit2.Signature('John Smith', '[email protected]')
20+
21+
introduced = []
22+
fixed = []
23+
last_affected = []
24+
limit = []
25+
26+
def __init__(self, name: str):
27+
self.name = name
28+
if (os.path.exists(f"osv/testdata/test_repositories/{name}")):
29+
shutil.rmtree(f"osv/testdata/test_repositories/{name}")
30+
self.repo = pygit2.init_repository(
31+
f"osv/testdata/test_repositories/{name}", bare=False)
32+
33+
def add_empty_commit(
34+
self,
35+
vulnerability: VulnerabilityType = VulnerabilityType.NONE,
36+
parents: list[pygit2.Oid] = [],
37+
message: str = "Empty") -> pygit2.Oid:
38+
"""
39+
Adds a empty commit to the repository, tags it with the vulnerability type and adds it to the vulnerability list if specified
40+
"""
41+
42+
tree = self.repo.TreeBuilder().write()
43+
44+
commit = None
45+
46+
if (self.repo.is_empty):
47+
commit = self.repo.create_commit('refs/heads/master', self.__AUTHOR,
48+
self.__COMMITER, message, tree, [])
49+
else:
50+
51+
created_branch: pygit2.Branch = None
52+
branch_commit: pygit2.Commit = None
53+
for parent in parents:
54+
if (self.__has_children(parent)):
55+
created_branch = self.repo.create_branch(
56+
f'branch_{parent.hex}', self.repo.revparse_single(parent.hex))
57+
branch_commit = parent
58+
self.repo.checkout(created_branch.name)
59+
break
60+
61+
if (created_branch is None):
62+
commit = self.repo.create_commit('refs/heads/master', self.__AUTHOR,
63+
self.__COMMITER, message, tree,
64+
parents)
65+
else:
66+
parents.remove(branch_commit)
67+
parents.insert(0, branch_commit)
68+
commit = self.repo.create_commit(created_branch.name, self.__AUTHOR,
69+
self.__COMMITER, message, tree,
70+
parents)
71+
72+
self.repo.checkout('refs/heads/master')
73+
74+
#self.repo.branches.delete(created_branch.branch_name)
75+
76+
match vulnerability:
77+
case self.VulnerabilityType.INTRODUCED:
78+
self.introduced.append(commit.hex)
79+
case self.VulnerabilityType.FIXED:
80+
self.fixed.append(commit.hex)
81+
case self.VulnerabilityType.LAST_AFFECTED:
82+
self.last_affected.append(commit.hex)
83+
case self.VulnerabilityType.LIMIT:
84+
self.limit.append(commit.hex)
85+
case self.VulnerabilityType.NONE:
86+
pass
87+
case _:
88+
raise ValueError("Invalid vulnerability type")
89+
return commit
90+
91+
def __has_children(self, commit: pygit2.Commit) -> bool:
92+
for current_commit in self.repo.walk(self.repo.head.target,
93+
pygit2.GIT_SORT_TOPOLOGICAL):
94+
for parent in current_commit.parents:
95+
if parent.hex == commit.hex:
96+
return True
97+
return False
98+
99+
def get_ranges(self):
100+
"""
101+
return the ranges of the repository
102+
"""
103+
return (self.introduced, self.fixed, self.last_affected, self.limit)
104+
105+
def print_commits(self):
106+
107+
commits = []
108+
for ref in self.repo.listall_reference_objects():
109+
print(ref.target)
110+
for commit in self.repo.walk(ref.target, pygit2.GIT_SORT_TIME):
111+
112+
curCommit = {
113+
'hash':
114+
commit.hex,
115+
'message':
116+
commit.message,
117+
'commit_date':
118+
datetime.utcfromtimestamp(commit.commit_time
119+
).strftime('%Y-%m-%dT%H:%M:%SZ'),
120+
'author_name':
121+
commit.author.name,
122+
'author_email':
123+
commit.author.email,
124+
'parents': [c.hex for c in commit.parents],
125+
}
126+
if (curCommit in commits):
127+
break
128+
commits.append(curCommit)
129+
130+
print(json.dumps(commits, indent=2))

osv/testdata/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version_enum
1+
version_enum
2+
test_repositories/**

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ python3 -m pipenv run python -m unittest osv.bug_test
66
python3 -m pipenv run python -m unittest osv.purl_helpers_test
77
python3 -m pipenv run python -m unittest osv.request_helper_test
88
python3 -m pipenv run python -m unittest osv.semver_index_test
9+
python3 -m pipenv run python -m unittest osv.impact_git_test
910
python3 -m pipenv run python -m unittest osv.impact_test
1011

1112
# Run all osv.ecosystems tests

0 commit comments

Comments
 (0)