Skip to content

Commit 70ab29d

Browse files
committed
All tests implemented accorded to documentation
1 parent 099356b commit 70ab29d

File tree

2 files changed

+290
-26
lines changed

2 files changed

+290
-26
lines changed

osv/impact_git_test.py

Lines changed: 274 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,34 @@
77
class TestImpactTest(unittest.TestCase):
88

99

10+
@classmethod
11+
def setUpClass(cls):
12+
cls.__repo_analyzer = impact.RepoAnalyzer(detect_cherrypicks=False)
13+
14+
15+
16+
######## 1rst : tests with only "introduced" and "fixed"
1017
def test_introduced_fixed_linear(self):
1118

12-
repo_analyzer = impact.RepoAnalyzer(detect_cherrypicks=False)
13-
repo = TestRepository('test_introduced_fixed_linear')
19+
repo = TestRepository('test_introduced_fixed_linear',debug=False)
1420

1521

1622
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
1723
second = repo.add_empty_commit(parents=[first])
1824
third = repo.add_empty_commit(parents=[second], vulnerability=TestRepository.VulnerabilityType.FIXED)
1925
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
2026

21-
result = repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
27+
28+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
2229
all_limit, all_last_affected)
2330

2431
expected = set([first.hex, second.hex])
2532
repo.remove()
26-
self.assertEqual(result.commits, expected )
33+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
2734

2835
def test_introduced_fixed_branch_propagation(self):
2936

30-
repo_analyzer = impact.RepoAnalyzer(detect_cherrypicks=False)
31-
repo = TestRepository('test_introduced_fixed_branch_propagation')
37+
repo = TestRepository('test_introduced_fixed_branch_propagation',debug=False)
3238

3339

3440
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
@@ -37,16 +43,15 @@ def test_introduced_fixed_branch_propagation(self):
3743
fourth = repo.add_empty_commit(parents=[second])
3844
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
3945

40-
result = repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
46+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
4147
all_limit, all_last_affected)
4248

4349
expected = set([first.hex,second.hex,fourth.hex])
4450
repo.remove()
45-
self.assertEqual(result.commits, expected )
51+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
4652

4753
def test_introduced_fixed_merge(self):
4854

49-
repo_analyzer = impact.RepoAnalyzer(detect_cherrypicks=False)
5055
repo = TestRepository('test_introduced_fixed_merge')
5156

5257

@@ -56,10 +61,268 @@ def test_introduced_fixed_merge(self):
5661
fourth = repo.add_empty_commit(parents=[third], vulnerability=TestRepository.VulnerabilityType.FIXED)
5762
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
5863

59-
result = repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
64+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
6065
all_limit, all_last_affected)
6166

6267
expected = set([first.hex,third.hex])
6368
repo.remove()
6469
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
65-
70+
71+
def test_introduced_fixed_two_linear(self):
72+
73+
repo = TestRepository('test_introduced_fixed_two_linear')
74+
75+
76+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
77+
second = repo.add_empty_commit(parents=[first], vulnerability=TestRepository.VulnerabilityType.FIXED)
78+
third = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.INTRODUCED)
79+
fourth = repo.add_empty_commit(parents=[third], vulnerability=TestRepository.VulnerabilityType.FIXED)
80+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
81+
82+
83+
84+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
85+
all_limit, all_last_affected)
86+
87+
expected = set([first.hex,third.hex])
88+
repo.remove()
89+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
90+
91+
def test_introduced_fixed_merge_propagation(self):
92+
repo = TestRepository('test_introduced_fixed_merge_propagation')
93+
94+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
95+
second = repo.add_empty_commit(parents=[first], vulnerability=TestRepository.VulnerabilityType.FIXED)
96+
third = repo.add_empty_commit( vulnerability= TestRepository.VulnerabilityType.INTRODUCED)
97+
fourth = repo.add_empty_commit(parents=[second,third])
98+
fifth = repo.add_empty_commit(parents=[fourth], vulnerability=TestRepository.VulnerabilityType.FIXED)
99+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
100+
101+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
102+
all_limit, all_last_affected)
103+
104+
expected = set([first.hex,third.hex,fourth.hex])
105+
repo.remove()
106+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
107+
108+
def test_introduced_fixed_fix_propagation(self):
109+
repo = TestRepository('test_introduced_fixed_fix_propagation')
110+
111+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
112+
second = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.FIXED)
113+
third = repo.add_empty_commit(parents=[first,second])
114+
fourth = repo.add_empty_commit(parents=[third],vulnerability=TestRepository.VulnerabilityType.FIXED)
115+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
116+
117+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
118+
all_limit, all_last_affected)
119+
120+
expected = set([first.hex])
121+
repo.remove()
122+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
123+
124+
######## 2nd : tests with "introduced" and "limit"
125+
126+
def test_introduced_limit_linear(self):
127+
128+
repo = TestRepository('test_intoduced_limit_linear')
129+
130+
131+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
132+
second = repo.add_empty_commit(parents=[first])
133+
third = repo.add_empty_commit(parents=[second], vulnerability=TestRepository.VulnerabilityType.LIMIT)
134+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
135+
136+
137+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
138+
all_limit, all_last_affected)
139+
140+
expected = set([first.hex, second.hex])
141+
repo.remove()
142+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
143+
144+
def test_introduced_limit_branch(self):
145+
146+
repo = TestRepository('test_intoduced_limit_branch')
147+
148+
149+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
150+
second = repo.add_empty_commit(parents=[first])
151+
third = repo.add_empty_commit(parents=[second], vulnerability=TestRepository.VulnerabilityType.LIMIT)
152+
fourth = repo.add_empty_commit(parents=[second])
153+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
154+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
155+
all_limit, all_last_affected)
156+
157+
expected = set([first.hex, second.hex,])
158+
repo.remove()
159+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
160+
161+
def test_introduced_limit_merge(self):
162+
163+
repo = TestRepository('test_intoduced_limit_merge')
164+
165+
166+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
167+
second = repo.add_empty_commit()
168+
third = repo.add_empty_commit(parents=[first,second])
169+
fourth = repo.add_empty_commit(parents=[third], vulnerability=TestRepository.VulnerabilityType.LIMIT)
170+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
171+
172+
173+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
174+
all_limit, all_last_affected)
175+
176+
expected = set([first.hex,third.hex])
177+
repo.remove()
178+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
179+
180+
def test_introduced_limit_two_linear(self):
181+
182+
repo = TestRepository('test_introduced_limit_two_linear')
183+
184+
185+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
186+
second = repo.add_empty_commit(parents=[first], vulnerability=TestRepository.VulnerabilityType.LIMIT)
187+
third = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.INTRODUCED)
188+
fourth = repo.add_empty_commit(parents=[third], vulnerability=TestRepository.VulnerabilityType.LIMIT)
189+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
190+
191+
192+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
193+
all_limit, all_last_affected)
194+
195+
expected = set([first.hex,third.hex])
196+
repo.remove()
197+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
198+
199+
200+
######## 2nd : tests with "introduced" and "last-affected"
201+
202+
def test_introduced_last_affected_linear(self):
203+
204+
repo = TestRepository('test_introduced_last_affected_linear')
205+
206+
207+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
208+
second = repo.add_empty_commit(parents=[first])
209+
third = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.LAST_AFFECTED)
210+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
211+
212+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
213+
all_limit, all_last_affected)
214+
215+
expected = set([first.hex,second.hex,third.hex])
216+
repo.remove()
217+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
218+
219+
def test_introduced_last_affected_branch_propagation(self):
220+
221+
repo = TestRepository('test_introduced_last_affected_branch_propagation')
222+
223+
224+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
225+
second = repo.add_empty_commit(parents=[first])
226+
third = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.LAST_AFFECTED)
227+
fourth = repo.add_empty_commit(parents=[second])
228+
(all_introduced, all_fixed, all_last_affected,all_limit) = repo.get_ranges()
229+
230+
231+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
232+
all_limit, all_last_affected)
233+
234+
expected = set([first.hex,second.hex,third.hex,fourth.hex])
235+
repo.remove()
236+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
237+
238+
239+
def test_introduced_last_affected_merge(self):
240+
241+
repo = TestRepository('test_introduced_last_affected_merge')
242+
243+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
244+
second = repo.add_empty_commit()
245+
third = repo.add_empty_commit(parents=[first,second])
246+
fourth = repo.add_empty_commit(parents=[third], vulnerability= TestRepository.VulnerabilityType.LAST_AFFECTED)
247+
(all_introduced, all_fixed, all_last_affected, all_limit) = repo.get_ranges()
248+
249+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
250+
all_limit, all_last_affected)
251+
252+
expected = set([first.hex,third.hex,fourth.hex])
253+
repo.remove()
254+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
255+
256+
def test_introduced_last_affected_two_linear(self):
257+
258+
repo = TestRepository('test_introduced_last_affected_two_linear')
259+
260+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
261+
second = repo.add_empty_commit(parents=[first], vulnerability=TestRepository.VulnerabilityType.LAST_AFFECTED)
262+
third = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.INTRODUCED)
263+
fourth = repo.add_empty_commit(parents=[third], vulnerability= TestRepository.VulnerabilityType.LAST_AFFECTED)
264+
265+
(all_introduced, all_fixed, all_last_affected, all_limit) = repo.get_ranges()
266+
267+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
268+
all_limit, all_last_affected)
269+
270+
expected = set([first.hex,second.hex,third.hex,fourth.hex])
271+
repo.remove()
272+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
273+
274+
275+
######## 3nd : tests with "introduced", "limit", and "fixed"
276+
277+
def test_introduced_limit_fixed_linear_lf(self):
278+
279+
repo = TestRepository('test_introduced_limit_fixed_linear_lf')
280+
281+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
282+
second = repo.add_empty_commit(parents=[first], vulnerability=TestRepository.VulnerabilityType.LIMIT)
283+
third = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.FIXED)
284+
285+
(all_introduced, all_fixed, all_last_affected, all_limit) = repo.get_ranges()
286+
287+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
288+
all_limit, all_last_affected)
289+
290+
expected = set([first.hex])
291+
repo.remove()
292+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
293+
294+
def test_introduced_limit_fixed_linear_fl(self):
295+
296+
repo = TestRepository('test_introduced_limit_fixed_linear_lf')
297+
298+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
299+
second = repo.add_empty_commit(parents=[first], vulnerability=TestRepository.VulnerabilityType.FIXED)
300+
third = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.LIMIT)
301+
302+
(all_introduced, all_fixed, all_last_affected, all_limit) = repo.get_ranges()
303+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
304+
all_limit, all_last_affected)
305+
306+
expected = set([first.hex])
307+
repo.remove()
308+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
309+
310+
def test_introduced_limit_branch_limit(self):
311+
312+
repo = TestRepository('test_introduced_limit_fixed_linear_lf',debug=True)
313+
314+
first = repo.add_empty_commit(vulnerability=TestRepository.VulnerabilityType.INTRODUCED)
315+
second = repo.add_empty_commit(parents=[first], vulnerability=TestRepository.VulnerabilityType.LIMIT)
316+
third = repo.add_empty_commit(parents=[first])
317+
fourth = repo.add_empty_commit(parents=[second], vulnerability= TestRepository.VulnerabilityType.FIXED)
318+
319+
(all_introduced, all_fixed, all_last_affected, all_limit) = repo.get_ranges()
320+
print("all_introduced",all_introduced)
321+
print("all_last_affected",all_last_affected)
322+
result = self.__repo_analyzer.get_affected(repo.repo, all_introduced, all_fixed,
323+
all_limit, all_last_affected)
324+
325+
expected = set([first.hex])
326+
repo.remove()
327+
self.assertEqual(result.commits, expected, "Expected: %s, got: %s" % (expected, result.commits))
328+

osv/test_tools/test_repository.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ class VulnerabilityType(Enum):
2020

2121
__initialCommit = None
2222

23-
introduced = []
24-
fixed = []
25-
last_affected = []
26-
limit = []
23+
2724

2825
def __init__(self, name: str,debug:bool = False):
2926
self.debug = debug
3027
self.name = name
28+
self.introduced = []
29+
self.fixed = []
30+
self.last_affected = []
31+
self.limit = []
32+
33+
34+
3135
if (os.path.exists(f"osv/testdata/test_repositories/{name}")):
3236
shutil.rmtree(f"osv/testdata/test_repositories/{name}")
3337
self.repo = pygit2.init_repository(
@@ -53,7 +57,7 @@ def add_empty_commit(
5357
"""
5458

5559
tree = self.repo.TreeBuilder().write()
56-
self.__AUTHOR = pygit2.Signature(str(uuid.uuid1()), '[email protected]')
60+
self.__AUTHOR = pygit2.Signature(str(uuid.uuid1()), '[email protected]')#using a random uuid to avoid commits being the same
5761
commit = None
5862

5963

@@ -82,7 +86,7 @@ def add_empty_commit(
8286

8387
if self.debug:
8488
os.system(f"echo ------------------------------------------------------------------")
85-
os.system(f"git -C /home/creux/Documents/software_heritage/swhsec/osv.dev/osv/testdata/test_repositories/test log --all --graph --decorate")
89+
os.system(f"git -C /home/creux/Documents/software_heritage/swhsec/osv.dev/osv/testdata/test_repositories/{self.name} log --all --graph --decorate")
8690

8791
#self.repo.branches.delete(created_branch.branch_name)
8892

@@ -102,19 +106,16 @@ def add_empty_commit(
102106
return commit
103107

104108

105-
106-
def __has_children(self, commit: pygit2.Commit) -> bool:
107-
for current_commit in self.repo.walk(self.repo.head.target,
108-
pygit2.GIT_SORT_TOPOLOGICAL):
109-
for parent in current_commit.parents:
110-
if parent.hex == commit.hex:
111-
return True
112-
return False
113-
114109
def remove(self):
115110
shutil.rmtree(f"osv/testdata/test_repositories/{self.name}/")
116111
while os.path.exists(f"osv/testdata/test_repositories/{self.name}/"): # check if it exists
117112
pass
113+
##cleanup
114+
self.introduced =[]
115+
self.fixed = []
116+
self.last_affected = []
117+
self.limit = []
118+
118119

119120
def get_ranges(self):
120121
"""

0 commit comments

Comments
 (0)